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 "trace_win.h"
12 
13 #include <cassert>
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 
32 namespace webrtc {
TraceWindows()33 TraceWindows::TraceWindows()
34     : _prevAPITickCount(0),
35       _prevTickCount(0)
36 {
37 }
38 
~TraceWindows()39 TraceWindows::~TraceWindows()
40 {
41     StopThread();
42 }
43 
AddTime(char * traceMessage,const TraceLevel level) const44 WebRtc_Word32 TraceWindows::AddTime(char* traceMessage,
45                                     const TraceLevel level) const
46 {
47     WebRtc_UWord32 dwCurrentTime = timeGetTime();
48     SYSTEMTIME systemTime;
49     GetSystemTime(&systemTime);
50 
51     if(level == kTraceApiCall)
52     {
53         WebRtc_UWord32 dwDeltaTime = dwCurrentTime- _prevTickCount;
54         _prevTickCount = dwCurrentTime;
55 
56         if(_prevTickCount == 0)
57         {
58             dwDeltaTime = 0;
59         }
60         if(dwDeltaTime > 0x0fffffff)
61         {
62             // Either wraparound or data race.
63             dwDeltaTime = 0;
64         }
65         if(dwDeltaTime > 99999)
66         {
67             dwDeltaTime = 99999;
68         }
69 
70         sprintf (traceMessage, "(%2u:%2u:%2u:%3u |%5lu) ", systemTime.wHour,
71                  systemTime.wMinute, systemTime.wSecond,
72                  systemTime.wMilliseconds, dwDeltaTime);
73     } else {
74         WebRtc_UWord32 dwDeltaTime = dwCurrentTime - _prevAPITickCount;
75         _prevAPITickCount = dwCurrentTime;
76 
77         if(_prevAPITickCount == 0)
78         {
79             dwDeltaTime = 0;
80         }
81         if(dwDeltaTime > 0x0fffffff)
82         {
83             // Either wraparound or data race.
84             dwDeltaTime = 0;
85         }
86         if(dwDeltaTime > 99999)
87         {
88             dwDeltaTime = 99999;
89         }
90         sprintf (traceMessage, "(%2u:%2u:%2u:%3u |%5lu) ", systemTime.wHour,
91                  systemTime.wMinute, systemTime.wSecond,
92                  systemTime.wMilliseconds, dwDeltaTime);
93     }
94     // Messages is 12 characters.
95     return 22;
96 }
97 
AddBuildInfo(char * traceMessage) const98 WebRtc_Word32 TraceWindows::AddBuildInfo(char* traceMessage) const
99 {
100     // write data and time to text file
101     sprintf(traceMessage, "Build info: %s", BUILDINFO);
102     // Include NULL termination (hence + 1).
103     return static_cast<WebRtc_Word32>(strlen(traceMessage)+1);
104 }
105 
AddDateTimeInfo(char * traceMessage) const106 WebRtc_Word32 TraceWindows::AddDateTimeInfo(char* traceMessage) const
107 {
108     _prevAPITickCount = timeGetTime();
109     _prevTickCount = _prevAPITickCount;
110 
111     SYSTEMTIME sysTime;
112     GetLocalTime (&sysTime);
113 
114     TCHAR szDateStr[20];
115     TCHAR szTimeStr[20];
116 
117     // Create date string (e.g. Apr 04 2002)
118     GetDateFormat(LOCALE_SYSTEM_DEFAULT, 0, &sysTime, TEXT("MMM dd yyyy"),
119                   szDateStr, 20);
120 
121     // Create time string (e.g. 15:32:08)
122     GetTimeFormat(LOCALE_SYSTEM_DEFAULT, 0, &sysTime, TEXT("HH':'mm':'ss"),
123                   szTimeStr, 20);
124 
125     sprintf(traceMessage, "Local Date: %s Local Time: %s", szDateStr,
126             szTimeStr);
127 
128     // Include NULL termination (hence + 1).
129     return static_cast<WebRtc_Word32>(strlen(traceMessage)+ 1);
130 }
131 } // namespace webrtc
132