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#if defined(_DEBUG)
21#define BUILDMODE "d"
22#elif defined(DEBUG)
23#define BUILDMODE "d"
24#elif defined(NDEBUG)
25#define BUILDMODE "r"
26#else
27#define BUILDMODE "?"
28#endif
29#define BUILDTIME __TIME__
30#define BUILDDATE __DATE__
31// example: "Oct 10 2002 12:05:30 r"
32#define BUILDINFO BUILDDATE " " BUILDTIME " " BUILDMODE
33
34namespace webrtc {
35
36TracePosix::TracePosix()
37    : crit_sect_(*CriticalSectionWrapper::CreateCriticalSection()) {
38  struct timeval system_time_high_res;
39  gettimeofday(&system_time_high_res, 0);
40  prev_api_tick_count_ = prev_tick_count_ = system_time_high_res.tv_sec;
41}
42
43TracePosix::~TracePosix() {
44  delete &crit_sect_;
45  StopThread();
46}
47
48int32_t TracePosix::AddTime(char* trace_message, const TraceLevel level) const {
49  struct timeval system_time_high_res;
50  if (gettimeofday(&system_time_high_res, 0) == -1) {
51    return -1;
52  }
53  struct tm buffer;
54  const struct tm* system_time =
55    localtime_r(&system_time_high_res.tv_sec, &buffer);
56
57  const uint32_t ms_time = system_time_high_res.tv_usec / 1000;
58  uint32_t prev_tickCount = 0;
59  {
60    CriticalSectionScoped lock(&crit_sect_);
61    if (level == kTraceApiCall) {
62      prev_tickCount = prev_tick_count_;
63      prev_tick_count_ = ms_time;
64    } else {
65      prev_tickCount = prev_api_tick_count_;
66      prev_api_tick_count_ = ms_time;
67    }
68  }
69
70  uint32_t dw_delta_time = ms_time - prev_tickCount;
71  if (prev_tickCount == 0) {
72    dw_delta_time = 0;
73  }
74  if (dw_delta_time > 0x0fffffff) {
75    // Either wraparound or data race.
76    dw_delta_time = 0;
77  }
78  if (dw_delta_time > 99999) {
79    dw_delta_time = 99999;
80  }
81
82  sprintf(trace_message, "(%2u:%2u:%2u:%3u |%5lu) ", system_time->tm_hour,
83          system_time->tm_min, system_time->tm_sec, ms_time,
84          static_cast<unsigned long>(dw_delta_time));
85  // Messages are 22 characters.
86  return 22;
87}
88
89int32_t TracePosix::AddBuildInfo(char* trace_message) const {
90  sprintf(trace_message, "Build info: %s", BUILDINFO);
91  // Include NULL termination (hence + 1).
92  return strlen(trace_message) + 1;
93}
94
95int32_t TracePosix::AddDateTimeInfo(char* trace_message) const {
96  time_t t;
97  time(&t);
98  char buffer[26];  // man ctime says buffer should have room for >=26 bytes.
99  sprintf(trace_message, "Local Date: %s", ctime_r(&t, buffer));
100  int32_t len = static_cast<int32_t>(strlen(trace_message));
101
102  if ('\n' == trace_message[len - 1]) {
103    trace_message[len - 1] = '\0';
104    --len;
105  }
106
107  // Messages is 12 characters.
108  return len + 1;
109}
110
111}  // namespace webrtc
112