niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
andrew@webrtc.org | e713fd0 | 2012-04-10 07:13:46 +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_win.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 13 | #include <assert.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 14 | #include <stdarg.h> |
| 15 | |
| 16 | #include "Mmsystem.h" |
| 17 | |
| 18 | #if defined(_DEBUG) |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 19 | #define BUILDMODE "d" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | #elif defined(DEBUG) |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 21 | #define BUILDMODE "d" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 22 | #elif defined(NDEBUG) |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 23 | #define BUILDMODE "r" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 24 | #else |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 25 | #define BUILDMODE "?" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 26 | #endif |
| 27 | #define BUILDTIME __TIME__ |
| 28 | #define BUILDDATE __DATE__ |
| 29 | // Example: "Oct 10 2002 12:05:30 r" |
| 30 | #define BUILDINFO BUILDDATE " " BUILDTIME " " BUILDMODE |
| 31 | |
| 32 | namespace webrtc { |
| 33 | TraceWindows::TraceWindows() |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 34 | : prev_api_tick_count_(0), |
| 35 | prev_tick_count_(0) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 36 | } |
| 37 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 38 | TraceWindows::~TraceWindows() { |
| 39 | StopThread(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 40 | } |
| 41 | |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 42 | int32_t TraceWindows::AddTime(char* trace_message, |
| 43 | const TraceLevel level) const { |
| 44 | uint32_t dw_current_time = timeGetTime(); |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 45 | SYSTEMTIME system_time; |
| 46 | GetSystemTime(&system_time); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 47 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 48 | if (level == kTraceApiCall) { |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 49 | uint32_t dw_delta_time = dw_current_time - prev_tick_count_; |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 50 | prev_tick_count_ = dw_current_time; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 51 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 52 | if (prev_tick_count_ == 0) { |
| 53 | dw_delta_time = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 54 | } |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 55 | if (dw_delta_time > 0x0fffffff) { |
| 56 | // Either wrap-around or data race. |
| 57 | dw_delta_time = 0; |
| 58 | } |
| 59 | if (dw_delta_time > 99999) { |
| 60 | dw_delta_time = 99999; |
| 61 | } |
| 62 | |
| 63 | sprintf(trace_message, "(%2u:%2u:%2u:%3u |%5lu) ", system_time.wHour, |
| 64 | system_time.wMinute, system_time.wSecond, |
| 65 | system_time.wMilliseconds, dw_delta_time); |
| 66 | } else { |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 67 | uint32_t dw_delta_time = dw_current_time - prev_api_tick_count_; |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 68 | prev_api_tick_count_ = dw_current_time; |
| 69 | |
| 70 | if (prev_api_tick_count_ == 0) { |
| 71 | dw_delta_time = 0; |
| 72 | } |
| 73 | if (dw_delta_time > 0x0fffffff) { |
| 74 | // Either wraparound or data race. |
| 75 | dw_delta_time = 0; |
| 76 | } |
| 77 | if (dw_delta_time > 99999) { |
| 78 | dw_delta_time = 99999; |
| 79 | } |
| 80 | sprintf(trace_message, "(%2u:%2u:%2u:%3u |%5lu) ", system_time.wHour, |
| 81 | system_time.wMinute, system_time.wSecond, |
| 82 | system_time.wMilliseconds, dw_delta_time); |
| 83 | } |
| 84 | return 22; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 85 | } |
| 86 | |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 87 | int32_t TraceWindows::AddBuildInfo(char* trace_message) const { |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 88 | // write data and time to text file |
| 89 | sprintf(trace_message, "Build info: %s", BUILDINFO); |
| 90 | // Include NULL termination (hence + 1). |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 91 | return static_cast<int32_t>(strlen(trace_message) + 1); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 92 | } |
| 93 | |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 94 | int32_t TraceWindows::AddDateTimeInfo(char* trace_message) const { |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 95 | prev_api_tick_count_ = timeGetTime(); |
| 96 | prev_tick_count_ = prev_api_tick_count_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 97 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 98 | SYSTEMTIME sys_time; |
| 99 | GetLocalTime(&sys_time); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 100 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 101 | TCHAR sz_date_str[20]; |
| 102 | TCHAR sz_time_str[20]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 103 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 104 | // Create date string (e.g. Apr 04 2002) |
| 105 | GetDateFormat(LOCALE_SYSTEM_DEFAULT, 0, &sys_time, TEXT("MMM dd yyyy"), |
| 106 | sz_date_str, 20); |
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 | // Create time string (e.g. 15:32:08) |
| 109 | GetTimeFormat(LOCALE_SYSTEM_DEFAULT, 0, &sys_time, TEXT("HH':'mm':'ss"), |
| 110 | sz_time_str, 20); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 111 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 112 | sprintf(trace_message, "Local Date: %s Local Time: %s", sz_date_str, |
| 113 | sz_time_str); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 114 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 115 | // Include NULL termination (hence + 1). |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 116 | return static_cast<int32_t>(strlen(trace_message) + 1); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 117 | } |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 118 | |
| 119 | } // namespace webrtc |