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_posix.h"
12
13 #include <cassert>
14 #include <stdarg.h>
15 #include <stdio.h>
16 #include <string.h>
17 #include <sys/time.h>
18 #include <time.h>
19 #ifdef WEBRTC_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 #ifdef __ANDROID__
37 #define BUILDINFO BUILDMODE
38 #else
39 // example: "Oct 10 2002 12:05:30 r"
40 #define BUILDINFO BUILDDATE " " BUILDTIME " " BUILDMODE
41 #endif
42
43 namespace webrtc {
TracePosix()44 TracePosix::TracePosix()
45 {
46 struct timeval systemTimeHighRes;
47 gettimeofday(&systemTimeHighRes, 0);
48 _prevAPITickCount = _prevTickCount = systemTimeHighRes.tv_sec;
49 }
50
~TracePosix()51 TracePosix::~TracePosix()
52 {
53 StopThread();
54 }
55
AddTime(char * traceMessage,const TraceLevel level) const56 WebRtc_Word32 TracePosix::AddTime(char* traceMessage,
57 const TraceLevel level) const
58 {
59 struct timeval systemTimeHighRes;
60 if (gettimeofday(&systemTimeHighRes, 0) == -1)
61 {
62 return -1;
63 }
64 struct tm buffer;
65 const struct tm* systemTime =
66 localtime_r(&systemTimeHighRes.tv_sec, &buffer);
67
68 const WebRtc_UWord32 ms_time = systemTimeHighRes.tv_usec / 1000;
69 WebRtc_UWord32 prevTickCount = 0;
70 if (level == kTraceApiCall)
71 {
72 prevTickCount = _prevTickCount;
73 _prevTickCount = ms_time;
74 } else {
75 prevTickCount = _prevAPITickCount;
76 _prevAPITickCount = ms_time;
77 }
78 WebRtc_UWord32 dwDeltaTime = ms_time - prevTickCount;
79 if (prevTickCount == 0)
80 {
81 dwDeltaTime = 0;
82 }
83 if (dwDeltaTime > 0x0fffffff)
84 {
85 // Either wraparound or data race.
86 dwDeltaTime = 0;
87 }
88 if(dwDeltaTime > 99999)
89 {
90 dwDeltaTime = 99999;
91 }
92
93 sprintf(traceMessage, "(%2u:%2u:%2u:%3u |%5lu) ", systemTime->tm_hour,
94 systemTime->tm_min, systemTime->tm_sec, ms_time,
95 static_cast<unsigned long>(dwDeltaTime));
96 // Messages are 22 characters.
97 return 22;
98 }
99
AddBuildInfo(char * traceMessage) const100 WebRtc_Word32 TracePosix::AddBuildInfo(char* traceMessage) const
101 {
102 sprintf(traceMessage, "Build info: %s", BUILDINFO);
103 // Include NULL termination (hence + 1).
104 return strlen(traceMessage) + 1;
105 }
106
AddDateTimeInfo(char * traceMessage) const107 WebRtc_Word32 TracePosix::AddDateTimeInfo(char* traceMessage) const
108 {
109 time_t t;
110 time(&t);
111 char buffer[26]; // man ctime says buffer should have room for >=26 bytes.
112 sprintf(traceMessage, "Local Date: %s", ctime_r(&t, buffer));
113 WebRtc_Word32 len = static_cast<WebRtc_Word32>(strlen(traceMessage));
114
115 if ('\n' == traceMessage[len - 1])
116 {
117 traceMessage[len - 1] = '\0';
118 --len;
119 }
120
121 // Messages is 12 characters.
122 return len + 1;
123 }
124 } // namespace webrtc
125