blob: 5223b40475072abe5d03761684afa92acf2c9fef [file] [log] [blame]
Mark Salyzyn12bac902014-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 Salyzyn277a6d22015-08-19 13:41:51 -070022#include <list>
Mark Salyzyn1c7b6fb2015-08-20 10:01:44 -070023#include <string>
Mark Salyzyn277a6d22015-08-19 13:41:51 -070024
Mark Salyzyn12bac902014-02-26 09:50:16 -080025#include <log/log.h>
26#include <sysutils/SocketClient.h>
Mark Salyzyn12bac902014-02-26 09:50:16 -080027
Mark Salyzyn276d5352014-06-12 11:16:16 -070028#include <private/android_filesystem_config.h>
29
Mark Salyzyn12bac902014-02-26 09:50:16 -080030#include "LogBufferElement.h"
31#include "LogTimes.h"
Mark Salyzynd774bce2014-02-06 14:48:50 -080032#include "LogStatistics.h"
Mark Salyzync89839a2014-02-11 12:29:31 -080033#include "LogWhiteBlackList.h"
Mark Salyzyn12bac902014-02-26 09:50:16 -080034
Mark Salyzyn277a6d22015-08-19 13:41:51 -070035typedef std::list<LogBufferElement *> LogBufferElementCollection;
Mark Salyzyn12bac902014-02-26 09:50:16 -080036
37class LogBuffer {
38 LogBufferElementCollection mLogElements;
39 pthread_mutex_t mLogElementsLock;
40
Mark Salyzynd774bce2014-02-06 14:48:50 -080041 LogStatistics stats;
Mark Salyzynfe639a02014-02-19 17:18:31 -080042
Mark Salyzync89839a2014-02-11 12:29:31 -080043 PruneList mPrune;
Mark Salyzyncfcffdaf2015-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 Salyzync89839a2014-02-11 12:29:31 -080049
50 unsigned long mMaxSize[LOG_ID_MAX];
Mark Salyzync89839a2014-02-11 12:29:31 -080051
Mark Salyzyn12bac902014-02-26 09:50:16 -080052public:
53 LastLogTimes &mTimes;
54
55 LogBuffer(LastLogTimes *times);
Mark Salyzyn3fe25932015-03-10 16:45:17 -070056 void init();
Mark Salyzyn12bac902014-02-26 09:50:16 -080057
Mark Salyzyn88fe7cc2015-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 Salyzyn0aaf6cd2015-03-03 13:39:37 -080061 uint64_t flushTo(SocketClient *writer, const uint64_t start,
Mark Salyzyn71391172014-03-05 07:41:49 -080062 bool privileged,
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -080063 int (*filter)(const LogBufferElement *element, void *arg) = NULL,
Mark Salyzyn71391172014-03-05 07:41:49 -080064 void *arg = NULL);
Mark Salyzyn12bac902014-02-26 09:50:16 -080065
Mark Salyzyn7702c3e2015-09-16 15:34:00 -070066 bool clear(log_id_t id, uid_t uid = AID_ROOT);
Mark Salyzyn12bac902014-02-26 09:50:16 -080067 unsigned long getSize(log_id_t id);
Mark Salyzync89839a2014-02-11 12:29:31 -080068 int setSize(log_id_t id, unsigned long size);
Mark Salyzyn12bac902014-02-26 09:50:16 -080069 unsigned long getSizeUsed(log_id_t id);
Mark Salyzynd774bce2014-02-06 14:48:50 -080070 // *strp uses malloc, use free to release.
Mark Salyzyn1c7b6fb2015-08-20 10:01:44 -070071 std::string formatStatistics(uid_t uid, unsigned int logMask);
Mark Salyzync89839a2014-02-11 12:29:31 -080072
Mark Salyzyn6b4a4d02014-09-21 14:22:18 -070073 void enableStatistics() {
74 stats.enableStatistics();
75 }
76
Mark Salyzyn1c7b6fb2015-08-20 10:01:44 -070077 int initPrune(const char *cp) { return mPrune.init(cp); }
78 std::string formatPrune() { return mPrune.format(); }
Mark Salyzyn12bac902014-02-26 09:50:16 -080079
Mark Salyzyn10acf782015-06-24 16:22:54 -070080 // helper must be protected directly or implicitly by lock()/unlock()
Mark Salyzyn34eb1b22015-08-21 16:44:30 -070081 const char *pidToName(pid_t pid) { return stats.pidToName(pid); }
Mark Salyzynf67c3792014-04-07 07:15:33 -070082 uid_t pidToUid(pid_t pid) { return stats.pidToUid(pid); }
Mark Salyzyn34eb1b22015-08-21 16:44:30 -070083 const char *uidToName(uid_t uid) { return stats.uidToName(uid); }
Mark Salyzyn10acf782015-06-24 16:22:54 -070084 void lock() { pthread_mutex_lock(&mLogElementsLock); }
85 void unlock() { pthread_mutex_unlock(&mLogElementsLock); }
Mark Salyzyn5766f802014-04-07 07:05:40 -070086
Mark Salyzyn12bac902014-02-26 09:50:16 -080087private:
Mark Salyzyn95ff2482015-09-30 07:40:09 -070088
89 static constexpr size_t maxPrune = 256;
90
Mark Salyzyn12bac902014-02-26 09:50:16 -080091 void maybePrune(log_id_t id);
Mark Salyzyn7702c3e2015-09-16 15:34:00 -070092 bool prune(log_id_t id, unsigned long pruneRows, uid_t uid = AID_ROOT);
Mark Salyzyn57b82a62015-09-03 16:08:50 -070093 LogBufferElementCollection::iterator erase(
Mark Salyzyn95ff2482015-09-30 07:40:09 -070094 LogBufferElementCollection::iterator it, bool coalesce = false);
Mark Salyzyn12bac902014-02-26 09:50:16 -080095};
96
Mark Salyzynd774bce2014-02-06 14:48:50 -080097#endif // _LOGD_LOG_BUFFER_H__