blob: 22db3fa51af0f721d8b545f44d1db59cf337706d [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
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000013#include <assert.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000014#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>
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000019
leozwang@google.comb3527002011-07-26 17:29:38 +000020#ifdef WEBRTC_ANDROID
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000021#include <pthread.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000022#else
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000023#include <iostream>
niklase@google.com470e71d2011-07-07 08:21:25 +000024#endif
25
26#if defined(_DEBUG)
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000027#define BUILDMODE "d"
niklase@google.com470e71d2011-07-07 08:21:25 +000028#elif defined(DEBUG)
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000029#define BUILDMODE "d"
niklase@google.com470e71d2011-07-07 08:21:25 +000030#elif defined(NDEBUG)
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000031#define BUILDMODE "r"
niklase@google.com470e71d2011-07-07 08:21:25 +000032#else
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000033#define BUILDMODE "?"
niklase@google.com470e71d2011-07-07 08:21:25 +000034#endif
35#define BUILDTIME __TIME__
36#define BUILDDATE __DATE__
37// example: "Oct 10 2002 12:05:30 r"
38#define BUILDINFO BUILDDATE " " BUILDTIME " " BUILDMODE
39
40namespace webrtc {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000041
henrika@webrtc.org19da7192013-04-05 14:34:57 +000042TracePosix::TracePosix()
43 : crit_sect_(*CriticalSectionWrapper::CreateCriticalSection()) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000044 struct timeval system_time_high_res;
45 gettimeofday(&system_time_high_res, 0);
46 prev_api_tick_count_ = prev_tick_count_ = system_time_high_res.tv_sec;
niklase@google.com470e71d2011-07-07 08:21:25 +000047}
48
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000049TracePosix::~TracePosix() {
henrika@webrtc.org19da7192013-04-05 14:34:57 +000050 delete &crit_sect_;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000051 StopThread();
niklase@google.com470e71d2011-07-07 08:21:25 +000052}
53
pbos@webrtc.org046deb92013-04-09 09:06:11 +000054int32_t TracePosix::AddTime(char* trace_message, const TraceLevel level) const {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000055 struct timeval system_time_high_res;
56 if (gettimeofday(&system_time_high_res, 0) == -1) {
57 return -1;
58 }
59 struct tm buffer;
60 const struct tm* system_time =
61 localtime_r(&system_time_high_res.tv_sec, &buffer);
henrike@webrtc.org0e7c0602012-02-08 18:53:50 +000062
pbos@webrtc.org046deb92013-04-09 09:06:11 +000063 const uint32_t ms_time = system_time_high_res.tv_usec / 1000;
64 uint32_t prev_tickCount = 0;
henrika@webrtc.org19da7192013-04-05 14:34:57 +000065 {
66 CriticalSectionScoped lock(&crit_sect_);
67 if (level == kTraceApiCall) {
68 prev_tickCount = prev_tick_count_;
69 prev_tick_count_ = ms_time;
70 } else {
71 prev_tickCount = prev_api_tick_count_;
72 prev_api_tick_count_ = ms_time;
73 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000074 }
henrika@webrtc.org19da7192013-04-05 14:34:57 +000075
pbos@webrtc.org046deb92013-04-09 09:06:11 +000076 uint32_t dw_delta_time = ms_time - prev_tickCount;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000077 if (prev_tickCount == 0) {
78 dw_delta_time = 0;
79 }
80 if (dw_delta_time > 0x0fffffff) {
81 // Either wraparound or data race.
82 dw_delta_time = 0;
83 }
84 if (dw_delta_time > 99999) {
85 dw_delta_time = 99999;
86 }
henrike@webrtc.org0e7c0602012-02-08 18:53:50 +000087
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000088 sprintf(trace_message, "(%2u:%2u:%2u:%3u |%5lu) ", system_time->tm_hour,
89 system_time->tm_min, system_time->tm_sec, ms_time,
90 static_cast<unsigned long>(dw_delta_time));
91 // Messages are 22 characters.
92 return 22;
niklase@google.com470e71d2011-07-07 08:21:25 +000093}
94
pbos@webrtc.org046deb92013-04-09 09:06:11 +000095int32_t TracePosix::AddBuildInfo(char* trace_message) const {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000096 sprintf(trace_message, "Build info: %s", BUILDINFO);
97 // Include NULL termination (hence + 1).
98 return strlen(trace_message) + 1;
niklase@google.com470e71d2011-07-07 08:21:25 +000099}
100
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000101int32_t TracePosix::AddDateTimeInfo(char* trace_message) const {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000102 time_t t;
103 time(&t);
104 char buffer[26]; // man ctime says buffer should have room for >=26 bytes.
105 sprintf(trace_message, "Local Date: %s", ctime_r(&t, buffer));
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000106 int32_t len = static_cast<int32_t>(strlen(trace_message));
niklase@google.com470e71d2011-07-07 08:21:25 +0000107
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000108 if ('\n' == trace_message[len - 1]) {
109 trace_message[len - 1] = '\0';
110 --len;
111 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000112
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000113 // Messages is 12 characters.
114 return len + 1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000115}
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000116
117} // namespace webrtc