| 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> | 
| Chih-Hung Hsieh | 08d470b | 2018-08-13 14:22:56 -0700 | [diff] [blame] | 21 | #include <stdint.h> | 
| Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 22 | #include <stdlib.h> | 
| Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 23 | #include <sys/types.h> | 
|  | 24 |  | 
| Mark Salyzyn | aeaaf81 | 2016-09-30 13:30:33 -0700 | [diff] [blame] | 25 | #include <log/log.h> | 
| Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 26 | #include <sysutils/SocketClient.h> | 
| Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 27 |  | 
| Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 28 | class LogBuffer; | 
|  | 29 |  | 
| Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 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 | 
| Mark Salyzyn | 833a9b1 | 2015-05-15 15:58:17 -0700 | [diff] [blame] | 35 |  | 
| Christopher Ferris | 74e74f9 | 2017-08-02 17:54:27 -0700 | [diff] [blame] | 36 | class __attribute__((packed)) LogBufferElement { | 
| Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 37 | friend LogBuffer; | 
|  | 38 |  | 
| Mark Salyzyn | 60636fa | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 39 | // sized to match reality of incoming log packets | 
| 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 { | 
| Christopher Ferris | 74e74f9 | 2017-08-02 17:54:27 -0700 | [diff] [blame] | 46 | const uint16_t mMsgLen;  // mDropped == false | 
|  | 47 | uint16_t mDroppedCount;  // mDropped == true | 
| Mark Salyzyn | 60636fa | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 48 | }; | 
|  | 49 | const uint8_t mLogId; | 
| Christopher Ferris | 74e74f9 | 2017-08-02 17:54:27 -0700 | [diff] [blame] | 50 | bool mDropped; | 
| Mark Salyzyn | 60636fa | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 51 |  | 
| Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 52 | static atomic_int_fast64_t sequence; | 
| Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 53 |  | 
| Christopher Ferris | 74e74f9 | 2017-08-02 17:54:27 -0700 | [diff] [blame] | 54 | // assumption: mDropped == true | 
| Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 55 | size_t populateDroppedMessage(char*& buffer, LogBuffer* parent, | 
| Mark Salyzyn | b5b8796 | 2017-01-23 14:20:31 -0800 | [diff] [blame] | 56 | bool lastSame); | 
| Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 57 |  | 
|  | 58 | public: | 
|  | 59 | LogBufferElement(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid, | 
| Chih-Hung Hsieh | 08d470b | 2018-08-13 14:22:56 -0700 | [diff] [blame] | 60 | pid_t tid, const char* msg, uint16_t len); | 
| Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 61 | LogBufferElement(const LogBufferElement& elem); | 
| Christopher Ferris | 74e74f9 | 2017-08-02 17:54:27 -0700 | [diff] [blame] | 62 | ~LogBufferElement(); | 
| Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 63 |  | 
| Mark Salyzyn | 60636fa | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 64 | bool isBinary(void) const { | 
|  | 65 | return (mLogId == LOG_ID_EVENTS) || (mLogId == LOG_ID_SECURITY); | 
|  | 66 | } | 
|  | 67 |  | 
| Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 68 | log_id_t getLogId() const { | 
|  | 69 | return static_cast<log_id_t>(mLogId); | 
|  | 70 | } | 
|  | 71 | uid_t getUid(void) const { | 
|  | 72 | return mUid; | 
|  | 73 | } | 
|  | 74 | pid_t getPid(void) const { | 
|  | 75 | return mPid; | 
|  | 76 | } | 
|  | 77 | pid_t getTid(void) const { | 
|  | 78 | return mTid; | 
|  | 79 | } | 
| Christopher Ferris | 74e74f9 | 2017-08-02 17:54:27 -0700 | [diff] [blame] | 80 | uint32_t getTag() const; | 
| Chih-Hung Hsieh | 08d470b | 2018-08-13 14:22:56 -0700 | [diff] [blame] | 81 | uint16_t getDropped(void) const { | 
| Christopher Ferris | 74e74f9 | 2017-08-02 17:54:27 -0700 | [diff] [blame] | 82 | return mDropped ? mDroppedCount : 0; | 
| Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 83 | } | 
| Chih-Hung Hsieh | 08d470b | 2018-08-13 14:22:56 -0700 | [diff] [blame] | 84 | uint16_t setDropped(uint16_t value); | 
|  | 85 | uint16_t getMsgLen() const { | 
| Christopher Ferris | 74e74f9 | 2017-08-02 17:54:27 -0700 | [diff] [blame] | 86 | return mDropped ? 0 : mMsgLen; | 
| Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 87 | } | 
|  | 88 | const char* getMsg() const { | 
| Christopher Ferris | 74e74f9 | 2017-08-02 17:54:27 -0700 | [diff] [blame] | 89 | return mDropped ? nullptr : mMsg; | 
| Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 90 | } | 
| Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 91 | log_time getRealTime(void) const { | 
|  | 92 | return mRealTime; | 
|  | 93 | } | 
| Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 94 |  | 
| Mark Salyzyn | 5a34d6e | 2017-03-10 08:44:14 -0800 | [diff] [blame] | 95 | static const log_time FLUSH_ERROR; | 
|  | 96 | log_time flushTo(SocketClient* writer, LogBuffer* parent, bool privileged, | 
| Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 97 | bool lastSame); | 
| Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 98 | }; | 
|  | 99 |  | 
|  | 100 | #endif |