diff --git a/webrtc/base/logging.cc b/webrtc/base/logging.cc index 58f8f58df5..e2ee115063 100644 --- a/webrtc/base/logging.cc +++ b/webrtc/base/logging.cc @@ -44,12 +44,25 @@ static const int kMaxLogLineSize = 1024 - 60; #include "webrtc/base/timeutils.h" namespace rtc { +namespace { + +// Return the filename portion of the string (that following the last slash). +const char* FilenameFromPath(const char* file) { + const char* end1 = ::strrchr(file, '/'); + const char* end2 = ::strrchr(file, '\\'); + if (!end1 && !end2) + return file; + else + return (end1 > end2) ? end1 + 1 : end2 + 1; +} + +} // namespace ///////////////////////////////////////////////////////////////////////////// // Constant Labels ///////////////////////////////////////////////////////////////////////////// -const char * FindLabel(int value, const ConstantLabel entries[]) { +const char* FindLabel(int value, const ConstantLabel entries[]) { for (int i = 0; entries[i].label; ++i) { if (value == entries[i].value) { return entries[i].label; @@ -58,12 +71,12 @@ const char * FindLabel(int value, const ConstantLabel entries[]) { return 0; } -std::string ErrorName(int err, const ConstantLabel * err_table) { +std::string ErrorName(int err, const ConstantLabel* err_table) { if (err == 0) return "No error"; if (err_table != 0) { - if (const char * value = FindLabel(err, err_table)) + if (const char* value = FindLabel(err, err_table)) return value; } @@ -84,6 +97,7 @@ LoggingSeverity LogMessage::dbg_sev_ = LS_INFO; LoggingSeverity LogMessage::min_sev_ = LS_NONE; LoggingSeverity LogMessage::dbg_sev_ = LS_NONE; #endif // !_DEBUG +bool LogMessage::log_to_stderr_ = true; // Global lock for log subsystem, only needed to serialize access to streams_. CriticalSection LogMessage::crit_; @@ -116,6 +130,8 @@ LogMessage::LogMessage(const char* file, int line, LoggingSeverity sev, print_stream_ << "[" << std::dec << id << "] "; } + print_stream_ << "(" << FilenameFromPath(file) << ":" << line << "): "; + if (err_ctx != ERRCTX_NONE) { std::ostringstream tmp; tmp << "[0x" << std::setfill('0') << std::hex << std::setw(8) << err << "]"; @@ -214,6 +230,10 @@ void LogMessage::LogToDebug(LoggingSeverity min_sev) { UpdateMinLogSeverity(); } +void LogMessage::SetLogToStderr(bool log_to_stderr) { + log_to_stderr_ = log_to_stderr; +} + int LogMessage::GetLogToStream(LogSink* stream) { CritScope cs(&crit_); LoggingSeverity sev = LS_NONE; @@ -314,7 +334,7 @@ void LogMessage::UpdateMinLogSeverity() EXCLUSIVE_LOCKS_REQUIRED(crit_) { void LogMessage::OutputToDebug(const std::string& str, LoggingSeverity severity) { - bool log_to_stderr = true; + bool log_to_stderr = log_to_stderr_; #if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) && (!defined(DEBUG) || defined(NDEBUG)) // On the Mac, all stderr output goes to the Console log and causes clutter. // So in opt builds, don't log to stderr unless the user specifically sets diff --git a/webrtc/base/logging.h b/webrtc/base/logging.h index 23c7373ef8..4840dfe462 100644 --- a/webrtc/base/logging.h +++ b/webrtc/base/logging.h @@ -80,7 +80,7 @@ struct ConstantLabel { int value; const char * label; }; #define TLABEL(x, y) { x, y } #define LASTLABEL { 0, 0 } -const char * FindLabel(int value, const ConstantLabel entries[]); +const char* FindLabel(int value, const ConstantLabel entries[]); std::string ErrorName(int err, const ConstantLabel* err_table); ////////////////////////////////////////////////////////////////////// @@ -163,6 +163,9 @@ class LogMessage { static void LogToDebug(LoggingSeverity min_sev); static LoggingSeverity GetLogToDebug() { return dbg_sev_; } + // Sets whether logs will be directed to stderr in debug mode. + static void SetLogToStderr(bool log_to_stderr); + // Stream: Any non-blocking stream interface. LogMessage takes ownership of // the stream. Multiple streams may be specified by using AddLogToStream. // LogToStream is retained for backwards compatibility; when invoked, it @@ -222,6 +225,9 @@ class LogMessage { // Flags for formatting options static bool thread_, timestamp_; + // Determines if logs will be directed to stderr in debug mode. + static bool log_to_stderr_; + DISALLOW_COPY_AND_ASSIGN(LogMessage); }; diff --git a/webrtc/system_wrappers/source/logging.cc b/webrtc/system_wrappers/source/logging.cc index da1c1a57d8..45a0985454 100644 --- a/webrtc/system_wrappers/source/logging.cc +++ b/webrtc/system_wrappers/source/logging.cc @@ -32,7 +32,8 @@ TraceLevel WebRtcSeverity(LoggingSeverity sev) { } } -const char* DescribeFile(const char* file) { +// Return the filename portion of the string (that following the last slash). +const char* FilenameFromPath(const char* file) { const char* end1 = ::strrchr(file, '/'); const char* end2 = ::strrchr(file, '\\'); if (!end1 && !end2) @@ -45,7 +46,7 @@ const char* DescribeFile(const char* file) { LogMessage::LogMessage(const char* file, int line, LoggingSeverity sev) : severity_(sev) { - print_stream_ << "(" << DescribeFile(file) << ":" << line << "): "; + print_stream_ << "(" << FilenameFromPath(file) << ":" << line << "): "; } bool LogMessage::Loggable(LoggingSeverity sev) { diff --git a/webrtc/test/test_suite.cc b/webrtc/test/test_suite.cc index e88b0301a2..2900f0eddb 100644 --- a/webrtc/test/test_suite.cc +++ b/webrtc/test/test_suite.cc @@ -13,6 +13,7 @@ #include "gflags/gflags.h" #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" +#include "webrtc/base/logging.h" #include "webrtc/test/testsupport/fileutils.h" #include "webrtc/test/testsupport/trace_to_stderr.h" #include "webrtc/test/field_trial.h" @@ -49,6 +50,7 @@ int TestSuite::Run() { } void TestSuite::Initialize() { + rtc::LogMessage::SetLogToStderr(FLAGS_logs); if (FLAGS_logs) trace_to_stderr_.reset(new TraceToStderr); }