1 /*
2  * Copyright (C) 2012-2014 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 
25 #include "LogWriter.h"
26 
27 #include "LogStatistics.h"
28 
29 #define EXPIRE_HOUR_THRESHOLD 24  // Only expire chatty UID logs to preserve
30                                   // non-chatty UIDs less than this age in hours
31 #define EXPIRE_THRESHOLD 10       // A smaller expire count is considered too
32                                   // chatty for the temporal expire messages
33 #define EXPIRE_RATELIMIT 10  // maximum rate in seconds to report expiration
34 
35 class __attribute__((packed)) LogBufferElement {
36   public:
37     LogBufferElement(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid, pid_t tid,
38                      uint64_t sequence, const char* msg, uint16_t len);
39     LogBufferElement(const LogBufferElement& elem);
40     LogBufferElement(LogBufferElement&& elem) noexcept;
41     ~LogBufferElement();
42 
43     uint32_t GetTag() const;
44     uint16_t SetDropped(uint16_t value);
45 
46     bool FlushTo(LogWriter* writer, LogStatistics* parent, bool lastSame);
47 
48     LogStatisticsElement ToLogStatisticsElement() const;
49 
log_id()50     log_id_t log_id() const { return static_cast<log_id_t>(log_id_); }
uid()51     uid_t uid() const { return uid_; }
pid()52     pid_t pid() const { return pid_; }
tid()53     pid_t tid() const { return tid_; }
msg_len()54     uint16_t msg_len() const { return dropped_ ? 0 : msg_len_; }
msg()55     const char* msg() const { return dropped_ ? nullptr : msg_; }
sequence()56     uint64_t sequence() const { return sequence_; }
realtime()57     log_time realtime() const { return realtime_; }
dropped_count()58     uint16_t dropped_count() const { return dropped_ ? dropped_count_ : 0; }
59 
60   private:
61     // assumption: mDropped == true
62     size_t PopulateDroppedMessage(char*& buffer, LogStatistics* parent, bool lastSame);
63 
64     // sized to match reality of incoming log packets
65     const uint32_t uid_;
66     const uint32_t pid_;
67     const uint32_t tid_;
68     uint64_t sequence_;
69     log_time realtime_;
70     union {
71         char* msg_;    // mDropped == false
72         int32_t tag_;  // mDropped == true
73     };
74     union {
75         const uint16_t msg_len_;  // mDropped == false
76         uint16_t dropped_count_;  // mDropped == true
77     };
78     const uint8_t log_id_;
79     bool dropped_;
80 };
81