LogBufferElement.h revision 5ac5c6b19364b5b3061a59db796b2357c95c3b64
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 <sysutils/SocketClient.h>
25#include <log/log.h>
26#include <log/log_read.h>
27
28class LogBuffer;
29
30#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
35
36class LogBufferElement {
37    const log_id_t mLogId;
38    const uid_t mUid;
39    const pid_t mPid;
40    const pid_t mTid;
41    char *mMsg;
42    union {
43        const unsigned short mMsgLen; // mMSg != NULL
44        unsigned short mDropped;      // mMsg == NULL
45    };
46    const uint64_t mSequence;
47    const log_time mRealTime;
48    static atomic_int_fast64_t sequence;
49
50    // assumption: mMsg == NULL
51    size_t populateDroppedMessage(char *&buffer,
52                                  LogBuffer *parent);
53
54public:
55    LogBufferElement(log_id_t log_id, log_time realtime,
56                     uid_t uid, pid_t pid, pid_t tid,
57                     const char *msg, unsigned short len);
58    virtual ~LogBufferElement();
59
60    log_id_t getLogId() const { return mLogId; }
61    uid_t getUid(void) const { return mUid; }
62    pid_t getPid(void) const { return mPid; }
63    pid_t getTid(void) const { return mTid; }
64    unsigned short getDropped(void) const { return mMsg ? 0 : mDropped; }
65    unsigned short setDropped(unsigned short value) {
66        if (mMsg) {
67            free(mMsg);
68            mMsg = NULL;
69        }
70        return mDropped = value;
71    }
72    unsigned short getMsgLen() const { return mMsg ? mMsgLen : 0; }
73    uint64_t getSequence(void) const { return mSequence; }
74    static uint64_t getCurrentSequence(void) { return sequence.load(memory_order_relaxed); }
75    log_time getRealTime(void) const { return mRealTime; }
76
77    uint32_t getTag(void) const;
78
79    static const uint64_t FLUSH_ERROR;
80    uint64_t flushTo(SocketClient *writer, LogBuffer *parent);
81};
82
83#endif
84