1 /*
2 * Copyright (c) 2013 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/test/testsupport/trace_to_stderr.h"
12
13 #include <assert.h>
14 #include <stdio.h>
15
16 #include <string>
17
18 namespace webrtc {
19 namespace test {
20
21 static const int kLevelFilter = kTraceError | kTraceWarning | kTraceTerseInfo;
22
TraceToStderr()23 TraceToStderr::TraceToStderr()
24 : override_time_(false),
25 time_seconds_(0) {
26 Trace::set_level_filter(kLevelFilter);
27 Trace::CreateTrace();
28 Trace::SetTraceCallback(this);
29 }
30
TraceToStderr(bool override_time)31 TraceToStderr::TraceToStderr(bool override_time)
32 : override_time_(override_time),
33 time_seconds_(0) {
34 Trace::set_level_filter(kLevelFilter);
35 Trace::CreateTrace();
36 Trace::SetTraceCallback(this);
37 }
38
~TraceToStderr()39 TraceToStderr::~TraceToStderr() {
40 Trace::SetTraceCallback(NULL);
41 Trace::ReturnTrace();
42 }
43
SetTimeSeconds(float time)44 void TraceToStderr::SetTimeSeconds(float time) { time_seconds_ = time; }
45
Print(TraceLevel level,const char * msg_array,int length)46 void TraceToStderr::Print(TraceLevel level, const char* msg_array, int length) {
47 if (level & kLevelFilter) {
48 assert(length > Trace::kBoilerplateLength);
49 std::string msg = msg_array;
50 std::string msg_log = msg.substr(Trace::kBoilerplateLength);
51 if (override_time_) {
52 fprintf(stderr, "%.2fs %s\n", time_seconds_, msg_log.c_str());
53 } else {
54 std::string msg_time = msg.substr(Trace::kTimestampPosition,
55 Trace::kTimestampLength);
56 fprintf(stderr, "%s %s\n", msg_time.c_str(), msg_log.c_str());
57 }
58 fflush(stderr);
59 }
60 }
61
62 } // namespace test
63 } // namespace webrtc
64