Line data Source code
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 "webrtc/system_wrappers/source/trace_posix.h"
12 :
13 : #include <assert.h>
14 : #include <stdarg.h>
15 : #include <stdio.h>
16 : #include <string.h>
17 : #include <sys/time.h>
18 : #include <time.h>
19 :
20 : namespace webrtc {
21 :
22 0 : TracePosix::TracePosix()
23 0 : : crit_sect_(*CriticalSectionWrapper::CreateCriticalSection()) {
24 : struct timeval system_time_high_res;
25 0 : gettimeofday(&system_time_high_res, 0);
26 0 : prev_api_tick_count_ = prev_tick_count_ = system_time_high_res.tv_sec;
27 0 : }
28 :
29 0 : TracePosix::~TracePosix() {
30 0 : delete &crit_sect_;
31 0 : }
32 :
33 0 : int32_t TracePosix::AddTime(char* trace_message, const TraceLevel level) const {
34 : struct timeval system_time_high_res;
35 0 : if (gettimeofday(&system_time_high_res, 0) == -1) {
36 0 : return -1;
37 : }
38 : struct tm buffer;
39 : const struct tm* system_time =
40 0 : localtime_r((const time_t *)(&system_time_high_res.tv_sec), &buffer);
41 :
42 0 : const uint32_t ms_time = system_time_high_res.tv_usec / 1000;
43 0 : uint32_t prev_tickCount = 0;
44 : {
45 0 : CriticalSectionScoped lock(&crit_sect_);
46 0 : if (level == kTraceApiCall) {
47 0 : prev_tickCount = prev_tick_count_;
48 0 : prev_tick_count_ = ms_time;
49 : } else {
50 0 : prev_tickCount = prev_api_tick_count_;
51 0 : prev_api_tick_count_ = ms_time;
52 : }
53 : }
54 :
55 0 : uint32_t dw_delta_time = ms_time - prev_tickCount;
56 0 : if (prev_tickCount == 0) {
57 0 : dw_delta_time = 0;
58 : }
59 0 : if (dw_delta_time > 0x0fffffff) {
60 : // Either wraparound or data race.
61 0 : dw_delta_time = 0;
62 : }
63 0 : if (dw_delta_time > 99999) {
64 0 : dw_delta_time = 99999;
65 : }
66 :
67 0 : sprintf(trace_message, "(%2u:%2u:%2u:%3u |%5lu) ", system_time->tm_hour,
68 0 : system_time->tm_min, system_time->tm_sec, ms_time,
69 0 : static_cast<unsigned long>(dw_delta_time));
70 : // Messages are 22 characters.
71 0 : return 22;
72 : }
73 :
74 0 : int32_t TracePosix::AddDateTimeInfo(char* trace_message) const {
75 : time_t t;
76 0 : time(&t);
77 : char buffer[26]; // man ctime says buffer should have room for >=26 bytes.
78 0 : sprintf(trace_message, "Local Date: %s", ctime_r(&t, buffer));
79 0 : int32_t len = static_cast<int32_t>(strlen(trace_message));
80 :
81 0 : if ('\n' == trace_message[len - 1]) {
82 0 : trace_message[len - 1] = '\0';
83 0 : --len;
84 : }
85 :
86 : // Messages is 12 characters.
87 0 : return len + 1;
88 : }
89 :
90 : } // namespace webrtc
|