blob: 3a313417f0f270bdd1fce2b8413ab240d3a53a8e [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;
37
pbos@webrtc.org046deb92013-04-09 09:06:11 +000038static uint32_t 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
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000134int32_t TraceImpl::AddThreadId(char* trace_message) const {
135 uint32_t 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
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000140int32_t TraceImpl::AddLevel(char* sz_message, const TraceLevel level) const {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000141 const int kMessageLength = 12;
142 switch (level) {
143 case kTraceTerseInfo:
144 // Add the appropriate amount of whitespace.
145 memset(sz_message, ' ', kMessageLength);
146 sz_message[kMessageLength] = '\0';
147 break;
148 case kTraceStateInfo:
149 sprintf(sz_message, "STATEINFO ; ");
150 break;
151 case kTraceWarning:
152 sprintf(sz_message, "WARNING ; ");
153 break;
154 case kTraceError:
155 sprintf(sz_message, "ERROR ; ");
156 break;
157 case kTraceCritical:
158 sprintf(sz_message, "CRITICAL ; ");
159 break;
160 case kTraceInfo:
161 sprintf(sz_message, "DEBUGINFO ; ");
162 break;
163 case kTraceModuleCall:
164 sprintf(sz_message, "MODULECALL; ");
165 break;
166 case kTraceMemory:
167 sprintf(sz_message, "MEMORY ; ");
168 break;
169 case kTraceTimer:
170 sprintf(sz_message, "TIMER ; ");
171 break;
172 case kTraceStream:
173 sprintf(sz_message, "STREAM ; ");
174 break;
175 case kTraceApiCall:
176 sprintf(sz_message, "APICALL ; ");
177 break;
178 case kTraceDebug:
179 sprintf(sz_message, "DEBUG ; ");
180 break;
181 default:
182 assert(false);
183 return 0;
184 }
185 // All messages are 12 characters.
186 return kMessageLength;
niklase@google.com470e71d2011-07-07 08:21:25 +0000187}
188
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000189int32_t TraceImpl::AddModuleAndId(char* trace_message,
190 const TraceModule module,
191 const int32_t id) const {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000192 // Use long int to prevent problems with different definitions of
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000193 // int32_t.
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000194 // TODO(hellner): is this actually a problem? If so, it should be better to
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000195 // clean up int32_t
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000196 const long int idl = id;
197 const int kMessageLength = 25;
198 if (idl != -1) {
199 const unsigned long int id_engine = id >> 16;
200 const unsigned long int id_channel = id & 0xffff;
niklase@google.com470e71d2011-07-07 08:21:25 +0000201
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000202 switch (module) {
203 case kTraceUndefined:
204 // Add the appropriate amount of whitespace.
205 memset(trace_message, ' ', kMessageLength);
206 trace_message[kMessageLength] = '\0';
207 break;
208 case kTraceVoice:
209 sprintf(trace_message, " VOICE:%5ld %5ld;", id_engine,
210 id_channel);
211 break;
212 case kTraceVideo:
213 sprintf(trace_message, " VIDEO:%5ld %5ld;", id_engine,
214 id_channel);
215 break;
216 case kTraceUtility:
217 sprintf(trace_message, " UTILITY:%5ld %5ld;", id_engine,
218 id_channel);
219 break;
220 case kTraceRtpRtcp:
221 sprintf(trace_message, " RTP/RTCP:%5ld %5ld;", id_engine,
222 id_channel);
223 break;
224 case kTraceTransport:
225 sprintf(trace_message, " TRANSPORT:%5ld %5ld;", id_engine,
226 id_channel);
227 break;
228 case kTraceAudioCoding:
229 sprintf(trace_message, "AUDIO CODING:%5ld %5ld;", id_engine,
230 id_channel);
231 break;
232 case kTraceSrtp:
233 sprintf(trace_message, " SRTP:%5ld %5ld;", id_engine,
234 id_channel);
235 break;
236 case kTraceAudioMixerServer:
237 sprintf(trace_message, " AUDIO MIX/S:%5ld %5ld;", id_engine,
238 id_channel);
239 break;
240 case kTraceAudioMixerClient:
241 sprintf(trace_message, " AUDIO MIX/C:%5ld %5ld;", id_engine,
242 id_channel);
243 break;
244 case kTraceVideoCoding:
245 sprintf(trace_message, "VIDEO CODING:%5ld %5ld;", id_engine,
246 id_channel);
247 break;
248 case kTraceVideoMixer:
249 // Print sleep time and API call
250 sprintf(trace_message, " VIDEO MIX:%5ld %5ld;", id_engine,
251 id_channel);
252 break;
253 case kTraceFile:
254 sprintf(trace_message, " FILE:%5ld %5ld;", id_engine,
255 id_channel);
256 break;
257 case kTraceAudioProcessing:
258 sprintf(trace_message, " AUDIO PROC:%5ld %5ld;", id_engine,
259 id_channel);
260 break;
261 case kTraceAudioDevice:
262 sprintf(trace_message, "AUDIO DEVICE:%5ld %5ld;", id_engine,
263 id_channel);
264 break;
265 case kTraceVideoRenderer:
266 sprintf(trace_message, "VIDEO RENDER:%5ld %5ld;", id_engine,
267 id_channel);
268 break;
269 case kTraceVideoCapture:
270 sprintf(trace_message, "VIDEO CAPTUR:%5ld %5ld;", id_engine,
271 id_channel);
272 break;
273 case kTraceVideoPreocessing:
274 sprintf(trace_message, " VIDEO PROC:%5ld %5ld;", id_engine,
275 id_channel);
276 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000277 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000278 } else {
279 switch (module) {
280 case kTraceUndefined:
281 // Add the appropriate amount of whitespace.
282 memset(trace_message, ' ', kMessageLength);
283 trace_message[kMessageLength] = '\0';
284 break;
285 case kTraceVoice:
286 sprintf(trace_message, " VOICE:%11ld;", idl);
287 break;
288 case kTraceVideo:
289 sprintf(trace_message, " VIDEO:%11ld;", idl);
290 break;
291 case kTraceUtility:
292 sprintf(trace_message, " UTILITY:%11ld;", idl);
293 break;
294 case kTraceRtpRtcp:
295 sprintf(trace_message, " RTP/RTCP:%11ld;", idl);
296 break;
297 case kTraceTransport:
298 sprintf(trace_message, " TRANSPORT:%11ld;", idl);
299 break;
300 case kTraceAudioCoding:
301 sprintf(trace_message, "AUDIO CODING:%11ld;", idl);
302 break;
303 case kTraceSrtp:
304 sprintf(trace_message, " SRTP:%11ld;", idl);
305 break;
306 case kTraceAudioMixerServer:
307 sprintf(trace_message, " AUDIO MIX/S:%11ld;", idl);
308 break;
309 case kTraceAudioMixerClient:
310 sprintf(trace_message, " AUDIO MIX/C:%11ld;", idl);
311 break;
312 case kTraceVideoCoding:
313 sprintf(trace_message, "VIDEO CODING:%11ld;", idl);
314 break;
315 case kTraceVideoMixer:
316 sprintf(trace_message, " VIDEO MIX:%11ld;", idl);
317 break;
318 case kTraceFile:
319 sprintf(trace_message, " FILE:%11ld;", idl);
320 break;
321 case kTraceAudioProcessing:
322 sprintf(trace_message, " AUDIO PROC:%11ld;", idl);
323 break;
324 case kTraceAudioDevice:
325 sprintf(trace_message, "AUDIO DEVICE:%11ld;", idl);
326 break;
327 case kTraceVideoRenderer:
328 sprintf(trace_message, "VIDEO RENDER:%11ld;", idl);
329 break;
330 case kTraceVideoCapture:
331 sprintf(trace_message, "VIDEO CAPTUR:%11ld;", idl);
332 break;
333 case kTraceVideoPreocessing:
334 sprintf(trace_message, " VIDEO PROC:%11ld;", idl);
335 break;
336 }
337 }
338 return kMessageLength;
niklase@google.com470e71d2011-07-07 08:21:25 +0000339}
340
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000341int32_t TraceImpl::SetTraceFileImpl(const char* file_name_utf8,
342 const bool add_file_counter) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000343 CriticalSectionScoped lock(critsect_interface_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000344
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000345 trace_file_.Flush();
346 trace_file_.CloseFile();
niklase@google.com470e71d2011-07-07 08:21:25 +0000347
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000348 if (file_name_utf8) {
349 if (add_file_counter) {
350 file_count_text_ = 1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000351
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000352 char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize];
353 CreateFileName(file_name_utf8, file_name_with_counter_utf8,
354 file_count_text_);
355 if (trace_file_.OpenFile(file_name_with_counter_utf8, false, false,
356 true) == -1) {
357 return -1;
358 }
359 } else {
360 file_count_text_ = 0;
361 if (trace_file_.OpenFile(file_name_utf8, false, false, true) == -1) {
362 return -1;
363 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000364 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000365 }
366 row_count_text_ = 0;
367 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000368}
369
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000370int32_t TraceImpl::TraceFileImpl(
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000371 char file_name_utf8[FileWrapper::kMaxFileNameSize]) {
372 CriticalSectionScoped lock(critsect_interface_);
373 return trace_file_.FileName(file_name_utf8, FileWrapper::kMaxFileNameSize);
niklase@google.com470e71d2011-07-07 08:21:25 +0000374}
375
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000376int32_t TraceImpl::SetTraceCallbackImpl(TraceCallback* callback) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000377 CriticalSectionScoped lock(critsect_interface_);
378 callback_ = callback;
379 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000380}
381
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000382int32_t TraceImpl::AddMessage(
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000383 char* trace_message,
niklase@google.com470e71d2011-07-07 08:21:25 +0000384 const char msg[WEBRTC_TRACE_MAX_MESSAGE_SIZE],
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000385 const uint16_t written_so_far) const {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000386 int length = 0;
387 if (written_so_far >= WEBRTC_TRACE_MAX_MESSAGE_SIZE) {
388 return -1;
389 }
390 // - 2 to leave room for newline and NULL termination.
niklase@google.com470e71d2011-07-07 08:21:25 +0000391#ifdef _WIN32
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000392 length = _snprintf(trace_message,
393 WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2,
394 "%s", msg);
395 if (length < 0) {
396 length = WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2;
397 trace_message[length] = 0;
398 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000399#else
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000400 length = snprintf(trace_message,
401 WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2,
402 "%s", msg);
403 if (length < 0 ||
404 length > WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2) {
405 length = WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2;
406 trace_message[length] = 0;
407 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000408#endif
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000409 // Length with NULL termination.
410 return length + 1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000411}
412
413void TraceImpl::AddMessageToList(
andrew@webrtc.orgc1ffd332013-03-22 17:13:23 +0000414 const char trace_message[WEBRTC_TRACE_MAX_MESSAGE_SIZE],
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000415 const uint16_t length,
andrew@webrtc.orgc1ffd332013-03-22 17:13:23 +0000416 const TraceLevel level) {
solenberg@webrtc.orgadb51f52013-06-10 09:03:41 +0000417// NOTE(andresp): Enabled externally.
418#ifdef WEBRTC_DIRECT_TRACE
419 if (callback_) {
420 callback_->Print(level, trace_message, length);
421 }
422 return;
423#endif
424
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000425 CriticalSectionScoped lock(critsect_array_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000426
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000427 if (next_free_idx_[active_queue_] >= WEBRTC_TRACE_MAX_QUEUE) {
428 if (!trace_file_.Open() && !callback_) {
429 // Keep at least the last 1/4 of old messages when not logging.
430 // TODO(hellner): isn't this redundant. The user will make it known
431 // when to start logging. Why keep messages before
432 // that?
433 for (int n = 0; n < WEBRTC_TRACE_MAX_QUEUE / 4; ++n) {
434 const int last_quarter_offset = (3 * WEBRTC_TRACE_MAX_QUEUE / 4);
435 memcpy(message_queue_[active_queue_][n],
436 message_queue_[active_queue_][n + last_quarter_offset],
437 WEBRTC_TRACE_MAX_MESSAGE_SIZE);
438 }
439 next_free_idx_[active_queue_] = WEBRTC_TRACE_MAX_QUEUE / 4;
niklase@google.com470e71d2011-07-07 08:21:25 +0000440 } else {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000441 // More messages are being written than there is room for in the
442 // buffer. Drop any new messages.
443 // TODO(hellner): its probably better to drop old messages instead
444 // of new ones. One step further: if this happens
445 // it's due to writing faster than what can be
446 // processed. Maybe modify the filter at this point.
447 // E.g. turn of STREAM.
448 return;
niklase@google.com470e71d2011-07-07 08:21:25 +0000449 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000450 }
451
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000452 uint16_t idx = next_free_idx_[active_queue_];
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000453 next_free_idx_[active_queue_]++;
454
455 level_[active_queue_][idx] = level;
456 length_[active_queue_][idx] = length;
457 memcpy(message_queue_[active_queue_][idx], trace_message, length);
458
459 if (next_free_idx_[active_queue_] == WEBRTC_TRACE_MAX_QUEUE - 1) {
460 // Logging more messages than can be worked off. Log a warning.
461 const char warning_msg[] = "WARNING MISSING TRACE MESSAGES\n";
462 level_[active_queue_][next_free_idx_[active_queue_]] = kTraceWarning;
463 length_[active_queue_][next_free_idx_[active_queue_]] = strlen(warning_msg);
464 memcpy(message_queue_[active_queue_][next_free_idx_[active_queue_]],
465 warning_msg, strlen(warning_msg));
466 next_free_idx_[active_queue_]++;
467 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000468}
469
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000470bool TraceImpl::Run(void* obj) {
471 return static_cast<TraceImpl*>(obj)->Process();
472}
niklase@google.com470e71d2011-07-07 08:21:25 +0000473
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000474bool TraceImpl::Process() {
475 if (event_.Wait(1000) == kEventSignaled) {
andrew@webrtc.orgc1ffd332013-03-22 17:13:23 +0000476 // This slightly odd construction is to avoid locking |critsect_interface_|
477 // while calling WriteToFile() since it's locked inside the function.
478 critsect_interface_->Enter();
479 bool write_to_file = trace_file_.Open() || callback_;
480 critsect_interface_->Leave();
481 if (write_to_file) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000482 WriteToFile();
niklase@google.com470e71d2011-07-07 08:21:25 +0000483 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000484 } else {
andrew@webrtc.orgc1ffd332013-03-22 17:13:23 +0000485 CriticalSectionScoped lock(critsect_interface_);
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000486 trace_file_.Flush();
487 }
488 return true;
489}
490
491void TraceImpl::WriteToFile() {
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000492 uint8_t local_queue_active = 0;
493 uint16_t local_next_free_idx = 0;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000494
495 // There are two buffers. One for reading (for writing to file) and one for
496 // writing (for storing new messages). Let new messages be posted to the
497 // unused buffer so that the current buffer can be flushed safely.
498 {
499 CriticalSectionScoped lock(critsect_array_);
500 local_next_free_idx = next_free_idx_[active_queue_];
501 next_free_idx_[active_queue_] = 0;
502 local_queue_active = active_queue_;
503 if (active_queue_ == 0) {
504 active_queue_ = 1;
505 } else {
506 active_queue_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000507 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000508 }
509 if (local_next_free_idx == 0) {
510 return;
511 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000512
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000513 CriticalSectionScoped lock(critsect_interface_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000514
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000515 for (uint16_t idx = 0; idx < local_next_free_idx; ++idx) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000516 TraceLevel local_level = level_[local_queue_active][idx];
517 if (callback_) {
518 callback_->Print(local_level, message_queue_[local_queue_active][idx],
519 length_[local_queue_active][idx]);
niklase@google.com470e71d2011-07-07 08:21:25 +0000520 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000521 if (trace_file_.Open()) {
522 if (row_count_text_ > WEBRTC_TRACE_MAX_FILE_SIZE) {
523 // wrap file
524 row_count_text_ = 0;
525 trace_file_.Flush();
526
527 if (file_count_text_ == 0) {
528 trace_file_.Rewind();
529 } else {
530 char old_file_name[FileWrapper::kMaxFileNameSize];
531 char new_file_name[FileWrapper::kMaxFileNameSize];
532
533 // get current name
534 trace_file_.FileName(old_file_name,
535 FileWrapper::kMaxFileNameSize);
536 trace_file_.CloseFile();
537
538 file_count_text_++;
539
540 UpdateFileName(old_file_name, new_file_name, file_count_text_);
541
542 if (trace_file_.OpenFile(new_file_name, false, false,
543 true) == -1) {
544 return;
545 }
546 }
547 }
548 if (row_count_text_ == 0) {
549 char message[WEBRTC_TRACE_MAX_MESSAGE_SIZE + 1];
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000550 int32_t length = AddDateTimeInfo(message);
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000551 if (length != -1) {
552 message[length] = 0;
553 message[length - 1] = '\n';
554 trace_file_.Write(message, length);
555 row_count_text_++;
556 }
557 length = AddBuildInfo(message);
558 if (length != -1) {
559 message[length + 1] = 0;
560 message[length] = '\n';
561 message[length - 1] = '\n';
562 trace_file_.Write(message, length + 1);
563 row_count_text_++;
564 row_count_text_++;
565 }
566 }
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000567 uint16_t length = length_[local_queue_active][idx];
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000568 message_queue_[local_queue_active][idx][length] = 0;
569 message_queue_[local_queue_active][idx][length - 1] = '\n';
570 trace_file_.Write(message_queue_[local_queue_active][idx], length);
571 row_count_text_++;
572 }
573 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000574}
575
576void TraceImpl::AddImpl(const TraceLevel level, const TraceModule module,
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000577 const int32_t id,
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000578 const char msg[WEBRTC_TRACE_MAX_MESSAGE_SIZE]) {
579 if (TraceCheck(level)) {
580 char trace_message[WEBRTC_TRACE_MAX_MESSAGE_SIZE];
581 char* message_ptr = trace_message;
niklase@google.com470e71d2011-07-07 08:21:25 +0000582
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000583 int32_t len = 0;
584 int32_t ack_len = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000585
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000586 len = AddLevel(message_ptr, level);
587 if (len == -1) {
588 return;
niklase@google.com470e71d2011-07-07 08:21:25 +0000589 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000590 message_ptr += len;
591 ack_len += len;
592
593 len = AddTime(message_ptr, level);
594 if (len == -1) {
595 return;
596 }
597 message_ptr += len;
598 ack_len += len;
599
600 len = AddModuleAndId(message_ptr, module, id);
601 if (len == -1) {
602 return;
603 }
604 message_ptr += len;
605 ack_len += len;
606
607 len = AddThreadId(message_ptr);
608 if (len < 0) {
609 return;
610 }
611 message_ptr += len;
612 ack_len += len;
613
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000614 len = AddMessage(message_ptr, msg, (uint16_t)ack_len);
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000615 if (len == -1) {
616 return;
617 }
618 ack_len += len;
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000619 AddMessageToList(trace_message, (uint16_t)ack_len, level);
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000620
621 // Make sure that messages are written as soon as possible.
622 event_.Set();
623 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000624}
625
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000626bool TraceImpl::TraceCheck(const TraceLevel level) const {
627 return (level & level_filter) ? true : false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000628}
629
630bool TraceImpl::UpdateFileName(
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000631 const char file_name_utf8[FileWrapper::kMaxFileNameSize],
632 char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize],
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000633 const uint32_t new_count) const {
634 int32_t length = (int32_t)strlen(file_name_utf8);
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000635 if (length < 0) {
636 return false;
637 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000638
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000639 int32_t length_without_file_ending = length - 1;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000640 while (length_without_file_ending > 0) {
641 if (file_name_utf8[length_without_file_ending] == '.') {
642 break;
643 } else {
644 length_without_file_ending--;
niklase@google.com470e71d2011-07-07 08:21:25 +0000645 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000646 }
647 if (length_without_file_ending == 0) {
648 length_without_file_ending = length;
649 }
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000650 int32_t length_to_ = length_without_file_ending - 1;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000651 while (length_to_ > 0) {
652 if (file_name_utf8[length_to_] == '_') {
653 break;
654 } else {
655 length_to_--;
niklase@google.com470e71d2011-07-07 08:21:25 +0000656 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000657 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000658
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000659 memcpy(file_name_with_counter_utf8, file_name_utf8, length_to_);
660 sprintf(file_name_with_counter_utf8 + length_to_, "_%lu%s",
661 static_cast<long unsigned int>(new_count),
662 file_name_utf8 + length_without_file_ending);
663 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000664}
665
666bool TraceImpl::CreateFileName(
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000667 const char file_name_utf8[FileWrapper::kMaxFileNameSize],
668 char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize],
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000669 const uint32_t new_count) const {
670 int32_t length = (int32_t)strlen(file_name_utf8);
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000671 if (length < 0) {
672 return false;
673 }
674
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000675 int32_t length_without_file_ending = length - 1;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000676 while (length_without_file_ending > 0) {
677 if (file_name_utf8[length_without_file_ending] == '.') {
678 break;
679 } else {
680 length_without_file_ending--;
niklase@google.com470e71d2011-07-07 08:21:25 +0000681 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000682 }
683 if (length_without_file_ending == 0) {
684 length_without_file_ending = length;
685 }
686 memcpy(file_name_with_counter_utf8, file_name_utf8,
687 length_without_file_ending);
688 sprintf(file_name_with_counter_utf8 + length_without_file_ending, "_%lu%s",
689 static_cast<long unsigned int>(new_count),
690 file_name_utf8 + length_without_file_ending);
691 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000692}
693
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000694void Trace::CreateTrace() {
695 TraceImpl::StaticInstance(kAddRef);
tommi@webrtc.orgcde1e7f2011-11-15 12:23:36 +0000696}
697
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000698void Trace::ReturnTrace() {
699 TraceImpl::StaticInstance(kRelease);
tommi@webrtc.orgcde1e7f2011-11-15 12:23:36 +0000700}
701
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000702int32_t Trace::SetLevelFilter(uint32_t filter) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000703 level_filter = filter;
704 return 0;
tommi@webrtc.orgcde1e7f2011-11-15 12:23:36 +0000705}
niklase@google.com470e71d2011-07-07 08:21:25 +0000706
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000707int32_t Trace::LevelFilter(uint32_t& filter) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000708 filter = level_filter;
709 return 0;
tommi@webrtc.orgcde1e7f2011-11-15 12:23:36 +0000710}
niklase@google.com470e71d2011-07-07 08:21:25 +0000711
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000712int32_t Trace::TraceFile(char file_name[FileWrapper::kMaxFileNameSize]) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000713 TraceImpl* trace = TraceImpl::GetTrace();
714 if (trace) {
715 int ret_val = trace->TraceFileImpl(file_name);
716 ReturnTrace();
717 return ret_val;
718 }
719 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000720}
721
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000722int32_t Trace::SetTraceFile(const char* file_name,
723 const bool add_file_counter) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000724 TraceImpl* trace = TraceImpl::GetTrace();
725 if (trace) {
726 int ret_val = trace->SetTraceFileImpl(file_name, add_file_counter);
727 ReturnTrace();
728 return ret_val;
729 }
730 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000731}
732
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000733int32_t Trace::SetTraceCallback(TraceCallback* callback) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000734 TraceImpl* trace = TraceImpl::GetTrace();
735 if (trace) {
736 int ret_val = trace->SetTraceCallbackImpl(callback);
737 ReturnTrace();
738 return ret_val;
739 }
740 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000741}
742
743void Trace::Add(const TraceLevel level, const TraceModule module,
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000744 const int32_t id, const char* msg, ...) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000745 TraceImpl* trace = TraceImpl::GetTrace(level);
746 if (trace) {
747 if (trace->TraceCheck(level)) {
748 char temp_buff[WEBRTC_TRACE_MAX_MESSAGE_SIZE];
749 char* buff = 0;
750 if (msg) {
751 va_list args;
752 va_start(args, msg);
niklase@google.com470e71d2011-07-07 08:21:25 +0000753#ifdef _WIN32
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000754 _vsnprintf(temp_buff, WEBRTC_TRACE_MAX_MESSAGE_SIZE - 1, msg, args);
niklase@google.com470e71d2011-07-07 08:21:25 +0000755#else
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000756 vsnprintf(temp_buff, WEBRTC_TRACE_MAX_MESSAGE_SIZE - 1, msg, args);
niklase@google.com470e71d2011-07-07 08:21:25 +0000757#endif
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000758 va_end(args);
759 buff = temp_buff;
760 }
761 trace->AddImpl(level, module, id, buff);
niklase@google.com470e71d2011-07-07 08:21:25 +0000762 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000763 ReturnTrace();
764 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000765}
tommi@webrtc.orgcde1e7f2011-11-15 12:23:36 +0000766
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000767} // namespace webrtc