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
18namespace webrtc {
19TraceWindows::TraceWindows()
20    : prev_api_tick_count_(0),
21      prev_tick_count_(0) {
22}
23
24TraceWindows::~TraceWindows() {
25}
26
27int32_t TraceWindows::AddTime(char* trace_message,
28                              const TraceLevel level) const {
29  uint32_t dw_current_time = timeGetTime();
30  SYSTEMTIME system_time;
31  GetSystemTime(&system_time);
32
33  if (level == kTraceApiCall) {
34    uint32_t dw_delta_time = dw_current_time - prev_tick_count_;
35    prev_tick_count_ = dw_current_time;
36
37    if (prev_tick_count_ == 0) {
38      dw_delta_time = 0;
39    }
40    if (dw_delta_time > 0x0fffffff) {
41      // Either wrap-around or data race.
42      dw_delta_time = 0;
43    }
44    if (dw_delta_time > 99999) {
45      dw_delta_time = 99999;
46    }
47
48    sprintf(trace_message, "(%2u:%2u:%2u:%3u |%5u) ", system_time.wHour,
49            system_time.wMinute, system_time.wSecond,
50            system_time.wMilliseconds, dw_delta_time);
51  } else {
52    uint32_t dw_delta_time = dw_current_time - prev_api_tick_count_;
53    prev_api_tick_count_ = dw_current_time;
54
55    if (prev_api_tick_count_ == 0) {
56      dw_delta_time = 0;
57    }
58    if (dw_delta_time > 0x0fffffff) {
59      // Either wraparound or data race.
60      dw_delta_time = 0;
61    }
62    if (dw_delta_time > 99999) {
63      dw_delta_time = 99999;
64    }
65    sprintf(trace_message, "(%2u:%2u:%2u:%3u |%5u) ", system_time.wHour,
66            system_time.wMinute, system_time.wSecond,
67            system_time.wMilliseconds, dw_delta_time);
68  }
69  return 22;
70}
71
72int32_t TraceWindows::AddDateTimeInfo(char* trace_message) const {
73  prev_api_tick_count_ = timeGetTime();
74  prev_tick_count_ = prev_api_tick_count_;
75
76  SYSTEMTIME sys_time;
77  GetLocalTime(&sys_time);
78
79  TCHAR sz_date_str[20];
80  TCHAR sz_time_str[20];
81
82  // Create date string (e.g. Apr 04 2002)
83  GetDateFormat(LOCALE_SYSTEM_DEFAULT, 0, &sys_time, TEXT("MMM dd yyyy"),
84                sz_date_str, 20);
85
86  // Create time string (e.g. 15:32:08)
87  GetTimeFormat(LOCALE_SYSTEM_DEFAULT, 0, &sys_time, TEXT("HH':'mm':'ss"),
88                sz_time_str, 20);
89
90  sprintf(trace_message, "Local Date: %ls Local Time: %ls", sz_date_str,
91          sz_time_str);
92
93  // Include NULL termination (hence + 1).
94  return static_cast<int32_t>(strlen(trace_message) + 1);
95}
96
97}  // namespace webrtc
98