blob: da4991b28c7d330f79cb55d2a0c27791208e597c [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>
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -070021#include <stdint.h>
Mark Salyzynab0dcf62015-03-16 12:04:09 -070022#include <stdlib.h>
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -070023#include <sys/types.h>
24
Mark Salyzynaeaaf812016-09-30 13:30:33 -070025#include <log/log.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080026#include <sysutils/SocketClient.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080027
Mark Salyzyn21fb7e02015-04-20 07:26:27 -070028class LogBuffer;
29
Mark Salyzyn501c3732017-03-10 14:31:54 -080030#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 Salyzyn833a9b12015-05-15 15:58:17 -070035
Christopher Ferris74e74f92017-08-02 17:54:27 -070036class __attribute__((packed)) LogBufferElement {
Mark Salyzynb6bee332015-09-08 08:56:32 -070037 friend LogBuffer;
38
Mark Salyzyn60636fa2016-10-24 16:22:17 -070039 // sized to match reality of incoming log packets
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;
Tom Cherrybe0f4ab2019-08-23 09:09:40 -070044 union {
45 char* mMsg; // mDropped == false
46 int32_t mTag; // mDropped == true
47 };
Mark Salyzyn60636fa2016-10-24 16:22:17 -070048 union {
Christopher Ferris74e74f92017-08-02 17:54:27 -070049 const uint16_t mMsgLen; // mDropped == false
50 uint16_t mDroppedCount; // mDropped == true
Mark Salyzyn60636fa2016-10-24 16:22:17 -070051 };
52 const uint8_t mLogId;
Christopher Ferris74e74f92017-08-02 17:54:27 -070053 bool mDropped;
Mark Salyzyn60636fa2016-10-24 16:22:17 -070054
Mark Salyzynf7c0f752015-03-03 13:39:37 -080055 static atomic_int_fast64_t sequence;
Mark Salyzyn0175b072014-02-26 09:50:16 -080056
Christopher Ferris74e74f92017-08-02 17:54:27 -070057 // assumption: mDropped == true
Mark Salyzyn501c3732017-03-10 14:31:54 -080058 size_t populateDroppedMessage(char*& buffer, LogBuffer* parent,
Mark Salyzynb5b87962017-01-23 14:20:31 -080059 bool lastSame);
Mark Salyzyn501c3732017-03-10 14:31:54 -080060
61 public:
62 LogBufferElement(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid,
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -070063 pid_t tid, const char* msg, uint16_t len);
Mark Salyzyn501c3732017-03-10 14:31:54 -080064 LogBufferElement(const LogBufferElement& elem);
Christopher Ferris74e74f92017-08-02 17:54:27 -070065 ~LogBufferElement();
Mark Salyzyn0175b072014-02-26 09:50:16 -080066
Mark Salyzyn60636fa2016-10-24 16:22:17 -070067 bool isBinary(void) const {
68 return (mLogId == LOG_ID_EVENTS) || (mLogId == LOG_ID_SECURITY);
69 }
70
Mark Salyzyn501c3732017-03-10 14:31:54 -080071 log_id_t getLogId() const {
72 return static_cast<log_id_t>(mLogId);
73 }
74 uid_t getUid(void) const {
75 return mUid;
76 }
77 pid_t getPid(void) const {
78 return mPid;
79 }
80 pid_t getTid(void) const {
81 return mTid;
82 }
Christopher Ferris74e74f92017-08-02 17:54:27 -070083 uint32_t getTag() const;
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -070084 uint16_t getDropped(void) const {
Christopher Ferris74e74f92017-08-02 17:54:27 -070085 return mDropped ? mDroppedCount : 0;
Mark Salyzyn501c3732017-03-10 14:31:54 -080086 }
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -070087 uint16_t setDropped(uint16_t value);
88 uint16_t getMsgLen() const {
Christopher Ferris74e74f92017-08-02 17:54:27 -070089 return mDropped ? 0 : mMsgLen;
Mark Salyzyn501c3732017-03-10 14:31:54 -080090 }
91 const char* getMsg() const {
Christopher Ferris74e74f92017-08-02 17:54:27 -070092 return mDropped ? nullptr : mMsg;
Mark Salyzyn501c3732017-03-10 14:31:54 -080093 }
Mark Salyzyn501c3732017-03-10 14:31:54 -080094 log_time getRealTime(void) const {
95 return mRealTime;
96 }
Mark Salyzyn0175b072014-02-26 09:50:16 -080097
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -080098 static const log_time FLUSH_ERROR;
99 log_time flushTo(SocketClient* writer, LogBuffer* parent, bool privileged,
Mark Salyzyn501c3732017-03-10 14:31:54 -0800100 bool lastSame);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800101};
102
103#endif