blob: 5b75d1bf8e5e92cc34337fe2eaed77cf489ffb89 [file] [log] [blame]
Fredrik Solenberg729b9102017-10-03 13:39:39 +00001/*
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 "test/testsupport/trace_to_stderr.h"
12
13#include <assert.h>
14#include <stdio.h>
15
16#include <string>
17
18namespace webrtc {
19namespace test {
20
21static const int kLevelFilter = kTraceError | kTraceWarning | kTraceTerseInfo;
22
23TraceToStderr::TraceToStderr()
24 : override_time_(false),
25 time_seconds_(0) {
26 Trace::set_level_filter(kLevelFilter);
27 Trace::CreateTrace();
28 Trace::SetTraceCallback(this);
29}
30
31TraceToStderr::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
39TraceToStderr::~TraceToStderr() {
40 Trace::SetTraceCallback(NULL);
41 Trace::ReturnTrace();
42}
43
44void TraceToStderr::SetTimeSeconds(float time) { time_seconds_ = time; }
45
46void 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