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:
SerializedLogEntry(uid_t uid,pid_t pid,pid_t tid,uint64_t sequence,log_time realtime,uint16_t len)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;
~SerializedLogEntry()46     ~SerializedLogEntry() {
47         // Never place anything in this destructor.  This class is in place constructed and never
48         // destructed.
49     }
50 
ToLogStatisticsElement(log_id_t log_id)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                 .total_len = total_len(),
63         };
64     }
65 
Flush(LogWriter * writer,log_id_t log_id)66     bool Flush(LogWriter* writer, log_id_t log_id) const {
67         struct logger_entry entry = {};
68 
69         entry.hdr_size = sizeof(struct logger_entry);
70         entry.lid = log_id;
71         entry.pid = pid();
72         entry.tid = tid();
73         entry.uid = uid();
74         entry.sec = realtime().tv_sec;
75         entry.nsec = realtime().tv_nsec;
76         entry.len = msg_len();
77 
78         return writer->Write(entry, msg());
79     }
80 
uid()81     uid_t uid() const { return uid_; }
pid()82     pid_t pid() const { return pid_; }
tid()83     pid_t tid() const { return tid_; }
msg_len()84     uint16_t msg_len() const { return msg_len_; }
sequence()85     uint64_t sequence() const { return sequence_; }
realtime()86     log_time realtime() const { return realtime_; }
87 
msg()88     char* msg() { return reinterpret_cast<char*>(this) + sizeof(*this); }
msg()89     const char* msg() const { return reinterpret_cast<const char*>(this) + sizeof(*this); }
total_len()90     uint16_t total_len() const { return sizeof(*this) + msg_len_; }
91 
92   private:
93     const uint32_t uid_;
94     const uint32_t pid_;
95     const uint32_t tid_;
96     const uint64_t sequence_;
97     const log_time realtime_;
98     const uint16_t msg_len_;
99 };
100