blob: cb6f56663b32fec417727960d54326052685547b [file] [log] [blame]
Mark Salyzyn0175b072014-02-26 09:50:16 -08001/*
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 <time.h>
22#include <sys/types.h>
23#include <sysutils/SocketClient.h>
24#include <utils/List.h>
25
26class LogReader;
27
28class LogTimeEntry {
29 static pthread_mutex_t timesLock;
30 unsigned int mRefCount;
31 bool mRelease;
32 bool mError;
33 bool threadRunning;
34 bool threadTriggered;
35 pthread_t mThread;
36 LogReader &mReader;
37 static void *threadStart(void *me);
38 static void threadStop(void *me);
39 const unsigned int mLogMask;
40 const pid_t mPid;
41 unsigned int skipAhead;
42 unsigned long mCount;
43 unsigned long mTail;
44 unsigned long mIndex;
45
46public:
47 LogTimeEntry(LogReader &reader, SocketClient *client, bool nonBlock,
48 unsigned long tail, unsigned int logMask, pid_t pid);
49
50 SocketClient *mClient;
51 static const struct timespec EPOCH;
52 log_time mStart;
53 const bool mNonBlock;
54 const log_time mEnd; // only relevant if mNonBlock
55
56 // Protect List manipulations
57 static void lock(void) { pthread_mutex_lock(&timesLock); }
58 static void unlock(void) { pthread_mutex_unlock(&timesLock); }
59
60 void startReader_Locked(void);
61
Mark Salyzync03e72c2014-02-18 11:23:53 -080062 bool runningReader_Locked(void) const {
Mark Salyzyn0175b072014-02-26 09:50:16 -080063 return threadRunning || mRelease || mError || mNonBlock;
64 }
65 void triggerReader_Locked(void) { threadTriggered = true; }
66 void triggerSkip_Locked(unsigned int skip) { skipAhead = skip; }
67
68 // Called after LogTimeEntry removed from list, lock implicitly held
Mark Salyzync03e72c2014-02-18 11:23:53 -080069 void release_Locked(void) {
Mark Salyzyn0175b072014-02-26 09:50:16 -080070 mRelease = true;
71 if (mRefCount || threadRunning) {
72 return;
73 }
74 // No one else is holding a reference to this
75 delete this;
76 }
77
78 // Called to mark socket in jeopardy
79 void error_Locked(void) { mError = true; }
80 void error(void) { lock(); mError = true; unlock(); }
81
82 bool isError_Locked(void) const { return mRelease || mError; }
83
84 // Mark Used
85 // Locking implied, grabbed when protection around loop iteration
86 void incRef_Locked(void) { ++mRefCount; }
87
88 bool owned_Locked(void) const { return mRefCount != 0; }
89
Mark Salyzync03e72c2014-02-18 11:23:53 -080090 void decRef_Locked(void) {
Mark Salyzyn0175b072014-02-26 09:50:16 -080091 if ((mRefCount && --mRefCount) || !mRelease || threadRunning) {
92 return;
93 }
94 // No one else is holding a reference to this
95 delete this;
96 }
97
98 // flushTo filter callbacks
99 static bool FilterFirstPass(const LogBufferElement *element, void *me);
100 static bool FilterSecondPass(const LogBufferElement *element, void *me);
101};
102
103typedef android::List<LogTimeEntry *> LastLogTimes;
104
105#endif