1 /* 2 * Copyright (C) 2012-2013 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_TIMES_H__ 18 #define _LOGD_LOG_TIMES_H__ 19 20 #include <pthread.h> 21 #include <sys/socket.h> 22 #include <sys/types.h> 23 #include <time.h> 24 25 #include <list> 26 #include <memory> 27 28 #include <log/log.h> 29 #include <sysutils/SocketClient.h> 30 31 typedef unsigned int log_mask_t; 32 33 class LogReader; 34 class LogBufferElement; 35 36 class LogTimeEntry { 37 static pthread_mutex_t timesLock; 38 bool mRelease = false; 39 bool leadingDropped; 40 pthread_cond_t threadTriggeredCondition; 41 pthread_t mThread; 42 LogReader& mReader; 43 static void* threadStart(void* me); 44 const log_mask_t mLogMask; 45 const pid_t mPid; 46 unsigned int skipAhead[LOG_ID_MAX]; 47 pid_t mLastTid[LOG_ID_MAX]; 48 unsigned long mCount; 49 unsigned long mTail; 50 unsigned long mIndex; 51 52 public: 53 LogTimeEntry(LogReader& reader, SocketClient* client, bool nonBlock, 54 unsigned long tail, log_mask_t logMask, pid_t pid, 55 log_time start, uint64_t timeout); 56 57 SocketClient* mClient; 58 log_time mStart; 59 struct timespec mTimeout; 60 const bool mNonBlock; 61 const log_time mEnd; // only relevant if mNonBlock 62 63 // Protect List manipulations wrlock(void)64 static void wrlock(void) { 65 pthread_mutex_lock(×Lock); 66 } rdlock(void)67 static void rdlock(void) { 68 pthread_mutex_lock(×Lock); 69 } unlock(void)70 static void unlock(void) { 71 pthread_mutex_unlock(×Lock); 72 } 73 74 bool startReader_Locked(); 75 triggerReader_Locked(void)76 void triggerReader_Locked(void) { 77 pthread_cond_signal(&threadTriggeredCondition); 78 } 79 triggerSkip_Locked(log_id_t id,unsigned int skip)80 void triggerSkip_Locked(log_id_t id, unsigned int skip) { 81 skipAhead[id] = skip; 82 } 83 void cleanSkip_Locked(void); 84 release_Locked(void)85 void release_Locked(void) { 86 // gracefully shut down the socket. 87 shutdown(mClient->getSocket(), SHUT_RDWR); 88 mRelease = true; 89 pthread_cond_signal(&threadTriggeredCondition); 90 } 91 isWatching(log_id_t id)92 bool isWatching(log_id_t id) const { 93 return mLogMask & (1 << id); 94 } isWatchingMultiple(log_mask_t logMask)95 bool isWatchingMultiple(log_mask_t logMask) const { 96 return mLogMask & logMask; 97 } 98 // flushTo filter callbacks 99 static int FilterFirstPass(const LogBufferElement* element, void* me); 100 static int FilterSecondPass(const LogBufferElement* element, void* me); 101 }; 102 103 typedef std::list<std::unique_ptr<LogTimeEntry>> LastLogTimes; 104 105 #endif // _LOGD_LOG_TIMES_H__ 106