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