Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 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 | |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 20 | #include <stdatomic.h> |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 21 | #include <stdlib.h> |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 22 | #include <sys/types.h> |
| 23 | |
Mark Salyzyn | aeaaf81 | 2016-09-30 13:30:33 -0700 | [diff] [blame] | 24 | #include <log/log.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 25 | #include <sysutils/SocketClient.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 26 | |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 27 | class LogBuffer; |
| 28 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 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 |
Mark Salyzyn | 833a9b1 | 2015-05-15 15:58:17 -0700 | [diff] [blame] | 34 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 35 | class LogBufferElement { |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 36 | friend LogBuffer; |
| 37 | |
Mark Salyzyn | 60636fa | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 38 | // sized to match reality of incoming log packets |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 39 | uint32_t mTag; // only valid for isBinary() |
Mark Salyzyn | 60636fa | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 40 | const uint32_t mUid; |
| 41 | const uint32_t mPid; |
| 42 | const uint32_t mTid; |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 43 | log_time mRealTime; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 44 | char* mMsg; |
Mark Salyzyn | 60636fa | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 45 | union { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 46 | const uint16_t mMsgLen; // mMSg != NULL |
| 47 | uint16_t mDropped; // mMsg == NULL |
Mark Salyzyn | 60636fa | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 48 | }; |
| 49 | const uint8_t mLogId; |
| 50 | |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 51 | static atomic_int_fast64_t sequence; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 52 | |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 53 | // assumption: mMsg == NULL |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 54 | size_t populateDroppedMessage(char*& buffer, LogBuffer* parent, |
Mark Salyzyn | b5b8796 | 2017-01-23 14:20:31 -0800 | [diff] [blame] | 55 | bool lastSame); |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 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); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 61 | virtual ~LogBufferElement(); |
| 62 | |
Mark Salyzyn | 60636fa | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 63 | bool isBinary(void) const { |
| 64 | return (mLogId == LOG_ID_EVENTS) || (mLogId == LOG_ID_SECURITY); |
| 65 | } |
| 66 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 67 | log_id_t getLogId() const { |
| 68 | return static_cast<log_id_t>(mLogId); |
| 69 | } |
| 70 | uid_t getUid(void) const { |
| 71 | return mUid; |
| 72 | } |
| 73 | pid_t getPid(void) const { |
| 74 | return mPid; |
| 75 | } |
| 76 | pid_t getTid(void) const { |
| 77 | return mTid; |
| 78 | } |
| 79 | uint32_t getTag() const { |
| 80 | return mTag; |
| 81 | } |
| 82 | unsigned short getDropped(void) const { |
| 83 | return mMsg ? 0 : mDropped; |
| 84 | } |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 85 | unsigned short setDropped(unsigned short value) { |
| 86 | if (mMsg) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 87 | delete[] mMsg; |
Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 88 | mMsg = NULL; |
| 89 | } |
| 90 | return mDropped = value; |
| 91 | } |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 92 | unsigned short getMsgLen() const { |
| 93 | return mMsg ? mMsgLen : 0; |
| 94 | } |
| 95 | const char* getMsg() const { |
| 96 | return mMsg; |
| 97 | } |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 98 | log_time getRealTime(void) const { |
| 99 | return mRealTime; |
| 100 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 101 | |
Mark Salyzyn | 5a34d6e | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 102 | static const log_time FLUSH_ERROR; |
| 103 | log_time flushTo(SocketClient* writer, LogBuffer* parent, bool privileged, |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 104 | bool lastSame); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | #endif |