blob: 6c08811fb618a38c13682ba9e44ab08cf9fd9738 [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
17#ifndef _LOGD_LOG_BUFFER_ELEMENT_H__
18#define _LOGD_LOG_BUFFER_ELEMENT_H__
19
Mark Salyzynf7c0f752015-03-03 13:39:37 -080020#include <stdatomic.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 Salyzyn0175b072014-02-26 09:50:16 -080024#include <sysutils/SocketClient.h>
25#include <log/log.h>
26#include <log/log_read.h>
27
Mark Salyzyn81b3eab2015-04-13 14:24:45 -070028// Hijack this header as a common include file used by most all sources
29// to report some utilities defined here and there.
30
Mark Salyzyn08739ba2015-03-16 08:26:05 -070031namespace android {
32
33// Furnished in main.cpp. Caller must own and free returned value
34// This function is designed for a single caller and is NOT thread-safe
35char *uidToName(uid_t uid);
36
37}
38
Mark Salyzynae769232015-03-17 17:17:25 -070039static inline bool worstUidEnabledForLogid(log_id_t id) {
40 return (id != LOG_ID_CRASH) && (id != LOG_ID_EVENTS);
41}
42
Mark Salyzyn0175b072014-02-26 09:50:16 -080043class LogBufferElement {
44 const log_id_t mLogId;
45 const uid_t mUid;
46 const pid_t mPid;
Mark Salyzynb992d0d2014-03-20 16:09:38 -070047 const pid_t mTid;
Mark Salyzyn0175b072014-02-26 09:50:16 -080048 char *mMsg;
Mark Salyzynab0dcf62015-03-16 12:04:09 -070049 union {
50 const unsigned short mMsgLen; // mMSg != NULL
51 unsigned short mDropped; // mMsg == NULL
52 };
Mark Salyzynf7c0f752015-03-03 13:39:37 -080053 const uint64_t mSequence;
Mark Salyzyn0175b072014-02-26 09:50:16 -080054 const log_time mRealTime;
Mark Salyzynf7c0f752015-03-03 13:39:37 -080055 static atomic_int_fast64_t sequence;
Mark Salyzyn0175b072014-02-26 09:50:16 -080056
Mark Salyzynab0dcf62015-03-16 12:04:09 -070057 // assumption: mMsg == NULL
58 size_t populateDroppedMessage(char *&buffer, bool privileged);
59
Mark Salyzyn0175b072014-02-26 09:50:16 -080060public:
Mark Salyzyn7e2f83c2014-03-05 07:41:49 -080061 LogBufferElement(log_id_t log_id, log_time realtime,
Mark Salyzynb992d0d2014-03-20 16:09:38 -070062 uid_t uid, pid_t pid, pid_t tid,
63 const char *msg, unsigned short len);
Mark Salyzyn0175b072014-02-26 09:50:16 -080064 virtual ~LogBufferElement();
65
66 log_id_t getLogId() const { return mLogId; }
67 uid_t getUid(void) const { return mUid; }
68 pid_t getPid(void) const { return mPid; }
Mark Salyzynb992d0d2014-03-20 16:09:38 -070069 pid_t getTid(void) const { return mTid; }
Mark Salyzynab0dcf62015-03-16 12:04:09 -070070 unsigned short getDropped(void) const { return mMsg ? 0 : mDropped; }
71 unsigned short setDropped(unsigned short value) {
72 if (mMsg) {
73 free(mMsg);
74 mMsg = NULL;
75 }
76 return mDropped = value;
77 }
78 unsigned short getMsgLen() const { return mMsg ? mMsgLen : 0; }
Mark Salyzynf7c0f752015-03-03 13:39:37 -080079 uint64_t getSequence(void) const { return mSequence; }
80 static uint64_t getCurrentSequence(void) { return sequence.load(memory_order_relaxed); }
Mark Salyzyn0175b072014-02-26 09:50:16 -080081 log_time getRealTime(void) const { return mRealTime; }
82
Mark Salyzynf7c0f752015-03-03 13:39:37 -080083 static const uint64_t FLUSH_ERROR;
84 uint64_t flushTo(SocketClient *writer);
Mark Salyzyn0175b072014-02-26 09:50:16 -080085};
86
87#endif