LogBuffer.h revision 1c04253af2077de0250e4082cadd4f9bfffc4ea2
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 <list>
23#include <string>
24
25#include <log/log.h>
26#include <sysutils/SocketClient.h>
27
28#include <private/android_filesystem_config.h>
29
30#include "LogBufferElement.h"
31#include "LogTimes.h"
32#include "LogStatistics.h"
33#include "LogWhiteBlackList.h"
34
35typedef std::list<LogBufferElement *> LogBufferElementCollection;
36
37class LogBuffer {
38    LogBufferElementCollection mLogElements;
39    pthread_mutex_t mLogElementsLock;
40
41    LogStatistics stats;
42
43    PruneList mPrune;
44    // watermark of any worst/chatty uid processing
45    typedef std::unordered_map<uid_t,
46                               LogBufferElementCollection::iterator>
47                LogBufferIteratorMap;
48    LogBufferIteratorMap mLastWorstUid[LOG_ID_MAX];
49
50    unsigned long mMaxSize[LOG_ID_MAX];
51
52public:
53    LastLogTimes &mTimes;
54
55    LogBuffer(LastLogTimes *times);
56    void init();
57
58    int log(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    uint64_t flushTo(SocketClient *writer, const uint64_t start,
62                     bool privileged,
63                     int (*filter)(const LogBufferElement *element, void *arg) = NULL,
64                     void *arg = NULL);
65
66    bool clear(log_id_t id, uid_t uid = AID_ROOT);
67    unsigned long getSize(log_id_t id);
68    int setSize(log_id_t id, unsigned long size);
69    unsigned long getSizeUsed(log_id_t id);
70    // *strp uses malloc, use free to release.
71    std::string formatStatistics(uid_t uid, unsigned int logMask);
72
73    void enableStatistics() {
74        stats.enableStatistics();
75    }
76
77    int initPrune(const char *cp) { return mPrune.init(cp); }
78    std::string formatPrune() { return mPrune.format(); }
79
80    // helper must be protected directly or implicitly by lock()/unlock()
81    const char *pidToName(pid_t pid) { return stats.pidToName(pid); }
82    uid_t pidToUid(pid_t pid) { return stats.pidToUid(pid); }
83    const char *uidToName(uid_t uid) { return stats.uidToName(uid); }
84    void lock() { pthread_mutex_lock(&mLogElementsLock); }
85    void unlock() { pthread_mutex_unlock(&mLogElementsLock); }
86
87private:
88    void maybePrune(log_id_t id);
89    bool prune(log_id_t id, unsigned long pruneRows, uid_t uid = AID_ROOT);
90    LogBufferElementCollection::iterator erase(
91        LogBufferElementCollection::iterator it, bool engageStats = true);
92};
93
94#endif // _LOGD_LOG_BUFFER_H__
95