1/*
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
20#include <stdatomic.h>
21#include <stdlib.h>
22#include <sys/types.h>
23
24#include <log/log.h>
25#include <sysutils/SocketClient.h>
26
27class LogBuffer;
28
29#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
34
35class __attribute__((packed)) LogBufferElement {
36    friend LogBuffer;
37
38    // sized to match reality of incoming log packets
39    const uint32_t mUid;
40    const uint32_t mPid;
41    const uint32_t mTid;
42    log_time mRealTime;
43    char* mMsg;
44    union {
45        const uint16_t mMsgLen;  // mDropped == false
46        uint16_t mDroppedCount;  // mDropped == true
47    };
48    const uint8_t mLogId;
49    bool mDropped;
50
51    static atomic_int_fast64_t sequence;
52
53    // assumption: mDropped == true
54    size_t populateDroppedMessage(char*& buffer, LogBuffer* parent,
55                                  bool lastSame);
56
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);
61    ~LogBufferElement();
62
63    bool isBinary(void) const {
64        return (mLogId == LOG_ID_EVENTS) || (mLogId == LOG_ID_SECURITY);
65    }
66
67    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    unsigned short getDropped(void) const {
81        return mDropped ? mDroppedCount : 0;
82    }
83    unsigned short setDropped(unsigned short value);
84    unsigned short getMsgLen() const {
85        return mDropped ? 0 : mMsgLen;
86    }
87    const char* getMsg() const {
88        return mDropped ? nullptr : mMsg;
89    }
90    log_time getRealTime(void) const {
91        return mRealTime;
92    }
93
94    static const log_time FLUSH_ERROR;
95    log_time flushTo(SocketClient* writer, LogBuffer* parent, bool privileged,
96                     bool lastSame);
97};
98
99#endif
100