blob: 814ec87857435b1e626bb036239def152315d86a [file] [log] [blame]
Mark Salyzyn0175b072014-02-26 09:50:16 -08001/*
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 Salyzynf7c0f752015-03-03 13:39:37 -080020#include <stdatomic.h>
Mark Salyzynab0dcf62015-03-16 12:04:09 -070021#include <stdlib.h>
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -070022#include <sys/types.h>
23
Mark Salyzynaeaaf812016-09-30 13:30:33 -070024#include <log/log.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080025#include <sysutils/SocketClient.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080026
Mark Salyzyn21fb7e02015-04-20 07:26:27 -070027class LogBuffer;
28
Mark Salyzyn501c3732017-03-10 14:31:54 -080029#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 Salyzyn833a9b12015-05-15 15:58:17 -070034
Mark Salyzyn0175b072014-02-26 09:50:16 -080035class LogBufferElement {
Mark Salyzynb6bee332015-09-08 08:56:32 -070036 friend LogBuffer;
37
Mark Salyzyn60636fa2016-10-24 16:22:17 -070038 // sized to match reality of incoming log packets
Mark Salyzyn501c3732017-03-10 14:31:54 -080039 uint32_t mTag; // only valid for isBinary()
Mark Salyzyn60636fa2016-10-24 16:22:17 -070040 const uint32_t mUid;
41 const uint32_t mPid;
42 const uint32_t mTid;
Mark Salyzynb6bee332015-09-08 08:56:32 -070043 log_time mRealTime;
Mark Salyzyn501c3732017-03-10 14:31:54 -080044 char* mMsg;
Mark Salyzyn60636fa2016-10-24 16:22:17 -070045 union {
Mark Salyzyn501c3732017-03-10 14:31:54 -080046 const uint16_t mMsgLen; // mMSg != NULL
47 uint16_t mDropped; // mMsg == NULL
Mark Salyzyn60636fa2016-10-24 16:22:17 -070048 };
49 const uint8_t mLogId;
50
Mark Salyzynf7c0f752015-03-03 13:39:37 -080051 static atomic_int_fast64_t sequence;
Mark Salyzyn0175b072014-02-26 09:50:16 -080052
Mark Salyzynab0dcf62015-03-16 12:04:09 -070053 // assumption: mMsg == NULL
Mark Salyzyn501c3732017-03-10 14:31:54 -080054 size_t populateDroppedMessage(char*& buffer, LogBuffer* parent,
Mark Salyzynb5b87962017-01-23 14:20:31 -080055 bool lastSame);
Mark Salyzyn501c3732017-03-10 14:31:54 -080056
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 Salyzyn0175b072014-02-26 09:50:16 -080061 virtual ~LogBufferElement();
62
Mark Salyzyn60636fa2016-10-24 16:22:17 -070063 bool isBinary(void) const {
64 return (mLogId == LOG_ID_EVENTS) || (mLogId == LOG_ID_SECURITY);
65 }
66
Mark Salyzyn501c3732017-03-10 14:31:54 -080067 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 Salyzynab0dcf62015-03-16 12:04:09 -070085 unsigned short setDropped(unsigned short value) {
86 if (mMsg) {
Mark Salyzyn501c3732017-03-10 14:31:54 -080087 delete[] mMsg;
Mark Salyzynab0dcf62015-03-16 12:04:09 -070088 mMsg = NULL;
89 }
90 return mDropped = value;
91 }
Mark Salyzyn501c3732017-03-10 14:31:54 -080092 unsigned short getMsgLen() const {
93 return mMsg ? mMsgLen : 0;
94 }
95 const char* getMsg() const {
96 return mMsg;
97 }
Mark Salyzyn501c3732017-03-10 14:31:54 -080098 log_time getRealTime(void) const {
99 return mRealTime;
100 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800101
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800102 static const log_time FLUSH_ERROR;
103 log_time flushTo(SocketClient* writer, LogBuffer* parent, bool privileged,
Mark Salyzyn501c3732017-03-10 14:31:54 -0800104 bool lastSame);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800105};
106
107#endif