blob: a777970d46afe23dd181b972190372458ab630cb [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
Tom Cherry64458c72019-10-15 15:10:26 -070017#pragma once
Mark Salyzyn0175b072014-02-26 09:50:16 -080018
Mark Salyzynf7c0f752015-03-03 13:39:37 -080019#include <stdatomic.h>
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -070020#include <stdint.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>
Tom Cherry283c9a12020-05-14 19:25:05 -070025
26#include "LogWriter.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080027
Tom Cherry64e90162020-05-07 14:44:43 -070028class LogStatistics;
Mark Salyzyn21fb7e02015-04-20 07:26:27 -070029
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 Salyzyn60636fa2016-10-24 16:22:17 -070037 // sized to match reality of incoming log packets
Mark Salyzyn60636fa2016-10-24 16:22:17 -070038 const uint32_t mUid;
39 const uint32_t mPid;
40 const uint32_t mTid;
Tom Cherry10d086e2019-08-21 14:16:34 -070041 uint64_t mSequence;
Mark Salyzynb6bee332015-09-08 08:56:32 -070042 log_time mRealTime;
Tom Cherrybe0f4ab2019-08-23 09:09:40 -070043 union {
44 char* mMsg; // mDropped == false
45 int32_t mTag; // mDropped == true
46 };
Mark Salyzyn60636fa2016-10-24 16:22:17 -070047 union {
Christopher Ferris74e74f92017-08-02 17:54:27 -070048 const uint16_t mMsgLen; // mDropped == false
49 uint16_t mDroppedCount; // mDropped == true
Mark Salyzyn60636fa2016-10-24 16:22:17 -070050 };
51 const uint8_t mLogId;
Christopher Ferris74e74f92017-08-02 17:54:27 -070052 bool mDropped;
Mark Salyzyn60636fa2016-10-24 16:22:17 -070053
Mark Salyzynf7c0f752015-03-03 13:39:37 -080054 static atomic_int_fast64_t sequence;
Mark Salyzyn0175b072014-02-26 09:50:16 -080055
Christopher Ferris74e74f92017-08-02 17:54:27 -070056 // assumption: mDropped == true
Tom Cherry64e90162020-05-07 14:44:43 -070057 size_t populateDroppedMessage(char*& buffer, LogStatistics* parent, bool lastSame);
Mark Salyzyn501c3732017-03-10 14:31:54 -080058
Tom Cherry64e90162020-05-07 14:44:43 -070059 public:
Mark Salyzyn501c3732017-03-10 14:31:54 -080060 LogBufferElement(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid,
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -070061 pid_t tid, const char* msg, uint16_t len);
Mark Salyzyn501c3732017-03-10 14:31:54 -080062 LogBufferElement(const LogBufferElement& elem);
Tom Cherry13224722020-05-19 18:02:00 -070063 LogBufferElement(LogBufferElement&& elem);
Christopher Ferris74e74f92017-08-02 17:54:27 -070064 ~LogBufferElement();
Mark Salyzyn0175b072014-02-26 09:50:16 -080065
Mark Salyzyn60636fa2016-10-24 16:22:17 -070066 bool isBinary(void) const {
67 return (mLogId == LOG_ID_EVENTS) || (mLogId == LOG_ID_SECURITY);
68 }
69
Mark Salyzyn501c3732017-03-10 14:31:54 -080070 log_id_t getLogId() const {
71 return static_cast<log_id_t>(mLogId);
72 }
73 uid_t getUid(void) const {
74 return mUid;
75 }
76 pid_t getPid(void) const {
77 return mPid;
78 }
79 pid_t getTid(void) const {
80 return mTid;
81 }
Christopher Ferris74e74f92017-08-02 17:54:27 -070082 uint32_t getTag() const;
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -070083 uint16_t getDropped(void) const {
Christopher Ferris74e74f92017-08-02 17:54:27 -070084 return mDropped ? mDroppedCount : 0;
Mark Salyzyn501c3732017-03-10 14:31:54 -080085 }
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -070086 uint16_t setDropped(uint16_t value);
87 uint16_t getMsgLen() const {
Christopher Ferris74e74f92017-08-02 17:54:27 -070088 return mDropped ? 0 : mMsgLen;
Mark Salyzyn501c3732017-03-10 14:31:54 -080089 }
90 const char* getMsg() const {
Christopher Ferris74e74f92017-08-02 17:54:27 -070091 return mDropped ? nullptr : mMsg;
Mark Salyzyn501c3732017-03-10 14:31:54 -080092 }
Tom Cherry10d086e2019-08-21 14:16:34 -070093 uint64_t getSequence() const { return mSequence; }
94 static uint64_t getCurrentSequence() { return sequence.load(memory_order_relaxed); }
Mark Salyzyn501c3732017-03-10 14:31:54 -080095 log_time getRealTime(void) const {
96 return mRealTime;
97 }
Mark Salyzyn0175b072014-02-26 09:50:16 -080098
Tom Cherry283c9a12020-05-14 19:25:05 -070099 bool FlushTo(LogWriter* writer, LogStatistics* parent, bool lastSame);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800100};