blob: 2f65d06b60c85d5426722b35bbe684e5bc5c0fb8 [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
34static WebRtc_UWord32 level_filter = kTraceDefault;
niklase@google.com470e71d2011-07-07 08:21:25 +000035
36// Construct On First Use idiom. Avoids "static initialization order fiasco".
henrike@webrtc.org315282c2011-12-09 17:46:20 +000037TraceImpl* TraceImpl::StaticInstance(CountOperation count_operation,
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000038 const TraceLevel level) {
39 // Sanities to avoid taking lock unless absolutely necessary (for
40 // performance reasons). count_operation == kAddRefNoCreate implies that a
41 // message will be written to file.
42 if ((level != kTraceAll) && (count_operation == kAddRefNoCreate)) {
43 if (!(level & level_filter)) {
44 return NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +000045 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000046 }
47 TraceImpl* impl =
48 GetStaticInstance<TraceImpl>(count_operation);
49 return impl;
niklase@google.com470e71d2011-07-07 08:21:25 +000050}
51
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000052TraceImpl* TraceImpl::GetTrace(const TraceLevel level) {
53 return StaticInstance(kAddRefNoCreate, level);
niklase@google.com470e71d2011-07-07 08:21:25 +000054}
55
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000056TraceImpl* TraceImpl::CreateInstance() {
henrike@webrtc.org2f47b5a2011-12-10 00:44:47 +000057#if defined(_WIN32)
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000058 return new TraceWindows();
niklase@google.com470e71d2011-07-07 08:21:25 +000059#else
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000060 return new TracePosix();
niklase@google.com470e71d2011-07-07 08:21:25 +000061#endif
62}
63
64TraceImpl::TraceImpl()
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000065 : critsect_interface_(CriticalSectionWrapper::CreateCriticalSection()),
66 callback_(NULL),
67 row_count_text_(0),
68 file_count_text_(0),
69 trace_file_(*FileWrapper::Create()),
70 thread_(*ThreadWrapper::CreateThread(TraceImpl::Run, this,
niklase@google.com470e71d2011-07-07 08:21:25 +000071 kHighestPriority, "Trace")),
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000072 event_(*EventWrapper::Create()),
73 critsect_array_(CriticalSectionWrapper::CreateCriticalSection()),
74 next_free_idx_(),
75 level_(),
76 length_(),
77 message_queue_(),
78 active_queue_(0) {
79 next_free_idx_[0] = 0;
80 next_free_idx_[1] = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000081
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000082 unsigned int tid = 0;
83 thread_.Start(tid);
niklase@google.com470e71d2011-07-07 08:21:25 +000084
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000085 for (int m = 0; m < WEBRTC_TRACE_NUM_ARRAY; ++m) {
86 for (int n = 0; n < WEBRTC_TRACE_MAX_QUEUE; ++n) {
87 message_queue_[m][n] = new
88 char[WEBRTC_TRACE_MAX_MESSAGE_SIZE];
niklase@google.com470e71d2011-07-07 08:21:25 +000089 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000090 }
niklase@google.com470e71d2011-07-07 08:21:25 +000091}
92
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000093bool TraceImpl::StopThread() {
94 // Release the worker thread so that it can flush any lingering messages.
95 event_.Set();
niklase@google.com470e71d2011-07-07 08:21:25 +000096
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000097 // Allow 10 ms for pending messages to be flushed out.
98 // TODO(hellner): why not use condition variables to do this? Or let the
99 // worker thread die and let this thread flush remaining
100 // messages?
101 SleepMs(10);
niklase@google.com470e71d2011-07-07 08:21:25 +0000102
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000103 thread_.SetNotAlive();
104 // Make sure the thread finishes as quickly as possible (instead of having
105 // to wait for the timeout).
106 event_.Set();
107 bool stopped = thread_.Stop();
niklase@google.com470e71d2011-07-07 08:21:25 +0000108
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000109 CriticalSectionScoped lock(critsect_interface_);
110 trace_file_.Flush();
111 trace_file_.CloseFile();
112 return stopped;
niklase@google.com470e71d2011-07-07 08:21:25 +0000113}
114
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000115TraceImpl::~TraceImpl() {
116 StopThread();
117 delete &event_;
118 delete &trace_file_;
119 delete &thread_;
120 delete critsect_interface_;
121 delete critsect_array_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000122
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000123 for (int m = 0; m < WEBRTC_TRACE_NUM_ARRAY; ++m) {
124 for (int n = 0; n < WEBRTC_TRACE_MAX_QUEUE; ++n) {
125 delete [] message_queue_[m][n];
niklase@google.com470e71d2011-07-07 08:21:25 +0000126 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000127 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000128}
129
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000130WebRtc_Word32 TraceImpl::AddThreadId(char* trace_message) const {
131 WebRtc_UWord32 thread_id = ThreadWrapper::GetThreadId();
pwestin@webrtc.orgb54d7272012-01-11 08:28:04 +0000132 // Messages is 12 characters.
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000133 return sprintf(trace_message, "%10u; ", thread_id);
pwestin@webrtc.orgb54d7272012-01-11 08:28:04 +0000134}
135
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000136WebRtc_Word32 TraceImpl::AddLevel(char* sz_message,
137 const TraceLevel level) const {
138 const int kMessageLength = 12;
139 switch (level) {
140 case kTraceTerseInfo:
141 // Add the appropriate amount of whitespace.
142 memset(sz_message, ' ', kMessageLength);
143 sz_message[kMessageLength] = '\0';
144 break;
145 case kTraceStateInfo:
146 sprintf(sz_message, "STATEINFO ; ");
147 break;
148 case kTraceWarning:
149 sprintf(sz_message, "WARNING ; ");
150 break;
151 case kTraceError:
152 sprintf(sz_message, "ERROR ; ");
153 break;
154 case kTraceCritical:
155 sprintf(sz_message, "CRITICAL ; ");
156 break;
157 case kTraceInfo:
158 sprintf(sz_message, "DEBUGINFO ; ");
159 break;
160 case kTraceModuleCall:
161 sprintf(sz_message, "MODULECALL; ");
162 break;
163 case kTraceMemory:
164 sprintf(sz_message, "MEMORY ; ");
165 break;
166 case kTraceTimer:
167 sprintf(sz_message, "TIMER ; ");
168 break;
169 case kTraceStream:
170 sprintf(sz_message, "STREAM ; ");
171 break;
172 case kTraceApiCall:
173 sprintf(sz_message, "APICALL ; ");
174 break;
175 case kTraceDebug:
176 sprintf(sz_message, "DEBUG ; ");
177 break;
178 default:
179 assert(false);
180 return 0;
181 }
182 // All messages are 12 characters.
183 return kMessageLength;
niklase@google.com470e71d2011-07-07 08:21:25 +0000184}
185
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000186WebRtc_Word32 TraceImpl::AddModuleAndId(char* trace_message,
niklase@google.com470e71d2011-07-07 08:21:25 +0000187 const TraceModule module,
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000188 const WebRtc_Word32 id) const {
189 // Use long int to prevent problems with different definitions of
190 // WebRtc_Word32.
191 // TODO(hellner): is this actually a problem? If so, it should be better to
192 // clean up WebRtc_Word32
193 const long int idl = id;
194 const int kMessageLength = 25;
195 if (idl != -1) {
196 const unsigned long int id_engine = id >> 16;
197 const unsigned long int id_channel = id & 0xffff;
niklase@google.com470e71d2011-07-07 08:21:25 +0000198
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000199 switch (module) {
200 case kTraceUndefined:
201 // Add the appropriate amount of whitespace.
202 memset(trace_message, ' ', kMessageLength);
203 trace_message[kMessageLength] = '\0';
204 break;
205 case kTraceVoice:
206 sprintf(trace_message, " VOICE:%5ld %5ld;", id_engine,
207 id_channel);
208 break;
209 case kTraceVideo:
210 sprintf(trace_message, " VIDEO:%5ld %5ld;", id_engine,
211 id_channel);
212 break;
213 case kTraceUtility:
214 sprintf(trace_message, " UTILITY:%5ld %5ld;", id_engine,
215 id_channel);
216 break;
217 case kTraceRtpRtcp:
218 sprintf(trace_message, " RTP/RTCP:%5ld %5ld;", id_engine,
219 id_channel);
220 break;
221 case kTraceTransport:
222 sprintf(trace_message, " TRANSPORT:%5ld %5ld;", id_engine,
223 id_channel);
224 break;
225 case kTraceAudioCoding:
226 sprintf(trace_message, "AUDIO CODING:%5ld %5ld;", id_engine,
227 id_channel);
228 break;
229 case kTraceSrtp:
230 sprintf(trace_message, " SRTP:%5ld %5ld;", id_engine,
231 id_channel);
232 break;
233 case kTraceAudioMixerServer:
234 sprintf(trace_message, " AUDIO MIX/S:%5ld %5ld;", id_engine,
235 id_channel);
236 break;
237 case kTraceAudioMixerClient:
238 sprintf(trace_message, " AUDIO MIX/C:%5ld %5ld;", id_engine,
239 id_channel);
240 break;
241 case kTraceVideoCoding:
242 sprintf(trace_message, "VIDEO CODING:%5ld %5ld;", id_engine,
243 id_channel);
244 break;
245 case kTraceVideoMixer:
246 // Print sleep time and API call
247 sprintf(trace_message, " VIDEO MIX:%5ld %5ld;", id_engine,
248 id_channel);
249 break;
250 case kTraceFile:
251 sprintf(trace_message, " FILE:%5ld %5ld;", id_engine,
252 id_channel);
253 break;
254 case kTraceAudioProcessing:
255 sprintf(trace_message, " AUDIO PROC:%5ld %5ld;", id_engine,
256 id_channel);
257 break;
258 case kTraceAudioDevice:
259 sprintf(trace_message, "AUDIO DEVICE:%5ld %5ld;", id_engine,
260 id_channel);
261 break;
262 case kTraceVideoRenderer:
263 sprintf(trace_message, "VIDEO RENDER:%5ld %5ld;", id_engine,
264 id_channel);
265 break;
266 case kTraceVideoCapture:
267 sprintf(trace_message, "VIDEO CAPTUR:%5ld %5ld;", id_engine,
268 id_channel);
269 break;
270 case kTraceVideoPreocessing:
271 sprintf(trace_message, " VIDEO PROC:%5ld %5ld;", id_engine,
272 id_channel);
273 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000274 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000275 } else {
276 switch (module) {
277 case kTraceUndefined:
278 // Add the appropriate amount of whitespace.
279 memset(trace_message, ' ', kMessageLength);
280 trace_message[kMessageLength] = '\0';
281 break;
282 case kTraceVoice:
283 sprintf(trace_message, " VOICE:%11ld;", idl);
284 break;
285 case kTraceVideo:
286 sprintf(trace_message, " VIDEO:%11ld;", idl);
287 break;
288 case kTraceUtility:
289 sprintf(trace_message, " UTILITY:%11ld;", idl);
290 break;
291 case kTraceRtpRtcp:
292 sprintf(trace_message, " RTP/RTCP:%11ld;", idl);
293 break;
294 case kTraceTransport:
295 sprintf(trace_message, " TRANSPORT:%11ld;", idl);
296 break;
297 case kTraceAudioCoding:
298 sprintf(trace_message, "AUDIO CODING:%11ld;", idl);
299 break;
300 case kTraceSrtp:
301 sprintf(trace_message, " SRTP:%11ld;", idl);
302 break;
303 case kTraceAudioMixerServer:
304 sprintf(trace_message, " AUDIO MIX/S:%11ld;", idl);
305 break;
306 case kTraceAudioMixerClient:
307 sprintf(trace_message, " AUDIO MIX/C:%11ld;", idl);
308 break;
309 case kTraceVideoCoding:
310 sprintf(trace_message, "VIDEO CODING:%11ld;", idl);
311 break;
312 case kTraceVideoMixer:
313 sprintf(trace_message, " VIDEO MIX:%11ld;", idl);
314 break;
315 case kTraceFile:
316 sprintf(trace_message, " FILE:%11ld;", idl);
317 break;
318 case kTraceAudioProcessing:
319 sprintf(trace_message, " AUDIO PROC:%11ld;", idl);
320 break;
321 case kTraceAudioDevice:
322 sprintf(trace_message, "AUDIO DEVICE:%11ld;", idl);
323 break;
324 case kTraceVideoRenderer:
325 sprintf(trace_message, "VIDEO RENDER:%11ld;", idl);
326 break;
327 case kTraceVideoCapture:
328 sprintf(trace_message, "VIDEO CAPTUR:%11ld;", idl);
329 break;
330 case kTraceVideoPreocessing:
331 sprintf(trace_message, " VIDEO PROC:%11ld;", idl);
332 break;
333 }
334 }
335 return kMessageLength;
niklase@google.com470e71d2011-07-07 08:21:25 +0000336}
337
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000338WebRtc_Word32 TraceImpl::SetTraceFileImpl(const char* file_name_utf8,
339 const bool add_file_counter) {
340 CriticalSectionScoped lock(critsect_interface_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000341
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000342 trace_file_.Flush();
343 trace_file_.CloseFile();
niklase@google.com470e71d2011-07-07 08:21:25 +0000344
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000345 if (file_name_utf8) {
346 if (add_file_counter) {
347 file_count_text_ = 1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000348
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000349 char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize];
350 CreateFileName(file_name_utf8, file_name_with_counter_utf8,
351 file_count_text_);
352 if (trace_file_.OpenFile(file_name_with_counter_utf8, false, false,
353 true) == -1) {
354 return -1;
355 }
356 } else {
357 file_count_text_ = 0;
358 if (trace_file_.OpenFile(file_name_utf8, false, false, true) == -1) {
359 return -1;
360 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000361 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000362 }
363 row_count_text_ = 0;
364 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000365}
366
367WebRtc_Word32 TraceImpl::TraceFileImpl(
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000368 char file_name_utf8[FileWrapper::kMaxFileNameSize]) {
369 CriticalSectionScoped lock(critsect_interface_);
370 return trace_file_.FileName(file_name_utf8, FileWrapper::kMaxFileNameSize);
niklase@google.com470e71d2011-07-07 08:21:25 +0000371}
372
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000373WebRtc_Word32 TraceImpl::SetTraceCallbackImpl(TraceCallback* callback) {
374 CriticalSectionScoped lock(critsect_interface_);
375 callback_ = callback;
376 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000377}
378
379WebRtc_Word32 TraceImpl::AddMessage(
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000380 char* trace_message,
niklase@google.com470e71d2011-07-07 08:21:25 +0000381 const char msg[WEBRTC_TRACE_MAX_MESSAGE_SIZE],
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000382 const WebRtc_UWord16 written_so_far) const {
383 int length = 0;
384 if (written_so_far >= WEBRTC_TRACE_MAX_MESSAGE_SIZE) {
385 return -1;
386 }
387 // - 2 to leave room for newline and NULL termination.
niklase@google.com470e71d2011-07-07 08:21:25 +0000388#ifdef _WIN32
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000389 length = _snprintf(trace_message,
390 WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2,
391 "%s", msg);
392 if (length < 0) {
393 length = WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2;
394 trace_message[length] = 0;
395 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000396#else
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000397 length = snprintf(trace_message,
398 WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2,
399 "%s", msg);
400 if (length < 0 ||
401 length > WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2) {
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#endif
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000406 // Length with NULL termination.
407 return length + 1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000408}
409
410void TraceImpl::AddMessageToList(
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000411 const char trace_message[WEBRTC_TRACE_MAX_MESSAGE_SIZE],
412 const WebRtc_UWord16 length,
413 const TraceLevel level) {
pwestin@webrtc.org27fe1b72012-04-04 08:08:30 +0000414#ifdef WEBRTC_DIRECT_TRACE
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000415 if (callback_) {
416 callback_->Print(level, trace_message, length);
417 }
418 return;
pwestin@webrtc.org27fe1b72012-04-04 08:08:30 +0000419#endif
420
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000421 CriticalSectionScoped lock(critsect_array_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000422
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000423 if (next_free_idx_[active_queue_] >= WEBRTC_TRACE_MAX_QUEUE) {
424 if (!trace_file_.Open() && !callback_) {
425 // Keep at least the last 1/4 of old messages when not logging.
426 // TODO(hellner): isn't this redundant. The user will make it known
427 // when to start logging. Why keep messages before
428 // that?
429 for (int n = 0; n < WEBRTC_TRACE_MAX_QUEUE / 4; ++n) {
430 const int last_quarter_offset = (3 * WEBRTC_TRACE_MAX_QUEUE / 4);
431 memcpy(message_queue_[active_queue_][n],
432 message_queue_[active_queue_][n + last_quarter_offset],
433 WEBRTC_TRACE_MAX_MESSAGE_SIZE);
434 }
435 next_free_idx_[active_queue_] = WEBRTC_TRACE_MAX_QUEUE / 4;
niklase@google.com470e71d2011-07-07 08:21:25 +0000436 } else {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000437 // More messages are being written than there is room for in the
438 // buffer. Drop any new messages.
439 // TODO(hellner): its probably better to drop old messages instead
440 // of new ones. One step further: if this happens
441 // it's due to writing faster than what can be
442 // processed. Maybe modify the filter at this point.
443 // E.g. turn of STREAM.
444 return;
niklase@google.com470e71d2011-07-07 08:21:25 +0000445 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000446 }
447
448 WebRtc_UWord16 idx = next_free_idx_[active_queue_];
449 next_free_idx_[active_queue_]++;
450
451 level_[active_queue_][idx] = level;
452 length_[active_queue_][idx] = length;
453 memcpy(message_queue_[active_queue_][idx], trace_message, length);
454
455 if (next_free_idx_[active_queue_] == WEBRTC_TRACE_MAX_QUEUE - 1) {
456 // Logging more messages than can be worked off. Log a warning.
457 const char warning_msg[] = "WARNING MISSING TRACE MESSAGES\n";
458 level_[active_queue_][next_free_idx_[active_queue_]] = kTraceWarning;
459 length_[active_queue_][next_free_idx_[active_queue_]] = strlen(warning_msg);
460 memcpy(message_queue_[active_queue_][next_free_idx_[active_queue_]],
461 warning_msg, strlen(warning_msg));
462 next_free_idx_[active_queue_]++;
463 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000464}
465
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000466bool TraceImpl::Run(void* obj) {
467 return static_cast<TraceImpl*>(obj)->Process();
468}
niklase@google.com470e71d2011-07-07 08:21:25 +0000469
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000470bool TraceImpl::Process() {
471 if (event_.Wait(1000) == kEventSignaled) {
472 if (trace_file_.Open() || callback_) {
473 // File mode (not callback mode).
474 WriteToFile();
niklase@google.com470e71d2011-07-07 08:21:25 +0000475 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000476 } else {
477 trace_file_.Flush();
478 }
479 return true;
480}
481
482void TraceImpl::WriteToFile() {
483 WebRtc_UWord8 local_queue_active = 0;
484 WebRtc_UWord16 local_next_free_idx = 0;
485
486 // There are two buffers. One for reading (for writing to file) and one for
487 // writing (for storing new messages). Let new messages be posted to the
488 // unused buffer so that the current buffer can be flushed safely.
489 {
490 CriticalSectionScoped lock(critsect_array_);
491 local_next_free_idx = next_free_idx_[active_queue_];
492 next_free_idx_[active_queue_] = 0;
493 local_queue_active = active_queue_;
494 if (active_queue_ == 0) {
495 active_queue_ = 1;
496 } else {
497 active_queue_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000498 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000499 }
500 if (local_next_free_idx == 0) {
501 return;
502 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000503
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000504 CriticalSectionScoped lock(critsect_interface_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000505
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000506 for (WebRtc_UWord16 idx = 0; idx < local_next_free_idx; ++idx) {
507 TraceLevel local_level = level_[local_queue_active][idx];
508 if (callback_) {
509 callback_->Print(local_level, message_queue_[local_queue_active][idx],
510 length_[local_queue_active][idx]);
niklase@google.com470e71d2011-07-07 08:21:25 +0000511 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000512 if (trace_file_.Open()) {
513 if (row_count_text_ > WEBRTC_TRACE_MAX_FILE_SIZE) {
514 // wrap file
515 row_count_text_ = 0;
516 trace_file_.Flush();
517
518 if (file_count_text_ == 0) {
519 trace_file_.Rewind();
520 } else {
521 char old_file_name[FileWrapper::kMaxFileNameSize];
522 char new_file_name[FileWrapper::kMaxFileNameSize];
523
524 // get current name
525 trace_file_.FileName(old_file_name,
526 FileWrapper::kMaxFileNameSize);
527 trace_file_.CloseFile();
528
529 file_count_text_++;
530
531 UpdateFileName(old_file_name, new_file_name, file_count_text_);
532
533 if (trace_file_.OpenFile(new_file_name, false, false,
534 true) == -1) {
535 return;
536 }
537 }
538 }
539 if (row_count_text_ == 0) {
540 char message[WEBRTC_TRACE_MAX_MESSAGE_SIZE + 1];
541 WebRtc_Word32 length = AddDateTimeInfo(message);
542 if (length != -1) {
543 message[length] = 0;
544 message[length - 1] = '\n';
545 trace_file_.Write(message, length);
546 row_count_text_++;
547 }
548 length = AddBuildInfo(message);
549 if (length != -1) {
550 message[length + 1] = 0;
551 message[length] = '\n';
552 message[length - 1] = '\n';
553 trace_file_.Write(message, length + 1);
554 row_count_text_++;
555 row_count_text_++;
556 }
557 }
558 WebRtc_UWord16 length = length_[local_queue_active][idx];
559 message_queue_[local_queue_active][idx][length] = 0;
560 message_queue_[local_queue_active][idx][length - 1] = '\n';
561 trace_file_.Write(message_queue_[local_queue_active][idx], length);
562 row_count_text_++;
563 }
564 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000565}
566
567void TraceImpl::AddImpl(const TraceLevel level, const TraceModule module,
568 const WebRtc_Word32 id,
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000569 const char msg[WEBRTC_TRACE_MAX_MESSAGE_SIZE]) {
570 if (TraceCheck(level)) {
571 char trace_message[WEBRTC_TRACE_MAX_MESSAGE_SIZE];
572 char* message_ptr = trace_message;
niklase@google.com470e71d2011-07-07 08:21:25 +0000573
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000574 WebRtc_Word32 len = 0;
575 WebRtc_Word32 ack_len = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000576
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000577 len = AddLevel(message_ptr, level);
578 if (len == -1) {
579 return;
niklase@google.com470e71d2011-07-07 08:21:25 +0000580 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000581 message_ptr += len;
582 ack_len += len;
583
584 len = AddTime(message_ptr, level);
585 if (len == -1) {
586 return;
587 }
588 message_ptr += len;
589 ack_len += len;
590
591 len = AddModuleAndId(message_ptr, module, id);
592 if (len == -1) {
593 return;
594 }
595 message_ptr += len;
596 ack_len += len;
597
598 len = AddThreadId(message_ptr);
599 if (len < 0) {
600 return;
601 }
602 message_ptr += len;
603 ack_len += len;
604
605 len = AddMessage(message_ptr, msg, (WebRtc_UWord16)ack_len);
606 if (len == -1) {
607 return;
608 }
609 ack_len += len;
610 AddMessageToList(trace_message, (WebRtc_UWord16)ack_len, level);
611
612 // Make sure that messages are written as soon as possible.
613 event_.Set();
614 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000615}
616
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000617bool TraceImpl::TraceCheck(const TraceLevel level) const {
618 return (level & level_filter) ? true : false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000619}
620
621bool TraceImpl::UpdateFileName(
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000622 const char file_name_utf8[FileWrapper::kMaxFileNameSize],
623 char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize],
624 const WebRtc_UWord32 new_count) const {
625 WebRtc_Word32 length = (WebRtc_Word32)strlen(file_name_utf8);
626 if (length < 0) {
627 return false;
628 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000629
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000630 WebRtc_Word32 length_without_file_ending = length - 1;
631 while (length_without_file_ending > 0) {
632 if (file_name_utf8[length_without_file_ending] == '.') {
633 break;
634 } else {
635 length_without_file_ending--;
niklase@google.com470e71d2011-07-07 08:21:25 +0000636 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000637 }
638 if (length_without_file_ending == 0) {
639 length_without_file_ending = length;
640 }
641 WebRtc_Word32 length_to_ = length_without_file_ending - 1;
642 while (length_to_ > 0) {
643 if (file_name_utf8[length_to_] == '_') {
644 break;
645 } else {
646 length_to_--;
niklase@google.com470e71d2011-07-07 08:21:25 +0000647 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000648 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000649
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000650 memcpy(file_name_with_counter_utf8, file_name_utf8, length_to_);
651 sprintf(file_name_with_counter_utf8 + length_to_, "_%lu%s",
652 static_cast<long unsigned int>(new_count),
653 file_name_utf8 + length_without_file_ending);
654 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000655}
656
657bool TraceImpl::CreateFileName(
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000658 const char file_name_utf8[FileWrapper::kMaxFileNameSize],
659 char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize],
660 const WebRtc_UWord32 new_count) const {
661 WebRtc_Word32 length = (WebRtc_Word32)strlen(file_name_utf8);
662 if (length < 0) {
663 return false;
664 }
665
666 WebRtc_Word32 length_without_file_ending = length - 1;
667 while (length_without_file_ending > 0) {
668 if (file_name_utf8[length_without_file_ending] == '.') {
669 break;
670 } else {
671 length_without_file_ending--;
niklase@google.com470e71d2011-07-07 08:21:25 +0000672 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000673 }
674 if (length_without_file_ending == 0) {
675 length_without_file_ending = length;
676 }
677 memcpy(file_name_with_counter_utf8, file_name_utf8,
678 length_without_file_ending);
679 sprintf(file_name_with_counter_utf8 + length_without_file_ending, "_%lu%s",
680 static_cast<long unsigned int>(new_count),
681 file_name_utf8 + length_without_file_ending);
682 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000683}
684
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000685void Trace::CreateTrace() {
686 TraceImpl::StaticInstance(kAddRef);
tommi@webrtc.orgcde1e7f2011-11-15 12:23:36 +0000687}
688
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000689void Trace::ReturnTrace() {
690 TraceImpl::StaticInstance(kRelease);
tommi@webrtc.orgcde1e7f2011-11-15 12:23:36 +0000691}
692
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000693WebRtc_Word32 Trace::SetLevelFilter(WebRtc_UWord32 filter) {
694 level_filter = filter;
695 return 0;
tommi@webrtc.orgcde1e7f2011-11-15 12:23:36 +0000696}
niklase@google.com470e71d2011-07-07 08:21:25 +0000697
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000698WebRtc_Word32 Trace::LevelFilter(WebRtc_UWord32& filter) {
699 filter = level_filter;
700 return 0;
tommi@webrtc.orgcde1e7f2011-11-15 12:23:36 +0000701}
niklase@google.com470e71d2011-07-07 08:21:25 +0000702
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000703WebRtc_Word32 Trace::TraceFile(char file_name[FileWrapper::kMaxFileNameSize]) {
704 TraceImpl* trace = TraceImpl::GetTrace();
705 if (trace) {
706 int ret_val = trace->TraceFileImpl(file_name);
707 ReturnTrace();
708 return ret_val;
709 }
710 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000711}
712
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000713WebRtc_Word32 Trace::SetTraceFile(const char* file_name,
714 const bool add_file_counter) {
715 TraceImpl* trace = TraceImpl::GetTrace();
716 if (trace) {
717 int ret_val = trace->SetTraceFileImpl(file_name, add_file_counter);
718 ReturnTrace();
719 return ret_val;
720 }
721 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000722}
723
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000724WebRtc_Word32 Trace::SetTraceCallback(TraceCallback* callback) {
725 TraceImpl* trace = TraceImpl::GetTrace();
726 if (trace) {
727 int ret_val = trace->SetTraceCallbackImpl(callback);
728 ReturnTrace();
729 return ret_val;
730 }
731 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000732}
733
734void Trace::Add(const TraceLevel level, const TraceModule module,
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000735 const WebRtc_Word32 id, const char* msg, ...) {
736 TraceImpl* trace = TraceImpl::GetTrace(level);
737 if (trace) {
738 if (trace->TraceCheck(level)) {
739 char temp_buff[WEBRTC_TRACE_MAX_MESSAGE_SIZE];
740 char* buff = 0;
741 if (msg) {
742 va_list args;
743 va_start(args, msg);
niklase@google.com470e71d2011-07-07 08:21:25 +0000744#ifdef _WIN32
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000745 _vsnprintf(temp_buff, WEBRTC_TRACE_MAX_MESSAGE_SIZE - 1, msg, args);
niklase@google.com470e71d2011-07-07 08:21:25 +0000746#else
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#endif
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000749 va_end(args);
750 buff = temp_buff;
751 }
752 trace->AddImpl(level, module, id, buff);
niklase@google.com470e71d2011-07-07 08:21:25 +0000753 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000754 ReturnTrace();
755 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000756}
tommi@webrtc.orgcde1e7f2011-11-15 12:23:36 +0000757
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000758} // namespace webrtc