blob: de7669310e2f55448723ef08bc964bb3199118cb [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_H__
18#define _LOGD_LOG_BUFFER_H__
19
20#include <sys/types.h>
21
Mark Salyzyn94a89c42015-08-19 13:41:51 -070022#include <list>
Mark Salyzyn73160ac2015-08-20 10:01:44 -070023#include <string>
Mark Salyzyn94a89c42015-08-19 13:41:51 -070024
Mark Salyzyn0175b072014-02-26 09:50:16 -080025#include <log/log.h>
26#include <sysutils/SocketClient.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080027
Mark Salyzyn1a240b42014-06-12 11:16:16 -070028#include <private/android_filesystem_config.h>
29
Mark Salyzyn0175b072014-02-26 09:50:16 -080030#include "LogBufferElement.h"
31#include "LogTimes.h"
Mark Salyzyn34facab2014-02-06 14:48:50 -080032#include "LogStatistics.h"
Mark Salyzyndfa7a072014-02-11 12:29:31 -080033#include "LogWhiteBlackList.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080034
Mark Salyzyn94a89c42015-08-19 13:41:51 -070035typedef std::list<LogBufferElement *> LogBufferElementCollection;
Mark Salyzyn0175b072014-02-26 09:50:16 -080036
37class LogBuffer {
38 LogBufferElementCollection mLogElements;
39 pthread_mutex_t mLogElementsLock;
40
Mark Salyzyn34facab2014-02-06 14:48:50 -080041 LogStatistics stats;
Mark Salyzyne457b742014-02-19 17:18:31 -080042
Mark Salyzyndfa7a072014-02-11 12:29:31 -080043 PruneList mPrune;
Mark Salyzync892ea32015-08-19 17:06:11 -070044 // 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];
Mark Salyzyndfa7a072014-02-11 12:29:31 -080049
50 unsigned long mMaxSize[LOG_ID_MAX];
Mark Salyzyndfa7a072014-02-11 12:29:31 -080051
Mark Salyzyn0175b072014-02-26 09:50:16 -080052public:
53 LastLogTimes &mTimes;
54
55 LogBuffer(LastLogTimes *times);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070056 void init();
Mark Salyzyn0175b072014-02-26 09:50:16 -080057
Mark Salyzyn202e1532015-02-09 08:21:05 -080058 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);
Mark Salyzynf7c0f752015-03-03 13:39:37 -080061 uint64_t flushTo(SocketClient *writer, const uint64_t start,
Mark Salyzyn7e2f83c2014-03-05 07:41:49 -080062 bool privileged,
Mark Salyzynf7c0f752015-03-03 13:39:37 -080063 int (*filter)(const LogBufferElement *element, void *arg) = NULL,
Mark Salyzyn7e2f83c2014-03-05 07:41:49 -080064 void *arg = NULL);
Mark Salyzyn0175b072014-02-26 09:50:16 -080065
Mark Salyzyn1a240b42014-06-12 11:16:16 -070066 void clear(log_id_t id, uid_t uid = AID_ROOT);
Mark Salyzyn0175b072014-02-26 09:50:16 -080067 unsigned long getSize(log_id_t id);
Mark Salyzyndfa7a072014-02-11 12:29:31 -080068 int setSize(log_id_t id, unsigned long size);
Mark Salyzyn0175b072014-02-26 09:50:16 -080069 unsigned long getSizeUsed(log_id_t id);
Mark Salyzyn34facab2014-02-06 14:48:50 -080070 // *strp uses malloc, use free to release.
Mark Salyzyn73160ac2015-08-20 10:01:44 -070071 std::string formatStatistics(uid_t uid, unsigned int logMask);
Mark Salyzyndfa7a072014-02-11 12:29:31 -080072
Mark Salyzynf5fc5092014-09-21 14:22:18 -070073 void enableStatistics() {
74 stats.enableStatistics();
75 }
76
Mark Salyzyn73160ac2015-08-20 10:01:44 -070077 int initPrune(const char *cp) { return mPrune.init(cp); }
78 std::string formatPrune() { return mPrune.format(); }
Mark Salyzyn0175b072014-02-26 09:50:16 -080079
Mark Salyzyned777e92015-06-24 16:22:54 -070080 // helper must be protected directly or implicitly by lock()/unlock()
Mark Salyzyn9a038632014-04-07 07:05:40 -070081 char *pidToName(pid_t pid) { return stats.pidToName(pid); }
Mark Salyzyn4ba03872014-04-07 07:15:33 -070082 uid_t pidToUid(pid_t pid) { return stats.pidToUid(pid); }
Mark Salyzyn21fb7e02015-04-20 07:26:27 -070083 char *uidToName(uid_t uid) { return stats.uidToName(uid); }
Mark Salyzyned777e92015-06-24 16:22:54 -070084 void lock() { pthread_mutex_lock(&mLogElementsLock); }
85 void unlock() { pthread_mutex_unlock(&mLogElementsLock); }
Mark Salyzyn9a038632014-04-07 07:05:40 -070086
Mark Salyzyn0175b072014-02-26 09:50:16 -080087private:
88 void maybePrune(log_id_t id);
Mark Salyzyn1a240b42014-06-12 11:16:16 -070089 void prune(log_id_t id, unsigned long pruneRows, uid_t uid = AID_ROOT);
Mark Salyzyn831aa292015-09-03 16:08:50 -070090 LogBufferElementCollection::iterator erase(
91 LogBufferElementCollection::iterator it, bool engageStats = true);
Mark Salyzyn0175b072014-02-26 09:50:16 -080092};
93
Mark Salyzyn34facab2014-02-06 14:48:50 -080094#endif // _LOGD_LOG_BUFFER_H__