aboutsummaryrefslogtreecommitdiff
path: root/webrtc/modules/remote_bitrate_estimator/test/bwe_test_logging.cc
blob: 3a84e81a0b973c24425f84264729be4050dfbbd2 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
 *  Copyright (c) 2013 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 "webrtc/modules/remote_bitrate_estimator/test/bwe_test_logging.h"

#if BWE_TEST_LOGGING_COMPILE_TIME_ENABLE

#include <stdarg.h>
#include <stdio.h>

#include <algorithm>
#include <sstream>

#include "webrtc/base/platform_thread.h"
#include "webrtc/system_wrappers/include/critical_section_wrapper.h"

namespace webrtc {
namespace testing {
namespace bwe {

Logging Logging::g_Logging;

static std::string ToString(uint32_t v) {
  std::stringstream ss;
  ss << v;
  return ss.str();
}

Logging::Context::Context(uint32_t name, int64_t timestamp_ms, bool enabled) {
  Logging::GetInstance()->PushState(ToString(name), timestamp_ms, enabled);
}

Logging::Context::Context(const std::string& name, int64_t timestamp_ms,
                          bool enabled) {
  Logging::GetInstance()->PushState(name, timestamp_ms, enabled);
}

Logging::Context::Context(const char* name, int64_t timestamp_ms,
                          bool enabled) {
  Logging::GetInstance()->PushState(name, timestamp_ms, enabled);
}

Logging::Context::~Context() {
  Logging::GetInstance()->PopState();
}

Logging* Logging::GetInstance() {
  return &g_Logging;
}

void Logging::SetGlobalContext(uint32_t name) {
  CriticalSectionScoped cs(crit_sect_.get());
  thread_map_[rtc::CurrentThreadId()].global_state.tag = ToString(name);
}

void Logging::SetGlobalContext(const std::string& name) {
  CriticalSectionScoped cs(crit_sect_.get());
  thread_map_[rtc::CurrentThreadId()].global_state.tag = name;
}

void Logging::SetGlobalContext(const char* name) {
  CriticalSectionScoped cs(crit_sect_.get());
  thread_map_[rtc::CurrentThreadId()].global_state.tag = name;
}

void Logging::SetGlobalEnable(bool enabled) {
  CriticalSectionScoped cs(crit_sect_.get());
  thread_map_[rtc::CurrentThreadId()].global_state.enabled = enabled;
}

void Logging::Log(const char format[], ...) {
  CriticalSectionScoped cs(crit_sect_.get());
  ThreadMap::iterator it = thread_map_.find(rtc::CurrentThreadId());
  assert(it != thread_map_.end());
  const State& state = it->second.stack.top();
  if (state.enabled) {
    printf("%s\t", state.tag.c_str());
    va_list args;
    va_start(args, format);
    vprintf(format, args);
    va_end(args);
    printf("\n");
  }
}

void Logging::Plot(int figure, double value) {
  Plot(figure, value, "-");
}

void Logging::Plot(int figure, double value, const std::string& alg_name) {
  CriticalSectionScoped cs(crit_sect_.get());
  ThreadMap::iterator it = thread_map_.find(rtc::CurrentThreadId());
  assert(it != thread_map_.end());
  const State& state = it->second.stack.top();
  std::string label = state.tag + '@' + alg_name;
  std::string prefix("Available");
  if (alg_name.compare(0, prefix.length(), prefix) == 0) {
    std::string receiver("Receiver");
    size_t start_pos = label.find(receiver);
    if (start_pos != std::string::npos) {
      label.replace(start_pos, receiver.length(), "Sender");
    }
  }
  if (state.enabled) {
    printf("PLOT\t%d\t%s\t%f\t%f\n", figure, label.c_str(),
           state.timestamp_ms * 0.001, value);
  }
}

void Logging::PlotBar(int figure,
                      const std::string& name,
                      double value,
                      int flow_id) {
  CriticalSectionScoped cs(crit_sect_.get());
  ThreadMap::iterator it = thread_map_.find(rtc::CurrentThreadId());
  assert(it != thread_map_.end());
  const State& state = it->second.stack.top();
  if (state.enabled) {
    printf("BAR\t%d\t%s_%d\t%f\n", figure, name.c_str(), flow_id, value);
  }
}

void Logging::PlotBaselineBar(int figure,
                              const std::string& name,
                              double value,
                              int flow_id) {
  CriticalSectionScoped cs(crit_sect_.get());
  ThreadMap::iterator it = thread_map_.find(rtc::CurrentThreadId());
  assert(it != thread_map_.end());
  const State& state = it->second.stack.top();
  if (state.enabled) {
    printf("BASELINE\t%d\t%s_%d\t%f\n", figure, name.c_str(), flow_id, value);
  }
}

void Logging::PlotErrorBar(int figure,
                           const std::string& name,
                           double value,
                           double ylow,
                           double yhigh,
                           const std::string& error_title,
                           int flow_id) {
  CriticalSectionScoped cs(crit_sect_.get());
  ThreadMap::iterator it = thread_map_.find(rtc::CurrentThreadId());
  assert(it != thread_map_.end());
  const State& state = it->second.stack.top();
  if (state.enabled) {
    printf("ERRORBAR\t%d\t%s_%d\t%f\t%f\t%f\t%s\n", figure, name.c_str(),
           flow_id, value, ylow, yhigh, error_title.c_str());
  }
}

void Logging::PlotLimitErrorBar(int figure,
                                const std::string& name,
                                double value,
                                double ylow,
                                double yhigh,
                                const std::string& error_title,
                                double ymax,
                                const std::string& limit_title,
                                int flow_id) {
  CriticalSectionScoped cs(crit_sect_.get());
  ThreadMap::iterator it = thread_map_.find(rtc::CurrentThreadId());
  assert(it != thread_map_.end());
  const State& state = it->second.stack.top();
  if (state.enabled) {
    printf("LIMITERRORBAR\t%d\t%s_%d\t%f\t%f\t%f\t%s\t%f\t%s\n", figure,
           name.c_str(), flow_id, value, ylow, yhigh, error_title.c_str(), ymax,
           limit_title.c_str());
  }
}

void Logging::PlotLabel(int figure,
                        const std::string& title,
                        const std::string& y_label,
                        int num_flows) {
  CriticalSectionScoped cs(crit_sect_.get());
  ThreadMap::iterator it = thread_map_.find(rtc::CurrentThreadId());
  assert(it != thread_map_.end());
  const State& state = it->second.stack.top();
  if (state.enabled) {
    printf("LABEL\t%d\t%s\t%s\t%d\n", figure, title.c_str(), y_label.c_str(),
           num_flows);
  }
}

Logging::Logging()
    : crit_sect_(CriticalSectionWrapper::CreateCriticalSection()),
      thread_map_() {
}

Logging::State::State() : tag(""), timestamp_ms(0), enabled(true) {}

Logging::State::State(const std::string& tag, int64_t timestamp_ms,
                      bool enabled)
    : tag(tag),
      timestamp_ms(timestamp_ms),
      enabled(enabled) {
}

void Logging::State::MergePrevious(const State& previous) {
  if (tag.empty()) {
    tag = previous.tag;
  } else if (!previous.tag.empty()) {
    tag = previous.tag + "_" + tag;
  }
  timestamp_ms = std::max(previous.timestamp_ms, timestamp_ms);
  enabled = previous.enabled && enabled;
}

void Logging::PushState(const std::string& append_to_tag, int64_t timestamp_ms,
                        bool enabled) {
  CriticalSectionScoped cs(crit_sect_.get());
  State new_state(append_to_tag, timestamp_ms, enabled);
  ThreadState* thread_state = &thread_map_[rtc::CurrentThreadId()];
  std::stack<State>* stack = &thread_state->stack;
  if (stack->empty()) {
    new_state.MergePrevious(thread_state->global_state);
  } else {
    new_state.MergePrevious(stack->top());
  }
  stack->push(new_state);
}

void Logging::PopState() {
  CriticalSectionScoped cs(crit_sect_.get());
  ThreadMap::iterator it = thread_map_.find(rtc::CurrentThreadId());
  assert(it != thread_map_.end());
  std::stack<State>* stack = &it->second.stack;
  int64_t newest_timestamp_ms = stack->top().timestamp_ms;
  stack->pop();
  if (!stack->empty()) {
    State* state = &stack->top();
    // Update time so that next log/plot will use the latest time seen so far
    // in this call tree.
    state->timestamp_ms = std::max(state->timestamp_ms, newest_timestamp_ms);
  }
}
}  // namespace bwe
}  // namespace testing
}  // namespace webrtc

#endif  // BWE_TEST_LOGGING_COMPILE_TIME_ENABLE