1/*
2 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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
11#include "webrtc/system_wrappers/source/trace_win.h"
12
13#include <assert.h>
14#include <stdarg.h>
15
16#include "Mmsystem.h"
17
18#if defined(_DEBUG)
19#define BUILDMODE "d"
20#elif defined(DEBUG)
21#define BUILDMODE "d"
22#elif defined(NDEBUG)
23#define BUILDMODE "r"
24#else
25#define BUILDMODE "?"
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
32namespace webrtc {
33TraceWindows::TraceWindows()
34    : prev_api_tick_count_(0),
35      prev_tick_count_(0) {
36}
37
38TraceWindows::~TraceWindows() {
39  StopThread();
40}
41
42int32_t TraceWindows::AddTime(char* trace_message,
43                              const TraceLevel level) const {
44  uint32_t dw_current_time = timeGetTime();
45  SYSTEMTIME system_time;
46  GetSystemTime(&system_time);
47
48  if (level == kTraceApiCall) {
49    uint32_t dw_delta_time = dw_current_time - prev_tick_count_;
50    prev_tick_count_ = dw_current_time;
51
52    if (prev_tick_count_ == 0) {
53      dw_delta_time = 0;
54    }
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 |%5u) ", system_time.wHour,
64            system_time.wMinute, system_time.wSecond,
65            system_time.wMilliseconds, dw_delta_time);
66  } else {
67    uint32_t dw_delta_time = dw_current_time - prev_api_tick_count_;
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 |%5u) ", system_time.wHour,
81            system_time.wMinute, system_time.wSecond,
82            system_time.wMilliseconds, dw_delta_time);
83  }
84  return 22;
85}
86
87int32_t TraceWindows::AddBuildInfo(char* trace_message) const {
88  // write data and time to text file
89  sprintf(trace_message, "Build info: %s", BUILDINFO);
90  // Include NULL termination (hence + 1).
91  return static_cast<int32_t>(strlen(trace_message) + 1);
92}
93
94int32_t TraceWindows::AddDateTimeInfo(char* trace_message) const {
95  prev_api_tick_count_ = timeGetTime();
96  prev_tick_count_ = prev_api_tick_count_;
97
98  SYSTEMTIME sys_time;
99  GetLocalTime(&sys_time);
100
101  TCHAR sz_date_str[20];
102  TCHAR sz_time_str[20];
103
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);
107
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);
111
112  sprintf(trace_message, "Local Date: %ls Local Time: %ls", sz_date_str,
113          sz_time_str);
114
115  // Include NULL termination (hence + 1).
116  return static_cast<int32_t>(strlen(trace_message) + 1);
117}
118
119}  // namespace webrtc
120