blob: de7c58c4397393b4d2a118d860d2fb84e6cb72a6 [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
13#include <cassert>
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;
37
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000038static WebRtc_UWord32 level_filter = kTraceDefault;
niklase@google.com470e71d2011-07-07 08:21:25 +000039
40// Construct On First Use idiom. Avoids "static initialization order fiasco".
henrike@webrtc.org315282c2011-12-09 17:46:20 +000041TraceImpl* TraceImpl::StaticInstance(CountOperation count_operation,
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000042 const TraceLevel level) {
43 // Sanities to avoid taking lock unless absolutely necessary (for
44 // performance reasons). count_operation == kAddRefNoCreate implies that a
45 // message will be written to file.
46 if ((level != kTraceAll) && (count_operation == kAddRefNoCreate)) {
47 if (!(level & level_filter)) {
48 return NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +000049 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000050 }
51 TraceImpl* impl =
52 GetStaticInstance<TraceImpl>(count_operation);
53 return impl;
niklase@google.com470e71d2011-07-07 08:21:25 +000054}
55
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000056TraceImpl* TraceImpl::GetTrace(const TraceLevel level) {
57 return StaticInstance(kAddRefNoCreate, level);
niklase@google.com470e71d2011-07-07 08:21:25 +000058}
59
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000060TraceImpl* TraceImpl::CreateInstance() {
henrike@webrtc.org2f47b5a2011-12-10 00:44:47 +000061#if defined(_WIN32)
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000062 return new TraceWindows();
niklase@google.com470e71d2011-07-07 08:21:25 +000063#else
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000064 return new TracePosix();
niklase@google.com470e71d2011-07-07 08:21:25 +000065#endif
66}
67
68TraceImpl::TraceImpl()
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000069 : critsect_interface_(CriticalSectionWrapper::CreateCriticalSection()),
70 callback_(NULL),
71 row_count_text_(0),
72 file_count_text_(0),
73 trace_file_(*FileWrapper::Create()),
74 thread_(*ThreadWrapper::CreateThread(TraceImpl::Run, this,
niklase@google.com470e71d2011-07-07 08:21:25 +000075 kHighestPriority, "Trace")),
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000076 event_(*EventWrapper::Create()),
77 critsect_array_(CriticalSectionWrapper::CreateCriticalSection()),
78 next_free_idx_(),
79 level_(),
80 length_(),
81 message_queue_(),
82 active_queue_(0) {
83 next_free_idx_[0] = 0;
84 next_free_idx_[1] = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000085
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000086 unsigned int tid = 0;
87 thread_.Start(tid);
niklase@google.com470e71d2011-07-07 08:21:25 +000088
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000089 for (int m = 0; m < WEBRTC_TRACE_NUM_ARRAY; ++m) {
90 for (int n = 0; n < WEBRTC_TRACE_MAX_QUEUE; ++n) {
91 message_queue_[m][n] = new
92 char[WEBRTC_TRACE_MAX_MESSAGE_SIZE];
niklase@google.com470e71d2011-07-07 08:21:25 +000093 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000094 }
niklase@google.com470e71d2011-07-07 08:21:25 +000095}
96
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000097bool TraceImpl::StopThread() {
98 // Release the worker thread so that it can flush any lingering messages.
99 event_.Set();
niklase@google.com470e71d2011-07-07 08:21:25 +0000100
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000101 // Allow 10 ms for pending messages to be flushed out.
102 // TODO(hellner): why not use condition variables to do this? Or let the
103 // worker thread die and let this thread flush remaining
104 // messages?
105 SleepMs(10);
niklase@google.com470e71d2011-07-07 08:21:25 +0000106
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000107 thread_.SetNotAlive();
108 // Make sure the thread finishes as quickly as possible (instead of having
109 // to wait for the timeout).
110 event_.Set();
111 bool stopped = thread_.Stop();
niklase@google.com470e71d2011-07-07 08:21:25 +0000112
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000113 CriticalSectionScoped lock(critsect_interface_);
114 trace_file_.Flush();
115 trace_file_.CloseFile();
116 return stopped;
niklase@google.com470e71d2011-07-07 08:21:25 +0000117}
118
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000119TraceImpl::~TraceImpl() {
120 StopThread();
121 delete &event_;
122 delete &trace_file_;
123 delete &thread_;
124 delete critsect_interface_;
125 delete critsect_array_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000126
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000127 for (int m = 0; m < WEBRTC_TRACE_NUM_ARRAY; ++m) {
128 for (int n = 0; n < WEBRTC_TRACE_MAX_QUEUE; ++n) {
129 delete [] message_queue_[m][n];
niklase@google.com470e71d2011-07-07 08:21:25 +0000130 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000131 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000132}
133
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000134WebRtc_Word32 TraceImpl::AddThreadId(char* trace_message) const {
135 WebRtc_UWord32 thread_id = ThreadWrapper::GetThreadId();
pwestin@webrtc.orgb54d7272012-01-11 08:28:04 +0000136 // Messages is 12 characters.
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000137 return sprintf(trace_message, "%10u; ", thread_id);
pwestin@webrtc.orgb54d7272012-01-11 08:28:04 +0000138}
139
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000140WebRtc_Word32 TraceImpl::AddLevel(char* sz_message,
141 const TraceLevel level) const {
142 const int kMessageLength = 12;
143 switch (level) {
144 case kTraceTerseInfo:
145 // Add the appropriate amount of whitespace.
146 memset(sz_message, ' ', kMessageLength);
147 sz_message[kMessageLength] = '\0';
148 break;
149 case kTraceStateInfo:
150 sprintf(sz_message, "STATEINFO ; ");
151 break;
152 case kTraceWarning:
153 sprintf(sz_message, "WARNING ; ");
154 break;
155 case kTraceError:
156 sprintf(sz_message, "ERROR ; ");
157 break;
158 case kTraceCritical:
159 sprintf(sz_message, "CRITICAL ; ");
160 break;
161 case kTraceInfo:
162 sprintf(sz_message, "DEBUGINFO ; ");
163 break;
164 case kTraceModuleCall:
165 sprintf(sz_message, "MODULECALL; ");
166 break;
167 case kTraceMemory:
168 sprintf(sz_message, "MEMORY ; ");
169 break;
170 case kTraceTimer:
171 sprintf(sz_message, "TIMER ; ");
172 break;
173 case kTraceStream:
174 sprintf(sz_message, "STREAM ; ");
175 break;
176 case kTraceApiCall:
177 sprintf(sz_message, "APICALL ; ");
178 break;
179 case kTraceDebug:
180 sprintf(sz_message, "DEBUG ; ");
181 break;
182 default:
183 assert(false);
184 return 0;
185 }
186 // All messages are 12 characters.
187 return kMessageLength;
niklase@google.com470e71d2011-07-07 08:21:25 +0000188}
189
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000190WebRtc_Word32 TraceImpl::AddModuleAndId(char* trace_message,
niklase@google.com470e71d2011-07-07 08:21:25 +0000191 const TraceModule module,
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000192 const WebRtc_Word32 id) const {
193 // Use long int to prevent problems with different definitions of
194 // WebRtc_Word32.
195 // TODO(hellner): is this actually a problem? If so, it should be better to
196 // clean up WebRtc_Word32
197 const long int idl = id;
198 const int kMessageLength = 25;
199 if (idl != -1) {
200 const unsigned long int id_engine = id >> 16;
201 const unsigned long int id_channel = id & 0xffff;
niklase@google.com470e71d2011-07-07 08:21:25 +0000202
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000203 switch (module) {
204 case kTraceUndefined:
205 // Add the appropriate amount of whitespace.
206 memset(trace_message, ' ', kMessageLength);
207 trace_message[kMessageLength] = '\0';
208 break;
209 case kTraceVoice:
210 sprintf(trace_message, " VOICE:%5ld %5ld;", id_engine,
211 id_channel);
212 break;
213 case kTraceVideo:
214 sprintf(trace_message, " VIDEO:%5ld %5ld;", id_engine,
215 id_channel);
216 break;
217 case kTraceUtility:
218 sprintf(trace_message, " UTILITY:%5ld %5ld;", id_engine,
219 id_channel);
220 break;
221 case kTraceRtpRtcp:
222 sprintf(trace_message, " RTP/RTCP:%5ld %5ld;", id_engine,
223 id_channel);
224 break;
225 case kTraceTransport:
226 sprintf(trace_message, " TRANSPORT:%5ld %5ld;", id_engine,
227 id_channel);
228 break;
229 case kTraceAudioCoding:
230 sprintf(trace_message, "AUDIO CODING:%5ld %5ld;", id_engine,
231 id_channel);
232 break;
233 case kTraceSrtp:
234 sprintf(trace_message, " SRTP:%5ld %5ld;", id_engine,
235 id_channel);
236 break;
237 case kTraceAudioMixerServer:
238 sprintf(trace_message, " AUDIO MIX/S:%5ld %5ld;", id_engine,
239 id_channel);
240 break;
241 case kTraceAudioMixerClient:
242 sprintf(trace_message, " AUDIO MIX/C:%5ld %5ld;", id_engine,
243 id_channel);
244 break;
245 case kTraceVideoCoding:
246 sprintf(trace_message, "VIDEO CODING:%5ld %5ld;", id_engine,
247 id_channel);
248 break;
249 case kTraceVideoMixer:
250 // Print sleep time and API call
251 sprintf(trace_message, " VIDEO MIX:%5ld %5ld;", id_engine,
252 id_channel);
253 break;
254 case kTraceFile:
255 sprintf(trace_message, " FILE:%5ld %5ld;", id_engine,
256 id_channel);
257 break;
258 case kTraceAudioProcessing:
259 sprintf(trace_message, " AUDIO PROC:%5ld %5ld;", id_engine,
260 id_channel);
261 break;
262 case kTraceAudioDevice:
263 sprintf(trace_message, "AUDIO DEVICE:%5ld %5ld;", id_engine,
264 id_channel);
265 break;
266 case kTraceVideoRenderer:
267 sprintf(trace_message, "VIDEO RENDER:%5ld %5ld;", id_engine,
268 id_channel);
269 break;
270 case kTraceVideoCapture:
271 sprintf(trace_message, "VIDEO CAPTUR:%5ld %5ld;", id_engine,
272 id_channel);
273 break;
274 case kTraceVideoPreocessing:
275 sprintf(trace_message, " VIDEO PROC:%5ld %5ld;", id_engine,
276 id_channel);
277 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000278 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000279 } else {
280 switch (module) {
281 case kTraceUndefined:
282 // Add the appropriate amount of whitespace.
283 memset(trace_message, ' ', kMessageLength);
284 trace_message[kMessageLength] = '\0';
285 break;
286 case kTraceVoice:
287 sprintf(trace_message, " VOICE:%11ld;", idl);
288 break;
289 case kTraceVideo:
290 sprintf(trace_message, " VIDEO:%11ld;", idl);
291 break;
292 case kTraceUtility:
293 sprintf(trace_message, " UTILITY:%11ld;", idl);
294 break;
295 case kTraceRtpRtcp:
296 sprintf(trace_message, " RTP/RTCP:%11ld;", idl);
297 break;
298 case kTraceTransport:
299 sprintf(trace_message, " TRANSPORT:%11ld;", idl);
300 break;
301 case kTraceAudioCoding:
302 sprintf(trace_message, "AUDIO CODING:%11ld;", idl);
303 break;
304 case kTraceSrtp:
305 sprintf(trace_message, " SRTP:%11ld;", idl);
306 break;
307 case kTraceAudioMixerServer:
308 sprintf(trace_message, " AUDIO MIX/S:%11ld;", idl);
309 break;
310 case kTraceAudioMixerClient:
311 sprintf(trace_message, " AUDIO MIX/C:%11ld;", idl);
312 break;
313 case kTraceVideoCoding:
314 sprintf(trace_message, "VIDEO CODING:%11ld;", idl);
315 break;
316 case kTraceVideoMixer:
317 sprintf(trace_message, " VIDEO MIX:%11ld;", idl);
318 break;
319 case kTraceFile:
320 sprintf(trace_message, " FILE:%11ld;", idl);
321 break;
322 case kTraceAudioProcessing:
323 sprintf(trace_message, " AUDIO PROC:%11ld;", idl);
324 break;
325 case kTraceAudioDevice:
326 sprintf(trace_message, "AUDIO DEVICE:%11ld;", idl);
327 break;
328 case kTraceVideoRenderer:
329 sprintf(trace_message, "VIDEO RENDER:%11ld;", idl);
330 break;
331 case kTraceVideoCapture:
332 sprintf(trace_message, "VIDEO CAPTUR:%11ld;", idl);
333 break;
334 case kTraceVideoPreocessing:
335 sprintf(trace_message, " VIDEO PROC:%11ld;", idl);
336 break;
337 }
338 }
339 return kMessageLength;
niklase@google.com470e71d2011-07-07 08:21:25 +0000340}
341
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000342WebRtc_Word32 TraceImpl::SetTraceFileImpl(const char* file_name_utf8,
343 const bool add_file_counter) {
344 CriticalSectionScoped lock(critsect_interface_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000345
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000346 trace_file_.Flush();
347 trace_file_.CloseFile();
niklase@google.com470e71d2011-07-07 08:21:25 +0000348
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000349 if (file_name_utf8) {
350 if (add_file_counter) {
351 file_count_text_ = 1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000352
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000353 char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize];
354 CreateFileName(file_name_utf8, file_name_with_counter_utf8,
355 file_count_text_);
356 if (trace_file_.OpenFile(file_name_with_counter_utf8, false, false,
357 true) == -1) {
358 return -1;
359 }
360 } else {
361 file_count_text_ = 0;
362 if (trace_file_.OpenFile(file_name_utf8, false, false, true) == -1) {
363 return -1;
364 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000365 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000366 }
367 row_count_text_ = 0;
368 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000369}
370
371WebRtc_Word32 TraceImpl::TraceFileImpl(
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000372 char file_name_utf8[FileWrapper::kMaxFileNameSize]) {
373 CriticalSectionScoped lock(critsect_interface_);
374 return trace_file_.FileName(file_name_utf8, FileWrapper::kMaxFileNameSize);
niklase@google.com470e71d2011-07-07 08:21:25 +0000375}
376
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000377WebRtc_Word32 TraceImpl::SetTraceCallbackImpl(TraceCallback* callback) {
378 CriticalSectionScoped lock(critsect_interface_);
379 callback_ = callback;
380 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000381}
382
383WebRtc_Word32 TraceImpl::AddMessage(
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000384 char* trace_message,
niklase@google.com470e71d2011-07-07 08:21:25 +0000385 const char msg[WEBRTC_TRACE_MAX_MESSAGE_SIZE],
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000386 const WebRtc_UWord16 written_so_far) const {
387 int length = 0;
388 if (written_so_far >= WEBRTC_TRACE_MAX_MESSAGE_SIZE) {
389 return -1;
390 }
391 // - 2 to leave room for newline and NULL termination.
niklase@google.com470e71d2011-07-07 08:21:25 +0000392#ifdef _WIN32
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000393 length = _snprintf(trace_message,
394 WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2,
395 "%s", msg);
396 if (length < 0) {
397 length = WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2;
398 trace_message[length] = 0;
399 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000400#else
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000401 length = snprintf(trace_message,
402 WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2,
403 "%s", msg);
404 if (length < 0 ||
405 length > WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2) {
406 length = WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2;
407 trace_message[length] = 0;
408 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000409#endif
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000410 // Length with NULL termination.
411 return length + 1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000412}
413
414void TraceImpl::AddMessageToList(
andrew@webrtc.orgc1ffd332013-03-22 17:13:23 +0000415 const char trace_message[WEBRTC_TRACE_MAX_MESSAGE_SIZE],
416 const WebRtc_UWord16 length,
417 const TraceLevel level) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000418 CriticalSectionScoped lock(critsect_array_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000419
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000420 if (next_free_idx_[active_queue_] >= WEBRTC_TRACE_MAX_QUEUE) {
421 if (!trace_file_.Open() && !callback_) {
422 // Keep at least the last 1/4 of old messages when not logging.
423 // TODO(hellner): isn't this redundant. The user will make it known
424 // when to start logging. Why keep messages before
425 // that?
426 for (int n = 0; n < WEBRTC_TRACE_MAX_QUEUE / 4; ++n) {
427 const int last_quarter_offset = (3 * WEBRTC_TRACE_MAX_QUEUE / 4);
428 memcpy(message_queue_[active_queue_][n],
429 message_queue_[active_queue_][n + last_quarter_offset],
430 WEBRTC_TRACE_MAX_MESSAGE_SIZE);
431 }
432 next_free_idx_[active_queue_] = WEBRTC_TRACE_MAX_QUEUE / 4;
niklase@google.com470e71d2011-07-07 08:21:25 +0000433 } else {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000434 // More messages are being written than there is room for in the
435 // buffer. Drop any new messages.
436 // TODO(hellner): its probably better to drop old messages instead
437 // of new ones. One step further: if this happens
438 // it's due to writing faster than what can be
439 // processed. Maybe modify the filter at this point.
440 // E.g. turn of STREAM.
441 return;
niklase@google.com470e71d2011-07-07 08:21:25 +0000442 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000443 }
444
445 WebRtc_UWord16 idx = next_free_idx_[active_queue_];
446 next_free_idx_[active_queue_]++;
447
448 level_[active_queue_][idx] = level;
449 length_[active_queue_][idx] = length;
450 memcpy(message_queue_[active_queue_][idx], trace_message, length);
451
452 if (next_free_idx_[active_queue_] == WEBRTC_TRACE_MAX_QUEUE - 1) {
453 // Logging more messages than can be worked off. Log a warning.
454 const char warning_msg[] = "WARNING MISSING TRACE MESSAGES\n";
455 level_[active_queue_][next_free_idx_[active_queue_]] = kTraceWarning;
456 length_[active_queue_][next_free_idx_[active_queue_]] = strlen(warning_msg);
457 memcpy(message_queue_[active_queue_][next_free_idx_[active_queue_]],
458 warning_msg, strlen(warning_msg));
459 next_free_idx_[active_queue_]++;
460 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000461}
462
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000463bool TraceImpl::Run(void* obj) {
464 return static_cast<TraceImpl*>(obj)->Process();
465}
niklase@google.com470e71d2011-07-07 08:21:25 +0000466
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000467bool TraceImpl::Process() {
468 if (event_.Wait(1000) == kEventSignaled) {
andrew@webrtc.orgc1ffd332013-03-22 17:13:23 +0000469 // This slightly odd construction is to avoid locking |critsect_interface_|
470 // while calling WriteToFile() since it's locked inside the function.
471 critsect_interface_->Enter();
472 bool write_to_file = trace_file_.Open() || callback_;
473 critsect_interface_->Leave();
474 if (write_to_file) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000475 WriteToFile();
niklase@google.com470e71d2011-07-07 08:21:25 +0000476 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000477 } else {
andrew@webrtc.orgc1ffd332013-03-22 17:13:23 +0000478 CriticalSectionScoped lock(critsect_interface_);
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000479 trace_file_.Flush();
480 }
481 return true;
482}
483
484void TraceImpl::WriteToFile() {
485 WebRtc_UWord8 local_queue_active = 0;
486 WebRtc_UWord16 local_next_free_idx = 0;
487
488 // There are two buffers. One for reading (for writing to file) and one for
489 // writing (for storing new messages). Let new messages be posted to the
490 // unused buffer so that the current buffer can be flushed safely.
491 {
492 CriticalSectionScoped lock(critsect_array_);
493 local_next_free_idx = next_free_idx_[active_queue_];
494 next_free_idx_[active_queue_] = 0;
495 local_queue_active = active_queue_;
496 if (active_queue_ == 0) {
497 active_queue_ = 1;
498 } else {
499 active_queue_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000500 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000501 }
502 if (local_next_free_idx == 0) {
503 return;
504 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000505
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000506 CriticalSectionScoped lock(critsect_interface_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000507
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000508 for (WebRtc_UWord16 idx = 0; idx < local_next_free_idx; ++idx) {
509 TraceLevel local_level = level_[local_queue_active][idx];
510 if (callback_) {
511 callback_->Print(local_level, message_queue_[local_queue_active][idx],
512 length_[local_queue_active][idx]);
niklase@google.com470e71d2011-07-07 08:21:25 +0000513 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000514 if (trace_file_.Open()) {
515 if (row_count_text_ > WEBRTC_TRACE_MAX_FILE_SIZE) {
516 // wrap file
517 row_count_text_ = 0;
518 trace_file_.Flush();
519
520 if (file_count_text_ == 0) {
521 trace_file_.Rewind();
522 } else {
523 char old_file_name[FileWrapper::kMaxFileNameSize];
524 char new_file_name[FileWrapper::kMaxFileNameSize];
525
526 // get current name
527 trace_file_.FileName(old_file_name,
528 FileWrapper::kMaxFileNameSize);
529 trace_file_.CloseFile();
530
531 file_count_text_++;
532
533 UpdateFileName(old_file_name, new_file_name, file_count_text_);
534
535 if (trace_file_.OpenFile(new_file_name, false, false,
536 true) == -1) {
537 return;
538 }
539 }
540 }
541 if (row_count_text_ == 0) {
542 char message[WEBRTC_TRACE_MAX_MESSAGE_SIZE + 1];
543 WebRtc_Word32 length = AddDateTimeInfo(message);
544 if (length != -1) {
545 message[length] = 0;
546 message[length - 1] = '\n';
547 trace_file_.Write(message, length);
548 row_count_text_++;
549 }
550 length = AddBuildInfo(message);
551 if (length != -1) {
552 message[length + 1] = 0;
553 message[length] = '\n';
554 message[length - 1] = '\n';
555 trace_file_.Write(message, length + 1);
556 row_count_text_++;
557 row_count_text_++;
558 }
559 }
560 WebRtc_UWord16 length = length_[local_queue_active][idx];
561 message_queue_[local_queue_active][idx][length] = 0;
562 message_queue_[local_queue_active][idx][length - 1] = '\n';
563 trace_file_.Write(message_queue_[local_queue_active][idx], length);
564 row_count_text_++;
565 }
566 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000567}
568
569void TraceImpl::AddImpl(const TraceLevel level, const TraceModule module,
570 const WebRtc_Word32 id,
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000571 const char msg[WEBRTC_TRACE_MAX_MESSAGE_SIZE]) {
572 if (TraceCheck(level)) {
573 char trace_message[WEBRTC_TRACE_MAX_MESSAGE_SIZE];
574 char* message_ptr = trace_message;
niklase@google.com470e71d2011-07-07 08:21:25 +0000575
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000576 WebRtc_Word32 len = 0;
577 WebRtc_Word32 ack_len = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000578
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000579 len = AddLevel(message_ptr, level);
580 if (len == -1) {
581 return;
niklase@google.com470e71d2011-07-07 08:21:25 +0000582 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000583 message_ptr += len;
584 ack_len += len;
585
586 len = AddTime(message_ptr, level);
587 if (len == -1) {
588 return;
589 }
590 message_ptr += len;
591 ack_len += len;
592
593 len = AddModuleAndId(message_ptr, module, id);
594 if (len == -1) {
595 return;
596 }
597 message_ptr += len;
598 ack_len += len;
599
600 len = AddThreadId(message_ptr);
601 if (len < 0) {
602 return;
603 }
604 message_ptr += len;
605 ack_len += len;
606
607 len = AddMessage(message_ptr, msg, (WebRtc_UWord16)ack_len);
608 if (len == -1) {
609 return;
610 }
611 ack_len += len;
612 AddMessageToList(trace_message, (WebRtc_UWord16)ack_len, level);
613
614 // Make sure that messages are written as soon as possible.
615 event_.Set();
616 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000617}
618
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000619bool TraceImpl::TraceCheck(const TraceLevel level) const {
620 return (level & level_filter) ? true : false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000621}
622
623bool TraceImpl::UpdateFileName(
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000624 const char file_name_utf8[FileWrapper::kMaxFileNameSize],
625 char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize],
626 const WebRtc_UWord32 new_count) const {
627 WebRtc_Word32 length = (WebRtc_Word32)strlen(file_name_utf8);
628 if (length < 0) {
629 return false;
630 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000631
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000632 WebRtc_Word32 length_without_file_ending = length - 1;
633 while (length_without_file_ending > 0) {
634 if (file_name_utf8[length_without_file_ending] == '.') {
635 break;
636 } else {
637 length_without_file_ending--;
niklase@google.com470e71d2011-07-07 08:21:25 +0000638 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000639 }
640 if (length_without_file_ending == 0) {
641 length_without_file_ending = length;
642 }
643 WebRtc_Word32 length_to_ = length_without_file_ending - 1;
644 while (length_to_ > 0) {
645 if (file_name_utf8[length_to_] == '_') {
646 break;
647 } else {
648 length_to_--;
niklase@google.com470e71d2011-07-07 08:21:25 +0000649 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000650 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000651
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000652 memcpy(file_name_with_counter_utf8, file_name_utf8, length_to_);
653 sprintf(file_name_with_counter_utf8 + length_to_, "_%lu%s",
654 static_cast<long unsigned int>(new_count),
655 file_name_utf8 + length_without_file_ending);
656 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000657}
658
659bool TraceImpl::CreateFileName(
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000660 const char file_name_utf8[FileWrapper::kMaxFileNameSize],
661 char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize],
662 const WebRtc_UWord32 new_count) const {
663 WebRtc_Word32 length = (WebRtc_Word32)strlen(file_name_utf8);
664 if (length < 0) {
665 return false;
666 }
667
668 WebRtc_Word32 length_without_file_ending = length - 1;
669 while (length_without_file_ending > 0) {
670 if (file_name_utf8[length_without_file_ending] == '.') {
671 break;
672 } else {
673 length_without_file_ending--;
niklase@google.com470e71d2011-07-07 08:21:25 +0000674 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000675 }
676 if (length_without_file_ending == 0) {
677 length_without_file_ending = length;
678 }
679 memcpy(file_name_with_counter_utf8, file_name_utf8,
680 length_without_file_ending);
681 sprintf(file_name_with_counter_utf8 + length_without_file_ending, "_%lu%s",
682 static_cast<long unsigned int>(new_count),
683 file_name_utf8 + length_without_file_ending);
684 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000685}
686
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000687void Trace::CreateTrace() {
688 TraceImpl::StaticInstance(kAddRef);
tommi@webrtc.orgcde1e7f2011-11-15 12:23:36 +0000689}
690
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000691void Trace::ReturnTrace() {
692 TraceImpl::StaticInstance(kRelease);
tommi@webrtc.orgcde1e7f2011-11-15 12:23:36 +0000693}
694
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000695WebRtc_Word32 Trace::SetLevelFilter(WebRtc_UWord32 filter) {
696 level_filter = filter;
697 return 0;
tommi@webrtc.orgcde1e7f2011-11-15 12:23:36 +0000698}
niklase@google.com470e71d2011-07-07 08:21:25 +0000699
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000700WebRtc_Word32 Trace::LevelFilter(WebRtc_UWord32& filter) {
701 filter = level_filter;
702 return 0;
tommi@webrtc.orgcde1e7f2011-11-15 12:23:36 +0000703}
niklase@google.com470e71d2011-07-07 08:21:25 +0000704
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000705WebRtc_Word32 Trace::TraceFile(char file_name[FileWrapper::kMaxFileNameSize]) {
706 TraceImpl* trace = TraceImpl::GetTrace();
707 if (trace) {
708 int ret_val = trace->TraceFileImpl(file_name);
709 ReturnTrace();
710 return ret_val;
711 }
712 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000713}
714
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000715WebRtc_Word32 Trace::SetTraceFile(const char* file_name,
716 const bool add_file_counter) {
717 TraceImpl* trace = TraceImpl::GetTrace();
718 if (trace) {
719 int ret_val = trace->SetTraceFileImpl(file_name, add_file_counter);
720 ReturnTrace();
721 return ret_val;
722 }
723 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000724}
725
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000726WebRtc_Word32 Trace::SetTraceCallback(TraceCallback* callback) {
727 TraceImpl* trace = TraceImpl::GetTrace();
728 if (trace) {
729 int ret_val = trace->SetTraceCallbackImpl(callback);
730 ReturnTrace();
731 return ret_val;
732 }
733 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000734}
735
736void Trace::Add(const TraceLevel level, const TraceModule module,
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000737 const WebRtc_Word32 id, const char* msg, ...) {
738 TraceImpl* trace = TraceImpl::GetTrace(level);
739 if (trace) {
740 if (trace->TraceCheck(level)) {
741 char temp_buff[WEBRTC_TRACE_MAX_MESSAGE_SIZE];
742 char* buff = 0;
743 if (msg) {
744 va_list args;
745 va_start(args, msg);
niklase@google.com470e71d2011-07-07 08:21:25 +0000746#ifdef _WIN32
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000747 _vsnprintf(temp_buff, WEBRTC_TRACE_MAX_MESSAGE_SIZE - 1, msg, args);
niklase@google.com470e71d2011-07-07 08:21:25 +0000748#else
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000749 vsnprintf(temp_buff, WEBRTC_TRACE_MAX_MESSAGE_SIZE - 1, msg, args);
niklase@google.com470e71d2011-07-07 08:21:25 +0000750#endif
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000751 va_end(args);
752 buff = temp_buff;
753 }
754 trace->AddImpl(level, module, id, buff);
niklase@google.com470e71d2011-07-07 08:21:25 +0000755 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000756 ReturnTrace();
757 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000758}
tommi@webrtc.orgcde1e7f2011-11-15 12:23:36 +0000759
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000760} // namespace webrtc