LogBuffer.h revision dfa7a07f5be656cfafdb4e75916bc9dcd9e592e7
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_H__
18#define _LOGD_LOG_BUFFER_H__
19
20#include <sys/types.h>
21
22#include <log/log.h>
23#include <sysutils/SocketClient.h>
24#include <utils/List.h>
25
26#include "LogBufferElement.h"
27#include "LogTimes.h"
28#include "LogStatistics.h"
29#include "LogWhiteBlackList.h"
30
31typedef android::List<LogBufferElement *> LogBufferElementCollection;
32
33class LogBuffer {
34    LogBufferElementCollection mLogElements;
35    pthread_mutex_t mLogElementsLock;
36
37    LogStatistics stats;
38
39#ifdef USERDEBUG_BUILD
40    PruneList mPrune;
41
42    unsigned long mMaxSize[LOG_ID_MAX];
43#endif
44
45public:
46    LastLogTimes &mTimes;
47
48    LogBuffer(LastLogTimes *times);
49
50    void log(log_id_t log_id, log_time realtime,
51             uid_t uid, pid_t pid, const char *msg, unsigned short len);
52    log_time flushTo(SocketClient *writer, const log_time start,
53                     bool privileged,
54                     bool (*filter)(const LogBufferElement *element, void *arg) = NULL,
55                     void *arg = NULL);
56
57    void clear(log_id_t id);
58    unsigned long getSize(log_id_t id);
59#ifdef USERDEBUG_BUILD
60    int setSize(log_id_t id, unsigned long size);
61#endif
62    unsigned long getSizeUsed(log_id_t id);
63    // *strp uses malloc, use free to release.
64    void formatStatistics(char **strp, uid_t uid, unsigned int logMask);
65
66#ifdef USERDEBUG_BUILD
67    int initPrune(char *cp) { return mPrune.init(cp); }
68    // *strp uses malloc, use free to release.
69    void formatPrune(char **strp) { mPrune.format(strp); }
70#endif
71
72private:
73    void maybePrune(log_id_t id);
74    void prune(log_id_t id, unsigned long pruneRows);
75
76};
77
78#endif // _LOGD_LOG_BUFFER_H__
79