| 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 |  | 
| Tom Cherry | 64458c7 | 2019-10-15 15:10:26 -0700 | [diff] [blame] | 17 | #pragma once | 
| Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 18 |  | 
| Chih-Hung Hsieh | 08d470b | 2018-08-13 14:22:56 -0700 | [diff] [blame] | 19 | #include <stdint.h> | 
| Mark Salyzyn | ab0dcf6 | 2015-03-16 12:04:09 -0700 | [diff] [blame] | 20 | #include <stdlib.h> | 
| Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 21 | #include <sys/types.h> | 
 | 22 |  | 
| Mark Salyzyn | aeaaf81 | 2016-09-30 13:30:33 -0700 | [diff] [blame] | 23 | #include <log/log.h> | 
| Tom Cherry | 283c9a1 | 2020-05-14 19:25:05 -0700 | [diff] [blame] | 24 |  | 
 | 25 | #include "LogWriter.h" | 
| Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 26 |  | 
| Tom Cherry | 64e9016 | 2020-05-07 14:44:43 -0700 | [diff] [blame] | 27 | class LogStatistics; | 
| Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 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 | 60636fa | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 36 |     // sized to match reality of incoming log packets | 
| Mark Salyzyn | 60636fa | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 37 |     const uint32_t mUid; | 
 | 38 |     const uint32_t mPid; | 
 | 39 |     const uint32_t mTid; | 
| Tom Cherry | 10d086e | 2019-08-21 14:16:34 -0700 | [diff] [blame] | 40 |     uint64_t mSequence; | 
| Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 41 |     log_time mRealTime; | 
| Tom Cherry | be0f4ab | 2019-08-23 09:09:40 -0700 | [diff] [blame] | 42 |     union { | 
 | 43 |         char* mMsg;    // mDropped == false | 
 | 44 |         int32_t mTag;  // mDropped == true | 
 | 45 |     }; | 
| Mark Salyzyn | 60636fa | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 46 |     union { | 
| Christopher Ferris | 74e74f9 | 2017-08-02 17:54:27 -0700 | [diff] [blame] | 47 |         const uint16_t mMsgLen;  // mDropped == false | 
 | 48 |         uint16_t mDroppedCount;  // mDropped == true | 
| Mark Salyzyn | 60636fa | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 49 |     }; | 
 | 50 |     const uint8_t mLogId; | 
| Christopher Ferris | 74e74f9 | 2017-08-02 17:54:27 -0700 | [diff] [blame] | 51 |     bool mDropped; | 
| Mark Salyzyn | 60636fa | 2016-10-24 16:22:17 -0700 | [diff] [blame] | 52 |  | 
| Christopher Ferris | 74e74f9 | 2017-08-02 17:54:27 -0700 | [diff] [blame] | 53 |     // assumption: mDropped == true | 
| Tom Cherry | 64e9016 | 2020-05-07 14:44:43 -0700 | [diff] [blame] | 54 |     size_t populateDroppedMessage(char*& buffer, LogStatistics* parent, bool lastSame); | 
| Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 55 |  | 
| Tom Cherry | 64e9016 | 2020-05-07 14:44:43 -0700 | [diff] [blame] | 56 |   public: | 
| Tom Cherry | a3c5ff5 | 2020-05-21 13:56:33 -0700 | [diff] [blame] | 57 |     LogBufferElement(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid, pid_t tid, | 
 | 58 |                      uint64_t sequence, const char* msg, uint16_t len); | 
| Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 59 |     LogBufferElement(const LogBufferElement& elem); | 
| Tom Cherry | 1322472 | 2020-05-19 18:02:00 -0700 | [diff] [blame] | 60 |     LogBufferElement(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; | 
| Chih-Hung Hsieh | 08d470b | 2018-08-13 14:22:56 -0700 | [diff] [blame] | 80 |     uint16_t 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 |     } | 
| Chih-Hung Hsieh | 08d470b | 2018-08-13 14:22:56 -0700 | [diff] [blame] | 83 |     uint16_t setDropped(uint16_t value); | 
 | 84 |     uint16_t 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 |     } | 
| Tom Cherry | 10d086e | 2019-08-21 14:16:34 -0700 | [diff] [blame] | 90 |     uint64_t getSequence() const { return mSequence; } | 
| 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 |  | 
| Tom Cherry | 283c9a1 | 2020-05-14 19:25:05 -0700 | [diff] [blame] | 95 |     bool FlushTo(LogWriter* writer, LogStatistics* parent, bool lastSame); | 
| Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 96 | }; |