blob: 5029f5ab6ea7267d82d6220928de0b77d9a24e92 [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
Peter Boströmff019b02015-04-30 14:16:07 +020018#include "webrtc/base/atomicops.h"
Tommidfafd122015-11-23 15:37:22 +010019#include "webrtc/base/platform_thread.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000020#ifdef _WIN32
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000021#include "webrtc/system_wrappers/source/trace_win.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000022#else
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000023#include "webrtc/system_wrappers/source/trace_posix.h"
24#endif // _WIN32
niklase@google.com470e71d2011-07-07 08:21:25 +000025
26#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;
pbos46ad5422015-12-07 14:29:14 -080037volatile int 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()
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +000068 : callback_(NULL),
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000069 row_count_text_(0),
70 file_count_text_(0),
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +000071 trace_file_(FileWrapper::Create()) {
niklase@google.com470e71d2011-07-07 08:21:25 +000072}
73
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000074TraceImpl::~TraceImpl() {
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +000075 trace_file_->Flush();
76 trace_file_->CloseFile();
niklase@google.com470e71d2011-07-07 08:21:25 +000077}
78
pbos@webrtc.org046deb92013-04-09 09:06:11 +000079int32_t TraceImpl::AddThreadId(char* trace_message) const {
Tommidfafd122015-11-23 15:37:22 +010080 uint32_t thread_id = rtc::CurrentThreadId();
pwestin@webrtc.orgb54d7272012-01-11 08:28:04 +000081 // Messages is 12 characters.
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000082 return sprintf(trace_message, "%10u; ", thread_id);
pwestin@webrtc.orgb54d7272012-01-11 08:28:04 +000083}
84
pbos@webrtc.org046deb92013-04-09 09:06:11 +000085int32_t TraceImpl::AddLevel(char* sz_message, const TraceLevel level) const {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000086 const int kMessageLength = 12;
87 switch (level) {
88 case kTraceTerseInfo:
89 // Add the appropriate amount of whitespace.
90 memset(sz_message, ' ', kMessageLength);
91 sz_message[kMessageLength] = '\0';
92 break;
93 case kTraceStateInfo:
94 sprintf(sz_message, "STATEINFO ; ");
95 break;
96 case kTraceWarning:
97 sprintf(sz_message, "WARNING ; ");
98 break;
99 case kTraceError:
100 sprintf(sz_message, "ERROR ; ");
101 break;
102 case kTraceCritical:
103 sprintf(sz_message, "CRITICAL ; ");
104 break;
105 case kTraceInfo:
106 sprintf(sz_message, "DEBUGINFO ; ");
107 break;
108 case kTraceModuleCall:
109 sprintf(sz_message, "MODULECALL; ");
110 break;
111 case kTraceMemory:
112 sprintf(sz_message, "MEMORY ; ");
113 break;
114 case kTraceTimer:
115 sprintf(sz_message, "TIMER ; ");
116 break;
117 case kTraceStream:
118 sprintf(sz_message, "STREAM ; ");
119 break;
120 case kTraceApiCall:
121 sprintf(sz_message, "APICALL ; ");
122 break;
123 case kTraceDebug:
124 sprintf(sz_message, "DEBUG ; ");
125 break;
126 default:
127 assert(false);
128 return 0;
129 }
130 // All messages are 12 characters.
131 return kMessageLength;
niklase@google.com470e71d2011-07-07 08:21:25 +0000132}
133
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000134int32_t TraceImpl::AddModuleAndId(char* trace_message,
135 const TraceModule module,
136 const int32_t id) const {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000137 // Use long int to prevent problems with different definitions of
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000138 // int32_t.
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000139 // TODO(hellner): is this actually a problem? If so, it should be better to
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000140 // clean up int32_t
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000141 const long int idl = id;
142 const int kMessageLength = 25;
143 if (idl != -1) {
144 const unsigned long int id_engine = id >> 16;
145 const unsigned long int id_channel = id & 0xffff;
niklase@google.com470e71d2011-07-07 08:21:25 +0000146
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000147 switch (module) {
148 case kTraceUndefined:
149 // Add the appropriate amount of whitespace.
150 memset(trace_message, ' ', kMessageLength);
151 trace_message[kMessageLength] = '\0';
152 break;
153 case kTraceVoice:
154 sprintf(trace_message, " VOICE:%5ld %5ld;", id_engine,
155 id_channel);
156 break;
157 case kTraceVideo:
158 sprintf(trace_message, " VIDEO:%5ld %5ld;", id_engine,
159 id_channel);
160 break;
161 case kTraceUtility:
162 sprintf(trace_message, " UTILITY:%5ld %5ld;", id_engine,
163 id_channel);
164 break;
165 case kTraceRtpRtcp:
166 sprintf(trace_message, " RTP/RTCP:%5ld %5ld;", id_engine,
167 id_channel);
168 break;
169 case kTraceTransport:
170 sprintf(trace_message, " TRANSPORT:%5ld %5ld;", id_engine,
171 id_channel);
172 break;
173 case kTraceAudioCoding:
174 sprintf(trace_message, "AUDIO CODING:%5ld %5ld;", id_engine,
175 id_channel);
176 break;
177 case kTraceSrtp:
178 sprintf(trace_message, " SRTP:%5ld %5ld;", id_engine,
179 id_channel);
180 break;
181 case kTraceAudioMixerServer:
182 sprintf(trace_message, " AUDIO MIX/S:%5ld %5ld;", id_engine,
183 id_channel);
184 break;
185 case kTraceAudioMixerClient:
186 sprintf(trace_message, " AUDIO MIX/C:%5ld %5ld;", id_engine,
187 id_channel);
188 break;
189 case kTraceVideoCoding:
190 sprintf(trace_message, "VIDEO CODING:%5ld %5ld;", id_engine,
191 id_channel);
192 break;
193 case kTraceVideoMixer:
194 // Print sleep time and API call
195 sprintf(trace_message, " VIDEO MIX:%5ld %5ld;", id_engine,
196 id_channel);
197 break;
198 case kTraceFile:
199 sprintf(trace_message, " FILE:%5ld %5ld;", id_engine,
200 id_channel);
201 break;
202 case kTraceAudioProcessing:
203 sprintf(trace_message, " AUDIO PROC:%5ld %5ld;", id_engine,
204 id_channel);
205 break;
206 case kTraceAudioDevice:
207 sprintf(trace_message, "AUDIO DEVICE:%5ld %5ld;", id_engine,
208 id_channel);
209 break;
210 case kTraceVideoRenderer:
211 sprintf(trace_message, "VIDEO RENDER:%5ld %5ld;", id_engine,
212 id_channel);
213 break;
214 case kTraceVideoCapture:
215 sprintf(trace_message, "VIDEO CAPTUR:%5ld %5ld;", id_engine,
216 id_channel);
217 break;
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000218 case kTraceRemoteBitrateEstimator:
219 sprintf(trace_message, " BWE RBE:%5ld %5ld;", id_engine,
220 id_channel);
221 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000222 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000223 } else {
224 switch (module) {
225 case kTraceUndefined:
226 // Add the appropriate amount of whitespace.
227 memset(trace_message, ' ', kMessageLength);
228 trace_message[kMessageLength] = '\0';
229 break;
230 case kTraceVoice:
231 sprintf(trace_message, " VOICE:%11ld;", idl);
232 break;
233 case kTraceVideo:
234 sprintf(trace_message, " VIDEO:%11ld;", idl);
235 break;
236 case kTraceUtility:
237 sprintf(trace_message, " UTILITY:%11ld;", idl);
238 break;
239 case kTraceRtpRtcp:
240 sprintf(trace_message, " RTP/RTCP:%11ld;", idl);
241 break;
242 case kTraceTransport:
243 sprintf(trace_message, " TRANSPORT:%11ld;", idl);
244 break;
245 case kTraceAudioCoding:
246 sprintf(trace_message, "AUDIO CODING:%11ld;", idl);
247 break;
248 case kTraceSrtp:
249 sprintf(trace_message, " SRTP:%11ld;", idl);
250 break;
251 case kTraceAudioMixerServer:
252 sprintf(trace_message, " AUDIO MIX/S:%11ld;", idl);
253 break;
254 case kTraceAudioMixerClient:
255 sprintf(trace_message, " AUDIO MIX/C:%11ld;", idl);
256 break;
257 case kTraceVideoCoding:
258 sprintf(trace_message, "VIDEO CODING:%11ld;", idl);
259 break;
260 case kTraceVideoMixer:
261 sprintf(trace_message, " VIDEO MIX:%11ld;", idl);
262 break;
263 case kTraceFile:
264 sprintf(trace_message, " FILE:%11ld;", idl);
265 break;
266 case kTraceAudioProcessing:
267 sprintf(trace_message, " AUDIO PROC:%11ld;", idl);
268 break;
269 case kTraceAudioDevice:
270 sprintf(trace_message, "AUDIO DEVICE:%11ld;", idl);
271 break;
272 case kTraceVideoRenderer:
273 sprintf(trace_message, "VIDEO RENDER:%11ld;", idl);
274 break;
275 case kTraceVideoCapture:
276 sprintf(trace_message, "VIDEO CAPTUR:%11ld;", idl);
277 break;
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000278 case kTraceRemoteBitrateEstimator:
279 sprintf(trace_message, " BWE RBE:%11ld;", idl);
280 break;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000281 }
282 }
283 return kMessageLength;
niklase@google.com470e71d2011-07-07 08:21:25 +0000284}
285
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000286int32_t TraceImpl::SetTraceFileImpl(const char* file_name_utf8,
287 const bool add_file_counter) {
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000288 rtc::CritScope lock(&crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000289
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000290 trace_file_->Flush();
291 trace_file_->CloseFile();
niklase@google.com470e71d2011-07-07 08:21:25 +0000292
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000293 if (file_name_utf8) {
294 if (add_file_counter) {
295 file_count_text_ = 1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000296
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000297 char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize];
298 CreateFileName(file_name_utf8, file_name_with_counter_utf8,
299 file_count_text_);
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000300 if (trace_file_->OpenFile(file_name_with_counter_utf8, false, false,
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000301 true) == -1) {
302 return -1;
303 }
304 } else {
305 file_count_text_ = 0;
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000306 if (trace_file_->OpenFile(file_name_utf8, false, false, true) == -1) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000307 return -1;
308 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000309 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000310 }
311 row_count_text_ = 0;
312 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000313}
314
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000315int32_t TraceImpl::TraceFileImpl(
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000316 char file_name_utf8[FileWrapper::kMaxFileNameSize]) {
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000317 rtc::CritScope lock(&crit_);
318 return trace_file_->FileName(file_name_utf8, FileWrapper::kMaxFileNameSize);
niklase@google.com470e71d2011-07-07 08:21:25 +0000319}
320
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000321int32_t TraceImpl::SetTraceCallbackImpl(TraceCallback* callback) {
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000322 rtc::CritScope lock(&crit_);
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000323 callback_ = callback;
324 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000325}
326
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000327int32_t TraceImpl::AddMessage(
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000328 char* trace_message,
niklase@google.com470e71d2011-07-07 08:21:25 +0000329 const char msg[WEBRTC_TRACE_MAX_MESSAGE_SIZE],
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000330 const uint16_t written_so_far) const {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000331 int length = 0;
332 if (written_so_far >= WEBRTC_TRACE_MAX_MESSAGE_SIZE) {
333 return -1;
334 }
335 // - 2 to leave room for newline and NULL termination.
niklase@google.com470e71d2011-07-07 08:21:25 +0000336#ifdef _WIN32
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000337 length = _snprintf(trace_message,
338 WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2,
339 "%s", msg);
340 if (length < 0) {
341 length = WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2;
342 trace_message[length] = 0;
343 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000344#else
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000345 length = snprintf(trace_message,
346 WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2,
347 "%s", msg);
348 if (length < 0 ||
349 length > WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2) {
350 length = WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2;
351 trace_message[length] = 0;
352 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000353#endif
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000354 // Length with NULL termination.
355 return length + 1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000356}
357
358void TraceImpl::AddMessageToList(
andrew@webrtc.orgc1ffd332013-03-22 17:13:23 +0000359 const char trace_message[WEBRTC_TRACE_MAX_MESSAGE_SIZE],
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000360 const uint16_t length,
andrew@webrtc.orgc1ffd332013-03-22 17:13:23 +0000361 const TraceLevel level) {
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000362 rtc::CritScope lock(&crit_);
363 if (callback_)
solenberg@webrtc.orgadb51f52013-06-10 09:03:41 +0000364 callback_->Print(level, trace_message, length);
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000365 WriteToFile(trace_message, length);
niklase@google.com470e71d2011-07-07 08:21:25 +0000366}
367
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000368void TraceImpl::WriteToFile(const char* msg, uint16_t length) {
369 if (!trace_file_->Open())
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000370 return;
niklase@google.com470e71d2011-07-07 08:21:25 +0000371
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000372 if (row_count_text_ > WEBRTC_TRACE_MAX_FILE_SIZE) {
373 // wrap file
374 row_count_text_ = 0;
375 trace_file_->Flush();
niklase@google.com470e71d2011-07-07 08:21:25 +0000376
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000377 if (file_count_text_ == 0) {
378 trace_file_->Rewind();
379 } else {
380 char old_file_name[FileWrapper::kMaxFileNameSize];
381 char new_file_name[FileWrapper::kMaxFileNameSize];
382
383 // get current name
384 trace_file_->FileName(old_file_name, FileWrapper::kMaxFileNameSize);
385 trace_file_->CloseFile();
386
387 file_count_text_++;
388
389 UpdateFileName(old_file_name, new_file_name, file_count_text_);
390
391 if (trace_file_->OpenFile(new_file_name, false, false, true) == -1) {
392 return;
393 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000394 }
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000395 }
396 if (row_count_text_ == 0) {
397 char message[WEBRTC_TRACE_MAX_MESSAGE_SIZE + 1];
398 int32_t length = AddDateTimeInfo(message);
399 if (length != -1) {
400 message[length] = 0;
401 message[length - 1] = '\n';
402 trace_file_->Write(message, length);
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000403 row_count_text_++;
404 }
405 }
Brave Yaoa4466092015-07-28 10:45:48 +0800406
407 char trace_message[WEBRTC_TRACE_MAX_MESSAGE_SIZE];
408 memcpy(trace_message, msg, length);
409 trace_message[length] = 0;
410 trace_message[length - 1] = '\n';
411 trace_file_->Write(trace_message, length);
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000412 row_count_text_++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000413}
414
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000415void TraceImpl::AddImpl(const TraceLevel level,
416 const TraceModule module,
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000417 const int32_t id,
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000418 const char msg[WEBRTC_TRACE_MAX_MESSAGE_SIZE]) {
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000419 if (!TraceCheck(level))
420 return;
niklase@google.com470e71d2011-07-07 08:21:25 +0000421
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000422 char trace_message[WEBRTC_TRACE_MAX_MESSAGE_SIZE];
423 char* message_ptr = &trace_message[0];
424 int32_t len = AddLevel(message_ptr, level);
425 if (len == -1)
426 return;
niklase@google.com470e71d2011-07-07 08:21:25 +0000427
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000428 message_ptr += len;
429 int32_t ack_len = len;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000430
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000431 len = AddTime(message_ptr, level);
432 if (len == -1)
433 return;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000434
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000435 message_ptr += len;
436 ack_len += len;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000437
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000438 len = AddModuleAndId(message_ptr, module, id);
439 if (len == -1)
440 return;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000441
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000442 message_ptr += len;
443 ack_len += len;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000444
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000445 len = AddThreadId(message_ptr);
446 if (len < 0)
447 return;
448
449 message_ptr += len;
450 ack_len += len;
451
452 len = AddMessage(message_ptr, msg, static_cast<uint16_t>(ack_len));
453 if (len == -1)
454 return;
455
456 ack_len += len;
457 AddMessageToList(trace_message, static_cast<uint16_t>(ack_len), level);
niklase@google.com470e71d2011-07-07 08:21:25 +0000458}
459
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000460bool TraceImpl::TraceCheck(const TraceLevel level) const {
andrew@webrtc.org90805182013-09-05 16:40:43 +0000461 return (level & level_filter()) ? true : false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000462}
463
464bool TraceImpl::UpdateFileName(
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000465 const char file_name_utf8[FileWrapper::kMaxFileNameSize],
466 char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize],
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000467 const uint32_t new_count) const {
468 int32_t length = (int32_t)strlen(file_name_utf8);
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000469 if (length < 0) {
470 return false;
471 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000472
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000473 int32_t length_without_file_ending = length - 1;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000474 while (length_without_file_ending > 0) {
475 if (file_name_utf8[length_without_file_ending] == '.') {
476 break;
477 } else {
478 length_without_file_ending--;
niklase@google.com470e71d2011-07-07 08:21:25 +0000479 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000480 }
481 if (length_without_file_ending == 0) {
482 length_without_file_ending = length;
483 }
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000484 int32_t length_to_ = length_without_file_ending - 1;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000485 while (length_to_ > 0) {
486 if (file_name_utf8[length_to_] == '_') {
487 break;
488 } else {
489 length_to_--;
niklase@google.com470e71d2011-07-07 08:21:25 +0000490 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000491 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000492
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000493 memcpy(file_name_with_counter_utf8, file_name_utf8, length_to_);
494 sprintf(file_name_with_counter_utf8 + length_to_, "_%lu%s",
495 static_cast<long unsigned int>(new_count),
496 file_name_utf8 + length_without_file_ending);
497 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000498}
499
500bool TraceImpl::CreateFileName(
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000501 const char file_name_utf8[FileWrapper::kMaxFileNameSize],
502 char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize],
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000503 const uint32_t new_count) const {
504 int32_t length = (int32_t)strlen(file_name_utf8);
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000505 if (length < 0) {
506 return false;
507 }
508
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000509 int32_t length_without_file_ending = length - 1;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000510 while (length_without_file_ending > 0) {
511 if (file_name_utf8[length_without_file_ending] == '.') {
512 break;
513 } else {
514 length_without_file_ending--;
niklase@google.com470e71d2011-07-07 08:21:25 +0000515 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000516 }
517 if (length_without_file_ending == 0) {
518 length_without_file_ending = length;
519 }
520 memcpy(file_name_with_counter_utf8, file_name_utf8,
521 length_without_file_ending);
522 sprintf(file_name_with_counter_utf8 + length_without_file_ending, "_%lu%s",
523 static_cast<long unsigned int>(new_count),
524 file_name_utf8 + length_without_file_ending);
525 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000526}
527
tommi@webrtc.orgc7157da2015-03-19 09:30:29 +0000528// static
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000529void Trace::CreateTrace() {
530 TraceImpl::StaticInstance(kAddRef);
tommi@webrtc.orgcde1e7f2011-11-15 12:23:36 +0000531}
532
tommi@webrtc.orgc7157da2015-03-19 09:30:29 +0000533// static
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000534void Trace::ReturnTrace() {
535 TraceImpl::StaticInstance(kRelease);
tommi@webrtc.orgcde1e7f2011-11-15 12:23:36 +0000536}
537
tommi@webrtc.orgc7157da2015-03-19 09:30:29 +0000538// static
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000539int32_t Trace::TraceFile(char file_name[FileWrapper::kMaxFileNameSize]) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000540 TraceImpl* trace = TraceImpl::GetTrace();
541 if (trace) {
542 int ret_val = trace->TraceFileImpl(file_name);
543 ReturnTrace();
544 return ret_val;
545 }
546 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000547}
548
tommi@webrtc.orgc7157da2015-03-19 09:30:29 +0000549// static
550void Trace::set_level_filter(int filter) {
pbos46ad5422015-12-07 14:29:14 -0800551 rtc::AtomicOps::ReleaseStore(&level_filter_, filter);
tommi@webrtc.orgc7157da2015-03-19 09:30:29 +0000552}
553
554// static
555int Trace::level_filter() {
pbos46ad5422015-12-07 14:29:14 -0800556 return rtc::AtomicOps::AcquireLoad(&level_filter_);
tommi@webrtc.orgc7157da2015-03-19 09:30:29 +0000557}
558
559// static
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000560int32_t Trace::SetTraceFile(const char* file_name,
561 const bool add_file_counter) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000562 TraceImpl* trace = TraceImpl::GetTrace();
563 if (trace) {
564 int ret_val = trace->SetTraceFileImpl(file_name, add_file_counter);
565 ReturnTrace();
566 return ret_val;
567 }
568 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000569}
570
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000571int32_t Trace::SetTraceCallback(TraceCallback* callback) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000572 TraceImpl* trace = TraceImpl::GetTrace();
573 if (trace) {
574 int ret_val = trace->SetTraceCallbackImpl(callback);
575 ReturnTrace();
576 return ret_val;
577 }
578 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000579}
580
581void Trace::Add(const TraceLevel level, const TraceModule module,
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000582 const int32_t id, const char* msg, ...) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000583 TraceImpl* trace = TraceImpl::GetTrace(level);
584 if (trace) {
585 if (trace->TraceCheck(level)) {
586 char temp_buff[WEBRTC_TRACE_MAX_MESSAGE_SIZE];
587 char* buff = 0;
588 if (msg) {
589 va_list args;
590 va_start(args, msg);
niklase@google.com470e71d2011-07-07 08:21:25 +0000591#ifdef _WIN32
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000592 _vsnprintf(temp_buff, WEBRTC_TRACE_MAX_MESSAGE_SIZE - 1, msg, args);
niklase@google.com470e71d2011-07-07 08:21:25 +0000593#else
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000594 vsnprintf(temp_buff, WEBRTC_TRACE_MAX_MESSAGE_SIZE - 1, msg, args);
niklase@google.com470e71d2011-07-07 08:21:25 +0000595#endif
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000596 va_end(args);
597 buff = temp_buff;
598 }
599 trace->AddImpl(level, module, id, buff);
niklase@google.com470e71d2011-07-07 08:21:25 +0000600 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000601 ReturnTrace();
602 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000603}
tommi@webrtc.orgcde1e7f2011-11-15 12:23:36 +0000604
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000605} // namespace webrtc