blob: 664dfeb1ee06cdf98edaf15f571d7132eea609db [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
henrike@webrtc.org0e7c0602012-02-08 18:53:50 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000011#include "webrtc/system_wrappers/source/trace_posix.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
13#include <cassert>
14#include <stdarg.h>
15#include <stdio.h>
16#include <string.h>
henrike@webrtc.org0e7c0602012-02-08 18:53:50 +000017#include <sys/time.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000018#include <time.h>
leozwang@google.comb3527002011-07-26 17:29:38 +000019#ifdef WEBRTC_ANDROID
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000020#include <pthread.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000021#else
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000022#include <iostream>
niklase@google.com470e71d2011-07-07 08:21:25 +000023#endif
24
25#if defined(_DEBUG)
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000026#define BUILDMODE "d"
niklase@google.com470e71d2011-07-07 08:21:25 +000027#elif defined(DEBUG)
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000028#define BUILDMODE "d"
niklase@google.com470e71d2011-07-07 08:21:25 +000029#elif defined(NDEBUG)
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000030#define BUILDMODE "r"
niklase@google.com470e71d2011-07-07 08:21:25 +000031#else
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000032#define BUILDMODE "?"
niklase@google.com470e71d2011-07-07 08:21:25 +000033#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 {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000040
henrika@webrtc.org19da7192013-04-05 14:34:57 +000041TracePosix::TracePosix()
42 : crit_sect_(*CriticalSectionWrapper::CreateCriticalSection()) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000043 struct timeval system_time_high_res;
44 gettimeofday(&system_time_high_res, 0);
45 prev_api_tick_count_ = prev_tick_count_ = system_time_high_res.tv_sec;
niklase@google.com470e71d2011-07-07 08:21:25 +000046}
47
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000048TracePosix::~TracePosix() {
henrika@webrtc.org19da7192013-04-05 14:34:57 +000049 delete &crit_sect_;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000050 StopThread();
niklase@google.com470e71d2011-07-07 08:21:25 +000051}
52
pbos@webrtc.org046deb92013-04-09 09:06:11 +000053int32_t TracePosix::AddTime(char* trace_message, const TraceLevel level) const {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000054 struct timeval system_time_high_res;
55 if (gettimeofday(&system_time_high_res, 0) == -1) {
56 return -1;
57 }
58 struct tm buffer;
59 const struct tm* system_time =
60 localtime_r(&system_time_high_res.tv_sec, &buffer);
henrike@webrtc.org0e7c0602012-02-08 18:53:50 +000061
pbos@webrtc.org046deb92013-04-09 09:06:11 +000062 const uint32_t ms_time = system_time_high_res.tv_usec / 1000;
63 uint32_t prev_tickCount = 0;
henrika@webrtc.org19da7192013-04-05 14:34:57 +000064 {
65 CriticalSectionScoped lock(&crit_sect_);
66 if (level == kTraceApiCall) {
67 prev_tickCount = prev_tick_count_;
68 prev_tick_count_ = ms_time;
69 } else {
70 prev_tickCount = prev_api_tick_count_;
71 prev_api_tick_count_ = ms_time;
72 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000073 }
henrika@webrtc.org19da7192013-04-05 14:34:57 +000074
pbos@webrtc.org046deb92013-04-09 09:06:11 +000075 uint32_t dw_delta_time = ms_time - prev_tickCount;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000076 if (prev_tickCount == 0) {
77 dw_delta_time = 0;
78 }
79 if (dw_delta_time > 0x0fffffff) {
80 // Either wraparound or data race.
81 dw_delta_time = 0;
82 }
83 if (dw_delta_time > 99999) {
84 dw_delta_time = 99999;
85 }
henrike@webrtc.org0e7c0602012-02-08 18:53:50 +000086
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000087 sprintf(trace_message, "(%2u:%2u:%2u:%3u |%5lu) ", system_time->tm_hour,
88 system_time->tm_min, system_time->tm_sec, ms_time,
89 static_cast<unsigned long>(dw_delta_time));
90 // Messages are 22 characters.
91 return 22;
niklase@google.com470e71d2011-07-07 08:21:25 +000092}
93
pbos@webrtc.org046deb92013-04-09 09:06:11 +000094int32_t TracePosix::AddBuildInfo(char* trace_message) const {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000095 sprintf(trace_message, "Build info: %s", BUILDINFO);
96 // Include NULL termination (hence + 1).
97 return strlen(trace_message) + 1;
niklase@google.com470e71d2011-07-07 08:21:25 +000098}
99
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000100int32_t TracePosix::AddDateTimeInfo(char* trace_message) const {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000101 time_t t;
102 time(&t);
103 char buffer[26]; // man ctime says buffer should have room for >=26 bytes.
104 sprintf(trace_message, "Local Date: %s", ctime_r(&t, buffer));
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000105 int32_t len = static_cast<int32_t>(strlen(trace_message));
niklase@google.com470e71d2011-07-07 08:21:25 +0000106
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000107 if ('\n' == trace_message[len - 1]) {
108 trace_message[len - 1] = '\0';
109 --len;
110 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000111
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000112 // Messages is 12 characters.
113 return len + 1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000114}
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000115
116} // namespace webrtc