aboutsummaryrefslogtreecommitdiff
path: root/src/system_wrappers/source/trace_linux.cc
blob: 8dba3beba894762bc21d2ea2cfe3b4e3f2fd6874 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "trace_linux.h"

#include <cassert>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

#ifdef ANDROID
    #include <pthread.h>
#else
    #include <iostream>
#endif

#if defined(_DEBUG)
    #define BUILDMODE "d"
#elif defined(DEBUG)
    #define BUILDMODE "d"
#elif defined(NDEBUG)
    #define BUILDMODE "r"
#else
    #define BUILDMODE "?"
#endif
#define BUILDTIME __TIME__
#define BUILDDATE __DATE__
// example: "Oct 10 2002 12:05:30 r"
#define BUILDINFO BUILDDATE " " BUILDTIME " " BUILDMODE

namespace webrtc {
TraceLinux::TraceLinux()
{
    _prevAPITickCount = time(NULL);
    _prevTickCount = _prevAPITickCount;
}

TraceLinux::~TraceLinux()
{
    StopThread();
}

WebRtc_Word32 TraceLinux::AddThreadId(char* traceMessage) const
{
    WebRtc_UWord64 threadId = (WebRtc_UWord64)pthread_self();
    sprintf(traceMessage, "%10llu; ", threadId);
    // 12 bytes are written.
    return 12;
}

WebRtc_Word32 TraceLinux::AddTime(char* traceMessage,
                                  const TraceLevel level) const
{
    time_t dwCurrentTimeInSeconds = time(NULL);
    struct tm systemTime;
    gmtime_r(&dwCurrentTimeInSeconds, &systemTime);

    if(level == kTraceApiCall)
    {
        WebRtc_UWord32 dwDeltaTime = dwCurrentTimeInSeconds - _prevTickCount;
        _prevTickCount = dwCurrentTimeInSeconds;

        if(_prevTickCount == 0)
        {
            dwDeltaTime = 0;
        }
        if(dwDeltaTime > 0x0fffffff)
        {
            // Either wraparound or data race.
            dwDeltaTime = 0;
        }
        if(dwDeltaTime > 99999)
        {
            dwDeltaTime = 99999;
        }

        sprintf(traceMessage, "(%2u:%2u:%2u:%3u |%5lu) ", systemTime.tm_hour,
                systemTime.tm_min, systemTime.tm_sec, 0,
                static_cast<unsigned long>(dwDeltaTime));
    } else {
        WebRtc_UWord32 dwDeltaTime = dwCurrentTimeInSeconds - _prevAPITickCount;
        _prevAPITickCount = dwCurrentTimeInSeconds;
        if(_prevAPITickCount == 0)
        {
            dwDeltaTime = 0;
        }
        if(dwDeltaTime > 0x0fffffff)
        {
            // Either wraparound or data race.
            dwDeltaTime = 0;
        }
        if(dwDeltaTime > 99999)
        {
            dwDeltaTime = 99999;
        }
        sprintf(traceMessage, "(%2u:%2u:%2u:%3u |%5lu) ", systemTime.tm_hour,
                systemTime.tm_min, systemTime.tm_sec, 0,
                static_cast<unsigned long>(dwDeltaTime));
    }
    // Messages is 22 characters.
    return 22;
}

WebRtc_Word32 TraceLinux::AddBuildInfo(char* traceMessage) const
{
    sprintf(traceMessage, "Build info: %s", BUILDINFO);
    // Include NULL termination (hence + 1).
    return strlen(traceMessage) + 1;
}

WebRtc_Word32 TraceLinux::AddDateTimeInfo(char* traceMessage) const
{
    time_t t;
    time(&t);
    sprintf(traceMessage, "Local Date: %s", ctime(&t));
    WebRtc_Word32 len = static_cast<WebRtc_Word32>(strlen(traceMessage));

    if ('\n' == traceMessage[len - 1])
    {
        traceMessage[len - 1] = '\0';
        --len;
    }

    // Messages is 12 characters.
    return len + 1;
}
} // namespace webrtc