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 #ifndef _LOGD_LOG_BUFFER_ELEMENT_H__
18 #define _LOGD_LOG_BUFFER_ELEMENT_H__
19 
20 #include <stdatomic.h>
21 #include <stdlib.h>
22 #include <sys/types.h>
23 
24 #include <log/log.h>
25 #include <sysutils/SocketClient.h>
26 
27 class LogBuffer;
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 LogBufferElement {
36     friend LogBuffer;
37 
38     // sized to match reality of incoming log packets
39     uint32_t mTag;  // only valid for isBinary()
40     const uint32_t mUid;
41     const uint32_t mPid;
42     const uint32_t mTid;
43     log_time mRealTime;
44     char* mMsg;
45     union {
46         const uint16_t mMsgLen;  // mMSg != NULL
47         uint16_t mDropped;       // mMsg == NULL
48     };
49     const uint8_t mLogId;
50 
51     static atomic_int_fast64_t sequence;
52 
53     // assumption: mMsg == NULL
54     size_t populateDroppedMessage(char*& buffer, LogBuffer* parent,
55                                   bool lastSame);
56 
57    public:
58     LogBufferElement(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid,
59                      pid_t tid, const char* msg, unsigned short len);
60     LogBufferElement(const LogBufferElement& elem);
61     virtual ~LogBufferElement();
62 
isBinary(void)63     bool isBinary(void) const {
64         return (mLogId == LOG_ID_EVENTS) || (mLogId == LOG_ID_SECURITY);
65     }
66 
getLogId()67     log_id_t getLogId() const {
68         return static_cast<log_id_t>(mLogId);
69     }
getUid(void)70     uid_t getUid(void) const {
71         return mUid;
72     }
getPid(void)73     pid_t getPid(void) const {
74         return mPid;
75     }
getTid(void)76     pid_t getTid(void) const {
77         return mTid;
78     }
getTag()79     uint32_t getTag() const {
80         return mTag;
81     }
getDropped(void)82     unsigned short getDropped(void) const {
83         return mMsg ? 0 : mDropped;
84     }
setDropped(unsigned short value)85     unsigned short setDropped(unsigned short value) {
86         if (mMsg) {
87             delete[] mMsg;
88             mMsg = NULL;
89         }
90         return mDropped = value;
91     }
getMsgLen()92     unsigned short getMsgLen() const {
93         return mMsg ? mMsgLen : 0;
94     }
getMsg()95     const char* getMsg() const {
96         return mMsg;
97     }
getRealTime(void)98     log_time getRealTime(void) const {
99         return mRealTime;
100     }
101 
102     static const log_time FLUSH_ERROR;
103     log_time flushTo(SocketClient* writer, LogBuffer* parent, bool privileged,
104                      bool lastSame);
105 };
106 
107 #endif
108