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 | |
Christopher Ferris | 74e74f9 | 2017-08-02 17:54:27 -0700 | [diff] [blame] | 35 | class __attribute__((packed)) 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 | 60636fa | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 39 | const uint32_t mUid; |
| 40 | const uint32_t mPid; |
| 41 | const uint32_t mTid; |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 42 | log_time mRealTime; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 43 | char* mMsg; |
Mark Salyzyn | 60636fa | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 44 | union { |
Christopher Ferris | 74e74f9 | 2017-08-02 17:54:27 -0700 | [diff] [blame] | 45 | const uint16_t mMsgLen; // mDropped == false |
| 46 | uint16_t mDroppedCount; // mDropped == true |
Mark Salyzyn | 60636fa | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 47 | }; |
| 48 | const uint8_t mLogId; |
Christopher Ferris | 74e74f9 | 2017-08-02 17:54:27 -0700 | [diff] [blame] | 49 | bool mDropped; |
Mark Salyzyn | 60636fa | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 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 | |
Christopher Ferris | 74e74f9 | 2017-08-02 17:54:27 -0700 | [diff] [blame] | 53 | // assumption: mDropped == true |
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); |
Christopher Ferris | 74e74f9 | 2017-08-02 17:54:27 -0700 | [diff] [blame] | 61 | ~LogBufferElement(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 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 | } |
Christopher Ferris | 74e74f9 | 2017-08-02 17:54:27 -0700 | [diff] [blame] | 79 | uint32_t getTag() const; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 80 | unsigned short getDropped(void) const { |
Christopher Ferris | 74e74f9 | 2017-08-02 17:54:27 -0700 | [diff] [blame] | 81 | return mDropped ? mDroppedCount : 0; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 82 | } |
Christopher Ferris | 74e74f9 | 2017-08-02 17:54:27 -0700 | [diff] [blame] | 83 | unsigned short setDropped(unsigned short value); |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 84 | unsigned short getMsgLen() const { |
Christopher Ferris | 74e74f9 | 2017-08-02 17:54:27 -0700 | [diff] [blame] | 85 | return mDropped ? 0 : mMsgLen; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 86 | } |
| 87 | const char* getMsg() const { |
Christopher Ferris | 74e74f9 | 2017-08-02 17:54:27 -0700 | [diff] [blame] | 88 | return mDropped ? nullptr : mMsg; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 89 | } |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 90 | log_time getRealTime(void) const { |
| 91 | return mRealTime; |
| 92 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 93 | |
Mark Salyzyn | 5a34d6e | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 94 | static const log_time FLUSH_ERROR; |
| 95 | log_time flushTo(SocketClient* writer, LogBuffer* parent, bool privileged, |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 96 | bool lastSame); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | #endif |