| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/logging.h" | 5 #include "base/logging.h" |
| 6 | 6 |
| 7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
| 8 #include <io.h> | 8 #include <io.h> |
| 9 #include <windows.h> | 9 #include <windows.h> |
| 10 typedef HANDLE FileHandle; | 10 typedef HANDLE FileHandle; |
| (...skipping 333 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 344 g_enable_dcheck = | 344 g_enable_dcheck = |
| 345 command_line->HasSwitch(switches::kEnableDCHECK); | 345 command_line->HasSwitch(switches::kEnableDCHECK); |
| 346 delete g_vlog_info; | 346 delete g_vlog_info; |
| 347 g_vlog_info = NULL; | 347 g_vlog_info = NULL; |
| 348 // Don't bother initializing g_vlog_info unless we use one of the | 348 // Don't bother initializing g_vlog_info unless we use one of the |
| 349 // vlog switches. | 349 // vlog switches. |
| 350 if (command_line->HasSwitch(switches::kV) || | 350 if (command_line->HasSwitch(switches::kV) || |
| 351 command_line->HasSwitch(switches::kVModule)) { | 351 command_line->HasSwitch(switches::kVModule)) { |
| 352 g_vlog_info = | 352 g_vlog_info = |
| 353 new VlogInfo(command_line->GetSwitchValueASCII(switches::kV), | 353 new VlogInfo(command_line->GetSwitchValueASCII(switches::kV), |
| 354 command_line->GetSwitchValueASCII(switches::kVModule)); | 354 command_line->GetSwitchValueASCII(switches::kVModule), |
| 355 &min_log_level); |
| 355 } | 356 } |
| 356 | 357 |
| 357 LoggingLock::Init(lock_log, new_log_file); | 358 LoggingLock::Init(lock_log, new_log_file); |
| 358 | 359 |
| 359 LoggingLock logging_lock; | 360 LoggingLock logging_lock; |
| 360 | 361 |
| 361 if (log_file) { | 362 if (log_file) { |
| 362 // calling InitLogging twice or after some log call has already opened the | 363 // calling InitLogging twice or after some log call has already opened the |
| 363 // default log file will re-initialize to the new options | 364 // default log file will re-initialize to the new options |
| 364 CloseFile(log_file); | 365 CloseFile(log_file); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 382 } | 383 } |
| 383 | 384 |
| 384 void SetMinLogLevel(int level) { | 385 void SetMinLogLevel(int level) { |
| 385 min_log_level = std::min(LOG_ERROR_REPORT, level); | 386 min_log_level = std::min(LOG_ERROR_REPORT, level); |
| 386 } | 387 } |
| 387 | 388 |
| 388 int GetMinLogLevel() { | 389 int GetMinLogLevel() { |
| 389 return min_log_level; | 390 return min_log_level; |
| 390 } | 391 } |
| 391 | 392 |
| 393 int GetVlogVerbosity() { |
| 394 return std::max(-1, LOG_INFO - GetMinLogLevel()); |
| 395 } |
| 396 |
| 392 int GetVlogLevelHelper(const char* file, size_t N) { | 397 int GetVlogLevelHelper(const char* file, size_t N) { |
| 393 DCHECK_GT(N, 0U); | 398 DCHECK_GT(N, 0U); |
| 394 return g_vlog_info ? | 399 return g_vlog_info ? |
| 395 g_vlog_info->GetVlogLevel(base::StringPiece(file, N - 1)) : | 400 g_vlog_info->GetVlogLevel(base::StringPiece(file, N - 1)) : |
| 396 VlogInfo::kDefaultVlogLevel; | 401 GetVlogVerbosity(); |
| 397 } | 402 } |
| 398 | 403 |
| 399 void SetLogItems(bool enable_process_id, bool enable_thread_id, | 404 void SetLogItems(bool enable_process_id, bool enable_thread_id, |
| 400 bool enable_timestamp, bool enable_tickcount) { | 405 bool enable_timestamp, bool enable_tickcount) { |
| 401 log_process_id = enable_process_id; | 406 log_process_id = enable_process_id; |
| 402 log_thread_id = enable_thread_id; | 407 log_thread_id = enable_thread_id; |
| 403 log_timestamp = enable_timestamp; | 408 log_timestamp = enable_timestamp; |
| 404 log_tickcount = enable_tickcount; | 409 log_tickcount = enable_tickcount; |
| 405 } | 410 } |
| 406 | 411 |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 501 LogMessage::SaveLastError::SaveLastError() : last_error_(::GetLastError()) { | 506 LogMessage::SaveLastError::SaveLastError() : last_error_(::GetLastError()) { |
| 502 } | 507 } |
| 503 | 508 |
| 504 LogMessage::SaveLastError::~SaveLastError() { | 509 LogMessage::SaveLastError::~SaveLastError() { |
| 505 ::SetLastError(last_error_); | 510 ::SetLastError(last_error_); |
| 506 } | 511 } |
| 507 #endif // defined(OS_WIN) | 512 #endif // defined(OS_WIN) |
| 508 | 513 |
| 509 LogMessage::LogMessage(const char* file, int line, LogSeverity severity, | 514 LogMessage::LogMessage(const char* file, int line, LogSeverity severity, |
| 510 int ctr) | 515 int ctr) |
| 511 : severity_(severity) { | 516 : severity_(severity), file_(file), line_(line) { |
| 512 Init(file, line); | 517 Init(file, line); |
| 513 } | 518 } |
| 514 | 519 |
| 515 LogMessage::LogMessage(const char* file, int line, const CheckOpString& result) | 520 LogMessage::LogMessage(const char* file, int line, const CheckOpString& result) |
| 516 : severity_(LOG_FATAL) { | 521 : severity_(LOG_FATAL), file_(file), line_(line) { |
| 517 Init(file, line); | 522 Init(file, line); |
| 518 stream_ << "Check failed: " << (*result.str_); | 523 stream_ << "Check failed: " << (*result.str_); |
| 519 } | 524 } |
| 520 | 525 |
| 521 LogMessage::LogMessage(const char* file, int line, LogSeverity severity, | 526 LogMessage::LogMessage(const char* file, int line, LogSeverity severity, |
| 522 const CheckOpString& result) | 527 const CheckOpString& result) |
| 523 : severity_(severity) { | 528 : severity_(severity), file_(file), line_(line) { |
| 524 Init(file, line); | 529 Init(file, line); |
| 525 stream_ << "Check failed: " << (*result.str_); | 530 stream_ << "Check failed: " << (*result.str_); |
| 526 } | 531 } |
| 527 | 532 |
| 528 LogMessage::LogMessage(const char* file, int line) | 533 LogMessage::LogMessage(const char* file, int line) |
| 529 : severity_(LOG_INFO) { | 534 : severity_(LOG_INFO), file_(file), line_(line) { |
| 530 Init(file, line); | 535 Init(file, line); |
| 531 } | 536 } |
| 532 | 537 |
| 533 LogMessage::LogMessage(const char* file, int line, LogSeverity severity) | 538 LogMessage::LogMessage(const char* file, int line, LogSeverity severity) |
| 534 : severity_(severity) { | 539 : severity_(severity), file_(file), line_(line) { |
| 535 Init(file, line); | 540 Init(file, line); |
| 536 } | 541 } |
| 537 | 542 |
| 538 // writes the common header info to the stream | 543 // writes the common header info to the stream |
| 539 void LogMessage::Init(const char* file, int line) { | 544 void LogMessage::Init(const char* file, int line) { |
| 540 base::StringPiece filename(file); | 545 base::StringPiece filename(file); |
| 541 size_t last_slash_pos = filename.find_last_of("\\/"); | 546 size_t last_slash_pos = filename.find_last_of("\\/"); |
| 542 if (last_slash_pos != base::StringPiece::npos) | 547 if (last_slash_pos != base::StringPiece::npos) |
| 543 filename.remove_prefix(last_slash_pos + 1); | 548 filename.remove_prefix(last_slash_pos + 1); |
| 544 | 549 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 562 << std::setw(2) << 1 + tm_time->tm_mon | 567 << std::setw(2) << 1 + tm_time->tm_mon |
| 563 << std::setw(2) << tm_time->tm_mday | 568 << std::setw(2) << tm_time->tm_mday |
| 564 << '/' | 569 << '/' |
| 565 << std::setw(2) << tm_time->tm_hour | 570 << std::setw(2) << tm_time->tm_hour |
| 566 << std::setw(2) << tm_time->tm_min | 571 << std::setw(2) << tm_time->tm_min |
| 567 << std::setw(2) << tm_time->tm_sec | 572 << std::setw(2) << tm_time->tm_sec |
| 568 << ':'; | 573 << ':'; |
| 569 } | 574 } |
| 570 if (log_tickcount) | 575 if (log_tickcount) |
| 571 stream_ << TickCount() << ':'; | 576 stream_ << TickCount() << ':'; |
| 572 stream_ << log_severity_names[severity_] << ":" << filename << | 577 if (severity_ >= 0) |
| 573 "(" << line << ")] "; | 578 stream_ << log_severity_names[severity_]; |
| 579 else |
| 580 stream_ << "VERBOSE" << -severity_; |
| 581 |
| 582 stream_ << ":" << file << "(" << line << ")] "; |
| 574 | 583 |
| 575 message_start_ = stream_.tellp(); | 584 message_start_ = stream_.tellp(); |
| 576 } | 585 } |
| 577 | 586 |
| 578 LogMessage::~LogMessage() { | 587 LogMessage::~LogMessage() { |
| 579 // The macros in logging.h should already avoid creating LogMessages | 588 // The macros in logging.h should already avoid creating LogMessages |
| 580 // when this holds, but it's possible that users create LogMessages | 589 // when this holds, but it's possible that users create LogMessages |
| 581 // directly (e.g., using LOG_STREAM() directly). | 590 // directly (e.g., using LOG_STREAM() directly). |
| 582 if (severity_ < min_log_level) | 591 if (severity_ < min_log_level) |
| 583 return; | 592 return; |
| 584 | 593 |
| 585 #ifndef NDEBUG | 594 #ifndef NDEBUG |
| 586 if (severity_ == LOG_FATAL) { | 595 if (severity_ == LOG_FATAL) { |
| 587 // Include a stack trace on a fatal. | 596 // Include a stack trace on a fatal. |
| 588 base::debug::StackTrace trace; | 597 base::debug::StackTrace trace; |
| 589 stream_ << std::endl; // Newline to separate from log message. | 598 stream_ << std::endl; // Newline to separate from log message. |
| 590 trace.OutputToStream(&stream_); | 599 trace.OutputToStream(&stream_); |
| 591 } | 600 } |
| 592 #endif | 601 #endif |
| 593 stream_ << std::endl; | 602 stream_ << std::endl; |
| 594 std::string str_newline(stream_.str()); | 603 std::string str_newline(stream_.str()); |
| 595 | 604 |
| 596 // Give any log message handler first dibs on the message. | 605 // Give any log message handler first dibs on the message. |
| 597 if (log_message_handler && log_message_handler(severity_, str_newline)) | 606 if (log_message_handler && log_message_handler(severity_, file_, line_, |
| 607 message_start_, str_newline)) { |
| 608 // The handler took care of it, no further processing. |
| 598 return; | 609 return; |
| 610 } |
| 599 | 611 |
| 600 if (logging_destination == LOG_ONLY_TO_SYSTEM_DEBUG_LOG || | 612 if (logging_destination == LOG_ONLY_TO_SYSTEM_DEBUG_LOG || |
| 601 logging_destination == LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG) { | 613 logging_destination == LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG) { |
| 602 #if defined(OS_WIN) | 614 #if defined(OS_WIN) |
| 603 OutputDebugStringA(str_newline.c_str()); | 615 OutputDebugStringA(str_newline.c_str()); |
| 604 #endif | 616 #endif |
| 605 fprintf(stderr, "%s", str_newline.c_str()); | 617 fprintf(stderr, "%s", str_newline.c_str()); |
| 606 fflush(stderr); | 618 fflush(stderr); |
| 607 } else if (severity_ >= kAlwaysPrintErrorLevel) { | 619 } else if (severity_ >= kAlwaysPrintErrorLevel) { |
| 608 // When we're only outputting to a log file, above a certain log level, we | 620 // When we're only outputting to a log file, above a certain log level, we |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 798 | 810 |
| 799 if (level == LOG_FATAL) | 811 if (level == LOG_FATAL) |
| 800 base::debug::BreakDebugger(); | 812 base::debug::BreakDebugger(); |
| 801 } | 813 } |
| 802 | 814 |
| 803 } // namespace logging | 815 } // namespace logging |
| 804 | 816 |
| 805 std::ostream& operator<<(std::ostream& out, const wchar_t* wstr) { | 817 std::ostream& operator<<(std::ostream& out, const wchar_t* wstr) { |
| 806 return out << WideToUTF8(std::wstring(wstr)); | 818 return out << WideToUTF8(std::wstring(wstr)); |
| 807 } | 819 } |
| OLD | NEW |