• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <stdatomic.h>
20 #include <stdint.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 __attribute__((packed)) LogBufferElement {
36     friend LogBuffer;
37 
38     // sized to match reality of incoming log packets
39     const uint32_t mUid;
40     const uint32_t mPid;
41     const uint32_t mTid;
42     log_time mRealTime;
43     union {
44         char* mMsg;    // mDropped == false
45         int32_t mTag;  // mDropped == true
46     };
47     union {
48         const uint16_t mMsgLen;  // mDropped == false
49         uint16_t mDroppedCount;  // mDropped == true
50     };
51     const uint8_t mLogId;
52     bool mDropped;
53 
54     static atomic_int_fast64_t sequence;
55 
56     // assumption: mDropped == true
57     size_t populateDroppedMessage(char*& buffer, LogBuffer* parent,
58                                   bool lastSame);
59 
60    public:
61     LogBufferElement(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid,
62                      pid_t tid, const char* msg, uint16_t len);
63     LogBufferElement(const LogBufferElement& elem);
64     ~LogBufferElement();
65 
isBinary(void)66     bool isBinary(void) const {
67         return (mLogId == LOG_ID_EVENTS) || (mLogId == LOG_ID_SECURITY);
68     }
69 
getLogId()70     log_id_t getLogId() const {
71         return static_cast<log_id_t>(mLogId);
72     }
getUid(void)73     uid_t getUid(void) const {
74         return mUid;
75     }
getPid(void)76     pid_t getPid(void) const {
77         return mPid;
78     }
getTid(void)79     pid_t getTid(void) const {
80         return mTid;
81     }
82     uint32_t getTag() const;
getDropped(void)83     uint16_t getDropped(void) const {
84         return mDropped ? mDroppedCount : 0;
85     }
86     uint16_t setDropped(uint16_t value);
getMsgLen()87     uint16_t getMsgLen() const {
88         return mDropped ? 0 : mMsgLen;
89     }
getMsg()90     const char* getMsg() const {
91         return mDropped ? nullptr : mMsg;
92     }
getRealTime(void)93     log_time getRealTime(void) const {
94         return mRealTime;
95     }
96 
97     static const log_time FLUSH_ERROR;
98     log_time flushTo(SocketClient* writer, LogBuffer* parent, bool lastSame);
99 };
100