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