blob: ac52db23d29e48c3a9e8346fdc5535699a79d49e [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
62 bool runningReader_Locked(void) const
63 {
64 return threadRunning || mRelease || mError || mNonBlock;
65 }
66 void triggerReader_Locked(void) { threadTriggered = true; }
67 void triggerSkip_Locked(unsigned int skip) { skipAhead = skip; }
68
69 // Called after LogTimeEntry removed from list, lock implicitly held
70 void release_Locked(void)
71 {
72 mRelease = true;
73 if (mRefCount || threadRunning) {
74 return;
75 }
76 // No one else is holding a reference to this
77 delete this;
78 }
79
80 // Called to mark socket in jeopardy
81 void error_Locked(void) { mError = true; }
82 void error(void) { lock(); mError = true; unlock(); }
83
84 bool isError_Locked(void) const { return mRelease || mError; }
85
86 // Mark Used
87 // Locking implied, grabbed when protection around loop iteration
88 void incRef_Locked(void) { ++mRefCount; }
89
90 bool owned_Locked(void) const { return mRefCount != 0; }
91
92 void decRef_Locked(void)
93 {
94 if ((mRefCount && --mRefCount) || !mRelease || threadRunning) {
95 return;
96 }
97 // No one else is holding a reference to this
98 delete this;
99 }
100
101 // flushTo filter callbacks
102 static bool FilterFirstPass(const LogBufferElement *element, void *me);
103 static bool FilterSecondPass(const LogBufferElement *element, void *me);
104};
105
106typedef android::List<LogTimeEntry *> LastLogTimes;
107
108#endif