niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
henrike@webrtc.org | 0e7c060 | 2012-02-08 18:53:50 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 11 | #include "webrtc/system_wrappers/source/trace_posix.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 13 | #include <assert.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 14 | #include <stdarg.h> |
| 15 | #include <stdio.h> |
| 16 | #include <string.h> |
henrike@webrtc.org | 0e7c060 | 2012-02-08 18:53:50 +0000 | [diff] [blame] | 17 | #include <sys/time.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 18 | #include <time.h> |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 19 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | namespace webrtc { |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 21 | |
kthelgason | d701dfd | 2017-03-27 07:24:57 -0700 | [diff] [blame] | 22 | TracePosix::TracePosix() { |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 23 | struct timeval system_time_high_res; |
| 24 | gettimeofday(&system_time_high_res, 0); |
| 25 | prev_api_tick_count_ = prev_tick_count_ = system_time_high_res.tv_sec; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 26 | } |
| 27 | |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 28 | int32_t TracePosix::AddTime(char* trace_message, const TraceLevel level) const { |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 29 | struct timeval system_time_high_res; |
| 30 | if (gettimeofday(&system_time_high_res, 0) == -1) { |
| 31 | return -1; |
| 32 | } |
| 33 | struct tm buffer; |
| 34 | const struct tm* system_time = |
| 35 | localtime_r(&system_time_high_res.tv_sec, &buffer); |
henrike@webrtc.org | 0e7c060 | 2012-02-08 18:53:50 +0000 | [diff] [blame] | 36 | |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 37 | const uint32_t ms_time = system_time_high_res.tv_usec / 1000; |
| 38 | uint32_t prev_tickCount = 0; |
henrika@webrtc.org | 19da719 | 2013-04-05 14:34:57 +0000 | [diff] [blame] | 39 | { |
kthelgason | d701dfd | 2017-03-27 07:24:57 -0700 | [diff] [blame] | 40 | rtc::CritScope lock(&crit_sect_); |
henrika@webrtc.org | 19da719 | 2013-04-05 14:34:57 +0000 | [diff] [blame] | 41 | if (level == kTraceApiCall) { |
| 42 | prev_tickCount = prev_tick_count_; |
| 43 | prev_tick_count_ = ms_time; |
| 44 | } else { |
| 45 | prev_tickCount = prev_api_tick_count_; |
| 46 | prev_api_tick_count_ = ms_time; |
| 47 | } |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 48 | } |
henrika@webrtc.org | 19da719 | 2013-04-05 14:34:57 +0000 | [diff] [blame] | 49 | |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 50 | uint32_t dw_delta_time = ms_time - prev_tickCount; |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 51 | if (prev_tickCount == 0) { |
| 52 | dw_delta_time = 0; |
| 53 | } |
| 54 | if (dw_delta_time > 0x0fffffff) { |
| 55 | // Either wraparound or data race. |
| 56 | dw_delta_time = 0; |
| 57 | } |
| 58 | if (dw_delta_time > 99999) { |
| 59 | dw_delta_time = 99999; |
| 60 | } |
henrike@webrtc.org | 0e7c060 | 2012-02-08 18:53:50 +0000 | [diff] [blame] | 61 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 62 | sprintf(trace_message, "(%2u:%2u:%2u:%3u |%5lu) ", system_time->tm_hour, |
| 63 | system_time->tm_min, system_time->tm_sec, ms_time, |
| 64 | static_cast<unsigned long>(dw_delta_time)); |
| 65 | // Messages are 22 characters. |
| 66 | return 22; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 67 | } |
| 68 | |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 69 | int32_t TracePosix::AddDateTimeInfo(char* trace_message) const { |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 70 | time_t t; |
| 71 | time(&t); |
| 72 | char buffer[26]; // man ctime says buffer should have room for >=26 bytes. |
| 73 | sprintf(trace_message, "Local Date: %s", ctime_r(&t, buffer)); |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 74 | int32_t len = static_cast<int32_t>(strlen(trace_message)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 75 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 76 | if ('\n' == trace_message[len - 1]) { |
| 77 | trace_message[len - 1] = '\0'; |
| 78 | --len; |
| 79 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 80 | |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 81 | // Messages is 12 characters. |
| 82 | return len + 1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 83 | } |
phoglund@webrtc.org | daabfd2 | 2013-01-03 09:37:03 +0000 | [diff] [blame] | 84 | |
| 85 | } // namespace webrtc |