blob: 28de280a9d954ebf6c1cb54cd416e057ab0123fc [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000011#include "webrtc/system_wrappers/source/trace_impl.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000013#include <assert.h>
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000014#include <stdarg.h>
15#include <stdio.h>
16#include <string.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000017
18#ifdef _WIN32
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000019#include "webrtc/system_wrappers/source/trace_win.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000020#else
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000021#include "webrtc/system_wrappers/source/trace_posix.h"
22#endif // _WIN32
niklase@google.com470e71d2011-07-07 08:21:25 +000023
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000024#include "webrtc/system_wrappers/interface/sleep.h"
hta@webrtc.org41adcdb2012-06-18 11:24:57 +000025
niklase@google.com470e71d2011-07-07 08:21:25 +000026#define KEY_LEN_CHARS 31
27
28#ifdef _WIN32
andrew@webrtc.org5dffebc2012-08-16 04:24:05 +000029#pragma warning(disable:4355)
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000030#endif // _WIN32
niklase@google.com470e71d2011-07-07 08:21:25 +000031
32namespace webrtc {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000033
andrew@webrtc.orgc1ffd332013-03-22 17:13:23 +000034const int Trace::kBoilerplateLength = 71;
35const int Trace::kTimestampPosition = 13;
36const int Trace::kTimestampLength = 12;
andrew@webrtc.org90805182013-09-05 16:40:43 +000037uint32_t Trace::level_filter_ = kTraceDefault;
niklase@google.com470e71d2011-07-07 08:21:25 +000038
39// Construct On First Use idiom. Avoids "static initialization order fiasco".
henrike@webrtc.org315282c2011-12-09 17:46:20 +000040TraceImpl* TraceImpl::StaticInstance(CountOperation count_operation,
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000041 const TraceLevel level) {
42 // Sanities to avoid taking lock unless absolutely necessary (for
43 // performance reasons). count_operation == kAddRefNoCreate implies that a
44 // message will be written to file.
45 if ((level != kTraceAll) && (count_operation == kAddRefNoCreate)) {
andrew@webrtc.org90805182013-09-05 16:40:43 +000046 if (!(level & level_filter())) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000047 return NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +000048 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000049 }
50 TraceImpl* impl =
51 GetStaticInstance<TraceImpl>(count_operation);
52 return impl;
niklase@google.com470e71d2011-07-07 08:21:25 +000053}
54
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000055TraceImpl* TraceImpl::GetTrace(const TraceLevel level) {
56 return StaticInstance(kAddRefNoCreate, level);
niklase@google.com470e71d2011-07-07 08:21:25 +000057}
58
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000059TraceImpl* TraceImpl::CreateInstance() {
henrike@webrtc.org2f47b5a2011-12-10 00:44:47 +000060#if defined(_WIN32)
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000061 return new TraceWindows();
niklase@google.com470e71d2011-07-07 08:21:25 +000062#else
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000063 return new TracePosix();
niklase@google.com470e71d2011-07-07 08:21:25 +000064#endif
65}
66
67TraceImpl::TraceImpl()
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000068 : critsect_interface_(CriticalSectionWrapper::CreateCriticalSection()),
69 callback_(NULL),
70 row_count_text_(0),
71 file_count_text_(0),
72 trace_file_(*FileWrapper::Create()),
73 thread_(*ThreadWrapper::CreateThread(TraceImpl::Run, this,
niklase@google.com470e71d2011-07-07 08:21:25 +000074 kHighestPriority, "Trace")),
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000075 event_(*EventWrapper::Create()),
76 critsect_array_(CriticalSectionWrapper::CreateCriticalSection()),
77 next_free_idx_(),
78 level_(),
79 length_(),
80 message_queue_(),
81 active_queue_(0) {
82 next_free_idx_[0] = 0;
83 next_free_idx_[1] = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000084
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000085 unsigned int tid = 0;
86 thread_.Start(tid);
niklase@google.com470e71d2011-07-07 08:21:25 +000087
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000088 for (int m = 0; m < WEBRTC_TRACE_NUM_ARRAY; ++m) {
89 for (int n = 0; n < WEBRTC_TRACE_MAX_QUEUE; ++n) {
90 message_queue_[m][n] = new
91 char[WEBRTC_TRACE_MAX_MESSAGE_SIZE];
niklase@google.com470e71d2011-07-07 08:21:25 +000092 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000093 }
niklase@google.com470e71d2011-07-07 08:21:25 +000094}
95
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000096bool TraceImpl::StopThread() {
97 // Release the worker thread so that it can flush any lingering messages.
98 event_.Set();
niklase@google.com470e71d2011-07-07 08:21:25 +000099
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000100 // Allow 10 ms for pending messages to be flushed out.
101 // TODO(hellner): why not use condition variables to do this? Or let the
102 // worker thread die and let this thread flush remaining
103 // messages?
104 SleepMs(10);
niklase@google.com470e71d2011-07-07 08:21:25 +0000105
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000106 // Make sure the thread finishes as quickly as possible (instead of having
107 // to wait for the timeout).
108 event_.Set();
109 bool stopped = thread_.Stop();
niklase@google.com470e71d2011-07-07 08:21:25 +0000110
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000111 CriticalSectionScoped lock(critsect_interface_);
112 trace_file_.Flush();
113 trace_file_.CloseFile();
114 return stopped;
niklase@google.com470e71d2011-07-07 08:21:25 +0000115}
116
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000117TraceImpl::~TraceImpl() {
118 StopThread();
119 delete &event_;
120 delete &trace_file_;
121 delete &thread_;
122 delete critsect_interface_;
123 delete critsect_array_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000124
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000125 for (int m = 0; m < WEBRTC_TRACE_NUM_ARRAY; ++m) {
126 for (int n = 0; n < WEBRTC_TRACE_MAX_QUEUE; ++n) {
127 delete [] message_queue_[m][n];
niklase@google.com470e71d2011-07-07 08:21:25 +0000128 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000129 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000130}
131
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000132int32_t TraceImpl::AddThreadId(char* trace_message) const {
133 uint32_t thread_id = ThreadWrapper::GetThreadId();
pwestin@webrtc.orgb54d7272012-01-11 08:28:04 +0000134 // Messages is 12 characters.
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000135 return sprintf(trace_message, "%10u; ", thread_id);
pwestin@webrtc.orgb54d7272012-01-11 08:28:04 +0000136}
137
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000138int32_t TraceImpl::AddLevel(char* sz_message, const TraceLevel level) const {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000139 const int kMessageLength = 12;
140 switch (level) {
141 case kTraceTerseInfo:
142 // Add the appropriate amount of whitespace.
143 memset(sz_message, ' ', kMessageLength);
144 sz_message[kMessageLength] = '\0';
145 break;
146 case kTraceStateInfo:
147 sprintf(sz_message, "STATEINFO ; ");
148 break;
149 case kTraceWarning:
150 sprintf(sz_message, "WARNING ; ");
151 break;
152 case kTraceError:
153 sprintf(sz_message, "ERROR ; ");
154 break;
155 case kTraceCritical:
156 sprintf(sz_message, "CRITICAL ; ");
157 break;
158 case kTraceInfo:
159 sprintf(sz_message, "DEBUGINFO ; ");
160 break;
161 case kTraceModuleCall:
162 sprintf(sz_message, "MODULECALL; ");
163 break;
164 case kTraceMemory:
165 sprintf(sz_message, "MEMORY ; ");
166 break;
167 case kTraceTimer:
168 sprintf(sz_message, "TIMER ; ");
169 break;
170 case kTraceStream:
171 sprintf(sz_message, "STREAM ; ");
172 break;
173 case kTraceApiCall:
174 sprintf(sz_message, "APICALL ; ");
175 break;
176 case kTraceDebug:
177 sprintf(sz_message, "DEBUG ; ");
178 break;
179 default:
180 assert(false);
181 return 0;
182 }
183 // All messages are 12 characters.
184 return kMessageLength;
niklase@google.com470e71d2011-07-07 08:21:25 +0000185}
186
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000187int32_t TraceImpl::AddModuleAndId(char* trace_message,
188 const TraceModule module,
189 const int32_t id) const {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000190 // Use long int to prevent problems with different definitions of
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000191 // int32_t.
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000192 // TODO(hellner): is this actually a problem? If so, it should be better to
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000193 // clean up int32_t
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000194 const long int idl = id;
195 const int kMessageLength = 25;
196 if (idl != -1) {
197 const unsigned long int id_engine = id >> 16;
198 const unsigned long int id_channel = id & 0xffff;
niklase@google.com470e71d2011-07-07 08:21:25 +0000199
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000200 switch (module) {
201 case kTraceUndefined:
202 // Add the appropriate amount of whitespace.
203 memset(trace_message, ' ', kMessageLength);
204 trace_message[kMessageLength] = '\0';
205 break;
206 case kTraceVoice:
207 sprintf(trace_message, " VOICE:%5ld %5ld;", id_engine,
208 id_channel);
209 break;
210 case kTraceVideo:
211 sprintf(trace_message, " VIDEO:%5ld %5ld;", id_engine,
212 id_channel);
213 break;
214 case kTraceUtility:
215 sprintf(trace_message, " UTILITY:%5ld %5ld;", id_engine,
216 id_channel);
217 break;
218 case kTraceRtpRtcp:
219 sprintf(trace_message, " RTP/RTCP:%5ld %5ld;", id_engine,
220 id_channel);
221 break;
222 case kTraceTransport:
223 sprintf(trace_message, " TRANSPORT:%5ld %5ld;", id_engine,
224 id_channel);
225 break;
226 case kTraceAudioCoding:
227 sprintf(trace_message, "AUDIO CODING:%5ld %5ld;", id_engine,
228 id_channel);
229 break;
230 case kTraceSrtp:
231 sprintf(trace_message, " SRTP:%5ld %5ld;", id_engine,
232 id_channel);
233 break;
234 case kTraceAudioMixerServer:
235 sprintf(trace_message, " AUDIO MIX/S:%5ld %5ld;", id_engine,
236 id_channel);
237 break;
238 case kTraceAudioMixerClient:
239 sprintf(trace_message, " AUDIO MIX/C:%5ld %5ld;", id_engine,
240 id_channel);
241 break;
242 case kTraceVideoCoding:
243 sprintf(trace_message, "VIDEO CODING:%5ld %5ld;", id_engine,
244 id_channel);
245 break;
246 case kTraceVideoMixer:
247 // Print sleep time and API call
248 sprintf(trace_message, " VIDEO MIX:%5ld %5ld;", id_engine,
249 id_channel);
250 break;
251 case kTraceFile:
252 sprintf(trace_message, " FILE:%5ld %5ld;", id_engine,
253 id_channel);
254 break;
255 case kTraceAudioProcessing:
256 sprintf(trace_message, " AUDIO PROC:%5ld %5ld;", id_engine,
257 id_channel);
258 break;
259 case kTraceAudioDevice:
260 sprintf(trace_message, "AUDIO DEVICE:%5ld %5ld;", id_engine,
261 id_channel);
262 break;
263 case kTraceVideoRenderer:
264 sprintf(trace_message, "VIDEO RENDER:%5ld %5ld;", id_engine,
265 id_channel);
266 break;
267 case kTraceVideoCapture:
268 sprintf(trace_message, "VIDEO CAPTUR:%5ld %5ld;", id_engine,
269 id_channel);
270 break;
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000271 case kTraceRemoteBitrateEstimator:
272 sprintf(trace_message, " BWE RBE:%5ld %5ld;", id_engine,
273 id_channel);
274 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000275 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000276 } else {
277 switch (module) {
278 case kTraceUndefined:
279 // Add the appropriate amount of whitespace.
280 memset(trace_message, ' ', kMessageLength);
281 trace_message[kMessageLength] = '\0';
282 break;
283 case kTraceVoice:
284 sprintf(trace_message, " VOICE:%11ld;", idl);
285 break;
286 case kTraceVideo:
287 sprintf(trace_message, " VIDEO:%11ld;", idl);
288 break;
289 case kTraceUtility:
290 sprintf(trace_message, " UTILITY:%11ld;", idl);
291 break;
292 case kTraceRtpRtcp:
293 sprintf(trace_message, " RTP/RTCP:%11ld;", idl);
294 break;
295 case kTraceTransport:
296 sprintf(trace_message, " TRANSPORT:%11ld;", idl);
297 break;
298 case kTraceAudioCoding:
299 sprintf(trace_message, "AUDIO CODING:%11ld;", idl);
300 break;
301 case kTraceSrtp:
302 sprintf(trace_message, " SRTP:%11ld;", idl);
303 break;
304 case kTraceAudioMixerServer:
305 sprintf(trace_message, " AUDIO MIX/S:%11ld;", idl);
306 break;
307 case kTraceAudioMixerClient:
308 sprintf(trace_message, " AUDIO MIX/C:%11ld;", idl);
309 break;
310 case kTraceVideoCoding:
311 sprintf(trace_message, "VIDEO CODING:%11ld;", idl);
312 break;
313 case kTraceVideoMixer:
314 sprintf(trace_message, " VIDEO MIX:%11ld;", idl);
315 break;
316 case kTraceFile:
317 sprintf(trace_message, " FILE:%11ld;", idl);
318 break;
319 case kTraceAudioProcessing:
320 sprintf(trace_message, " AUDIO PROC:%11ld;", idl);
321 break;
322 case kTraceAudioDevice:
323 sprintf(trace_message, "AUDIO DEVICE:%11ld;", idl);
324 break;
325 case kTraceVideoRenderer:
326 sprintf(trace_message, "VIDEO RENDER:%11ld;", idl);
327 break;
328 case kTraceVideoCapture:
329 sprintf(trace_message, "VIDEO CAPTUR:%11ld;", idl);
330 break;
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000331 case kTraceRemoteBitrateEstimator:
332 sprintf(trace_message, " BWE RBE:%11ld;", idl);
333 break;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000334 }
335 }
336 return kMessageLength;
niklase@google.com470e71d2011-07-07 08:21:25 +0000337}
338
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000339int32_t TraceImpl::SetTraceFileImpl(const char* file_name_utf8,
340 const bool add_file_counter) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000341 CriticalSectionScoped lock(critsect_interface_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000342
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000343 trace_file_.Flush();
344 trace_file_.CloseFile();
niklase@google.com470e71d2011-07-07 08:21:25 +0000345
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000346 if (file_name_utf8) {
347 if (add_file_counter) {
348 file_count_text_ = 1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000349
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000350 char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize];
351 CreateFileName(file_name_utf8, file_name_with_counter_utf8,
352 file_count_text_);
353 if (trace_file_.OpenFile(file_name_with_counter_utf8, false, false,
354 true) == -1) {
355 return -1;
356 }
357 } else {
358 file_count_text_ = 0;
359 if (trace_file_.OpenFile(file_name_utf8, false, false, true) == -1) {
360 return -1;
361 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000362 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000363 }
364 row_count_text_ = 0;
365 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000366}
367
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000368int32_t TraceImpl::TraceFileImpl(
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000369 char file_name_utf8[FileWrapper::kMaxFileNameSize]) {
370 CriticalSectionScoped lock(critsect_interface_);
371 return trace_file_.FileName(file_name_utf8, FileWrapper::kMaxFileNameSize);
niklase@google.com470e71d2011-07-07 08:21:25 +0000372}
373
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000374int32_t TraceImpl::SetTraceCallbackImpl(TraceCallback* callback) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000375 CriticalSectionScoped lock(critsect_interface_);
376 callback_ = callback;
377 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000378}
379
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000380int32_t TraceImpl::AddMessage(
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000381 char* trace_message,
niklase@google.com470e71d2011-07-07 08:21:25 +0000382 const char msg[WEBRTC_TRACE_MAX_MESSAGE_SIZE],
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000383 const uint16_t written_so_far) const {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000384 int length = 0;
385 if (written_so_far >= WEBRTC_TRACE_MAX_MESSAGE_SIZE) {
386 return -1;
387 }
388 // - 2 to leave room for newline and NULL termination.
niklase@google.com470e71d2011-07-07 08:21:25 +0000389#ifdef _WIN32
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000390 length = _snprintf(trace_message,
391 WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2,
392 "%s", msg);
393 if (length < 0) {
394 length = WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2;
395 trace_message[length] = 0;
396 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000397#else
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000398 length = snprintf(trace_message,
399 WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2,
400 "%s", msg);
401 if (length < 0 ||
402 length > WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2) {
403 length = WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2;
404 trace_message[length] = 0;
405 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000406#endif
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000407 // Length with NULL termination.
408 return length + 1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000409}
410
411void TraceImpl::AddMessageToList(
andrew@webrtc.orgc1ffd332013-03-22 17:13:23 +0000412 const char trace_message[WEBRTC_TRACE_MAX_MESSAGE_SIZE],
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000413 const uint16_t length,
andrew@webrtc.orgc1ffd332013-03-22 17:13:23 +0000414 const TraceLevel level) {
solenberg@webrtc.orgadb51f52013-06-10 09:03:41 +0000415// NOTE(andresp): Enabled externally.
416#ifdef WEBRTC_DIRECT_TRACE
417 if (callback_) {
418 callback_->Print(level, trace_message, length);
419 }
420 return;
421#endif
422
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000423 CriticalSectionScoped lock(critsect_array_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000424
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000425 if (next_free_idx_[active_queue_] >= WEBRTC_TRACE_MAX_QUEUE) {
426 if (!trace_file_.Open() && !callback_) {
427 // Keep at least the last 1/4 of old messages when not logging.
428 // TODO(hellner): isn't this redundant. The user will make it known
429 // when to start logging. Why keep messages before
430 // that?
431 for (int n = 0; n < WEBRTC_TRACE_MAX_QUEUE / 4; ++n) {
432 const int last_quarter_offset = (3 * WEBRTC_TRACE_MAX_QUEUE / 4);
433 memcpy(message_queue_[active_queue_][n],
434 message_queue_[active_queue_][n + last_quarter_offset],
435 WEBRTC_TRACE_MAX_MESSAGE_SIZE);
436 }
437 next_free_idx_[active_queue_] = WEBRTC_TRACE_MAX_QUEUE / 4;
niklase@google.com470e71d2011-07-07 08:21:25 +0000438 } else {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000439 // More messages are being written than there is room for in the
440 // buffer. Drop any new messages.
441 // TODO(hellner): its probably better to drop old messages instead
442 // of new ones. One step further: if this happens
443 // it's due to writing faster than what can be
444 // processed. Maybe modify the filter at this point.
445 // E.g. turn of STREAM.
446 return;
niklase@google.com470e71d2011-07-07 08:21:25 +0000447 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000448 }
449
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000450 uint16_t idx = next_free_idx_[active_queue_];
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000451 next_free_idx_[active_queue_]++;
452
453 level_[active_queue_][idx] = level;
454 length_[active_queue_][idx] = length;
455 memcpy(message_queue_[active_queue_][idx], trace_message, length);
456
457 if (next_free_idx_[active_queue_] == WEBRTC_TRACE_MAX_QUEUE - 1) {
458 // Logging more messages than can be worked off. Log a warning.
459 const char warning_msg[] = "WARNING MISSING TRACE MESSAGES\n";
460 level_[active_queue_][next_free_idx_[active_queue_]] = kTraceWarning;
461 length_[active_queue_][next_free_idx_[active_queue_]] = strlen(warning_msg);
462 memcpy(message_queue_[active_queue_][next_free_idx_[active_queue_]],
463 warning_msg, strlen(warning_msg));
464 next_free_idx_[active_queue_]++;
465 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000466}
467
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000468bool TraceImpl::Run(void* obj) {
469 return static_cast<TraceImpl*>(obj)->Process();
470}
niklase@google.com470e71d2011-07-07 08:21:25 +0000471
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000472bool TraceImpl::Process() {
473 if (event_.Wait(1000) == kEventSignaled) {
andrew@webrtc.orgc1ffd332013-03-22 17:13:23 +0000474 // This slightly odd construction is to avoid locking |critsect_interface_|
475 // while calling WriteToFile() since it's locked inside the function.
476 critsect_interface_->Enter();
477 bool write_to_file = trace_file_.Open() || callback_;
478 critsect_interface_->Leave();
479 if (write_to_file) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000480 WriteToFile();
niklase@google.com470e71d2011-07-07 08:21:25 +0000481 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000482 } else {
andrew@webrtc.orgc1ffd332013-03-22 17:13:23 +0000483 CriticalSectionScoped lock(critsect_interface_);
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000484 trace_file_.Flush();
485 }
486 return true;
487}
488
489void TraceImpl::WriteToFile() {
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000490 uint8_t local_queue_active = 0;
491 uint16_t local_next_free_idx = 0;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000492
493 // There are two buffers. One for reading (for writing to file) and one for
494 // writing (for storing new messages). Let new messages be posted to the
495 // unused buffer so that the current buffer can be flushed safely.
496 {
497 CriticalSectionScoped lock(critsect_array_);
498 local_next_free_idx = next_free_idx_[active_queue_];
499 next_free_idx_[active_queue_] = 0;
500 local_queue_active = active_queue_;
501 if (active_queue_ == 0) {
502 active_queue_ = 1;
503 } else {
504 active_queue_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000505 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000506 }
507 if (local_next_free_idx == 0) {
508 return;
509 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000510
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000511 CriticalSectionScoped lock(critsect_interface_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000512
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000513 for (uint16_t idx = 0; idx < local_next_free_idx; ++idx) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000514 TraceLevel local_level = level_[local_queue_active][idx];
515 if (callback_) {
516 callback_->Print(local_level, message_queue_[local_queue_active][idx],
517 length_[local_queue_active][idx]);
niklase@google.com470e71d2011-07-07 08:21:25 +0000518 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000519 if (trace_file_.Open()) {
520 if (row_count_text_ > WEBRTC_TRACE_MAX_FILE_SIZE) {
521 // wrap file
522 row_count_text_ = 0;
523 trace_file_.Flush();
524
525 if (file_count_text_ == 0) {
526 trace_file_.Rewind();
527 } else {
528 char old_file_name[FileWrapper::kMaxFileNameSize];
529 char new_file_name[FileWrapper::kMaxFileNameSize];
530
531 // get current name
532 trace_file_.FileName(old_file_name,
533 FileWrapper::kMaxFileNameSize);
534 trace_file_.CloseFile();
535
536 file_count_text_++;
537
538 UpdateFileName(old_file_name, new_file_name, file_count_text_);
539
540 if (trace_file_.OpenFile(new_file_name, false, false,
541 true) == -1) {
542 return;
543 }
544 }
545 }
546 if (row_count_text_ == 0) {
547 char message[WEBRTC_TRACE_MAX_MESSAGE_SIZE + 1];
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000548 int32_t length = AddDateTimeInfo(message);
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000549 if (length != -1) {
550 message[length] = 0;
551 message[length - 1] = '\n';
552 trace_file_.Write(message, length);
553 row_count_text_++;
554 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000555 }
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000556 uint16_t length = length_[local_queue_active][idx];
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000557 message_queue_[local_queue_active][idx][length] = 0;
558 message_queue_[local_queue_active][idx][length - 1] = '\n';
559 trace_file_.Write(message_queue_[local_queue_active][idx], length);
560 row_count_text_++;
561 }
562 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000563}
564
565void TraceImpl::AddImpl(const TraceLevel level, const TraceModule module,
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000566 const int32_t id,
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000567 const char msg[WEBRTC_TRACE_MAX_MESSAGE_SIZE]) {
568 if (TraceCheck(level)) {
569 char trace_message[WEBRTC_TRACE_MAX_MESSAGE_SIZE];
570 char* message_ptr = trace_message;
niklase@google.com470e71d2011-07-07 08:21:25 +0000571
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000572 int32_t len = 0;
573 int32_t ack_len = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000574
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000575 len = AddLevel(message_ptr, level);
576 if (len == -1) {
577 return;
niklase@google.com470e71d2011-07-07 08:21:25 +0000578 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000579 message_ptr += len;
580 ack_len += len;
581
582 len = AddTime(message_ptr, level);
583 if (len == -1) {
584 return;
585 }
586 message_ptr += len;
587 ack_len += len;
588
589 len = AddModuleAndId(message_ptr, module, id);
590 if (len == -1) {
591 return;
592 }
593 message_ptr += len;
594 ack_len += len;
595
596 len = AddThreadId(message_ptr);
597 if (len < 0) {
598 return;
599 }
600 message_ptr += len;
601 ack_len += len;
602
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000603 len = AddMessage(message_ptr, msg, (uint16_t)ack_len);
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000604 if (len == -1) {
605 return;
606 }
607 ack_len += len;
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000608 AddMessageToList(trace_message, (uint16_t)ack_len, level);
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000609
610 // Make sure that messages are written as soon as possible.
611 event_.Set();
612 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000613}
614
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000615bool TraceImpl::TraceCheck(const TraceLevel level) const {
andrew@webrtc.org90805182013-09-05 16:40:43 +0000616 return (level & level_filter()) ? true : false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000617}
618
619bool TraceImpl::UpdateFileName(
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000620 const char file_name_utf8[FileWrapper::kMaxFileNameSize],
621 char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize],
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000622 const uint32_t new_count) const {
623 int32_t length = (int32_t)strlen(file_name_utf8);
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000624 if (length < 0) {
625 return false;
626 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000627
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000628 int32_t length_without_file_ending = length - 1;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000629 while (length_without_file_ending > 0) {
630 if (file_name_utf8[length_without_file_ending] == '.') {
631 break;
632 } else {
633 length_without_file_ending--;
niklase@google.com470e71d2011-07-07 08:21:25 +0000634 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000635 }
636 if (length_without_file_ending == 0) {
637 length_without_file_ending = length;
638 }
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000639 int32_t length_to_ = length_without_file_ending - 1;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000640 while (length_to_ > 0) {
641 if (file_name_utf8[length_to_] == '_') {
642 break;
643 } else {
644 length_to_--;
niklase@google.com470e71d2011-07-07 08:21:25 +0000645 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000646 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000647
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000648 memcpy(file_name_with_counter_utf8, file_name_utf8, length_to_);
649 sprintf(file_name_with_counter_utf8 + length_to_, "_%lu%s",
650 static_cast<long unsigned int>(new_count),
651 file_name_utf8 + length_without_file_ending);
652 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000653}
654
655bool TraceImpl::CreateFileName(
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000656 const char file_name_utf8[FileWrapper::kMaxFileNameSize],
657 char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize],
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000658 const uint32_t new_count) const {
659 int32_t length = (int32_t)strlen(file_name_utf8);
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000660 if (length < 0) {
661 return false;
662 }
663
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000664 int32_t length_without_file_ending = length - 1;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000665 while (length_without_file_ending > 0) {
666 if (file_name_utf8[length_without_file_ending] == '.') {
667 break;
668 } else {
669 length_without_file_ending--;
niklase@google.com470e71d2011-07-07 08:21:25 +0000670 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000671 }
672 if (length_without_file_ending == 0) {
673 length_without_file_ending = length;
674 }
675 memcpy(file_name_with_counter_utf8, file_name_utf8,
676 length_without_file_ending);
677 sprintf(file_name_with_counter_utf8 + length_without_file_ending, "_%lu%s",
678 static_cast<long unsigned int>(new_count),
679 file_name_utf8 + length_without_file_ending);
680 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000681}
682
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000683void Trace::CreateTrace() {
684 TraceImpl::StaticInstance(kAddRef);
tommi@webrtc.orgcde1e7f2011-11-15 12:23:36 +0000685}
686
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000687void Trace::ReturnTrace() {
688 TraceImpl::StaticInstance(kRelease);
tommi@webrtc.orgcde1e7f2011-11-15 12:23:36 +0000689}
690
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000691int32_t Trace::TraceFile(char file_name[FileWrapper::kMaxFileNameSize]) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000692 TraceImpl* trace = TraceImpl::GetTrace();
693 if (trace) {
694 int ret_val = trace->TraceFileImpl(file_name);
695 ReturnTrace();
696 return ret_val;
697 }
698 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000699}
700
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000701int32_t Trace::SetTraceFile(const char* file_name,
702 const bool add_file_counter) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000703 TraceImpl* trace = TraceImpl::GetTrace();
704 if (trace) {
705 int ret_val = trace->SetTraceFileImpl(file_name, add_file_counter);
706 ReturnTrace();
707 return ret_val;
708 }
709 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000710}
711
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000712int32_t Trace::SetTraceCallback(TraceCallback* callback) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000713 TraceImpl* trace = TraceImpl::GetTrace();
714 if (trace) {
715 int ret_val = trace->SetTraceCallbackImpl(callback);
716 ReturnTrace();
717 return ret_val;
718 }
719 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000720}
721
722void Trace::Add(const TraceLevel level, const TraceModule module,
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000723 const int32_t id, const char* msg, ...) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000724 TraceImpl* trace = TraceImpl::GetTrace(level);
725 if (trace) {
726 if (trace->TraceCheck(level)) {
727 char temp_buff[WEBRTC_TRACE_MAX_MESSAGE_SIZE];
728 char* buff = 0;
729 if (msg) {
730 va_list args;
731 va_start(args, msg);
niklase@google.com470e71d2011-07-07 08:21:25 +0000732#ifdef _WIN32
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000733 _vsnprintf(temp_buff, WEBRTC_TRACE_MAX_MESSAGE_SIZE - 1, msg, args);
niklase@google.com470e71d2011-07-07 08:21:25 +0000734#else
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000735 vsnprintf(temp_buff, WEBRTC_TRACE_MAX_MESSAGE_SIZE - 1, msg, args);
niklase@google.com470e71d2011-07-07 08:21:25 +0000736#endif
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000737 va_end(args);
738 buff = temp_buff;
739 }
740 trace->AddImpl(level, module, id, buff);
niklase@google.com470e71d2011-07-07 08:21:25 +0000741 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000742 ReturnTrace();
743 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000744}
tommi@webrtc.orgcde1e7f2011-11-15 12:23:36 +0000745
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000746} // namespace webrtc