blob: c1ec984f6d30d234236934438e4d2eced7843edc [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/atomicops.h"
19#include "rtc_base/platform_thread.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000020#ifdef _WIN32
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "system_wrappers/source/trace_win.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000022#else
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "system_wrappers/source/trace_posix.h"
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000024#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_->CloseFile();
niklase@google.com470e71d2011-07-07 08:21:25 +000076}
77
pbos@webrtc.org046deb92013-04-09 09:06:11 +000078int32_t TraceImpl::AddThreadId(char* trace_message) const {
Tommidfafd122015-11-23 15:37:22 +010079 uint32_t thread_id = rtc::CurrentThreadId();
pwestin@webrtc.orgb54d7272012-01-11 08:28:04 +000080 // Messages is 12 characters.
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000081 return sprintf(trace_message, "%10u; ", thread_id);
pwestin@webrtc.orgb54d7272012-01-11 08:28:04 +000082}
83
pbos@webrtc.org046deb92013-04-09 09:06:11 +000084int32_t TraceImpl::AddLevel(char* sz_message, const TraceLevel level) const {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000085 const int kMessageLength = 12;
86 switch (level) {
87 case kTraceTerseInfo:
88 // Add the appropriate amount of whitespace.
89 memset(sz_message, ' ', kMessageLength);
90 sz_message[kMessageLength] = '\0';
91 break;
92 case kTraceStateInfo:
93 sprintf(sz_message, "STATEINFO ; ");
94 break;
95 case kTraceWarning:
96 sprintf(sz_message, "WARNING ; ");
97 break;
98 case kTraceError:
99 sprintf(sz_message, "ERROR ; ");
100 break;
101 case kTraceCritical:
102 sprintf(sz_message, "CRITICAL ; ");
103 break;
104 case kTraceInfo:
105 sprintf(sz_message, "DEBUGINFO ; ");
106 break;
107 case kTraceModuleCall:
108 sprintf(sz_message, "MODULECALL; ");
109 break;
110 case kTraceMemory:
111 sprintf(sz_message, "MEMORY ; ");
112 break;
113 case kTraceTimer:
114 sprintf(sz_message, "TIMER ; ");
115 break;
116 case kTraceStream:
117 sprintf(sz_message, "STREAM ; ");
118 break;
119 case kTraceApiCall:
120 sprintf(sz_message, "APICALL ; ");
121 break;
122 case kTraceDebug:
123 sprintf(sz_message, "DEBUG ; ");
124 break;
125 default:
126 assert(false);
127 return 0;
128 }
129 // All messages are 12 characters.
130 return kMessageLength;
niklase@google.com470e71d2011-07-07 08:21:25 +0000131}
132
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000133int32_t TraceImpl::AddModuleAndId(char* trace_message,
134 const TraceModule module,
135 const int32_t id) const {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000136 // Use long int to prevent problems with different definitions of
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000137 // int32_t.
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000138 // TODO(hellner): is this actually a problem? If so, it should be better to
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000139 // clean up int32_t
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000140 const long int idl = id;
141 const int kMessageLength = 25;
142 if (idl != -1) {
143 const unsigned long int id_engine = id >> 16;
144 const unsigned long int id_channel = id & 0xffff;
niklase@google.com470e71d2011-07-07 08:21:25 +0000145
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000146 switch (module) {
147 case kTraceUndefined:
148 // Add the appropriate amount of whitespace.
149 memset(trace_message, ' ', kMessageLength);
150 trace_message[kMessageLength] = '\0';
151 break;
152 case kTraceVoice:
153 sprintf(trace_message, " VOICE:%5ld %5ld;", id_engine,
154 id_channel);
155 break;
156 case kTraceVideo:
157 sprintf(trace_message, " VIDEO:%5ld %5ld;", id_engine,
158 id_channel);
159 break;
160 case kTraceUtility:
161 sprintf(trace_message, " UTILITY:%5ld %5ld;", id_engine,
162 id_channel);
163 break;
164 case kTraceRtpRtcp:
165 sprintf(trace_message, " RTP/RTCP:%5ld %5ld;", id_engine,
166 id_channel);
167 break;
168 case kTraceTransport:
169 sprintf(trace_message, " TRANSPORT:%5ld %5ld;", id_engine,
170 id_channel);
171 break;
172 case kTraceAudioCoding:
173 sprintf(trace_message, "AUDIO CODING:%5ld %5ld;", id_engine,
174 id_channel);
175 break;
176 case kTraceSrtp:
177 sprintf(trace_message, " SRTP:%5ld %5ld;", id_engine,
178 id_channel);
179 break;
180 case kTraceAudioMixerServer:
181 sprintf(trace_message, " AUDIO MIX/S:%5ld %5ld;", id_engine,
182 id_channel);
183 break;
184 case kTraceAudioMixerClient:
185 sprintf(trace_message, " AUDIO MIX/C:%5ld %5ld;", id_engine,
186 id_channel);
187 break;
188 case kTraceVideoCoding:
189 sprintf(trace_message, "VIDEO CODING:%5ld %5ld;", id_engine,
190 id_channel);
191 break;
192 case kTraceVideoMixer:
193 // Print sleep time and API call
194 sprintf(trace_message, " VIDEO MIX:%5ld %5ld;", id_engine,
195 id_channel);
196 break;
197 case kTraceFile:
198 sprintf(trace_message, " FILE:%5ld %5ld;", id_engine,
199 id_channel);
200 break;
201 case kTraceAudioProcessing:
202 sprintf(trace_message, " AUDIO PROC:%5ld %5ld;", id_engine,
203 id_channel);
204 break;
205 case kTraceAudioDevice:
206 sprintf(trace_message, "AUDIO DEVICE:%5ld %5ld;", id_engine,
207 id_channel);
208 break;
209 case kTraceVideoRenderer:
210 sprintf(trace_message, "VIDEO RENDER:%5ld %5ld;", id_engine,
211 id_channel);
212 break;
213 case kTraceVideoCapture:
214 sprintf(trace_message, "VIDEO CAPTUR:%5ld %5ld;", id_engine,
215 id_channel);
216 break;
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000217 case kTraceRemoteBitrateEstimator:
218 sprintf(trace_message, " BWE RBE:%5ld %5ld;", id_engine,
219 id_channel);
220 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000221 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000222 } else {
223 switch (module) {
224 case kTraceUndefined:
225 // Add the appropriate amount of whitespace.
226 memset(trace_message, ' ', kMessageLength);
227 trace_message[kMessageLength] = '\0';
228 break;
229 case kTraceVoice:
230 sprintf(trace_message, " VOICE:%11ld;", idl);
231 break;
232 case kTraceVideo:
233 sprintf(trace_message, " VIDEO:%11ld;", idl);
234 break;
235 case kTraceUtility:
236 sprintf(trace_message, " UTILITY:%11ld;", idl);
237 break;
238 case kTraceRtpRtcp:
239 sprintf(trace_message, " RTP/RTCP:%11ld;", idl);
240 break;
241 case kTraceTransport:
242 sprintf(trace_message, " TRANSPORT:%11ld;", idl);
243 break;
244 case kTraceAudioCoding:
245 sprintf(trace_message, "AUDIO CODING:%11ld;", idl);
246 break;
247 case kTraceSrtp:
248 sprintf(trace_message, " SRTP:%11ld;", idl);
249 break;
250 case kTraceAudioMixerServer:
251 sprintf(trace_message, " AUDIO MIX/S:%11ld;", idl);
252 break;
253 case kTraceAudioMixerClient:
254 sprintf(trace_message, " AUDIO MIX/C:%11ld;", idl);
255 break;
256 case kTraceVideoCoding:
257 sprintf(trace_message, "VIDEO CODING:%11ld;", idl);
258 break;
259 case kTraceVideoMixer:
260 sprintf(trace_message, " VIDEO MIX:%11ld;", idl);
261 break;
262 case kTraceFile:
263 sprintf(trace_message, " FILE:%11ld;", idl);
264 break;
265 case kTraceAudioProcessing:
266 sprintf(trace_message, " AUDIO PROC:%11ld;", idl);
267 break;
268 case kTraceAudioDevice:
269 sprintf(trace_message, "AUDIO DEVICE:%11ld;", idl);
270 break;
271 case kTraceVideoRenderer:
272 sprintf(trace_message, "VIDEO RENDER:%11ld;", idl);
273 break;
274 case kTraceVideoCapture:
275 sprintf(trace_message, "VIDEO CAPTUR:%11ld;", idl);
276 break;
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000277 case kTraceRemoteBitrateEstimator:
278 sprintf(trace_message, " BWE RBE:%11ld;", idl);
279 break;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000280 }
281 }
282 return kMessageLength;
niklase@google.com470e71d2011-07-07 08:21:25 +0000283}
284
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000285int32_t TraceImpl::SetTraceFileImpl(const char* file_name_utf8,
286 const bool add_file_counter) {
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000287 rtc::CritScope lock(&crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000288
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000289 trace_file_->CloseFile();
tommia6219cc2016-06-15 10:30:14 -0700290 trace_file_path_.clear();
niklase@google.com470e71d2011-07-07 08:21:25 +0000291
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000292 if (file_name_utf8) {
293 if (add_file_counter) {
294 file_count_text_ = 1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000295
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000296 char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize];
297 CreateFileName(file_name_utf8, file_name_with_counter_utf8,
298 file_count_text_);
tommia6219cc2016-06-15 10:30:14 -0700299 if (!trace_file_->OpenFile(file_name_with_counter_utf8, false)) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000300 return -1;
301 }
tommia6219cc2016-06-15 10:30:14 -0700302 trace_file_path_ = file_name_with_counter_utf8;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000303 } else {
304 file_count_text_ = 0;
tommia6219cc2016-06-15 10:30:14 -0700305 if (!trace_file_->OpenFile(file_name_utf8, false)) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000306 return -1;
307 }
tommia6219cc2016-06-15 10:30:14 -0700308 trace_file_path_ = file_name_utf8;
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::SetTraceCallbackImpl(TraceCallback* callback) {
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000316 rtc::CritScope lock(&crit_);
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000317 callback_ = callback;
318 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000319}
320
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000321int32_t TraceImpl::AddMessage(
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000322 char* trace_message,
niklase@google.com470e71d2011-07-07 08:21:25 +0000323 const char msg[WEBRTC_TRACE_MAX_MESSAGE_SIZE],
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000324 const uint16_t written_so_far) const {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000325 int length = 0;
326 if (written_so_far >= WEBRTC_TRACE_MAX_MESSAGE_SIZE) {
327 return -1;
328 }
329 // - 2 to leave room for newline and NULL termination.
niklase@google.com470e71d2011-07-07 08:21:25 +0000330#ifdef _WIN32
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000331 length = _snprintf(trace_message,
332 WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2,
333 "%s", msg);
334 if (length < 0) {
335 length = WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2;
336 trace_message[length] = 0;
337 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000338#else
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000339 length = snprintf(trace_message,
340 WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2,
341 "%s", msg);
342 if (length < 0 ||
343 length > WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2) {
344 length = WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2;
345 trace_message[length] = 0;
346 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000347#endif
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000348 // Length with NULL termination.
349 return length + 1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000350}
351
352void TraceImpl::AddMessageToList(
andrew@webrtc.orgc1ffd332013-03-22 17:13:23 +0000353 const char trace_message[WEBRTC_TRACE_MAX_MESSAGE_SIZE],
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000354 const uint16_t length,
andrew@webrtc.orgc1ffd332013-03-22 17:13:23 +0000355 const TraceLevel level) {
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000356 rtc::CritScope lock(&crit_);
357 if (callback_)
solenberg@webrtc.orgadb51f52013-06-10 09:03:41 +0000358 callback_->Print(level, trace_message, length);
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000359 WriteToFile(trace_message, length);
niklase@google.com470e71d2011-07-07 08:21:25 +0000360}
361
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000362void TraceImpl::WriteToFile(const char* msg, uint16_t length) {
tommia6219cc2016-06-15 10:30:14 -0700363 if (!trace_file_->is_open())
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000364 return;
niklase@google.com470e71d2011-07-07 08:21:25 +0000365
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000366 if (row_count_text_ > WEBRTC_TRACE_MAX_FILE_SIZE) {
367 // wrap file
368 row_count_text_ = 0;
369 trace_file_->Flush();
niklase@google.com470e71d2011-07-07 08:21:25 +0000370
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000371 if (file_count_text_ == 0) {
372 trace_file_->Rewind();
373 } else {
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000374 char new_file_name[FileWrapper::kMaxFileNameSize];
375
376 // get current name
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000377 file_count_text_++;
tommia6219cc2016-06-15 10:30:14 -0700378 UpdateFileName(new_file_name, file_count_text_);
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000379
tommia6219cc2016-06-15 10:30:14 -0700380 trace_file_->CloseFile();
381 trace_file_path_.clear();
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000382
tommia6219cc2016-06-15 10:30:14 -0700383 if (!trace_file_->OpenFile(new_file_name, false)) {
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000384 return;
385 }
tommia6219cc2016-06-15 10:30:14 -0700386 trace_file_path_ = new_file_name;
niklase@google.com470e71d2011-07-07 08:21:25 +0000387 }
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000388 }
389 if (row_count_text_ == 0) {
390 char message[WEBRTC_TRACE_MAX_MESSAGE_SIZE + 1];
391 int32_t length = AddDateTimeInfo(message);
392 if (length != -1) {
393 message[length] = 0;
394 message[length - 1] = '\n';
395 trace_file_->Write(message, length);
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000396 row_count_text_++;
397 }
398 }
Brave Yaoa4466092015-07-28 10:45:48 +0800399
400 char trace_message[WEBRTC_TRACE_MAX_MESSAGE_SIZE];
401 memcpy(trace_message, msg, length);
402 trace_message[length] = 0;
403 trace_message[length - 1] = '\n';
404 trace_file_->Write(trace_message, length);
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000405 row_count_text_++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000406}
407
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000408void TraceImpl::AddImpl(const TraceLevel level,
409 const TraceModule module,
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000410 const int32_t id,
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000411 const char msg[WEBRTC_TRACE_MAX_MESSAGE_SIZE]) {
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000412 if (!TraceCheck(level))
413 return;
niklase@google.com470e71d2011-07-07 08:21:25 +0000414
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000415 char trace_message[WEBRTC_TRACE_MAX_MESSAGE_SIZE];
416 char* message_ptr = &trace_message[0];
417 int32_t len = AddLevel(message_ptr, level);
418 if (len == -1)
419 return;
niklase@google.com470e71d2011-07-07 08:21:25 +0000420
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000421 message_ptr += len;
422 int32_t ack_len = len;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000423
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000424 len = AddTime(message_ptr, level);
425 if (len == -1)
426 return;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000427
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000428 message_ptr += len;
429 ack_len += len;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000430
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000431 len = AddModuleAndId(message_ptr, module, id);
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 = AddThreadId(message_ptr);
439 if (len < 0)
440 return;
441
442 message_ptr += len;
443 ack_len += len;
444
445 len = AddMessage(message_ptr, msg, static_cast<uint16_t>(ack_len));
446 if (len == -1)
447 return;
448
449 ack_len += len;
450 AddMessageToList(trace_message, static_cast<uint16_t>(ack_len), level);
niklase@google.com470e71d2011-07-07 08:21:25 +0000451}
452
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000453bool TraceImpl::TraceCheck(const TraceLevel level) const {
andrew@webrtc.org90805182013-09-05 16:40:43 +0000454 return (level & level_filter()) ? true : false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000455}
456
457bool TraceImpl::UpdateFileName(
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000458 char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize],
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000459 const uint32_t new_count) const {
tommia6219cc2016-06-15 10:30:14 -0700460 int32_t length = static_cast<int32_t>(trace_file_path_.length());
niklase@google.com470e71d2011-07-07 08:21:25 +0000461
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000462 int32_t length_without_file_ending = length - 1;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000463 while (length_without_file_ending > 0) {
tommia6219cc2016-06-15 10:30:14 -0700464 if (trace_file_path_[length_without_file_ending] == '.') {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000465 break;
466 } else {
467 length_without_file_ending--;
niklase@google.com470e71d2011-07-07 08:21:25 +0000468 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000469 }
470 if (length_without_file_ending == 0) {
471 length_without_file_ending = length;
472 }
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000473 int32_t length_to_ = length_without_file_ending - 1;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000474 while (length_to_ > 0) {
tommia6219cc2016-06-15 10:30:14 -0700475 if (trace_file_path_[length_to_] == '_') {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000476 break;
477 } else {
478 length_to_--;
niklase@google.com470e71d2011-07-07 08:21:25 +0000479 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000480 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000481
tommia6219cc2016-06-15 10:30:14 -0700482 memcpy(file_name_with_counter_utf8, &trace_file_path_[0], length_to_);
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000483 sprintf(file_name_with_counter_utf8 + length_to_, "_%lu%s",
484 static_cast<long unsigned int>(new_count),
tommia6219cc2016-06-15 10:30:14 -0700485 &trace_file_path_[length_without_file_ending]);
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000486 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000487}
488
489bool TraceImpl::CreateFileName(
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000490 const char file_name_utf8[FileWrapper::kMaxFileNameSize],
491 char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize],
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000492 const uint32_t new_count) const {
493 int32_t length = (int32_t)strlen(file_name_utf8);
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000494 if (length < 0) {
495 return false;
496 }
497
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000498 int32_t length_without_file_ending = length - 1;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000499 while (length_without_file_ending > 0) {
500 if (file_name_utf8[length_without_file_ending] == '.') {
501 break;
502 } else {
503 length_without_file_ending--;
niklase@google.com470e71d2011-07-07 08:21:25 +0000504 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000505 }
506 if (length_without_file_ending == 0) {
507 length_without_file_ending = length;
508 }
509 memcpy(file_name_with_counter_utf8, file_name_utf8,
510 length_without_file_ending);
511 sprintf(file_name_with_counter_utf8 + length_without_file_ending, "_%lu%s",
512 static_cast<long unsigned int>(new_count),
513 file_name_utf8 + length_without_file_ending);
514 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000515}
516
tommi@webrtc.orgc7157da2015-03-19 09:30:29 +0000517// static
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000518void Trace::CreateTrace() {
519 TraceImpl::StaticInstance(kAddRef);
tommi@webrtc.orgcde1e7f2011-11-15 12:23:36 +0000520}
521
tommi@webrtc.orgc7157da2015-03-19 09:30:29 +0000522// static
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000523void Trace::ReturnTrace() {
524 TraceImpl::StaticInstance(kRelease);
tommi@webrtc.orgcde1e7f2011-11-15 12:23:36 +0000525}
526
tommi@webrtc.orgc7157da2015-03-19 09:30:29 +0000527// static
tommi@webrtc.orgc7157da2015-03-19 09:30:29 +0000528void Trace::set_level_filter(int filter) {
pbos46ad5422015-12-07 14:29:14 -0800529 rtc::AtomicOps::ReleaseStore(&level_filter_, filter);
tommi@webrtc.orgc7157da2015-03-19 09:30:29 +0000530}
531
532// static
533int Trace::level_filter() {
pbos46ad5422015-12-07 14:29:14 -0800534 return rtc::AtomicOps::AcquireLoad(&level_filter_);
tommi@webrtc.orgc7157da2015-03-19 09:30:29 +0000535}
536
537// static
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000538int32_t Trace::SetTraceFile(const char* file_name,
539 const bool add_file_counter) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000540 TraceImpl* trace = TraceImpl::GetTrace();
541 if (trace) {
542 int ret_val = trace->SetTraceFileImpl(file_name, add_file_counter);
543 ReturnTrace();
544 return ret_val;
545 }
546 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000547}
548
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000549int32_t Trace::SetTraceCallback(TraceCallback* callback) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000550 TraceImpl* trace = TraceImpl::GetTrace();
551 if (trace) {
552 int ret_val = trace->SetTraceCallbackImpl(callback);
553 ReturnTrace();
554 return ret_val;
555 }
556 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000557}
558
559void Trace::Add(const TraceLevel level, const TraceModule module,
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000560 const int32_t id, const char* msg, ...) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000561 TraceImpl* trace = TraceImpl::GetTrace(level);
562 if (trace) {
563 if (trace->TraceCheck(level)) {
564 char temp_buff[WEBRTC_TRACE_MAX_MESSAGE_SIZE];
565 char* buff = 0;
566 if (msg) {
567 va_list args;
568 va_start(args, msg);
niklase@google.com470e71d2011-07-07 08:21:25 +0000569#ifdef _WIN32
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000570 _vsnprintf(temp_buff, WEBRTC_TRACE_MAX_MESSAGE_SIZE - 1, msg, args);
niklase@google.com470e71d2011-07-07 08:21:25 +0000571#else
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000572 vsnprintf(temp_buff, WEBRTC_TRACE_MAX_MESSAGE_SIZE - 1, msg, args);
niklase@google.com470e71d2011-07-07 08:21:25 +0000573#endif
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000574 va_end(args);
575 buff = temp_buff;
576 }
577 trace->AddImpl(level, module, id, buff);
niklase@google.com470e71d2011-07-07 08:21:25 +0000578 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000579 ReturnTrace();
580 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000581}
tommi@webrtc.orgcde1e7f2011-11-15 12:23:36 +0000582
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000583} // namespace webrtc