LogBufferElement.h revision d1371a8b8b55352e19332ba87f4b3ece6db58e1f
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
28// Hijack this header as a common include file used by most all sources
29// to report some utilities defined here and there.
30
31namespace android {
32
33// Furnished in main.cpp. Caller must own and free returned value
34char *uidToName(uid_t uid);
35
36// Furnished in LogStatistics.cpp. Caller must own and free returned value
37char *pidToName(pid_t pid);
38char *tidToName(pid_t tid);
39
40// Furnished in main.cpp. Thread safe.
41const char *tagToName(uint32_t tag);
42
43}
44
45static inline bool worstUidEnabledForLogid(log_id_t id) {
46    return (id != LOG_ID_CRASH) && (id != LOG_ID_KERNEL) && (id != LOG_ID_EVENTS);
47}
48
49class LogBuffer;
50
51class LogBufferElement {
52    const log_id_t mLogId;
53    const uid_t mUid;
54    const pid_t mPid;
55    const pid_t mTid;
56    char *mMsg;
57    union {
58        const unsigned short mMsgLen; // mMSg != NULL
59        unsigned short mDropped;      // mMsg == NULL
60    };
61    const uint64_t mSequence;
62    const log_time mRealTime;
63    static atomic_int_fast64_t sequence;
64
65    // assumption: mMsg == NULL
66    size_t populateDroppedMessage(char *&buffer,
67                                  LogBuffer *parent);
68
69public:
70    LogBufferElement(log_id_t log_id, log_time realtime,
71                     uid_t uid, pid_t pid, pid_t tid,
72                     const char *msg, unsigned short len);
73    virtual ~LogBufferElement();
74
75    log_id_t getLogId() const { return mLogId; }
76    uid_t getUid(void) const { return mUid; }
77    pid_t getPid(void) const { return mPid; }
78    pid_t getTid(void) const { return mTid; }
79    unsigned short getDropped(void) const { return mMsg ? 0 : mDropped; }
80    unsigned short setDropped(unsigned short value) {
81        if (mMsg) {
82            free(mMsg);
83            mMsg = NULL;
84        }
85        return mDropped = value;
86    }
87    unsigned short getMsgLen() const { return mMsg ? mMsgLen : 0; }
88    uint64_t getSequence(void) const { return mSequence; }
89    static uint64_t getCurrentSequence(void) { return sequence.load(memory_order_relaxed); }
90    log_time getRealTime(void) const { return mRealTime; }
91
92    uint32_t getTag(void) const;
93
94    static const uint64_t FLUSH_ERROR;
95    uint64_t flushTo(SocketClient *writer, LogBuffer *parent);
96};
97
98#endif
99