2011-07-07 08:21:25 +00:00
|
|
|
/*
|
2012-02-08 18:53:50 +00:00
|
|
|
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
2011-07-07 08:21:25 +00:00
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2011-08-01 17:04:04 +00:00
|
|
|
#include "trace_posix.h"
|
2011-07-07 08:21:25 +00:00
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
2012-02-08 18:53:50 +00:00
|
|
|
#include <sys/time.h>
|
2011-07-07 08:21:25 +00:00
|
|
|
#include <time.h>
|
2011-07-26 17:29:38 +00:00
|
|
|
#ifdef WEBRTC_ANDROID
|
2011-07-07 08:21:25 +00:00
|
|
|
#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 {
|
2011-08-01 17:04:04 +00:00
|
|
|
TracePosix::TracePosix()
|
2011-07-07 08:21:25 +00:00
|
|
|
{
|
2012-02-08 18:53:50 +00:00
|
|
|
struct timeval systemTimeHighRes;
|
|
|
|
|
gettimeofday(&systemTimeHighRes, 0);
|
|
|
|
|
_prevAPITickCount = _prevTickCount = systemTimeHighRes.tv_sec;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2011-08-01 17:04:04 +00:00
|
|
|
TracePosix::~TracePosix()
|
2011-07-07 08:21:25 +00:00
|
|
|
{
|
|
|
|
|
StopThread();
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-01 17:04:04 +00:00
|
|
|
WebRtc_Word32 TracePosix::AddTime(char* traceMessage,
|
2011-07-07 08:21:25 +00:00
|
|
|
const TraceLevel level) const
|
|
|
|
|
{
|
2012-02-08 18:53:50 +00:00
|
|
|
struct timeval systemTimeHighRes;
|
|
|
|
|
if (gettimeofday(&systemTimeHighRes, 0) == -1)
|
2011-07-07 08:21:25 +00:00
|
|
|
{
|
2012-02-08 18:53:50 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
2012-05-23 15:39:17 +00:00
|
|
|
struct tm buffer;
|
2012-02-08 18:53:50 +00:00
|
|
|
const struct tm* systemTime =
|
2012-05-23 15:39:17 +00:00
|
|
|
localtime_r(&systemTimeHighRes.tv_sec, &buffer);
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2012-02-08 18:53:50 +00:00
|
|
|
const WebRtc_UWord32 ms_time = systemTimeHighRes.tv_usec / 1000;
|
|
|
|
|
WebRtc_UWord32 prevTickCount = 0;
|
|
|
|
|
if (level == kTraceApiCall)
|
|
|
|
|
{
|
|
|
|
|
prevTickCount = _prevTickCount;
|
|
|
|
|
_prevTickCount = ms_time;
|
2011-07-07 08:21:25 +00:00
|
|
|
} else {
|
2012-02-08 18:53:50 +00:00
|
|
|
prevTickCount = _prevAPITickCount;
|
|
|
|
|
_prevAPITickCount = ms_time;
|
|
|
|
|
}
|
|
|
|
|
WebRtc_UWord32 dwDeltaTime = ms_time - prevTickCount;
|
|
|
|
|
if (prevTickCount == 0)
|
|
|
|
|
{
|
|
|
|
|
dwDeltaTime = 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
2012-02-08 18:53:50 +00:00
|
|
|
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, ms_time,
|
|
|
|
|
static_cast<unsigned long>(dwDeltaTime));
|
|
|
|
|
// Messages are 22 characters.
|
2011-07-07 08:21:25 +00:00
|
|
|
return 22;
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-01 17:04:04 +00:00
|
|
|
WebRtc_Word32 TracePosix::AddBuildInfo(char* traceMessage) const
|
2011-07-07 08:21:25 +00:00
|
|
|
{
|
|
|
|
|
sprintf(traceMessage, "Build info: %s", BUILDINFO);
|
|
|
|
|
// Include NULL termination (hence + 1).
|
|
|
|
|
return strlen(traceMessage) + 1;
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-01 17:04:04 +00:00
|
|
|
WebRtc_Word32 TracePosix::AddDateTimeInfo(char* traceMessage) const
|
2011-07-07 08:21:25 +00:00
|
|
|
{
|
|
|
|
|
time_t t;
|
|
|
|
|
time(&t);
|
2012-05-23 15:39:17 +00:00
|
|
|
char buffer[26]; // man ctime says buffer should have room for >=26 bytes.
|
|
|
|
|
sprintf(traceMessage, "Local Date: %s", ctime_r(&t, buffer));
|
2011-07-07 08:21:25 +00:00
|
|
|
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
|