blob: 129e3dd64971fc36fd5480f9007adee5bd0c3f3b [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
andrew@webrtc.orge713fd02012-04-10 07:13:46 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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.orgdaabfd22013-01-03 09:37:03 +000011#include "webrtc/system_wrappers/source/trace_win.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000013#include <assert.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000014#include <stdarg.h>
15
16#include "Mmsystem.h"
17
18#if defined(_DEBUG)
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000019#define BUILDMODE "d"
niklase@google.com470e71d2011-07-07 08:21:25 +000020#elif defined(DEBUG)
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000021#define BUILDMODE "d"
niklase@google.com470e71d2011-07-07 08:21:25 +000022#elif defined(NDEBUG)
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000023#define BUILDMODE "r"
niklase@google.com470e71d2011-07-07 08:21:25 +000024#else
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000025#define BUILDMODE "?"
niklase@google.com470e71d2011-07-07 08:21:25 +000026#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
32namespace webrtc {
33TraceWindows::TraceWindows()
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000034 : prev_api_tick_count_(0),
35 prev_tick_count_(0) {
niklase@google.com470e71d2011-07-07 08:21:25 +000036}
37
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000038TraceWindows::~TraceWindows() {
39 StopThread();
niklase@google.com470e71d2011-07-07 08:21:25 +000040}
41
pbos@webrtc.org046deb92013-04-09 09:06:11 +000042int32_t TraceWindows::AddTime(char* trace_message,
43 const TraceLevel level) const {
44 uint32_t dw_current_time = timeGetTime();
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000045 SYSTEMTIME system_time;
46 GetSystemTime(&system_time);
niklase@google.com470e71d2011-07-07 08:21:25 +000047
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000048 if (level == kTraceApiCall) {
pbos@webrtc.org046deb92013-04-09 09:06:11 +000049 uint32_t dw_delta_time = dw_current_time - prev_tick_count_;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000050 prev_tick_count_ = dw_current_time;
niklase@google.com470e71d2011-07-07 08:21:25 +000051
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000052 if (prev_tick_count_ == 0) {
53 dw_delta_time = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000054 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000055 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.org046deb92013-04-09 09:06:11 +000067 uint32_t dw_delta_time = dw_current_time - prev_api_tick_count_;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000068 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.com470e71d2011-07-07 08:21:25 +000085}
86
pbos@webrtc.org046deb92013-04-09 09:06:11 +000087int32_t TraceWindows::AddBuildInfo(char* trace_message) const {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000088 // write data and time to text file
89 sprintf(trace_message, "Build info: %s", BUILDINFO);
90 // Include NULL termination (hence + 1).
pbos@webrtc.org046deb92013-04-09 09:06:11 +000091 return static_cast<int32_t>(strlen(trace_message) + 1);
niklase@google.com470e71d2011-07-07 08:21:25 +000092}
93
pbos@webrtc.org046deb92013-04-09 09:06:11 +000094int32_t TraceWindows::AddDateTimeInfo(char* trace_message) const {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000095 prev_api_tick_count_ = timeGetTime();
96 prev_tick_count_ = prev_api_tick_count_;
niklase@google.com470e71d2011-07-07 08:21:25 +000097
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000098 SYSTEMTIME sys_time;
99 GetLocalTime(&sys_time);
niklase@google.com470e71d2011-07-07 08:21:25 +0000100
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000101 TCHAR sz_date_str[20];
102 TCHAR sz_time_str[20];
niklase@google.com470e71d2011-07-07 08:21:25 +0000103
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000104 // 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.com470e71d2011-07-07 08:21:25 +0000107
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000108 // 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.com470e71d2011-07-07 08:21:25 +0000111
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000112 sprintf(trace_message, "Local Date: %s Local Time: %s", sz_date_str,
113 sz_time_str);
niklase@google.com470e71d2011-07-07 08:21:25 +0000114
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000115 // Include NULL termination (hence + 1).
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000116 return static_cast<int32_t>(strlen(trace_message) + 1);
niklase@google.com470e71d2011-07-07 08:21:25 +0000117}
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000118
119} // namespace webrtc