blob: 90756dd4b3a1425b839bca2755d346721599bac2 [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 Salyzyn1d84f0b2017-03-03 10:21:23 -080043 uint64_t mSequence;
Mark Salyzynb6bee332015-09-08 08:56:32 -070044 log_time mRealTime;
Mark Salyzyn501c3732017-03-10 14:31:54 -080045 char* mMsg;
Mark Salyzyn60636fa2016-10-24 16:22:17 -070046 union {
Mark Salyzyn501c3732017-03-10 14:31:54 -080047 const uint16_t mMsgLen; // mMSg != NULL
48 uint16_t mDropped; // mMsg == NULL
Mark Salyzyn60636fa2016-10-24 16:22:17 -070049 };
50 const uint8_t mLogId;
51
Mark Salyzynf7c0f752015-03-03 13:39:37 -080052 static atomic_int_fast64_t sequence;
Mark Salyzyn0175b072014-02-26 09:50:16 -080053
Mark Salyzynab0dcf62015-03-16 12:04:09 -070054 // assumption: mMsg == NULL
Mark Salyzyn501c3732017-03-10 14:31:54 -080055 size_t populateDroppedMessage(char*& buffer, LogBuffer* parent,
Mark Salyzynb5b87962017-01-23 14:20:31 -080056 bool lastSame);
Mark Salyzyn501c3732017-03-10 14:31:54 -080057
58 public:
59 LogBufferElement(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid,
60 pid_t tid, const char* msg, unsigned short len);
61 LogBufferElement(const LogBufferElement& elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -080062 virtual ~LogBufferElement();
63
Mark Salyzyn60636fa2016-10-24 16:22:17 -070064 bool isBinary(void) const {
65 return (mLogId == LOG_ID_EVENTS) || (mLogId == LOG_ID_SECURITY);
66 }
67
Mark Salyzyn501c3732017-03-10 14:31:54 -080068 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 }
80 uint32_t getTag() const {
81 return mTag;
82 }
83 unsigned short getDropped(void) const {
84 return mMsg ? 0 : mDropped;
85 }
Mark Salyzynab0dcf62015-03-16 12:04:09 -070086 unsigned short setDropped(unsigned short value) {
87 if (mMsg) {
Mark Salyzyn501c3732017-03-10 14:31:54 -080088 delete[] mMsg;
Mark Salyzynab0dcf62015-03-16 12:04:09 -070089 mMsg = NULL;
90 }
91 return mDropped = value;
92 }
Mark Salyzyn501c3732017-03-10 14:31:54 -080093 unsigned short getMsgLen() const {
94 return mMsg ? mMsgLen : 0;
95 }
96 const char* getMsg() const {
97 return mMsg;
98 }
99 uint64_t getSequence(void) const {
100 return mSequence;
101 }
102 static uint64_t getCurrentSequence(void) {
103 return sequence.load(memory_order_relaxed);
104 }
105 log_time getRealTime(void) const {
106 return mRealTime;
107 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800108
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800109 static const uint64_t FLUSH_ERROR;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800110 uint64_t flushTo(SocketClient* writer, LogBuffer* parent, bool privileged,
111 bool lastSame);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800112};
113
114#endif