niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
henrike@webrtc.org | 0e7c060 | 2012-02-08 18:53:50 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 11 | #include "webrtc/system_wrappers/source/trace_posix.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
| 13 | #include <cassert> |
| 14 | #include <stdarg.h> |
| 15 | #include <stdio.h> |
| 16 | #include <string.h> |
henrike@webrtc.org | 0e7c060 | 2012-02-08 18:53:50 +0000 | [diff] [blame] | 17 | #include <sys/time.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 18 | #include <time.h> |
leozwang@google.com | b352700 | 2011-07-26 17:29:38 +0000 | [diff] [blame] | 19 | #ifdef WEBRTC_ANDROID |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 20 | #include <pthread.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 21 | #else |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 22 | #include <iostream> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 23 | #endif |
| 24 | |
| 25 | #if defined(_DEBUG) |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 26 | #define BUILDMODE "d" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 27 | #elif defined(DEBUG) |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 28 | #define BUILDMODE "d" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 29 | #elif defined(NDEBUG) |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 30 | #define BUILDMODE "r" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 31 | #else |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 32 | #define BUILDMODE "?" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 33 | #endif |
| 34 | #define BUILDTIME __TIME__ |
| 35 | #define BUILDDATE __DATE__ |
| 36 | // example: "Oct 10 2002 12:05:30 r" |
| 37 | #define BUILDINFO BUILDDATE " " BUILDTIME " " BUILDMODE |
| 38 | |
| 39 | namespace webrtc { |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 40 | |
henrika@webrtc.org | 19da719 | 2013-04-05 14:34:57 +0000 | [diff] [blame^] | 41 | TracePosix::TracePosix() |
| 42 | : crit_sect_(*CriticalSectionWrapper::CreateCriticalSection()) { |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 43 | struct timeval system_time_high_res; |
| 44 | gettimeofday(&system_time_high_res, 0); |
| 45 | prev_api_tick_count_ = prev_tick_count_ = system_time_high_res.tv_sec; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 46 | } |
| 47 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 48 | TracePosix::~TracePosix() { |
henrika@webrtc.org | 19da719 | 2013-04-05 14:34:57 +0000 | [diff] [blame^] | 49 | delete &crit_sect_; |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 50 | StopThread(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 51 | } |
| 52 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 53 | WebRtc_Word32 TracePosix::AddTime(char* trace_message, |
| 54 | const TraceLevel level) const { |
| 55 | struct timeval system_time_high_res; |
| 56 | if (gettimeofday(&system_time_high_res, 0) == -1) { |
| 57 | return -1; |
| 58 | } |
| 59 | struct tm buffer; |
| 60 | const struct tm* system_time = |
| 61 | localtime_r(&system_time_high_res.tv_sec, &buffer); |
henrike@webrtc.org | 0e7c060 | 2012-02-08 18:53:50 +0000 | [diff] [blame] | 62 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 63 | const WebRtc_UWord32 ms_time = system_time_high_res.tv_usec / 1000; |
| 64 | WebRtc_UWord32 prev_tickCount = 0; |
henrika@webrtc.org | 19da719 | 2013-04-05 14:34:57 +0000 | [diff] [blame^] | 65 | { |
| 66 | CriticalSectionScoped lock(&crit_sect_); |
| 67 | if (level == kTraceApiCall) { |
| 68 | prev_tickCount = prev_tick_count_; |
| 69 | prev_tick_count_ = ms_time; |
| 70 | } else { |
| 71 | prev_tickCount = prev_api_tick_count_; |
| 72 | prev_api_tick_count_ = ms_time; |
| 73 | } |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 74 | } |
henrika@webrtc.org | 19da719 | 2013-04-05 14:34:57 +0000 | [diff] [blame^] | 75 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 76 | WebRtc_UWord32 dw_delta_time = ms_time - prev_tickCount; |
| 77 | if (prev_tickCount == 0) { |
| 78 | dw_delta_time = 0; |
| 79 | } |
| 80 | if (dw_delta_time > 0x0fffffff) { |
| 81 | // Either wraparound or data race. |
| 82 | dw_delta_time = 0; |
| 83 | } |
| 84 | if (dw_delta_time > 99999) { |
| 85 | dw_delta_time = 99999; |
| 86 | } |
henrike@webrtc.org | 0e7c060 | 2012-02-08 18:53:50 +0000 | [diff] [blame] | 87 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 88 | sprintf(trace_message, "(%2u:%2u:%2u:%3u |%5lu) ", system_time->tm_hour, |
| 89 | system_time->tm_min, system_time->tm_sec, ms_time, |
| 90 | static_cast<unsigned long>(dw_delta_time)); |
| 91 | // Messages are 22 characters. |
| 92 | return 22; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 93 | } |
| 94 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 95 | WebRtc_Word32 TracePosix::AddBuildInfo(char* trace_message) const { |
| 96 | sprintf(trace_message, "Build info: %s", BUILDINFO); |
| 97 | // Include NULL termination (hence + 1). |
| 98 | return strlen(trace_message) + 1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 99 | } |
| 100 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 101 | WebRtc_Word32 TracePosix::AddDateTimeInfo(char* trace_message) const { |
| 102 | time_t t; |
| 103 | time(&t); |
| 104 | char buffer[26]; // man ctime says buffer should have room for >=26 bytes. |
| 105 | sprintf(trace_message, "Local Date: %s", ctime_r(&t, buffer)); |
| 106 | WebRtc_Word32 len = static_cast<WebRtc_Word32>(strlen(trace_message)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 107 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 108 | if ('\n' == trace_message[len - 1]) { |
| 109 | trace_message[len - 1] = '\0'; |
| 110 | --len; |
| 111 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 112 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 113 | // Messages is 12 characters. |
| 114 | return len + 1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 115 | } |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 116 | |
| 117 | } // namespace webrtc |