blob: 8dbe76b1137031b18b7f9e53bd01b47afde3abdf [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000011#include "webrtc/system_wrappers/source/trace_impl.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000013#include <assert.h>
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000014#include <stdarg.h>
15#include <stdio.h>
16#include <string.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000017
18#ifdef _WIN32
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000019#include "webrtc/system_wrappers/source/trace_win.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000020#else
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000021#include "webrtc/system_wrappers/source/trace_posix.h"
22#endif // _WIN32
niklase@google.com470e71d2011-07-07 08:21:25 +000023
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000024#include "webrtc/system_wrappers/interface/sleep.h"
hta@webrtc.org41adcdb2012-06-18 11:24:57 +000025
niklase@google.com470e71d2011-07-07 08:21:25 +000026#define KEY_LEN_CHARS 31
27
28#ifdef _WIN32
andrew@webrtc.org5dffebc2012-08-16 04:24:05 +000029#pragma warning(disable:4355)
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000030#endif // _WIN32
niklase@google.com470e71d2011-07-07 08:21:25 +000031
32namespace webrtc {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000033
andrew@webrtc.orgc1ffd332013-03-22 17:13:23 +000034const int Trace::kBoilerplateLength = 71;
35const int Trace::kTimestampPosition = 13;
36const int Trace::kTimestampLength = 12;
andrew@webrtc.org90805182013-09-05 16:40:43 +000037uint32_t Trace::level_filter_ = kTraceDefault;
niklase@google.com470e71d2011-07-07 08:21:25 +000038
39// Construct On First Use idiom. Avoids "static initialization order fiasco".
henrike@webrtc.org315282c2011-12-09 17:46:20 +000040TraceImpl* TraceImpl::StaticInstance(CountOperation count_operation,
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000041 const TraceLevel level) {
42 // Sanities to avoid taking lock unless absolutely necessary (for
43 // performance reasons). count_operation == kAddRefNoCreate implies that a
44 // message will be written to file.
45 if ((level != kTraceAll) && (count_operation == kAddRefNoCreate)) {
andrew@webrtc.org90805182013-09-05 16:40:43 +000046 if (!(level & level_filter())) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000047 return NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +000048 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000049 }
50 TraceImpl* impl =
51 GetStaticInstance<TraceImpl>(count_operation);
52 return impl;
niklase@google.com470e71d2011-07-07 08:21:25 +000053}
54
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000055TraceImpl* TraceImpl::GetTrace(const TraceLevel level) {
56 return StaticInstance(kAddRefNoCreate, level);
niklase@google.com470e71d2011-07-07 08:21:25 +000057}
58
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000059TraceImpl* TraceImpl::CreateInstance() {
henrike@webrtc.org2f47b5a2011-12-10 00:44:47 +000060#if defined(_WIN32)
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000061 return new TraceWindows();
niklase@google.com470e71d2011-07-07 08:21:25 +000062#else
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000063 return new TracePosix();
niklase@google.com470e71d2011-07-07 08:21:25 +000064#endif
65}
66
67TraceImpl::TraceImpl()
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000068 : critsect_interface_(CriticalSectionWrapper::CreateCriticalSection()),
69 callback_(NULL),
70 row_count_text_(0),
71 file_count_text_(0),
72 trace_file_(*FileWrapper::Create()),
73 thread_(*ThreadWrapper::CreateThread(TraceImpl::Run, this,
niklase@google.com470e71d2011-07-07 08:21:25 +000074 kHighestPriority, "Trace")),
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000075 event_(*EventWrapper::Create()),
76 critsect_array_(CriticalSectionWrapper::CreateCriticalSection()),
77 next_free_idx_(),
78 level_(),
79 length_(),
80 message_queue_(),
81 active_queue_(0) {
82 next_free_idx_[0] = 0;
83 next_free_idx_[1] = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000084
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000085 unsigned int tid = 0;
86 thread_.Start(tid);
niklase@google.com470e71d2011-07-07 08:21:25 +000087
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000088 for (int m = 0; m < WEBRTC_TRACE_NUM_ARRAY; ++m) {
89 for (int n = 0; n < WEBRTC_TRACE_MAX_QUEUE; ++n) {
90 message_queue_[m][n] = new
91 char[WEBRTC_TRACE_MAX_MESSAGE_SIZE];
niklase@google.com470e71d2011-07-07 08:21:25 +000092 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000093 }
niklase@google.com470e71d2011-07-07 08:21:25 +000094}
95
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000096bool TraceImpl::StopThread() {
97 // Release the worker thread so that it can flush any lingering messages.
98 event_.Set();
niklase@google.com470e71d2011-07-07 08:21:25 +000099
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000100 // Allow 10 ms for pending messages to be flushed out.
101 // TODO(hellner): why not use condition variables to do this? Or let the
102 // worker thread die and let this thread flush remaining
103 // messages?
104 SleepMs(10);
niklase@google.com470e71d2011-07-07 08:21:25 +0000105
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000106 thread_.SetNotAlive();
107 // Make sure the thread finishes as quickly as possible (instead of having
108 // to wait for the timeout).
109 event_.Set();
110 bool stopped = thread_.Stop();
niklase@google.com470e71d2011-07-07 08:21:25 +0000111
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000112 CriticalSectionScoped lock(critsect_interface_);
113 trace_file_.Flush();
114 trace_file_.CloseFile();
115 return stopped;
niklase@google.com470e71d2011-07-07 08:21:25 +0000116}
117
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000118TraceImpl::~TraceImpl() {
119 StopThread();
120 delete &event_;
121 delete &trace_file_;
122 delete &thread_;
123 delete critsect_interface_;
124 delete critsect_array_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000125
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000126 for (int m = 0; m < WEBRTC_TRACE_NUM_ARRAY; ++m) {
127 for (int n = 0; n < WEBRTC_TRACE_MAX_QUEUE; ++n) {
128 delete [] message_queue_[m][n];
niklase@google.com470e71d2011-07-07 08:21:25 +0000129 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000130 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000131}
132
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000133int32_t TraceImpl::AddThreadId(char* trace_message) const {
134 uint32_t thread_id = ThreadWrapper::GetThreadId();
pwestin@webrtc.orgb54d7272012-01-11 08:28:04 +0000135 // Messages is 12 characters.
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000136 return sprintf(trace_message, "%10u; ", thread_id);
pwestin@webrtc.orgb54d7272012-01-11 08:28:04 +0000137}
138
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000139int32_t TraceImpl::AddLevel(char* sz_message, const TraceLevel level) const {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000140 const int kMessageLength = 12;
141 switch (level) {
142 case kTraceTerseInfo:
143 // Add the appropriate amount of whitespace.
144 memset(sz_message, ' ', kMessageLength);
145 sz_message[kMessageLength] = '\0';
146 break;
147 case kTraceStateInfo:
148 sprintf(sz_message, "STATEINFO ; ");
149 break;
150 case kTraceWarning:
151 sprintf(sz_message, "WARNING ; ");
152 break;
153 case kTraceError:
154 sprintf(sz_message, "ERROR ; ");
155 break;
156 case kTraceCritical:
157 sprintf(sz_message, "CRITICAL ; ");
158 break;
159 case kTraceInfo:
160 sprintf(sz_message, "DEBUGINFO ; ");
161 break;
162 case kTraceModuleCall:
163 sprintf(sz_message, "MODULECALL; ");
164 break;
165 case kTraceMemory:
166 sprintf(sz_message, "MEMORY ; ");
167 break;
168 case kTraceTimer:
169 sprintf(sz_message, "TIMER ; ");
170 break;
171 case kTraceStream:
172 sprintf(sz_message, "STREAM ; ");
173 break;
174 case kTraceApiCall:
175 sprintf(sz_message, "APICALL ; ");
176 break;
177 case kTraceDebug:
178 sprintf(sz_message, "DEBUG ; ");
179 break;
180 default:
181 assert(false);
182 return 0;
183 }
184 // All messages are 12 characters.
185 return kMessageLength;
niklase@google.com470e71d2011-07-07 08:21:25 +0000186}
187
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000188int32_t TraceImpl::AddModuleAndId(char* trace_message,
189 const TraceModule module,
190 const int32_t id) const {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000191 // Use long int to prevent problems with different definitions of
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000192 // int32_t.
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000193 // TODO(hellner): is this actually a problem? If so, it should be better to
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000194 // clean up int32_t
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000195 const long int idl = id;
196 const int kMessageLength = 25;
197 if (idl != -1) {
198 const unsigned long int id_engine = id >> 16;
199 const unsigned long int id_channel = id & 0xffff;
niklase@google.com470e71d2011-07-07 08:21:25 +0000200
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000201 switch (module) {
202 case kTraceUndefined:
203 // Add the appropriate amount of whitespace.
204 memset(trace_message, ' ', kMessageLength);
205 trace_message[kMessageLength] = '\0';
206 break;
207 case kTraceVoice:
208 sprintf(trace_message, " VOICE:%5ld %5ld;", id_engine,
209 id_channel);
210 break;
211 case kTraceVideo:
212 sprintf(trace_message, " VIDEO:%5ld %5ld;", id_engine,
213 id_channel);
214 break;
215 case kTraceUtility:
216 sprintf(trace_message, " UTILITY:%5ld %5ld;", id_engine,
217 id_channel);
218 break;
219 case kTraceRtpRtcp:
220 sprintf(trace_message, " RTP/RTCP:%5ld %5ld;", id_engine,
221 id_channel);
222 break;
223 case kTraceTransport:
224 sprintf(trace_message, " TRANSPORT:%5ld %5ld;", id_engine,
225 id_channel);
226 break;
227 case kTraceAudioCoding:
228 sprintf(trace_message, "AUDIO CODING:%5ld %5ld;", id_engine,
229 id_channel);
230 break;
231 case kTraceSrtp:
232 sprintf(trace_message, " SRTP:%5ld %5ld;", id_engine,
233 id_channel);
234 break;
235 case kTraceAudioMixerServer:
236 sprintf(trace_message, " AUDIO MIX/S:%5ld %5ld;", id_engine,
237 id_channel);
238 break;
239 case kTraceAudioMixerClient:
240 sprintf(trace_message, " AUDIO MIX/C:%5ld %5ld;", id_engine,
241 id_channel);
242 break;
243 case kTraceVideoCoding:
244 sprintf(trace_message, "VIDEO CODING:%5ld %5ld;", id_engine,
245 id_channel);
246 break;
247 case kTraceVideoMixer:
248 // Print sleep time and API call
249 sprintf(trace_message, " VIDEO MIX:%5ld %5ld;", id_engine,
250 id_channel);
251 break;
252 case kTraceFile:
253 sprintf(trace_message, " FILE:%5ld %5ld;", id_engine,
254 id_channel);
255 break;
256 case kTraceAudioProcessing:
257 sprintf(trace_message, " AUDIO PROC:%5ld %5ld;", id_engine,
258 id_channel);
259 break;
260 case kTraceAudioDevice:
261 sprintf(trace_message, "AUDIO DEVICE:%5ld %5ld;", id_engine,
262 id_channel);
263 break;
264 case kTraceVideoRenderer:
265 sprintf(trace_message, "VIDEO RENDER:%5ld %5ld;", id_engine,
266 id_channel);
267 break;
268 case kTraceVideoCapture:
269 sprintf(trace_message, "VIDEO CAPTUR:%5ld %5ld;", id_engine,
270 id_channel);
271 break;
272 case kTraceVideoPreocessing:
273 sprintf(trace_message, " VIDEO PROC:%5ld %5ld;", id_engine,
274 id_channel);
275 break;
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000276 case kTraceRemoteBitrateEstimator:
277 sprintf(trace_message, " BWE RBE:%5ld %5ld;", id_engine,
278 id_channel);
279 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000280 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000281 } else {
282 switch (module) {
283 case kTraceUndefined:
284 // Add the appropriate amount of whitespace.
285 memset(trace_message, ' ', kMessageLength);
286 trace_message[kMessageLength] = '\0';
287 break;
288 case kTraceVoice:
289 sprintf(trace_message, " VOICE:%11ld;", idl);
290 break;
291 case kTraceVideo:
292 sprintf(trace_message, " VIDEO:%11ld;", idl);
293 break;
294 case kTraceUtility:
295 sprintf(trace_message, " UTILITY:%11ld;", idl);
296 break;
297 case kTraceRtpRtcp:
298 sprintf(trace_message, " RTP/RTCP:%11ld;", idl);
299 break;
300 case kTraceTransport:
301 sprintf(trace_message, " TRANSPORT:%11ld;", idl);
302 break;
303 case kTraceAudioCoding:
304 sprintf(trace_message, "AUDIO CODING:%11ld;", idl);
305 break;
306 case kTraceSrtp:
307 sprintf(trace_message, " SRTP:%11ld;", idl);
308 break;
309 case kTraceAudioMixerServer:
310 sprintf(trace_message, " AUDIO MIX/S:%11ld;", idl);
311 break;
312 case kTraceAudioMixerClient:
313 sprintf(trace_message, " AUDIO MIX/C:%11ld;", idl);
314 break;
315 case kTraceVideoCoding:
316 sprintf(trace_message, "VIDEO CODING:%11ld;", idl);
317 break;
318 case kTraceVideoMixer:
319 sprintf(trace_message, " VIDEO MIX:%11ld;", idl);
320 break;
321 case kTraceFile:
322 sprintf(trace_message, " FILE:%11ld;", idl);
323 break;
324 case kTraceAudioProcessing:
325 sprintf(trace_message, " AUDIO PROC:%11ld;", idl);
326 break;
327 case kTraceAudioDevice:
328 sprintf(trace_message, "AUDIO DEVICE:%11ld;", idl);
329 break;
330 case kTraceVideoRenderer:
331 sprintf(trace_message, "VIDEO RENDER:%11ld;", idl);
332 break;
333 case kTraceVideoCapture:
334 sprintf(trace_message, "VIDEO CAPTUR:%11ld;", idl);
335 break;
336 case kTraceVideoPreocessing:
337 sprintf(trace_message, " VIDEO PROC:%11ld;", idl);
338 break;
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000339 case kTraceRemoteBitrateEstimator:
340 sprintf(trace_message, " BWE RBE:%11ld;", idl);
341 break;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000342 }
343 }
344 return kMessageLength;
niklase@google.com470e71d2011-07-07 08:21:25 +0000345}
346
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000347int32_t TraceImpl::SetTraceFileImpl(const char* file_name_utf8,
348 const bool add_file_counter) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000349 CriticalSectionScoped lock(critsect_interface_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000350
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000351 trace_file_.Flush();
352 trace_file_.CloseFile();
niklase@google.com470e71d2011-07-07 08:21:25 +0000353
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000354 if (file_name_utf8) {
355 if (add_file_counter) {
356 file_count_text_ = 1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000357
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000358 char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize];
359 CreateFileName(file_name_utf8, file_name_with_counter_utf8,
360 file_count_text_);
361 if (trace_file_.OpenFile(file_name_with_counter_utf8, false, false,
362 true) == -1) {
363 return -1;
364 }
365 } else {
366 file_count_text_ = 0;
367 if (trace_file_.OpenFile(file_name_utf8, false, false, true) == -1) {
368 return -1;
369 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000370 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000371 }
372 row_count_text_ = 0;
373 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000374}
375
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000376int32_t TraceImpl::TraceFileImpl(
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000377 char file_name_utf8[FileWrapper::kMaxFileNameSize]) {
378 CriticalSectionScoped lock(critsect_interface_);
379 return trace_file_.FileName(file_name_utf8, FileWrapper::kMaxFileNameSize);
niklase@google.com470e71d2011-07-07 08:21:25 +0000380}
381
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000382int32_t TraceImpl::SetTraceCallbackImpl(TraceCallback* callback) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000383 CriticalSectionScoped lock(critsect_interface_);
384 callback_ = callback;
385 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000386}
387
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000388int32_t TraceImpl::AddMessage(
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000389 char* trace_message,
niklase@google.com470e71d2011-07-07 08:21:25 +0000390 const char msg[WEBRTC_TRACE_MAX_MESSAGE_SIZE],
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000391 const uint16_t written_so_far) const {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000392 int length = 0;
393 if (written_so_far >= WEBRTC_TRACE_MAX_MESSAGE_SIZE) {
394 return -1;
395 }
396 // - 2 to leave room for newline and NULL termination.
niklase@google.com470e71d2011-07-07 08:21:25 +0000397#ifdef _WIN32
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000398 length = _snprintf(trace_message,
399 WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2,
400 "%s", msg);
401 if (length < 0) {
402 length = WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2;
403 trace_message[length] = 0;
404 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000405#else
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000406 length = snprintf(trace_message,
407 WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2,
408 "%s", msg);
409 if (length < 0 ||
410 length > WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2) {
411 length = WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2;
412 trace_message[length] = 0;
413 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000414#endif
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000415 // Length with NULL termination.
416 return length + 1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000417}
418
419void TraceImpl::AddMessageToList(
andrew@webrtc.orgc1ffd332013-03-22 17:13:23 +0000420 const char trace_message[WEBRTC_TRACE_MAX_MESSAGE_SIZE],
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000421 const uint16_t length,
andrew@webrtc.orgc1ffd332013-03-22 17:13:23 +0000422 const TraceLevel level) {
solenberg@webrtc.orgadb51f52013-06-10 09:03:41 +0000423// NOTE(andresp): Enabled externally.
424#ifdef WEBRTC_DIRECT_TRACE
425 if (callback_) {
426 callback_->Print(level, trace_message, length);
427 }
428 return;
429#endif
430
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000431 CriticalSectionScoped lock(critsect_array_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000432
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000433 if (next_free_idx_[active_queue_] >= WEBRTC_TRACE_MAX_QUEUE) {
434 if (!trace_file_.Open() && !callback_) {
435 // Keep at least the last 1/4 of old messages when not logging.
436 // TODO(hellner): isn't this redundant. The user will make it known
437 // when to start logging. Why keep messages before
438 // that?
439 for (int n = 0; n < WEBRTC_TRACE_MAX_QUEUE / 4; ++n) {
440 const int last_quarter_offset = (3 * WEBRTC_TRACE_MAX_QUEUE / 4);
441 memcpy(message_queue_[active_queue_][n],
442 message_queue_[active_queue_][n + last_quarter_offset],
443 WEBRTC_TRACE_MAX_MESSAGE_SIZE);
444 }
445 next_free_idx_[active_queue_] = WEBRTC_TRACE_MAX_QUEUE / 4;
niklase@google.com470e71d2011-07-07 08:21:25 +0000446 } else {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000447 // More messages are being written than there is room for in the
448 // buffer. Drop any new messages.
449 // TODO(hellner): its probably better to drop old messages instead
450 // of new ones. One step further: if this happens
451 // it's due to writing faster than what can be
452 // processed. Maybe modify the filter at this point.
453 // E.g. turn of STREAM.
454 return;
niklase@google.com470e71d2011-07-07 08:21:25 +0000455 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000456 }
457
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000458 uint16_t idx = next_free_idx_[active_queue_];
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000459 next_free_idx_[active_queue_]++;
460
461 level_[active_queue_][idx] = level;
462 length_[active_queue_][idx] = length;
463 memcpy(message_queue_[active_queue_][idx], trace_message, length);
464
465 if (next_free_idx_[active_queue_] == WEBRTC_TRACE_MAX_QUEUE - 1) {
466 // Logging more messages than can be worked off. Log a warning.
467 const char warning_msg[] = "WARNING MISSING TRACE MESSAGES\n";
468 level_[active_queue_][next_free_idx_[active_queue_]] = kTraceWarning;
469 length_[active_queue_][next_free_idx_[active_queue_]] = strlen(warning_msg);
470 memcpy(message_queue_[active_queue_][next_free_idx_[active_queue_]],
471 warning_msg, strlen(warning_msg));
472 next_free_idx_[active_queue_]++;
473 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000474}
475
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000476bool TraceImpl::Run(void* obj) {
477 return static_cast<TraceImpl*>(obj)->Process();
478}
niklase@google.com470e71d2011-07-07 08:21:25 +0000479
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000480bool TraceImpl::Process() {
481 if (event_.Wait(1000) == kEventSignaled) {
andrew@webrtc.orgc1ffd332013-03-22 17:13:23 +0000482 // This slightly odd construction is to avoid locking |critsect_interface_|
483 // while calling WriteToFile() since it's locked inside the function.
484 critsect_interface_->Enter();
485 bool write_to_file = trace_file_.Open() || callback_;
486 critsect_interface_->Leave();
487 if (write_to_file) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000488 WriteToFile();
niklase@google.com470e71d2011-07-07 08:21:25 +0000489 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000490 } else {
andrew@webrtc.orgc1ffd332013-03-22 17:13:23 +0000491 CriticalSectionScoped lock(critsect_interface_);
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000492 trace_file_.Flush();
493 }
494 return true;
495}
496
497void TraceImpl::WriteToFile() {
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000498 uint8_t local_queue_active = 0;
499 uint16_t local_next_free_idx = 0;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000500
501 // There are two buffers. One for reading (for writing to file) and one for
502 // writing (for storing new messages). Let new messages be posted to the
503 // unused buffer so that the current buffer can be flushed safely.
504 {
505 CriticalSectionScoped lock(critsect_array_);
506 local_next_free_idx = next_free_idx_[active_queue_];
507 next_free_idx_[active_queue_] = 0;
508 local_queue_active = active_queue_;
509 if (active_queue_ == 0) {
510 active_queue_ = 1;
511 } else {
512 active_queue_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000513 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000514 }
515 if (local_next_free_idx == 0) {
516 return;
517 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000518
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000519 CriticalSectionScoped lock(critsect_interface_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000520
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000521 for (uint16_t idx = 0; idx < local_next_free_idx; ++idx) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000522 TraceLevel local_level = level_[local_queue_active][idx];
523 if (callback_) {
524 callback_->Print(local_level, message_queue_[local_queue_active][idx],
525 length_[local_queue_active][idx]);
niklase@google.com470e71d2011-07-07 08:21:25 +0000526 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000527 if (trace_file_.Open()) {
528 if (row_count_text_ > WEBRTC_TRACE_MAX_FILE_SIZE) {
529 // wrap file
530 row_count_text_ = 0;
531 trace_file_.Flush();
532
533 if (file_count_text_ == 0) {
534 trace_file_.Rewind();
535 } else {
536 char old_file_name[FileWrapper::kMaxFileNameSize];
537 char new_file_name[FileWrapper::kMaxFileNameSize];
538
539 // get current name
540 trace_file_.FileName(old_file_name,
541 FileWrapper::kMaxFileNameSize);
542 trace_file_.CloseFile();
543
544 file_count_text_++;
545
546 UpdateFileName(old_file_name, new_file_name, file_count_text_);
547
548 if (trace_file_.OpenFile(new_file_name, false, false,
549 true) == -1) {
550 return;
551 }
552 }
553 }
554 if (row_count_text_ == 0) {
555 char message[WEBRTC_TRACE_MAX_MESSAGE_SIZE + 1];
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000556 int32_t length = AddDateTimeInfo(message);
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000557 if (length != -1) {
558 message[length] = 0;
559 message[length - 1] = '\n';
560 trace_file_.Write(message, length);
561 row_count_text_++;
562 }
563 length = AddBuildInfo(message);
564 if (length != -1) {
565 message[length + 1] = 0;
566 message[length] = '\n';
567 message[length - 1] = '\n';
568 trace_file_.Write(message, length + 1);
569 row_count_text_++;
570 row_count_text_++;
571 }
572 }
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000573 uint16_t length = length_[local_queue_active][idx];
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000574 message_queue_[local_queue_active][idx][length] = 0;
575 message_queue_[local_queue_active][idx][length - 1] = '\n';
576 trace_file_.Write(message_queue_[local_queue_active][idx], length);
577 row_count_text_++;
578 }
579 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000580}
581
582void TraceImpl::AddImpl(const TraceLevel level, const TraceModule module,
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000583 const int32_t id,
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000584 const char msg[WEBRTC_TRACE_MAX_MESSAGE_SIZE]) {
585 if (TraceCheck(level)) {
586 char trace_message[WEBRTC_TRACE_MAX_MESSAGE_SIZE];
587 char* message_ptr = trace_message;
niklase@google.com470e71d2011-07-07 08:21:25 +0000588
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000589 int32_t len = 0;
590 int32_t ack_len = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000591
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000592 len = AddLevel(message_ptr, level);
593 if (len == -1) {
594 return;
niklase@google.com470e71d2011-07-07 08:21:25 +0000595 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000596 message_ptr += len;
597 ack_len += len;
598
599 len = AddTime(message_ptr, level);
600 if (len == -1) {
601 return;
602 }
603 message_ptr += len;
604 ack_len += len;
605
606 len = AddModuleAndId(message_ptr, module, id);
607 if (len == -1) {
608 return;
609 }
610 message_ptr += len;
611 ack_len += len;
612
613 len = AddThreadId(message_ptr);
614 if (len < 0) {
615 return;
616 }
617 message_ptr += len;
618 ack_len += len;
619
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000620 len = AddMessage(message_ptr, msg, (uint16_t)ack_len);
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000621 if (len == -1) {
622 return;
623 }
624 ack_len += len;
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000625 AddMessageToList(trace_message, (uint16_t)ack_len, level);
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000626
627 // Make sure that messages are written as soon as possible.
628 event_.Set();
629 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000630}
631
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000632bool TraceImpl::TraceCheck(const TraceLevel level) const {
andrew@webrtc.org90805182013-09-05 16:40:43 +0000633 return (level & level_filter()) ? true : false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000634}
635
636bool TraceImpl::UpdateFileName(
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000637 const char file_name_utf8[FileWrapper::kMaxFileNameSize],
638 char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize],
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000639 const uint32_t new_count) const {
640 int32_t length = (int32_t)strlen(file_name_utf8);
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000641 if (length < 0) {
642 return false;
643 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000644
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000645 int32_t length_without_file_ending = length - 1;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000646 while (length_without_file_ending > 0) {
647 if (file_name_utf8[length_without_file_ending] == '.') {
648 break;
649 } else {
650 length_without_file_ending--;
niklase@google.com470e71d2011-07-07 08:21:25 +0000651 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000652 }
653 if (length_without_file_ending == 0) {
654 length_without_file_ending = length;
655 }
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000656 int32_t length_to_ = length_without_file_ending - 1;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000657 while (length_to_ > 0) {
658 if (file_name_utf8[length_to_] == '_') {
659 break;
660 } else {
661 length_to_--;
niklase@google.com470e71d2011-07-07 08:21:25 +0000662 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000663 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000664
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000665 memcpy(file_name_with_counter_utf8, file_name_utf8, length_to_);
666 sprintf(file_name_with_counter_utf8 + length_to_, "_%lu%s",
667 static_cast<long unsigned int>(new_count),
668 file_name_utf8 + length_without_file_ending);
669 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000670}
671
672bool TraceImpl::CreateFileName(
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000673 const char file_name_utf8[FileWrapper::kMaxFileNameSize],
674 char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize],
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000675 const uint32_t new_count) const {
676 int32_t length = (int32_t)strlen(file_name_utf8);
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000677 if (length < 0) {
678 return false;
679 }
680
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000681 int32_t length_without_file_ending = length - 1;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000682 while (length_without_file_ending > 0) {
683 if (file_name_utf8[length_without_file_ending] == '.') {
684 break;
685 } else {
686 length_without_file_ending--;
niklase@google.com470e71d2011-07-07 08:21:25 +0000687 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000688 }
689 if (length_without_file_ending == 0) {
690 length_without_file_ending = length;
691 }
692 memcpy(file_name_with_counter_utf8, file_name_utf8,
693 length_without_file_ending);
694 sprintf(file_name_with_counter_utf8 + length_without_file_ending, "_%lu%s",
695 static_cast<long unsigned int>(new_count),
696 file_name_utf8 + length_without_file_ending);
697 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000698}
699
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000700void Trace::CreateTrace() {
701 TraceImpl::StaticInstance(kAddRef);
tommi@webrtc.orgcde1e7f2011-11-15 12:23:36 +0000702}
703
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000704void Trace::ReturnTrace() {
705 TraceImpl::StaticInstance(kRelease);
tommi@webrtc.orgcde1e7f2011-11-15 12:23:36 +0000706}
707
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000708int32_t Trace::TraceFile(char file_name[FileWrapper::kMaxFileNameSize]) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000709 TraceImpl* trace = TraceImpl::GetTrace();
710 if (trace) {
711 int ret_val = trace->TraceFileImpl(file_name);
712 ReturnTrace();
713 return ret_val;
714 }
715 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000716}
717
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000718int32_t Trace::SetTraceFile(const char* file_name,
719 const bool add_file_counter) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000720 TraceImpl* trace = TraceImpl::GetTrace();
721 if (trace) {
722 int ret_val = trace->SetTraceFileImpl(file_name, add_file_counter);
723 ReturnTrace();
724 return ret_val;
725 }
726 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000727}
728
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000729int32_t Trace::SetTraceCallback(TraceCallback* callback) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000730 TraceImpl* trace = TraceImpl::GetTrace();
731 if (trace) {
732 int ret_val = trace->SetTraceCallbackImpl(callback);
733 ReturnTrace();
734 return ret_val;
735 }
736 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000737}
738
739void Trace::Add(const TraceLevel level, const TraceModule module,
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000740 const int32_t id, const char* msg, ...) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000741 TraceImpl* trace = TraceImpl::GetTrace(level);
742 if (trace) {
743 if (trace->TraceCheck(level)) {
744 char temp_buff[WEBRTC_TRACE_MAX_MESSAGE_SIZE];
745 char* buff = 0;
746 if (msg) {
747 va_list args;
748 va_start(args, msg);
niklase@google.com470e71d2011-07-07 08:21:25 +0000749#ifdef _WIN32
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000750 _vsnprintf(temp_buff, WEBRTC_TRACE_MAX_MESSAGE_SIZE - 1, msg, args);
niklase@google.com470e71d2011-07-07 08:21:25 +0000751#else
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000752 vsnprintf(temp_buff, WEBRTC_TRACE_MAX_MESSAGE_SIZE - 1, msg, args);
niklase@google.com470e71d2011-07-07 08:21:25 +0000753#endif
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000754 va_end(args);
755 buff = temp_buff;
756 }
757 trace->AddImpl(level, module, id, buff);
niklase@google.com470e71d2011-07-07 08:21:25 +0000758 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000759 ReturnTrace();
760 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000761}
tommi@webrtc.orgcde1e7f2011-11-15 12:23:36 +0000762
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000763} // namespace webrtc