blob: cb702d8cea19457347e568a185eb7c4a85c00a8b [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
niklase@google.com470e71d2011-07-07 08:21:25 +000020namespace webrtc {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000021
henrika@webrtc.org19da7192013-04-05 14:34:57 +000022TracePosix::TracePosix()
23 : crit_sect_(*CriticalSectionWrapper::CreateCriticalSection()) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000024 struct timeval system_time_high_res;
25 gettimeofday(&system_time_high_res, 0);
26 prev_api_tick_count_ = prev_tick_count_ = system_time_high_res.tv_sec;
niklase@google.com470e71d2011-07-07 08:21:25 +000027}
28
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000029TracePosix::~TracePosix() {
henrika@webrtc.org19da7192013-04-05 14:34:57 +000030 delete &crit_sect_;
niklase@google.com470e71d2011-07-07 08:21:25 +000031}
32
pbos@webrtc.org046deb92013-04-09 09:06:11 +000033int32_t TracePosix::AddTime(char* trace_message, const TraceLevel level) const {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000034 struct timeval system_time_high_res;
35 if (gettimeofday(&system_time_high_res, 0) == -1) {
36 return -1;
37 }
38 struct tm buffer;
39 const struct tm* system_time =
40 localtime_r(&system_time_high_res.tv_sec, &buffer);
henrike@webrtc.org0e7c0602012-02-08 18:53:50 +000041
pbos@webrtc.org046deb92013-04-09 09:06:11 +000042 const uint32_t ms_time = system_time_high_res.tv_usec / 1000;
43 uint32_t prev_tickCount = 0;
henrika@webrtc.org19da7192013-04-05 14:34:57 +000044 {
45 CriticalSectionScoped lock(&crit_sect_);
46 if (level == kTraceApiCall) {
47 prev_tickCount = prev_tick_count_;
48 prev_tick_count_ = ms_time;
49 } else {
50 prev_tickCount = prev_api_tick_count_;
51 prev_api_tick_count_ = ms_time;
52 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000053 }
henrika@webrtc.org19da7192013-04-05 14:34:57 +000054
pbos@webrtc.org046deb92013-04-09 09:06:11 +000055 uint32_t dw_delta_time = ms_time - prev_tickCount;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000056 if (prev_tickCount == 0) {
57 dw_delta_time = 0;
58 }
59 if (dw_delta_time > 0x0fffffff) {
60 // Either wraparound or data race.
61 dw_delta_time = 0;
62 }
63 if (dw_delta_time > 99999) {
64 dw_delta_time = 99999;
65 }
henrike@webrtc.org0e7c0602012-02-08 18:53:50 +000066
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000067 sprintf(trace_message, "(%2u:%2u:%2u:%3u |%5lu) ", system_time->tm_hour,
68 system_time->tm_min, system_time->tm_sec, ms_time,
69 static_cast<unsigned long>(dw_delta_time));
70 // Messages are 22 characters.
71 return 22;
niklase@google.com470e71d2011-07-07 08:21:25 +000072}
73
pbos@webrtc.org046deb92013-04-09 09:06:11 +000074int32_t TracePosix::AddDateTimeInfo(char* trace_message) const {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000075 time_t t;
76 time(&t);
77 char buffer[26]; // man ctime says buffer should have room for >=26 bytes.
78 sprintf(trace_message, "Local Date: %s", ctime_r(&t, buffer));
pbos@webrtc.org046deb92013-04-09 09:06:11 +000079 int32_t len = static_cast<int32_t>(strlen(trace_message));
niklase@google.com470e71d2011-07-07 08:21:25 +000080
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000081 if ('\n' == trace_message[len - 1]) {
82 trace_message[len - 1] = '\0';
83 --len;
84 }
niklase@google.com470e71d2011-07-07 08:21:25 +000085
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000086 // Messages is 12 characters.
87 return len + 1;
niklase@google.com470e71d2011-07-07 08:21:25 +000088}
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000089
90} // namespace webrtc