blob: ffe79b9862e151a3227a2ca38bbf70967b917d9a [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"
niklase@google.com470e71d2011-07-07 08:21:25 +000019#ifdef _WIN32
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000020#include "webrtc/system_wrappers/source/trace_win.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000021#else
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000022#include "webrtc/system_wrappers/source/trace_posix.h"
23#endif // _WIN32
niklase@google.com470e71d2011-07-07 08:21:25 +000024
25#define KEY_LEN_CHARS 31
26
27#ifdef _WIN32
andrew@webrtc.org5dffebc2012-08-16 04:24:05 +000028#pragma warning(disable:4355)
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000029#endif // _WIN32
niklase@google.com470e71d2011-07-07 08:21:25 +000030
31namespace webrtc {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000032
andrew@webrtc.orgc1ffd332013-03-22 17:13:23 +000033const int Trace::kBoilerplateLength = 71;
34const int Trace::kTimestampPosition = 13;
35const int Trace::kTimestampLength = 12;
tommi@webrtc.orgc7157da2015-03-19 09:30:29 +000036volatile int Trace::level_filter_ = kTraceDefault;
niklase@google.com470e71d2011-07-07 08:21:25 +000037
38// Construct On First Use idiom. Avoids "static initialization order fiasco".
henrike@webrtc.org315282c2011-12-09 17:46:20 +000039TraceImpl* TraceImpl::StaticInstance(CountOperation count_operation,
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000040 const TraceLevel level) {
41 // Sanities to avoid taking lock unless absolutely necessary (for
42 // performance reasons). count_operation == kAddRefNoCreate implies that a
43 // message will be written to file.
44 if ((level != kTraceAll) && (count_operation == kAddRefNoCreate)) {
andrew@webrtc.org90805182013-09-05 16:40:43 +000045 if (!(level & level_filter())) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000046 return NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +000047 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000048 }
49 TraceImpl* impl =
50 GetStaticInstance<TraceImpl>(count_operation);
51 return impl;
niklase@google.com470e71d2011-07-07 08:21:25 +000052}
53
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000054TraceImpl* TraceImpl::GetTrace(const TraceLevel level) {
55 return StaticInstance(kAddRefNoCreate, level);
niklase@google.com470e71d2011-07-07 08:21:25 +000056}
57
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000058TraceImpl* TraceImpl::CreateInstance() {
henrike@webrtc.org2f47b5a2011-12-10 00:44:47 +000059#if defined(_WIN32)
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000060 return new TraceWindows();
niklase@google.com470e71d2011-07-07 08:21:25 +000061#else
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000062 return new TracePosix();
niklase@google.com470e71d2011-07-07 08:21:25 +000063#endif
64}
65
66TraceImpl::TraceImpl()
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +000067 : callback_(NULL),
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000068 row_count_text_(0),
69 file_count_text_(0),
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +000070 trace_file_(FileWrapper::Create()) {
niklase@google.com470e71d2011-07-07 08:21:25 +000071}
72
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +000073TraceImpl::~TraceImpl() {
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +000074 trace_file_->Flush();
75 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 {
79 uint32_t thread_id = ThreadWrapper::GetThreadId();
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_->Flush();
290 trace_file_->CloseFile();
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_);
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000299 if (trace_file_->OpenFile(file_name_with_counter_utf8, false, false,
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000300 true) == -1) {
301 return -1;
302 }
303 } else {
304 file_count_text_ = 0;
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000305 if (trace_file_->OpenFile(file_name_utf8, false, false, true) == -1) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000306 return -1;
307 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000308 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000309 }
310 row_count_text_ = 0;
311 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000312}
313
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000314int32_t TraceImpl::TraceFileImpl(
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000315 char file_name_utf8[FileWrapper::kMaxFileNameSize]) {
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000316 rtc::CritScope lock(&crit_);
317 return trace_file_->FileName(file_name_utf8, FileWrapper::kMaxFileNameSize);
niklase@google.com470e71d2011-07-07 08:21:25 +0000318}
319
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000320int32_t TraceImpl::SetTraceCallbackImpl(TraceCallback* callback) {
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000321 rtc::CritScope lock(&crit_);
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000322 callback_ = callback;
323 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000324}
325
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000326int32_t TraceImpl::AddMessage(
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000327 char* trace_message,
niklase@google.com470e71d2011-07-07 08:21:25 +0000328 const char msg[WEBRTC_TRACE_MAX_MESSAGE_SIZE],
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000329 const uint16_t written_so_far) const {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000330 int length = 0;
331 if (written_so_far >= WEBRTC_TRACE_MAX_MESSAGE_SIZE) {
332 return -1;
333 }
334 // - 2 to leave room for newline and NULL termination.
niklase@google.com470e71d2011-07-07 08:21:25 +0000335#ifdef _WIN32
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000336 length = _snprintf(trace_message,
337 WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2,
338 "%s", msg);
339 if (length < 0) {
340 length = WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2;
341 trace_message[length] = 0;
342 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000343#else
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000344 length = snprintf(trace_message,
345 WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2,
346 "%s", msg);
347 if (length < 0 ||
348 length > WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2) {
349 length = WEBRTC_TRACE_MAX_MESSAGE_SIZE - written_so_far - 2;
350 trace_message[length] = 0;
351 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000352#endif
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000353 // Length with NULL termination.
354 return length + 1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000355}
356
357void TraceImpl::AddMessageToList(
andrew@webrtc.orgc1ffd332013-03-22 17:13:23 +0000358 const char trace_message[WEBRTC_TRACE_MAX_MESSAGE_SIZE],
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000359 const uint16_t length,
andrew@webrtc.orgc1ffd332013-03-22 17:13:23 +0000360 const TraceLevel level) {
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000361 rtc::CritScope lock(&crit_);
362 if (callback_)
solenberg@webrtc.orgadb51f52013-06-10 09:03:41 +0000363 callback_->Print(level, trace_message, length);
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000364 WriteToFile(trace_message, length);
niklase@google.com470e71d2011-07-07 08:21:25 +0000365}
366
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000367void TraceImpl::WriteToFile(const char* msg, uint16_t length) {
368 if (!trace_file_->Open())
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000369 return;
niklase@google.com470e71d2011-07-07 08:21:25 +0000370
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000371 if (row_count_text_ > WEBRTC_TRACE_MAX_FILE_SIZE) {
372 // wrap file
373 row_count_text_ = 0;
374 trace_file_->Flush();
niklase@google.com470e71d2011-07-07 08:21:25 +0000375
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000376 if (file_count_text_ == 0) {
377 trace_file_->Rewind();
378 } else {
379 char old_file_name[FileWrapper::kMaxFileNameSize];
380 char new_file_name[FileWrapper::kMaxFileNameSize];
381
382 // get current name
383 trace_file_->FileName(old_file_name, FileWrapper::kMaxFileNameSize);
384 trace_file_->CloseFile();
385
386 file_count_text_++;
387
388 UpdateFileName(old_file_name, new_file_name, file_count_text_);
389
390 if (trace_file_->OpenFile(new_file_name, false, false, true) == -1) {
391 return;
392 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000393 }
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000394 }
395 if (row_count_text_ == 0) {
396 char message[WEBRTC_TRACE_MAX_MESSAGE_SIZE + 1];
397 int32_t length = AddDateTimeInfo(message);
398 if (length != -1) {
399 message[length] = 0;
400 message[length - 1] = '\n';
401 trace_file_->Write(message, length);
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000402 row_count_text_++;
403 }
404 }
Brave Yaoa4466092015-07-28 10:45:48 +0800405
406 char trace_message[WEBRTC_TRACE_MAX_MESSAGE_SIZE];
407 memcpy(trace_message, msg, length);
408 trace_message[length] = 0;
409 trace_message[length - 1] = '\n';
410 trace_file_->Write(trace_message, length);
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000411 row_count_text_++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000412}
413
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000414void TraceImpl::AddImpl(const TraceLevel level,
415 const TraceModule module,
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000416 const int32_t id,
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000417 const char msg[WEBRTC_TRACE_MAX_MESSAGE_SIZE]) {
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000418 if (!TraceCheck(level))
419 return;
niklase@google.com470e71d2011-07-07 08:21:25 +0000420
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000421 char trace_message[WEBRTC_TRACE_MAX_MESSAGE_SIZE];
422 char* message_ptr = &trace_message[0];
423 int32_t len = AddLevel(message_ptr, level);
424 if (len == -1)
425 return;
niklase@google.com470e71d2011-07-07 08:21:25 +0000426
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000427 message_ptr += len;
428 int32_t ack_len = len;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000429
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000430 len = AddTime(message_ptr, level);
431 if (len == -1)
432 return;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000433
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000434 message_ptr += len;
435 ack_len += len;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000436
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000437 len = AddModuleAndId(message_ptr, module, id);
438 if (len == -1)
439 return;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000440
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000441 message_ptr += len;
442 ack_len += len;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000443
pbos@webrtc.org4b3618c2015-02-27 15:05:03 +0000444 len = AddThreadId(message_ptr);
445 if (len < 0)
446 return;
447
448 message_ptr += len;
449 ack_len += len;
450
451 len = AddMessage(message_ptr, msg, static_cast<uint16_t>(ack_len));
452 if (len == -1)
453 return;
454
455 ack_len += len;
456 AddMessageToList(trace_message, static_cast<uint16_t>(ack_len), level);
niklase@google.com470e71d2011-07-07 08:21:25 +0000457}
458
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000459bool TraceImpl::TraceCheck(const TraceLevel level) const {
andrew@webrtc.org90805182013-09-05 16:40:43 +0000460 return (level & level_filter()) ? true : false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000461}
462
463bool TraceImpl::UpdateFileName(
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000464 const char file_name_utf8[FileWrapper::kMaxFileNameSize],
465 char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize],
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000466 const uint32_t new_count) const {
467 int32_t length = (int32_t)strlen(file_name_utf8);
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000468 if (length < 0) {
469 return false;
470 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000471
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000472 int32_t length_without_file_ending = length - 1;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000473 while (length_without_file_ending > 0) {
474 if (file_name_utf8[length_without_file_ending] == '.') {
475 break;
476 } else {
477 length_without_file_ending--;
niklase@google.com470e71d2011-07-07 08:21:25 +0000478 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000479 }
480 if (length_without_file_ending == 0) {
481 length_without_file_ending = length;
482 }
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000483 int32_t length_to_ = length_without_file_ending - 1;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000484 while (length_to_ > 0) {
485 if (file_name_utf8[length_to_] == '_') {
486 break;
487 } else {
488 length_to_--;
niklase@google.com470e71d2011-07-07 08:21:25 +0000489 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000490 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000491
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000492 memcpy(file_name_with_counter_utf8, file_name_utf8, length_to_);
493 sprintf(file_name_with_counter_utf8 + length_to_, "_%lu%s",
494 static_cast<long unsigned int>(new_count),
495 file_name_utf8 + length_without_file_ending);
496 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000497}
498
499bool TraceImpl::CreateFileName(
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000500 const char file_name_utf8[FileWrapper::kMaxFileNameSize],
501 char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize],
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000502 const uint32_t new_count) const {
503 int32_t length = (int32_t)strlen(file_name_utf8);
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000504 if (length < 0) {
505 return false;
506 }
507
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000508 int32_t length_without_file_ending = length - 1;
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000509 while (length_without_file_ending > 0) {
510 if (file_name_utf8[length_without_file_ending] == '.') {
511 break;
512 } else {
513 length_without_file_ending--;
niklase@google.com470e71d2011-07-07 08:21:25 +0000514 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000515 }
516 if (length_without_file_ending == 0) {
517 length_without_file_ending = length;
518 }
519 memcpy(file_name_with_counter_utf8, file_name_utf8,
520 length_without_file_ending);
521 sprintf(file_name_with_counter_utf8 + length_without_file_ending, "_%lu%s",
522 static_cast<long unsigned int>(new_count),
523 file_name_utf8 + length_without_file_ending);
524 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000525}
526
tommi@webrtc.orgc7157da2015-03-19 09:30:29 +0000527// static
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000528void Trace::CreateTrace() {
529 TraceImpl::StaticInstance(kAddRef);
tommi@webrtc.orgcde1e7f2011-11-15 12:23:36 +0000530}
531
tommi@webrtc.orgc7157da2015-03-19 09:30:29 +0000532// static
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000533void Trace::ReturnTrace() {
534 TraceImpl::StaticInstance(kRelease);
tommi@webrtc.orgcde1e7f2011-11-15 12:23:36 +0000535}
536
tommi@webrtc.orgc7157da2015-03-19 09:30:29 +0000537// static
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000538int32_t Trace::TraceFile(char file_name[FileWrapper::kMaxFileNameSize]) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000539 TraceImpl* trace = TraceImpl::GetTrace();
540 if (trace) {
541 int ret_val = trace->TraceFileImpl(file_name);
542 ReturnTrace();
543 return ret_val;
544 }
545 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000546}
547
tommi@webrtc.orgc7157da2015-03-19 09:30:29 +0000548// static
549void Trace::set_level_filter(int filter) {
pbos235c35f2015-07-22 08:34:59 -0700550 rtc::AtomicOps::ReleaseStore(&level_filter_, filter);
tommi@webrtc.orgc7157da2015-03-19 09:30:29 +0000551}
552
553// static
554int Trace::level_filter() {
pbos235c35f2015-07-22 08:34:59 -0700555 return rtc::AtomicOps::AcquireLoad(&level_filter_);
tommi@webrtc.orgc7157da2015-03-19 09:30:29 +0000556}
557
558// static
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000559int32_t Trace::SetTraceFile(const char* file_name,
560 const bool add_file_counter) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000561 TraceImpl* trace = TraceImpl::GetTrace();
562 if (trace) {
563 int ret_val = trace->SetTraceFileImpl(file_name, add_file_counter);
564 ReturnTrace();
565 return ret_val;
566 }
567 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000568}
569
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000570int32_t Trace::SetTraceCallback(TraceCallback* callback) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000571 TraceImpl* trace = TraceImpl::GetTrace();
572 if (trace) {
573 int ret_val = trace->SetTraceCallbackImpl(callback);
574 ReturnTrace();
575 return ret_val;
576 }
577 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000578}
579
580void Trace::Add(const TraceLevel level, const TraceModule module,
pbos@webrtc.org046deb92013-04-09 09:06:11 +0000581 const int32_t id, const char* msg, ...) {
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000582 TraceImpl* trace = TraceImpl::GetTrace(level);
583 if (trace) {
584 if (trace->TraceCheck(level)) {
585 char temp_buff[WEBRTC_TRACE_MAX_MESSAGE_SIZE];
586 char* buff = 0;
587 if (msg) {
588 va_list args;
589 va_start(args, msg);
niklase@google.com470e71d2011-07-07 08:21:25 +0000590#ifdef _WIN32
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000591 _vsnprintf(temp_buff, WEBRTC_TRACE_MAX_MESSAGE_SIZE - 1, msg, args);
niklase@google.com470e71d2011-07-07 08:21:25 +0000592#else
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000593 vsnprintf(temp_buff, WEBRTC_TRACE_MAX_MESSAGE_SIZE - 1, msg, args);
niklase@google.com470e71d2011-07-07 08:21:25 +0000594#endif
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000595 va_end(args);
596 buff = temp_buff;
597 }
598 trace->AddImpl(level, module, id, buff);
niklase@google.com470e71d2011-07-07 08:21:25 +0000599 }
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000600 ReturnTrace();
601 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000602}
tommi@webrtc.orgcde1e7f2011-11-15 12:23:36 +0000603
phoglund@webrtc.orgdaabfd22013-01-03 09:37:03 +0000604} // namespace webrtc