Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2020 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #pragma once |
| 18 | |
| 19 | #include <stdint.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <sys/types.h> |
| 22 | |
| 23 | #include <log/log.h> |
| 24 | #include <log/log_read.h> |
| 25 | |
| 26 | #include "LogStatistics.h" |
| 27 | #include "LogWriter.h" |
| 28 | |
| 29 | // These structs are packed into a single chunk of memory for each log type within a |
| 30 | // SerializedLogChunk object. Their message is contained immediately at the end of the struct. The |
| 31 | // address of the next log in the buffer is *this + sizeof(SerializedLogEntry) + msg_len_. If that |
| 32 | // value would overflow the chunk of memory associated with the SerializedLogChunk object, then a |
| 33 | // new SerializedLogChunk must be allocated to contain the next SerializedLogEntry. |
| 34 | class __attribute__((packed)) SerializedLogEntry { |
| 35 | public: |
| 36 | SerializedLogEntry(uid_t uid, pid_t pid, pid_t tid, uint64_t sequence, log_time realtime, |
| 37 | uint16_t len) |
| 38 | : uid_(uid), |
| 39 | pid_(pid), |
| 40 | tid_(tid), |
| 41 | sequence_(sequence), |
| 42 | realtime_(realtime), |
| 43 | msg_len_(len) {} |
| 44 | SerializedLogEntry(const SerializedLogEntry& elem) = delete; |
| 45 | SerializedLogEntry& operator=(const SerializedLogEntry& elem) = delete; |
| 46 | ~SerializedLogEntry() { |
| 47 | // Never place anything in this destructor. This class is in place constructed and never |
| 48 | // destructed. |
| 49 | } |
| 50 | |
| 51 | LogStatisticsElement ToLogStatisticsElement(log_id_t log_id) const { |
| 52 | return LogStatisticsElement{ |
| 53 | .uid = uid(), |
| 54 | .pid = pid(), |
| 55 | .tid = tid(), |
| 56 | .tag = IsBinary(log_id) ? MsgToTag(msg(), msg_len()) : 0, |
| 57 | .realtime = realtime(), |
| 58 | .msg = msg(), |
| 59 | .msg_len = msg_len(), |
| 60 | .dropped_count = 0, |
| 61 | .log_id = log_id, |
| 62 | }; |
| 63 | } |
| 64 | |
| 65 | bool Flush(LogWriter* writer, log_id_t log_id) const { |
| 66 | struct logger_entry entry = {}; |
| 67 | |
| 68 | entry.hdr_size = sizeof(struct logger_entry); |
| 69 | entry.lid = log_id; |
| 70 | entry.pid = pid(); |
| 71 | entry.tid = tid(); |
| 72 | entry.uid = uid(); |
| 73 | entry.sec = realtime().tv_sec; |
| 74 | entry.nsec = realtime().tv_nsec; |
| 75 | entry.len = msg_len(); |
| 76 | |
| 77 | return writer->Write(entry, msg()); |
| 78 | } |
| 79 | |
| 80 | uid_t uid() const { return uid_; } |
| 81 | pid_t pid() const { return pid_; } |
| 82 | pid_t tid() const { return tid_; } |
| 83 | uint16_t msg_len() const { return msg_len_; } |
| 84 | uint64_t sequence() const { return sequence_; } |
| 85 | log_time realtime() const { return realtime_; } |
| 86 | |
| 87 | char* msg() { return reinterpret_cast<char*>(this) + sizeof(*this); } |
| 88 | const char* msg() const { return reinterpret_cast<const char*>(this) + sizeof(*this); } |
| 89 | uint16_t total_len() const { return sizeof(*this) + msg_len_; } |
| 90 | |
| 91 | private: |
| 92 | const uint32_t uid_; |
| 93 | const uint32_t pid_; |
| 94 | const uint32_t tid_; |
| 95 | const uint64_t sequence_; |
| 96 | const log_time realtime_; |
| 97 | const uint16_t msg_len_; |
| 98 | }; |