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 <stdint.h> 22 #include <stdlib.h> 23 #include <sys/types.h> 24 25 #include <log/log.h> 26 #include <sysutils/SocketClient.h> 27 28 class LogBuffer; 29 30 #define EXPIRE_HOUR_THRESHOLD 24 // Only expire chatty UID logs to preserve 31 // non-chatty UIDs less than this age in hours 32 #define EXPIRE_THRESHOLD 10 // A smaller expire count is considered too 33 // chatty for the temporal expire messages 34 #define EXPIRE_RATELIMIT 10 // maximum rate in seconds to report expiration 35 36 class __attribute__((packed)) LogBufferElement { 37 friend LogBuffer; 38 39 // sized to match reality of incoming log packets 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; // mDropped == false 47 uint16_t mDroppedCount; // mDropped == true 48 }; 49 const uint8_t mLogId; 50 bool mDropped; 51 52 static atomic_int_fast64_t sequence; 53 54 // assumption: mDropped == true 55 size_t populateDroppedMessage(char*& buffer, LogBuffer* parent, 56 bool lastSame); 57 58 public: 59 LogBufferElement(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid, 60 pid_t tid, const char* msg, uint16_t len); 61 LogBufferElement(const LogBufferElement& elem); 62 ~LogBufferElement(); 63 isBinary(void)64 bool isBinary(void) const { 65 return (mLogId == LOG_ID_EVENTS) || (mLogId == LOG_ID_SECURITY); 66 } 67 getLogId()68 log_id_t getLogId() const { 69 return static_cast<log_id_t>(mLogId); 70 } getUid(void)71 uid_t getUid(void) const { 72 return mUid; 73 } getPid(void)74 pid_t getPid(void) const { 75 return mPid; 76 } getTid(void)77 pid_t getTid(void) const { 78 return mTid; 79 } 80 uint32_t getTag() const; getDropped(void)81 uint16_t getDropped(void) const { 82 return mDropped ? mDroppedCount : 0; 83 } 84 uint16_t setDropped(uint16_t value); getMsgLen()85 uint16_t getMsgLen() const { 86 return mDropped ? 0 : mMsgLen; 87 } getMsg()88 const char* getMsg() const { 89 return mDropped ? nullptr : mMsg; 90 } getRealTime(void)91 log_time getRealTime(void) const { 92 return mRealTime; 93 } 94 95 static const log_time FLUSH_ERROR; 96 log_time flushTo(SocketClient* writer, LogBuffer* parent, bool privileged, 97 bool lastSame); 98 }; 99 100 #endif 101