1c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman/* 2c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman * Copyright (C) 2012 The Android Open Source Project 3c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman * 4c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman * Licensed under the Apache License, Version 2.0 (the "License"); 5c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman * you may not use this file except in compliance with the License. 6c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman * You may obtain a copy of the License at 7c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman * 8c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman * http://www.apache.org/licenses/LICENSE-2.0 9c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman * 10c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman * Unless required by applicable law or agreed to in writing, software 11c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman * distributed under the License is distributed on an "AS IS" BASIS, 12c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman * See the License for the specific language governing permissions and 14c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman * limitations under the License. 15c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman */ 16c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman 17c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman#ifndef __UTILS_H__ 18c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman#define __UTILS_H__ 19c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman 20c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman#include <stdint.h> 21c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman#include <unistd.h> 22c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman 2379489c4c65d3c8e628991995b4a18f2a81802ee6John Grossman#include <utils/String8.h> 2479489c4c65d3c8e628991995b4a18f2a81802ee6John Grossman#include <utils/threads.h> 25c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman#include <utils/Timers.h> 26c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman 27c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossmannamespace android { 28c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman 29c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossmanclass Timeout { 30c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman public: 31c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman Timeout() : mSystemEndTime(0) { } 32c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman 33c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman // Set a timeout which should occur msec milliseconds from now. 34c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman // Negative values will cancel any current timeout; 35c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman void setTimeout(int msec); 36c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman 37c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman // Return the number of milliseconds until the timeout occurs, or -1 if 38c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman // no timeout is scheduled. 39c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman int msecTillTimeout(nsecs_t nowTime); 40c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman int msecTillTimeout() { return msecTillTimeout(systemTime()); } 41c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman 42c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman private: 43c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman // The systemTime() at which the timeout will be complete, or 0 if no 44c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman // timeout is currently scheduled. 45c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman nsecs_t mSystemEndTime; 46c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman}; 47c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman 4879489c4c65d3c8e628991995b4a18f2a81802ee6John Grossmanclass LogRing { 4979489c4c65d3c8e628991995b4a18f2a81802ee6John Grossman public: 5079489c4c65d3c8e628991995b4a18f2a81802ee6John Grossman LogRing(const char* header, size_t entries); 5179489c4c65d3c8e628991995b4a18f2a81802ee6John Grossman ~LogRing(); 5279489c4c65d3c8e628991995b4a18f2a81802ee6John Grossman 5379489c4c65d3c8e628991995b4a18f2a81802ee6John Grossman // Send a log message to logcat as well as storing it in the ring buffer. 5479489c4c65d3c8e628991995b4a18f2a81802ee6John Grossman void log(int prio, const char* tag, const char* fmt, ...); 5579489c4c65d3c8e628991995b4a18f2a81802ee6John Grossman 5679489c4c65d3c8e628991995b4a18f2a81802ee6John Grossman // Add a log message the ring buffer, do not send the message to logcat. 5779489c4c65d3c8e628991995b4a18f2a81802ee6John Grossman void log(const char* fmt, ...); 5879489c4c65d3c8e628991995b4a18f2a81802ee6John Grossman 5979489c4c65d3c8e628991995b4a18f2a81802ee6John Grossman // Dump the log to an fd (dumpsys style) 6079489c4c65d3c8e628991995b4a18f2a81802ee6John Grossman void dumpLog(int fd); 6179489c4c65d3c8e628991995b4a18f2a81802ee6John Grossman 6279489c4c65d3c8e628991995b4a18f2a81802ee6John Grossman private: 6379489c4c65d3c8e628991995b4a18f2a81802ee6John Grossman class Entry { 6479489c4c65d3c8e628991995b4a18f2a81802ee6John Grossman public: 6579489c4c65d3c8e628991995b4a18f2a81802ee6John Grossman uint32_t count; 6679489c4c65d3c8e628991995b4a18f2a81802ee6John Grossman struct timeval first_ts; 6779489c4c65d3c8e628991995b4a18f2a81802ee6John Grossman struct timeval last_ts; 6879489c4c65d3c8e628991995b4a18f2a81802ee6John Grossman String8 s; 6979489c4c65d3c8e628991995b4a18f2a81802ee6John Grossman }; 7079489c4c65d3c8e628991995b4a18f2a81802ee6John Grossman 7179489c4c65d3c8e628991995b4a18f2a81802ee6John Grossman Mutex mLock; 7279489c4c65d3c8e628991995b4a18f2a81802ee6John Grossman Entry* mRingBuffer; 7379489c4c65d3c8e628991995b4a18f2a81802ee6John Grossman size_t mSize; 7479489c4c65d3c8e628991995b4a18f2a81802ee6John Grossman size_t mWr; 7579489c4c65d3c8e628991995b4a18f2a81802ee6John Grossman bool mIsFull; 7679489c4c65d3c8e628991995b4a18f2a81802ee6John Grossman const char* mHeader; 7779489c4c65d3c8e628991995b4a18f2a81802ee6John Grossman 7879489c4c65d3c8e628991995b4a18f2a81802ee6John Grossman void internalLog(int prio, const char* tag, const char* fmt, va_list va); 7979489c4c65d3c8e628991995b4a18f2a81802ee6John Grossman}; 8079489c4c65d3c8e628991995b4a18f2a81802ee6John Grossman 81c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman} // namespace android 82c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman 83c7f57c6f9289d0e3aaecc0bca4ae7b6eed1c93d7John Grossman#endif // __UTILS_H__ 84