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 | |
ajm@google.com | b5c49ff | 2011-08-01 17:04:04 +0000 | [diff] [blame] | 11 | #include "trace_posix.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
| 13 | #include <cassert> |
| 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> |
leozwang@google.com | b352700 | 2011-07-26 17:29:38 +0000 | [diff] [blame] | 19 | #ifdef WEBRTC_ANDROID |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | #include <pthread.h> |
| 21 | #else |
| 22 | #include <iostream> |
| 23 | #endif |
| 24 | |
| 25 | #if defined(_DEBUG) |
| 26 | #define BUILDMODE "d" |
| 27 | #elif defined(DEBUG) |
| 28 | #define BUILDMODE "d" |
| 29 | #elif defined(NDEBUG) |
| 30 | #define BUILDMODE "r" |
| 31 | #else |
| 32 | #define BUILDMODE "?" |
| 33 | #endif |
| 34 | #define BUILDTIME __TIME__ |
| 35 | #define BUILDDATE __DATE__ |
| 36 | // example: "Oct 10 2002 12:05:30 r" |
| 37 | #define BUILDINFO BUILDDATE " " BUILDTIME " " BUILDMODE |
| 38 | |
| 39 | namespace webrtc { |
ajm@google.com | b5c49ff | 2011-08-01 17:04:04 +0000 | [diff] [blame] | 40 | TracePosix::TracePosix() |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 41 | { |
henrike@webrtc.org | 0e7c060 | 2012-02-08 18:53:50 +0000 | [diff] [blame] | 42 | struct timeval systemTimeHighRes; |
| 43 | gettimeofday(&systemTimeHighRes, 0); |
| 44 | _prevAPITickCount = _prevTickCount = systemTimeHighRes.tv_sec; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 45 | } |
| 46 | |
ajm@google.com | b5c49ff | 2011-08-01 17:04:04 +0000 | [diff] [blame] | 47 | TracePosix::~TracePosix() |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 48 | { |
| 49 | StopThread(); |
| 50 | } |
| 51 | |
ajm@google.com | b5c49ff | 2011-08-01 17:04:04 +0000 | [diff] [blame] | 52 | WebRtc_Word32 TracePosix::AddTime(char* traceMessage, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 53 | const TraceLevel level) const |
| 54 | { |
henrike@webrtc.org | 0e7c060 | 2012-02-08 18:53:50 +0000 | [diff] [blame] | 55 | struct timeval systemTimeHighRes; |
| 56 | if (gettimeofday(&systemTimeHighRes, 0) == -1) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 57 | { |
henrike@webrtc.org | 0e7c060 | 2012-02-08 18:53:50 +0000 | [diff] [blame] | 58 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 59 | } |
hta@webrtc.org | 1484ac0 | 2012-05-23 15:39:17 +0000 | [diff] [blame] | 60 | struct tm buffer; |
henrike@webrtc.org | 0e7c060 | 2012-02-08 18:53:50 +0000 | [diff] [blame] | 61 | const struct tm* systemTime = |
hta@webrtc.org | 1484ac0 | 2012-05-23 15:39:17 +0000 | [diff] [blame] | 62 | localtime_r(&systemTimeHighRes.tv_sec, &buffer); |
henrike@webrtc.org | 0e7c060 | 2012-02-08 18:53:50 +0000 | [diff] [blame] | 63 | |
| 64 | const WebRtc_UWord32 ms_time = systemTimeHighRes.tv_usec / 1000; |
| 65 | WebRtc_UWord32 prevTickCount = 0; |
| 66 | if (level == kTraceApiCall) |
| 67 | { |
| 68 | prevTickCount = _prevTickCount; |
| 69 | _prevTickCount = ms_time; |
| 70 | } else { |
| 71 | prevTickCount = _prevAPITickCount; |
| 72 | _prevAPITickCount = ms_time; |
| 73 | } |
| 74 | WebRtc_UWord32 dwDeltaTime = ms_time - prevTickCount; |
| 75 | if (prevTickCount == 0) |
| 76 | { |
| 77 | dwDeltaTime = 0; |
| 78 | } |
| 79 | if (dwDeltaTime > 0x0fffffff) |
| 80 | { |
| 81 | // Either wraparound or data race. |
| 82 | dwDeltaTime = 0; |
| 83 | } |
| 84 | if(dwDeltaTime > 99999) |
| 85 | { |
| 86 | dwDeltaTime = 99999; |
| 87 | } |
| 88 | |
| 89 | sprintf(traceMessage, "(%2u:%2u:%2u:%3u |%5lu) ", systemTime->tm_hour, |
| 90 | systemTime->tm_min, systemTime->tm_sec, ms_time, |
| 91 | static_cast<unsigned long>(dwDeltaTime)); |
| 92 | // Messages are 22 characters. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 93 | return 22; |
| 94 | } |
| 95 | |
ajm@google.com | b5c49ff | 2011-08-01 17:04:04 +0000 | [diff] [blame] | 96 | WebRtc_Word32 TracePosix::AddBuildInfo(char* traceMessage) const |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 97 | { |
| 98 | sprintf(traceMessage, "Build info: %s", BUILDINFO); |
| 99 | // Include NULL termination (hence + 1). |
| 100 | return strlen(traceMessage) + 1; |
| 101 | } |
| 102 | |
ajm@google.com | b5c49ff | 2011-08-01 17:04:04 +0000 | [diff] [blame] | 103 | WebRtc_Word32 TracePosix::AddDateTimeInfo(char* traceMessage) const |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 104 | { |
| 105 | time_t t; |
| 106 | time(&t); |
hta@webrtc.org | 1484ac0 | 2012-05-23 15:39:17 +0000 | [diff] [blame] | 107 | char buffer[26]; // man ctime says buffer should have room for >=26 bytes. |
| 108 | sprintf(traceMessage, "Local Date: %s", ctime_r(&t, buffer)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 109 | WebRtc_Word32 len = static_cast<WebRtc_Word32>(strlen(traceMessage)); |
| 110 | |
| 111 | if ('\n' == traceMessage[len - 1]) |
| 112 | { |
| 113 | traceMessage[len - 1] = '\0'; |
| 114 | --len; |
| 115 | } |
| 116 | |
| 117 | // Messages is 12 characters. |
| 118 | return len + 1; |
| 119 | } |
| 120 | } // namespace webrtc |