Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 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 | |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 26 | #include <private/android_filesystem_config.h> |
| 27 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 28 | #include "LogBufferElement.h" |
| 29 | #include "LogTimes.h" |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 30 | #include "LogStatistics.h" |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 31 | #include "LogWhiteBlackList.h" |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 32 | |
| 33 | typedef android::List<LogBufferElement *> LogBufferElementCollection; |
| 34 | |
| 35 | class LogBuffer { |
| 36 | LogBufferElementCollection mLogElements; |
| 37 | pthread_mutex_t mLogElementsLock; |
| 38 | |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 39 | LogStatistics stats; |
Mark Salyzyn | e457b74 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 40 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 41 | PruneList mPrune; |
| 42 | |
| 43 | unsigned long mMaxSize[LOG_ID_MAX]; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 44 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 45 | public: |
| 46 | LastLogTimes &mTimes; |
| 47 | |
| 48 | LogBuffer(LastLogTimes *times); |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 49 | void init(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 50 | |
Mark Salyzyn | 202e153 | 2015-02-09 08:21:05 -0800 | [diff] [blame] | 51 | int log(log_id_t log_id, log_time realtime, |
| 52 | uid_t uid, pid_t pid, pid_t tid, |
| 53 | const char *msg, unsigned short len); |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 54 | uint64_t flushTo(SocketClient *writer, const uint64_t start, |
Mark Salyzyn | 7e2f83c | 2014-03-05 07:41:49 -0800 | [diff] [blame] | 55 | bool privileged, |
Mark Salyzyn | f7c0f75 | 2015-03-03 13:39:37 -0800 | [diff] [blame] | 56 | int (*filter)(const LogBufferElement *element, void *arg) = NULL, |
Mark Salyzyn | 7e2f83c | 2014-03-05 07:41:49 -0800 | [diff] [blame] | 57 | void *arg = NULL); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 58 | |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 59 | void clear(log_id_t id, uid_t uid = AID_ROOT); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 60 | unsigned long getSize(log_id_t id); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 61 | int setSize(log_id_t id, unsigned long size); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 62 | unsigned long getSizeUsed(log_id_t id); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 63 | // *strp uses malloc, use free to release. |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 64 | void formatStatistics(char **strp, uid_t uid, unsigned int logMask); |
| 65 | |
Mark Salyzyn | f5fc509 | 2014-09-21 14:22:18 -0700 | [diff] [blame] | 66 | void enableStatistics() { |
| 67 | stats.enableStatistics(); |
| 68 | } |
| 69 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 70 | int initPrune(char *cp) { return mPrune.init(cp); } |
| 71 | // *strp uses malloc, use free to release. |
| 72 | void formatPrune(char **strp) { mPrune.format(strp); } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 73 | |
Mark Salyzyn | ed777e9 | 2015-06-24 16:22:54 -0700 | [diff] [blame] | 74 | // helper must be protected directly or implicitly by lock()/unlock() |
Mark Salyzyn | 9a03863 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 75 | char *pidToName(pid_t pid) { return stats.pidToName(pid); } |
Mark Salyzyn | 4ba0387 | 2014-04-07 07:15:33 -0700 | [diff] [blame] | 76 | uid_t pidToUid(pid_t pid) { return stats.pidToUid(pid); } |
Mark Salyzyn | 21fb7e0 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 77 | char *uidToName(uid_t uid) { return stats.uidToName(uid); } |
Mark Salyzyn | ed777e9 | 2015-06-24 16:22:54 -0700 | [diff] [blame] | 78 | void lock() { pthread_mutex_lock(&mLogElementsLock); } |
| 79 | void unlock() { pthread_mutex_unlock(&mLogElementsLock); } |
Mark Salyzyn | 9a03863 | 2014-04-07 07:05:40 -0700 | [diff] [blame] | 80 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 81 | private: |
| 82 | void maybePrune(log_id_t id); |
Mark Salyzyn | 1a240b4 | 2014-06-12 11:16:16 -0700 | [diff] [blame] | 83 | void prune(log_id_t id, unsigned long pruneRows, uid_t uid = AID_ROOT); |
Mark Salyzyn | 97c1c2b | 2015-03-10 13:51:35 -0700 | [diff] [blame] | 84 | LogBufferElementCollection::iterator erase(LogBufferElementCollection::iterator it); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 85 | }; |
| 86 | |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 87 | #endif // _LOGD_LOG_BUFFER_H__ |