blob: f4e1d23434d1ccc288c836bd27af38390f12d8ca [file] [log] [blame]
niklase@google.comf0779a22011-05-30 11:39:38 +00001/*
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_linux.h"
12
13#include <cassert>
14#include <stdarg.h>
15#include <stdio.h>
16#include <string.h>
17#include <time.h>
18
19#ifdef ANDROID
20 #include <pthread.h>
21#else
22 #include <iostream>
23#endif
24
25#if defined(_DEBUG)
26 #define BUILDMODE "d"
27#elif defined(DEBUG)
28 #define BUILDMODE "d"
29#elif defined(NDEBUG)
30 #define BUILDMODE "r"
31#else
32 #define BUILDMODE "?"
33#endif
34#define BUILDTIME __TIME__
35#define BUILDDATE __DATE__
36// example: "Oct 10 2002 12:05:30 r"
37#define BUILDINFO BUILDDATE " " BUILDTIME " " BUILDMODE
38
39namespace webrtc {
40TraceLinux::TraceLinux()
41{
42 _prevAPITickCount = time(NULL);
43 _prevTickCount = _prevAPITickCount;
44}
45
46TraceLinux::~TraceLinux()
47{
48 StopThread();
49}
50
51WebRtc_Word32 TraceLinux::AddThreadId(char* traceMessage) const
52{
53 WebRtc_UWord64 threadId = (WebRtc_UWord64)pthread_self();
54 sprintf(traceMessage, "%10llu; ", threadId);
55 // 12 bytes are written.
56 return 12;
57}
58
59WebRtc_Word32 TraceLinux::AddTime(char* traceMessage,
60 const TraceLevel level) const
61{
62 time_t dwCurrentTimeInSeconds = time(NULL);
63 struct tm systemTime;
64 gmtime_r(&dwCurrentTimeInSeconds, &systemTime);
65
66 if(level == kTraceApiCall)
67 {
68 WebRtc_UWord32 dwDeltaTime = dwCurrentTimeInSeconds - _prevTickCount;
69 _prevTickCount = dwCurrentTimeInSeconds;
70
71 if(_prevTickCount == 0)
72 {
73 dwDeltaTime = 0;
74 }
75 if(dwDeltaTime > 0x0fffffff)
76 {
77 // Either wraparound or data race.
78 dwDeltaTime = 0;
79 }
80 if(dwDeltaTime > 99999)
81 {
82 dwDeltaTime = 99999;
83 }
84
85 sprintf (traceMessage, "(%2u:%2u:%2u:%3u |%5lu) ", systemTime.tm_hour,
86 systemTime.tm_min, systemTime.tm_sec, 0, dwDeltaTime);
87 } else {
88 WebRtc_UWord32 dwDeltaTime = dwCurrentTimeInSeconds - _prevAPITickCount;
89 _prevAPITickCount = dwCurrentTimeInSeconds;
90 if(_prevAPITickCount == 0)
91 {
92 dwDeltaTime = 0;
93 }
94 if(dwDeltaTime > 0x0fffffff)
95 {
96 // Either wraparound or data race.
97 dwDeltaTime = 0;
98 }
99 if(dwDeltaTime > 99999)
100 {
101 dwDeltaTime = 99999;
102 }
103 sprintf (traceMessage, "(%2u:%2u:%2u:%3u |%5lu) ", systemTime.tm_hour,
104 systemTime.tm_min, systemTime.tm_sec, 0, dwDeltaTime);
105 }
106 // Messages is 22 characters.
107 return 22;
108}
109
110WebRtc_Word32 TraceLinux::AddBuildInfo(char* traceMessage) const
111{
112 sprintf(traceMessage, "Build info: %s", BUILDINFO);
113 // Include NULL termination (hence + 1).
114 return strlen(traceMessage) + 1;
115}
116
117WebRtc_Word32 TraceLinux::AddDateTimeInfo(char* traceMessage) const
118{
119 time_t t;
120 time(&t);
121 sprintf(traceMessage, "Local Date: %s", ctime(&t));
122 WebRtc_Word32 len = static_cast<WebRtc_Word32>(strlen(traceMessage));
123
124 if ('\n' == traceMessage[len - 1])
125 {
126 traceMessage[len - 1] = '\0';
127 --len;
128 }
129
130 // Messages is 12 characters.
131 return len + 1;
132}
133} // namespace webrtc