OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 } | 65 } |
66 | 66 |
67 TraceImpl::TraceImpl() | 67 TraceImpl::TraceImpl() |
68 : callback_(NULL), | 68 : callback_(NULL), |
69 row_count_text_(0), | 69 row_count_text_(0), |
70 file_count_text_(0), | 70 file_count_text_(0), |
71 trace_file_(FileWrapper::Create()) { | 71 trace_file_(FileWrapper::Create()) { |
72 } | 72 } |
73 | 73 |
74 TraceImpl::~TraceImpl() { | 74 TraceImpl::~TraceImpl() { |
75 trace_file_->Flush(); | |
76 trace_file_->CloseFile(); | 75 trace_file_->CloseFile(); |
77 } | 76 } |
78 | 77 |
79 int32_t TraceImpl::AddThreadId(char* trace_message) const { | 78 int32_t TraceImpl::AddThreadId(char* trace_message) const { |
80 uint32_t thread_id = rtc::CurrentThreadId(); | 79 uint32_t thread_id = rtc::CurrentThreadId(); |
81 // Messages is 12 characters. | 80 // Messages is 12 characters. |
82 return sprintf(trace_message, "%10u; ", thread_id); | 81 return sprintf(trace_message, "%10u; ", thread_id); |
83 } | 82 } |
84 | 83 |
85 int32_t TraceImpl::AddLevel(char* sz_message, const TraceLevel level) const { | 84 int32_t TraceImpl::AddLevel(char* sz_message, const TraceLevel level) const { |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 break; | 279 break; |
281 } | 280 } |
282 } | 281 } |
283 return kMessageLength; | 282 return kMessageLength; |
284 } | 283 } |
285 | 284 |
286 int32_t TraceImpl::SetTraceFileImpl(const char* file_name_utf8, | 285 int32_t TraceImpl::SetTraceFileImpl(const char* file_name_utf8, |
287 const bool add_file_counter) { | 286 const bool add_file_counter) { |
288 rtc::CritScope lock(&crit_); | 287 rtc::CritScope lock(&crit_); |
289 | 288 |
290 trace_file_->Flush(); | |
291 trace_file_->CloseFile(); | 289 trace_file_->CloseFile(); |
| 290 trace_file_path_.clear(); |
292 | 291 |
293 if (file_name_utf8) { | 292 if (file_name_utf8) { |
294 if (add_file_counter) { | 293 if (add_file_counter) { |
295 file_count_text_ = 1; | 294 file_count_text_ = 1; |
296 | 295 |
297 char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize]; | 296 char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize]; |
298 CreateFileName(file_name_utf8, file_name_with_counter_utf8, | 297 CreateFileName(file_name_utf8, file_name_with_counter_utf8, |
299 file_count_text_); | 298 file_count_text_); |
300 if (trace_file_->OpenFile(file_name_with_counter_utf8, false, false, | 299 if (trace_file_->OpenFile(file_name_with_counter_utf8, false) == -1) { |
301 true) == -1) { | |
302 return -1; | 300 return -1; |
303 } | 301 } |
| 302 trace_file_path_ = file_name_with_counter_utf8; |
304 } else { | 303 } else { |
305 file_count_text_ = 0; | 304 file_count_text_ = 0; |
306 if (trace_file_->OpenFile(file_name_utf8, false, false, true) == -1) { | 305 if (trace_file_->OpenFile(file_name_utf8, false) == -1) { |
307 return -1; | 306 return -1; |
308 } | 307 } |
| 308 trace_file_path_ = file_name_utf8; |
309 } | 309 } |
310 } | 310 } |
311 row_count_text_ = 0; | 311 row_count_text_ = 0; |
312 return 0; | 312 return 0; |
313 } | 313 } |
314 | 314 |
315 int32_t TraceImpl::TraceFileImpl( | |
316 char file_name_utf8[FileWrapper::kMaxFileNameSize]) { | |
317 rtc::CritScope lock(&crit_); | |
318 return trace_file_->FileName(file_name_utf8, FileWrapper::kMaxFileNameSize); | |
319 } | |
320 | |
321 int32_t TraceImpl::SetTraceCallbackImpl(TraceCallback* callback) { | 315 int32_t TraceImpl::SetTraceCallbackImpl(TraceCallback* callback) { |
322 rtc::CritScope lock(&crit_); | 316 rtc::CritScope lock(&crit_); |
323 callback_ = callback; | 317 callback_ = callback; |
324 return 0; | 318 return 0; |
325 } | 319 } |
326 | 320 |
327 int32_t TraceImpl::AddMessage( | 321 int32_t TraceImpl::AddMessage( |
328 char* trace_message, | 322 char* trace_message, |
329 const char msg[WEBRTC_TRACE_MAX_MESSAGE_SIZE], | 323 const char msg[WEBRTC_TRACE_MAX_MESSAGE_SIZE], |
330 const uint16_t written_so_far) const { | 324 const uint16_t written_so_far) const { |
(...skipping 28 matching lines...) Expand all Loading... |
359 const char trace_message[WEBRTC_TRACE_MAX_MESSAGE_SIZE], | 353 const char trace_message[WEBRTC_TRACE_MAX_MESSAGE_SIZE], |
360 const uint16_t length, | 354 const uint16_t length, |
361 const TraceLevel level) { | 355 const TraceLevel level) { |
362 rtc::CritScope lock(&crit_); | 356 rtc::CritScope lock(&crit_); |
363 if (callback_) | 357 if (callback_) |
364 callback_->Print(level, trace_message, length); | 358 callback_->Print(level, trace_message, length); |
365 WriteToFile(trace_message, length); | 359 WriteToFile(trace_message, length); |
366 } | 360 } |
367 | 361 |
368 void TraceImpl::WriteToFile(const char* msg, uint16_t length) { | 362 void TraceImpl::WriteToFile(const char* msg, uint16_t length) { |
369 if (!trace_file_->Open()) | 363 if (!trace_file_->is_open()) |
370 return; | 364 return; |
371 | 365 |
372 if (row_count_text_ > WEBRTC_TRACE_MAX_FILE_SIZE) { | 366 if (row_count_text_ > WEBRTC_TRACE_MAX_FILE_SIZE) { |
373 // wrap file | 367 // wrap file |
374 row_count_text_ = 0; | 368 row_count_text_ = 0; |
375 trace_file_->Flush(); | 369 trace_file_->Flush(); |
376 | 370 |
377 if (file_count_text_ == 0) { | 371 if (file_count_text_ == 0) { |
378 trace_file_->Rewind(); | 372 trace_file_->Rewind(); |
379 } else { | 373 } else { |
380 char old_file_name[FileWrapper::kMaxFileNameSize]; | |
381 char new_file_name[FileWrapper::kMaxFileNameSize]; | 374 char new_file_name[FileWrapper::kMaxFileNameSize]; |
382 | 375 |
383 // get current name | 376 // get current name |
384 trace_file_->FileName(old_file_name, FileWrapper::kMaxFileNameSize); | 377 file_count_text_++; |
| 378 UpdateFileName(new_file_name, file_count_text_); |
| 379 |
385 trace_file_->CloseFile(); | 380 trace_file_->CloseFile(); |
| 381 trace_file_path_.clear(); |
386 | 382 |
387 file_count_text_++; | 383 if (trace_file_->OpenFile(new_file_name, false) == -1) { |
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; | 384 return; |
393 } | 385 } |
| 386 trace_file_path_ = new_file_name; |
394 } | 387 } |
395 } | 388 } |
396 if (row_count_text_ == 0) { | 389 if (row_count_text_ == 0) { |
397 char message[WEBRTC_TRACE_MAX_MESSAGE_SIZE + 1]; | 390 char message[WEBRTC_TRACE_MAX_MESSAGE_SIZE + 1]; |
398 int32_t length = AddDateTimeInfo(message); | 391 int32_t length = AddDateTimeInfo(message); |
399 if (length != -1) { | 392 if (length != -1) { |
400 message[length] = 0; | 393 message[length] = 0; |
401 message[length - 1] = '\n'; | 394 message[length - 1] = '\n'; |
402 trace_file_->Write(message, length); | 395 trace_file_->Write(message, length); |
403 row_count_text_++; | 396 row_count_text_++; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
455 | 448 |
456 ack_len += len; | 449 ack_len += len; |
457 AddMessageToList(trace_message, static_cast<uint16_t>(ack_len), level); | 450 AddMessageToList(trace_message, static_cast<uint16_t>(ack_len), level); |
458 } | 451 } |
459 | 452 |
460 bool TraceImpl::TraceCheck(const TraceLevel level) const { | 453 bool TraceImpl::TraceCheck(const TraceLevel level) const { |
461 return (level & level_filter()) ? true : false; | 454 return (level & level_filter()) ? true : false; |
462 } | 455 } |
463 | 456 |
464 bool TraceImpl::UpdateFileName( | 457 bool TraceImpl::UpdateFileName( |
465 const char file_name_utf8[FileWrapper::kMaxFileNameSize], | |
466 char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize], | 458 char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize], |
467 const uint32_t new_count) const { | 459 const uint32_t new_count) const { |
468 int32_t length = (int32_t)strlen(file_name_utf8); | 460 int32_t length = static_cast<int32_t>(trace_file_path_.length()); |
469 if (length < 0) { | |
470 return false; | |
471 } | |
472 | 461 |
473 int32_t length_without_file_ending = length - 1; | 462 int32_t length_without_file_ending = length - 1; |
474 while (length_without_file_ending > 0) { | 463 while (length_without_file_ending > 0) { |
475 if (file_name_utf8[length_without_file_ending] == '.') { | 464 if (trace_file_path_[length_without_file_ending] == '.') { |
476 break; | 465 break; |
477 } else { | 466 } else { |
478 length_without_file_ending--; | 467 length_without_file_ending--; |
479 } | 468 } |
480 } | 469 } |
481 if (length_without_file_ending == 0) { | 470 if (length_without_file_ending == 0) { |
482 length_without_file_ending = length; | 471 length_without_file_ending = length; |
483 } | 472 } |
484 int32_t length_to_ = length_without_file_ending - 1; | 473 int32_t length_to_ = length_without_file_ending - 1; |
485 while (length_to_ > 0) { | 474 while (length_to_ > 0) { |
486 if (file_name_utf8[length_to_] == '_') { | 475 if (trace_file_path_[length_to_] == '_') { |
487 break; | 476 break; |
488 } else { | 477 } else { |
489 length_to_--; | 478 length_to_--; |
490 } | 479 } |
491 } | 480 } |
492 | 481 |
493 memcpy(file_name_with_counter_utf8, file_name_utf8, length_to_); | 482 memcpy(file_name_with_counter_utf8, &trace_file_path_[0], length_to_); |
494 sprintf(file_name_with_counter_utf8 + length_to_, "_%lu%s", | 483 sprintf(file_name_with_counter_utf8 + length_to_, "_%lu%s", |
495 static_cast<long unsigned int>(new_count), | 484 static_cast<long unsigned int>(new_count), |
496 file_name_utf8 + length_without_file_ending); | 485 &trace_file_path_[length_without_file_ending]); |
497 return true; | 486 return true; |
498 } | 487 } |
499 | 488 |
500 bool TraceImpl::CreateFileName( | 489 bool TraceImpl::CreateFileName( |
501 const char file_name_utf8[FileWrapper::kMaxFileNameSize], | 490 const char file_name_utf8[FileWrapper::kMaxFileNameSize], |
502 char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize], | 491 char file_name_with_counter_utf8[FileWrapper::kMaxFileNameSize], |
503 const uint32_t new_count) const { | 492 const uint32_t new_count) const { |
504 int32_t length = (int32_t)strlen(file_name_utf8); | 493 int32_t length = (int32_t)strlen(file_name_utf8); |
505 if (length < 0) { | 494 if (length < 0) { |
506 return false; | 495 return false; |
(...skipping 22 matching lines...) Expand all Loading... |
529 void Trace::CreateTrace() { | 518 void Trace::CreateTrace() { |
530 TraceImpl::StaticInstance(kAddRef); | 519 TraceImpl::StaticInstance(kAddRef); |
531 } | 520 } |
532 | 521 |
533 // static | 522 // static |
534 void Trace::ReturnTrace() { | 523 void Trace::ReturnTrace() { |
535 TraceImpl::StaticInstance(kRelease); | 524 TraceImpl::StaticInstance(kRelease); |
536 } | 525 } |
537 | 526 |
538 // static | 527 // static |
539 int32_t Trace::TraceFile(char file_name[FileWrapper::kMaxFileNameSize]) { | |
540 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; | |
547 } | |
548 | |
549 // static | |
550 void Trace::set_level_filter(int filter) { | 528 void Trace::set_level_filter(int filter) { |
551 rtc::AtomicOps::ReleaseStore(&level_filter_, filter); | 529 rtc::AtomicOps::ReleaseStore(&level_filter_, filter); |
552 } | 530 } |
553 | 531 |
554 // static | 532 // static |
555 int Trace::level_filter() { | 533 int Trace::level_filter() { |
556 return rtc::AtomicOps::AcquireLoad(&level_filter_); | 534 return rtc::AtomicOps::AcquireLoad(&level_filter_); |
557 } | 535 } |
558 | 536 |
559 // static | 537 // static |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
596 va_end(args); | 574 va_end(args); |
597 buff = temp_buff; | 575 buff = temp_buff; |
598 } | 576 } |
599 trace->AddImpl(level, module, id, buff); | 577 trace->AddImpl(level, module, id, buff); |
600 } | 578 } |
601 ReturnTrace(); | 579 ReturnTrace(); |
602 } | 580 } |
603 } | 581 } |
604 | 582 |
605 } // namespace webrtc | 583 } // namespace webrtc |
OLD | NEW |