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