LogBufferElement.h revision ae769238391f7f9fa5c03a436d5f1fd73130e6bd
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
28namespace android {
29
30// Furnished in main.cpp. Caller must own and free returned value
31// This function is designed for a single caller and is NOT thread-safe
32char *uidToName(uid_t uid);
33
34}
35
36static inline bool worstUidEnabledForLogid(log_id_t id) {
37    return (id != LOG_ID_CRASH) && (id != LOG_ID_EVENTS);
38}
39
40class LogBufferElement {
41    const log_id_t mLogId;
42    const uid_t mUid;
43    const pid_t mPid;
44    const pid_t mTid;
45    char *mMsg;
46    union {
47        const unsigned short mMsgLen; // mMSg != NULL
48        unsigned short mDropped;      // mMsg == NULL
49    };
50    const uint64_t mSequence;
51    const log_time mRealTime;
52    static atomic_int_fast64_t sequence;
53
54    // assumption: mMsg == NULL
55    size_t populateDroppedMessage(char *&buffer, bool privileged);
56
57public:
58    LogBufferElement(log_id_t log_id, log_time realtime,
59                     uid_t uid, pid_t pid, pid_t tid,
60                     const char *msg, unsigned short len);
61    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; }
66    pid_t getTid(void) const { return mTid; }
67    unsigned short getDropped(void) const { return mMsg ? 0 : mDropped; }
68    unsigned short setDropped(unsigned short value) {
69        if (mMsg) {
70            free(mMsg);
71            mMsg = NULL;
72        }
73        return mDropped = value;
74    }
75    unsigned short getMsgLen() const { return mMsg ? mMsgLen : 0; }
76    uint64_t getSequence(void) const { return mSequence; }
77    static uint64_t getCurrentSequence(void) { return sequence.load(memory_order_relaxed); }
78    log_time getRealTime(void) const { return mRealTime; }
79
80    static const uint64_t FLUSH_ERROR;
81    uint64_t flushTo(SocketClient *writer);
82};
83
84#endif
85