• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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 __UTILS_H__
18 #define __UTILS_H__
19 
20 #include <stdint.h>
21 #include <unistd.h>
22 
23 #include <utils/String8.h>
24 #include <utils/threads.h>
25 #include <utils/Timers.h>
26 
27 namespace android {
28 
29 class Timeout {
30   public:
Timeout()31     Timeout() : mSystemEndTime(0) { }
32 
33     // Set a timeout which should occur msec milliseconds from now.
34     // Negative values will cancel any current timeout;
35     void setTimeout(int msec);
36 
37     // Return the number of milliseconds until the timeout occurs, or -1 if
38     // no timeout is scheduled.
39     int msecTillTimeout(nsecs_t nowTime);
msecTillTimeout()40     int msecTillTimeout() { return msecTillTimeout(systemTime()); }
41 
42   private:
43     // The systemTime() at which the timeout will be complete, or 0 if no
44     // timeout is currently scheduled.
45     nsecs_t mSystemEndTime;
46 };
47 
48 class LogRing {
49   public:
50     LogRing(const char* header, size_t entries);
51     ~LogRing();
52 
53     // Send a log message to logcat as well as storing it in the ring buffer.
54     void log(int prio, const char* tag, const char* fmt, ...);
55 
56     // Add a log message the ring buffer, do not send the message to logcat.
57     void log(const char* fmt, ...);
58 
59     // Dump the log to an fd (dumpsys style)
60     void dumpLog(int fd);
61 
62   private:
63     class Entry {
64       public:
65         uint32_t count;
66         struct timeval first_ts;
67         struct timeval last_ts;
68         String8 s;
69     };
70 
71     Mutex  mLock;
72     Entry* mRingBuffer;
73     size_t mSize;
74     size_t mWr;
75     bool   mIsFull;
76     const char* mHeader;
77 
78     void internalLog(int prio, const char* tag, const char* fmt, va_list va);
79 };
80 
81 }  // namespace android
82 
83 #endif  // __UTILS_H__
84