blob: e7f88b94dbec067a0d1c4a98fad5b96a9aedde64 [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 Salyzyn0175b072014-02-26 09:50:16 -080024#include <sysutils/SocketClient.h>
25#include <log/log.h>
26#include <log/log_read.h>
27
Mark Salyzyn21fb7e02015-04-20 07:26:27 -070028class LogBuffer;
29
Mark Salyzyn833a9b12015-05-15 15:58:17 -070030#define EXPIRE_HOUR_THRESHOLD 24 // Only expire chatty UID logs to preserve
31 // non-chatty UIDs less than this age in hours
Mark Salyzyn047cc072015-06-04 13:35:30 -070032#define EXPIRE_THRESHOLD 10 // A smaller expire count is considered too
Mark Salyzyn833a9b12015-05-15 15:58:17 -070033 // chatty for the temporal expire messages
Mark Salyzyn047cc072015-06-04 13:35:30 -070034#define EXPIRE_RATELIMIT 10 // maximum rate in seconds to report expiration
Mark Salyzyn833a9b12015-05-15 15:58:17 -070035
Mark Salyzyn0175b072014-02-26 09:50:16 -080036class LogBufferElement {
Mark Salyzynb6bee332015-09-08 08:56:32 -070037
38 friend LogBuffer;
39
Mark Salyzyn0175b072014-02-26 09:50:16 -080040 const log_id_t mLogId;
41 const uid_t mUid;
42 const pid_t mPid;
Mark Salyzynb992d0d2014-03-20 16:09:38 -070043 const pid_t mTid;
Mark Salyzyn0175b072014-02-26 09:50:16 -080044 char *mMsg;
Mark Salyzynab0dcf62015-03-16 12:04:09 -070045 union {
46 const unsigned short mMsgLen; // mMSg != NULL
47 unsigned short mDropped; // mMsg == NULL
48 };
Mark Salyzynf7c0f752015-03-03 13:39:37 -080049 const uint64_t mSequence;
Mark Salyzynb6bee332015-09-08 08:56:32 -070050 log_time mRealTime;
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 Salyzyn21fb7e02015-04-20 07:26:27 -070054 size_t populateDroppedMessage(char *&buffer,
55 LogBuffer *parent);
Mark Salyzynab0dcf62015-03-16 12:04:09 -070056
Mark Salyzyn0175b072014-02-26 09:50:16 -080057public:
Mark Salyzyn7e2f83c2014-03-05 07:41:49 -080058 LogBufferElement(log_id_t log_id, log_time realtime,
Mark Salyzynb992d0d2014-03-20 16:09:38 -070059 uid_t uid, pid_t pid, pid_t tid,
60 const char *msg, unsigned short len);
Mark Salyzyn0175b072014-02-26 09:50:16 -080061 virtual ~LogBufferElement();
62
63 log_id_t getLogId() const { return mLogId; }
64 uid_t getUid(void) const { return mUid; }
65 pid_t getPid(void) const { return mPid; }
Mark Salyzynb992d0d2014-03-20 16:09:38 -070066 pid_t getTid(void) const { return mTid; }
Mark Salyzynab0dcf62015-03-16 12:04:09 -070067 unsigned short getDropped(void) const { return mMsg ? 0 : mDropped; }
68 unsigned short setDropped(unsigned short value) {
69 if (mMsg) {
Mark Salyzynddda2122015-10-02 09:22:52 -070070 delete [] mMsg;
Mark Salyzynab0dcf62015-03-16 12:04:09 -070071 mMsg = NULL;
72 }
73 return mDropped = value;
74 }
75 unsigned short getMsgLen() const { return mMsg ? mMsgLen : 0; }
Mark Salyzynf7c0f752015-03-03 13:39:37 -080076 uint64_t getSequence(void) const { return mSequence; }
77 static uint64_t getCurrentSequence(void) { return sequence.load(memory_order_relaxed); }
Mark Salyzyn0175b072014-02-26 09:50:16 -080078 log_time getRealTime(void) const { return mRealTime; }
79
Mark Salyzyn344bff42015-04-13 14:24:45 -070080 uint32_t getTag(void) const;
81
Mark Salyzynf7c0f752015-03-03 13:39:37 -080082 static const uint64_t FLUSH_ERROR;
Mark Salyzyn7b873652015-12-03 15:38:35 -080083 uint64_t flushTo(SocketClient *writer, LogBuffer *parent, bool privileged);
Mark Salyzyn0175b072014-02-26 09:50:16 -080084};
85
86#endif