henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 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 | |
| 11 | #if defined(WEBRTC_WIN) |
Tommi | 23edcff | 2015-05-25 10:45:43 +0200 | [diff] [blame] | 12 | #if !defined(WIN32_LEAN_AND_MEAN) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | #define WIN32_LEAN_AND_MEAN |
Tommi | 23edcff | 2015-05-25 10:45:43 +0200 | [diff] [blame] | 14 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 15 | #include <windows.h> |
| 16 | #define snprintf _snprintf |
| 17 | #undef ERROR // wingdi.h |
| 18 | #endif |
| 19 | |
| 20 | #if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) |
| 21 | #include <CoreServices/CoreServices.h> |
| 22 | #elif defined(WEBRTC_ANDROID) |
| 23 | #include <android/log.h> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 24 | // Android has a 1024 limit on log inputs. We use 60 chars as an |
| 25 | // approx for the header/tag portion. |
| 26 | // See android/system/core/liblog/logd_write.c |
| 27 | static const int kMaxLogLineSize = 1024 - 60; |
| 28 | #endif // WEBRTC_MAC && !defined(WEBRTC_IOS) || WEBRTC_ANDROID |
| 29 | |
jiayl | 66f0da2 | 2015-09-14 15:06:39 -0700 | [diff] [blame] | 30 | static const char kLibjingle[] = "libjingle"; |
| 31 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 32 | #include <time.h> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 33 | #include <limits.h> |
andresp@webrtc.org | ff689be | 2015-02-12 11:54:26 +0000 | [diff] [blame] | 34 | |
| 35 | #include <algorithm> |
| 36 | #include <iomanip> |
| 37 | #include <ostream> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 38 | #include <vector> |
| 39 | |
| 40 | #include "webrtc/base/logging.h" |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 41 | #include "webrtc/base/platform_thread.h" |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 42 | #include "webrtc/base/scoped_ptr.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 43 | #include "webrtc/base/stringencode.h" |
| 44 | #include "webrtc/base/stringutils.h" |
| 45 | #include "webrtc/base/timeutils.h" |
| 46 | |
| 47 | namespace rtc { |
andrew | 88703d7 | 2015-09-07 00:34:56 -0700 | [diff] [blame] | 48 | namespace { |
| 49 | |
| 50 | // Return the filename portion of the string (that following the last slash). |
| 51 | const char* FilenameFromPath(const char* file) { |
| 52 | const char* end1 = ::strrchr(file, '/'); |
| 53 | const char* end2 = ::strrchr(file, '\\'); |
| 54 | if (!end1 && !end2) |
| 55 | return file; |
| 56 | else |
| 57 | return (end1 > end2) ? end1 + 1 : end2 + 1; |
| 58 | } |
| 59 | |
| 60 | } // namespace |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 61 | |
| 62 | ///////////////////////////////////////////////////////////////////////////// |
| 63 | // Constant Labels |
| 64 | ///////////////////////////////////////////////////////////////////////////// |
| 65 | |
andrew | 88703d7 | 2015-09-07 00:34:56 -0700 | [diff] [blame] | 66 | const char* FindLabel(int value, const ConstantLabel entries[]) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 67 | for (int i = 0; entries[i].label; ++i) { |
| 68 | if (value == entries[i].value) { |
| 69 | return entries[i].label; |
| 70 | } |
| 71 | } |
| 72 | return 0; |
| 73 | } |
| 74 | |
andrew | 88703d7 | 2015-09-07 00:34:56 -0700 | [diff] [blame] | 75 | std::string ErrorName(int err, const ConstantLabel* err_table) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 76 | if (err == 0) |
| 77 | return "No error"; |
| 78 | |
| 79 | if (err_table != 0) { |
andrew | 88703d7 | 2015-09-07 00:34:56 -0700 | [diff] [blame] | 80 | if (const char* value = FindLabel(err, err_table)) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 81 | return value; |
| 82 | } |
| 83 | |
| 84 | char buffer[16]; |
| 85 | snprintf(buffer, sizeof(buffer), "0x%08x", err); |
| 86 | return buffer; |
| 87 | } |
| 88 | |
| 89 | ///////////////////////////////////////////////////////////////////////////// |
| 90 | // LogMessage |
| 91 | ///////////////////////////////////////////////////////////////////////////// |
| 92 | |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 93 | // By default, release builds don't log, debug builds at info level |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 94 | #if _DEBUG |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 95 | LoggingSeverity LogMessage::min_sev_ = LS_INFO; |
| 96 | LoggingSeverity LogMessage::dbg_sev_ = LS_INFO; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 97 | #else // !_DEBUG |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 98 | LoggingSeverity LogMessage::min_sev_ = LS_NONE; |
| 99 | LoggingSeverity LogMessage::dbg_sev_ = LS_NONE; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 100 | #endif // !_DEBUG |
andrew | 88703d7 | 2015-09-07 00:34:56 -0700 | [diff] [blame] | 101 | bool LogMessage::log_to_stderr_ = true; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 102 | |
| 103 | // Global lock for log subsystem, only needed to serialize access to streams_. |
| 104 | CriticalSection LogMessage::crit_; |
| 105 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 106 | // The list of logging streams currently configured. |
| 107 | // Note: we explicitly do not clean this up, because of the uncertain ordering |
| 108 | // of destructors at program exit. Let the person who sets the stream trigger |
| 109 | // cleanup by setting to NULL, or let it leak (safe at program exit). |
Tommi | 00aac5a | 2015-05-25 11:25:59 +0200 | [diff] [blame] | 110 | LogMessage::StreamList LogMessage::streams_ GUARDED_BY(LogMessage::crit_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 111 | |
| 112 | // Boolean options default to false (0) |
| 113 | bool LogMessage::thread_, LogMessage::timestamp_; |
| 114 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 115 | LogMessage::LogMessage(const char* file, int line, LoggingSeverity sev, |
| 116 | LogErrorContext err_ctx, int err, const char* module) |
| 117 | : severity_(sev), |
jiayl | 66f0da2 | 2015-09-14 15:06:39 -0700 | [diff] [blame] | 118 | tag_(kLibjingle), |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 119 | warn_slow_logs_delay_(WARN_SLOW_LOGS_DELAY) { |
| 120 | if (timestamp_) { |
| 121 | uint32 time = TimeSince(LogStartTime()); |
| 122 | // Also ensure WallClockStartTime is initialized, so that it matches |
| 123 | // LogStartTime. |
| 124 | WallClockStartTime(); |
| 125 | print_stream_ << "[" << std::setfill('0') << std::setw(3) << (time / 1000) |
| 126 | << ":" << std::setw(3) << (time % 1000) << std::setfill(' ') |
| 127 | << "] "; |
| 128 | } |
| 129 | |
| 130 | if (thread_) { |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 131 | PlatformThreadId id = CurrentThreadId(); |
| 132 | print_stream_ << "[" << std::dec << id << "] "; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Alex Glaznev | ebed24d | 2015-09-15 11:05:24 -0700 | [diff] [blame^] | 135 | if (file != NULL) |
| 136 | print_stream_ << "(" << FilenameFromPath(file) << ":" << line << "): "; |
andrew | 88703d7 | 2015-09-07 00:34:56 -0700 | [diff] [blame] | 137 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 138 | if (err_ctx != ERRCTX_NONE) { |
| 139 | std::ostringstream tmp; |
| 140 | tmp << "[0x" << std::setfill('0') << std::hex << std::setw(8) << err << "]"; |
| 141 | switch (err_ctx) { |
| 142 | case ERRCTX_ERRNO: |
| 143 | tmp << " " << strerror(err); |
| 144 | break; |
| 145 | #if WEBRTC_WIN |
| 146 | case ERRCTX_HRESULT: { |
| 147 | char msgbuf[256]; |
| 148 | DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM; |
| 149 | HMODULE hmod = GetModuleHandleA(module); |
| 150 | if (hmod) |
| 151 | flags |= FORMAT_MESSAGE_FROM_HMODULE; |
| 152 | if (DWORD len = FormatMessageA( |
| 153 | flags, hmod, err, |
| 154 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 155 | msgbuf, sizeof(msgbuf) / sizeof(msgbuf[0]), NULL)) { |
| 156 | while ((len > 0) && |
| 157 | isspace(static_cast<unsigned char>(msgbuf[len-1]))) { |
| 158 | msgbuf[--len] = 0; |
| 159 | } |
| 160 | tmp << " " << msgbuf; |
| 161 | } |
| 162 | break; |
| 163 | } |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 164 | #endif // WEBRTC_WIN |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 165 | #if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) |
| 166 | case ERRCTX_OSSTATUS: { |
| 167 | tmp << " " << nonnull(GetMacOSStatusErrorString(err), "Unknown error"); |
| 168 | if (const char* desc = GetMacOSStatusCommentString(err)) { |
| 169 | tmp << ": " << desc; |
| 170 | } |
| 171 | break; |
| 172 | } |
| 173 | #endif // WEBRTC_MAC && !defined(WEBRTC_IOS) |
| 174 | default: |
| 175 | break; |
| 176 | } |
| 177 | extra_ = tmp.str(); |
| 178 | } |
| 179 | } |
| 180 | |
jiayl | 66f0da2 | 2015-09-14 15:06:39 -0700 | [diff] [blame] | 181 | LogMessage::LogMessage(const char* file, |
| 182 | int line, |
| 183 | LoggingSeverity sev, |
| 184 | const std::string& tag) |
| 185 | : LogMessage(file, line, sev, ERRCTX_NONE, 0 /* err */, NULL /* module */) { |
| 186 | tag_ = tag; |
| 187 | } |
| 188 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 189 | LogMessage::~LogMessage() { |
| 190 | if (!extra_.empty()) |
| 191 | print_stream_ << " : " << extra_; |
| 192 | print_stream_ << std::endl; |
| 193 | |
| 194 | const std::string& str = print_stream_.str(); |
| 195 | if (severity_ >= dbg_sev_) { |
jiayl | 66f0da2 | 2015-09-14 15:06:39 -0700 | [diff] [blame] | 196 | OutputToDebug(str, severity_, tag_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | uint32 before = Time(); |
| 200 | // Must lock streams_ before accessing |
| 201 | CritScope cs(&crit_); |
| 202 | for (StreamList::iterator it = streams_.begin(); it != streams_.end(); ++it) { |
| 203 | if (severity_ >= it->second) { |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 204 | it->first->OnLogMessage(str); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | uint32 delay = TimeSince(before); |
| 208 | if (delay >= warn_slow_logs_delay_) { |
tommi | 9a78d22 | 2015-09-10 01:41:55 -0700 | [diff] [blame] | 209 | LogMessage slow_log_warning = |
| 210 | rtc::LogMessage(__FILE__, __LINE__, LS_WARNING); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 211 | // If our warning is slow, we don't want to warn about it, because |
| 212 | // that would lead to inifinite recursion. So, give a really big |
| 213 | // number for the delay threshold. |
| 214 | slow_log_warning.warn_slow_logs_delay_ = UINT_MAX; |
| 215 | slow_log_warning.stream() << "Slow log: took " << delay << "ms to write " |
| 216 | << str.size() << " bytes."; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | uint32 LogMessage::LogStartTime() { |
| 221 | static const uint32 g_start = Time(); |
| 222 | return g_start; |
| 223 | } |
| 224 | |
| 225 | uint32 LogMessage::WallClockStartTime() { |
| 226 | static const uint32 g_start_wallclock = time(NULL); |
| 227 | return g_start_wallclock; |
| 228 | } |
| 229 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 230 | void LogMessage::LogThreads(bool on) { |
| 231 | thread_ = on; |
| 232 | } |
| 233 | |
| 234 | void LogMessage::LogTimestamps(bool on) { |
| 235 | timestamp_ = on; |
| 236 | } |
| 237 | |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 238 | void LogMessage::LogToDebug(LoggingSeverity min_sev) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 239 | dbg_sev_ = min_sev; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 240 | CritScope cs(&crit_); |
Tommi | 00aac5a | 2015-05-25 11:25:59 +0200 | [diff] [blame] | 241 | UpdateMinLogSeverity(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 242 | } |
| 243 | |
andrew | 88703d7 | 2015-09-07 00:34:56 -0700 | [diff] [blame] | 244 | void LogMessage::SetLogToStderr(bool log_to_stderr) { |
| 245 | log_to_stderr_ = log_to_stderr; |
| 246 | } |
| 247 | |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 248 | int LogMessage::GetLogToStream(LogSink* stream) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 249 | CritScope cs(&crit_); |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 250 | LoggingSeverity sev = LS_NONE; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 251 | for (StreamList::iterator it = streams_.begin(); it != streams_.end(); ++it) { |
| 252 | if (!stream || stream == it->first) { |
andresp@webrtc.org | ff689be | 2015-02-12 11:54:26 +0000 | [diff] [blame] | 253 | sev = std::min(sev, it->second); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | return sev; |
| 257 | } |
| 258 | |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 259 | void LogMessage::AddLogToStream(LogSink* stream, LoggingSeverity min_sev) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 260 | CritScope cs(&crit_); |
| 261 | streams_.push_back(std::make_pair(stream, min_sev)); |
| 262 | UpdateMinLogSeverity(); |
| 263 | } |
| 264 | |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 265 | void LogMessage::RemoveLogToStream(LogSink* stream) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 266 | CritScope cs(&crit_); |
| 267 | for (StreamList::iterator it = streams_.begin(); it != streams_.end(); ++it) { |
| 268 | if (stream == it->first) { |
| 269 | streams_.erase(it); |
| 270 | break; |
| 271 | } |
| 272 | } |
| 273 | UpdateMinLogSeverity(); |
| 274 | } |
| 275 | |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 276 | void LogMessage::ConfigureLogging(const char* params) { |
| 277 | LoggingSeverity current_level = LS_VERBOSE; |
| 278 | LoggingSeverity debug_level = GetLogToDebug(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 279 | |
| 280 | std::vector<std::string> tokens; |
| 281 | tokenize(params, ' ', &tokens); |
| 282 | |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 283 | for (const std::string& token : tokens) { |
| 284 | if (token.empty()) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 285 | continue; |
| 286 | |
| 287 | // Logging features |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 288 | if (token == "tstamp") { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 289 | LogTimestamps(); |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 290 | } else if (token == "thread") { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 291 | LogThreads(); |
| 292 | |
| 293 | // Logging levels |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 294 | } else if (token == "sensitive") { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 295 | current_level = LS_SENSITIVE; |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 296 | } else if (token == "verbose") { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 297 | current_level = LS_VERBOSE; |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 298 | } else if (token == "info") { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 299 | current_level = LS_INFO; |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 300 | } else if (token == "warning") { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 301 | current_level = LS_WARNING; |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 302 | } else if (token == "error") { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 303 | current_level = LS_ERROR; |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 304 | } else if (token == "none") { |
| 305 | current_level = LS_NONE; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 306 | |
| 307 | // Logging targets |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 308 | } else if (token == "debug") { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 309 | debug_level = current_level; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | #if defined(WEBRTC_WIN) |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 314 | if ((LS_NONE != debug_level) && !::IsDebuggerPresent()) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 315 | // First, attempt to attach to our parent's console... so if you invoke |
| 316 | // from the command line, we'll see the output there. Otherwise, create |
| 317 | // our own console window. |
| 318 | // Note: These methods fail if a console already exists, which is fine. |
| 319 | bool success = false; |
| 320 | typedef BOOL (WINAPI* PFN_AttachConsole)(DWORD); |
| 321 | if (HINSTANCE kernel32 = ::LoadLibrary(L"kernel32.dll")) { |
| 322 | // AttachConsole is defined on WinXP+. |
| 323 | if (PFN_AttachConsole attach_console = reinterpret_cast<PFN_AttachConsole> |
| 324 | (::GetProcAddress(kernel32, "AttachConsole"))) { |
| 325 | success = (FALSE != attach_console(ATTACH_PARENT_PROCESS)); |
| 326 | } |
| 327 | ::FreeLibrary(kernel32); |
| 328 | } |
| 329 | if (!success) { |
| 330 | ::AllocConsole(); |
| 331 | } |
| 332 | } |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 333 | #endif // WEBRTC_WIN |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 334 | |
| 335 | LogToDebug(debug_level); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Tommi | 00aac5a | 2015-05-25 11:25:59 +0200 | [diff] [blame] | 338 | void LogMessage::UpdateMinLogSeverity() EXCLUSIVE_LOCKS_REQUIRED(crit_) { |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 339 | LoggingSeverity min_sev = dbg_sev_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 340 | for (StreamList::iterator it = streams_.begin(); it != streams_.end(); ++it) { |
andresp@webrtc.org | ff689be | 2015-02-12 11:54:26 +0000 | [diff] [blame] | 341 | min_sev = std::min(dbg_sev_, it->second); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 342 | } |
| 343 | min_sev_ = min_sev; |
| 344 | } |
| 345 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 346 | void LogMessage::OutputToDebug(const std::string& str, |
jiayl | 66f0da2 | 2015-09-14 15:06:39 -0700 | [diff] [blame] | 347 | LoggingSeverity severity, |
| 348 | const std::string& tag) { |
andrew | 88703d7 | 2015-09-07 00:34:56 -0700 | [diff] [blame] | 349 | bool log_to_stderr = log_to_stderr_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 350 | #if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) && (!defined(DEBUG) || defined(NDEBUG)) |
| 351 | // On the Mac, all stderr output goes to the Console log and causes clutter. |
| 352 | // So in opt builds, don't log to stderr unless the user specifically sets |
| 353 | // a preference to do so. |
| 354 | CFStringRef key = CFStringCreateWithCString(kCFAllocatorDefault, |
| 355 | "logToStdErr", |
| 356 | kCFStringEncodingUTF8); |
| 357 | CFStringRef domain = CFBundleGetIdentifier(CFBundleGetMainBundle()); |
| 358 | if (key != NULL && domain != NULL) { |
| 359 | Boolean exists_and_is_valid; |
| 360 | Boolean should_log = |
| 361 | CFPreferencesGetAppBooleanValue(key, domain, &exists_and_is_valid); |
| 362 | // If the key doesn't exist or is invalid or is false, we will not log to |
| 363 | // stderr. |
| 364 | log_to_stderr = exists_and_is_valid && should_log; |
| 365 | } |
| 366 | if (key != NULL) { |
| 367 | CFRelease(key); |
| 368 | } |
| 369 | #endif |
| 370 | #if defined(WEBRTC_WIN) |
| 371 | // Always log to the debugger. |
| 372 | // Perhaps stderr should be controlled by a preference, as on Mac? |
| 373 | OutputDebugStringA(str.c_str()); |
| 374 | if (log_to_stderr) { |
| 375 | // This handles dynamically allocated consoles, too. |
| 376 | if (HANDLE error_handle = ::GetStdHandle(STD_ERROR_HANDLE)) { |
| 377 | log_to_stderr = false; |
| 378 | DWORD written = 0; |
| 379 | ::WriteFile(error_handle, str.data(), static_cast<DWORD>(str.size()), |
| 380 | &written, 0); |
| 381 | } |
| 382 | } |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 383 | #endif // WEBRTC_WIN |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 384 | #if defined(WEBRTC_ANDROID) |
| 385 | // Android's logging facility uses severity to log messages but we |
| 386 | // need to map libjingle's severity levels to Android ones first. |
| 387 | // Also write to stderr which maybe available to executable started |
| 388 | // from the shell. |
| 389 | int prio; |
| 390 | switch (severity) { |
| 391 | case LS_SENSITIVE: |
jiayl | 66f0da2 | 2015-09-14 15:06:39 -0700 | [diff] [blame] | 392 | __android_log_write(ANDROID_LOG_INFO, tag.c_str(), "SENSITIVE"); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 393 | if (log_to_stderr) { |
| 394 | fprintf(stderr, "SENSITIVE"); |
| 395 | fflush(stderr); |
| 396 | } |
| 397 | return; |
| 398 | case LS_VERBOSE: |
| 399 | prio = ANDROID_LOG_VERBOSE; |
| 400 | break; |
| 401 | case LS_INFO: |
| 402 | prio = ANDROID_LOG_INFO; |
| 403 | break; |
| 404 | case LS_WARNING: |
| 405 | prio = ANDROID_LOG_WARN; |
| 406 | break; |
| 407 | case LS_ERROR: |
| 408 | prio = ANDROID_LOG_ERROR; |
| 409 | break; |
| 410 | default: |
| 411 | prio = ANDROID_LOG_UNKNOWN; |
| 412 | } |
| 413 | |
| 414 | int size = str.size(); |
| 415 | int line = 0; |
| 416 | int idx = 0; |
| 417 | const int max_lines = size / kMaxLogLineSize + 1; |
| 418 | if (max_lines == 1) { |
jiayl | 66f0da2 | 2015-09-14 15:06:39 -0700 | [diff] [blame] | 419 | __android_log_print(prio, tag.c_str(), "%.*s", size, str.c_str()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 420 | } else { |
| 421 | while (size > 0) { |
| 422 | const int len = std::min(size, kMaxLogLineSize); |
| 423 | // Use the size of the string in the format (str may have \0 in the |
| 424 | // middle). |
jiayl | 66f0da2 | 2015-09-14 15:06:39 -0700 | [diff] [blame] | 425 | __android_log_print(prio, tag.c_str(), "[%d/%d] %.*s", |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 426 | line + 1, max_lines, |
| 427 | len, str.c_str() + idx); |
| 428 | idx += len; |
| 429 | size -= len; |
| 430 | ++line; |
| 431 | } |
| 432 | } |
| 433 | #endif // WEBRTC_ANDROID |
| 434 | if (log_to_stderr) { |
| 435 | fprintf(stderr, "%s", str.c_str()); |
| 436 | fflush(stderr); |
| 437 | } |
| 438 | } |
| 439 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 440 | ////////////////////////////////////////////////////////////////////// |
| 441 | // Logging Helpers |
| 442 | ////////////////////////////////////////////////////////////////////// |
| 443 | |
| 444 | void LogMultiline(LoggingSeverity level, const char* label, bool input, |
| 445 | const void* data, size_t len, bool hex_mode, |
| 446 | LogMultilineState* state) { |
| 447 | if (!LOG_CHECK_LEVEL_V(level)) |
| 448 | return; |
| 449 | |
| 450 | const char * direction = (input ? " << " : " >> "); |
| 451 | |
| 452 | // NULL data means to flush our count of unprintable characters. |
| 453 | if (!data) { |
| 454 | if (state && state->unprintable_count_[input]) { |
| 455 | LOG_V(level) << label << direction << "## " |
| 456 | << state->unprintable_count_[input] |
| 457 | << " consecutive unprintable ##"; |
| 458 | state->unprintable_count_[input] = 0; |
| 459 | } |
| 460 | return; |
| 461 | } |
| 462 | |
| 463 | // The ctype classification functions want unsigned chars. |
| 464 | const unsigned char* udata = static_cast<const unsigned char*>(data); |
| 465 | |
| 466 | if (hex_mode) { |
| 467 | const size_t LINE_SIZE = 24; |
| 468 | char hex_line[LINE_SIZE * 9 / 4 + 2], asc_line[LINE_SIZE + 1]; |
| 469 | while (len > 0) { |
| 470 | memset(asc_line, ' ', sizeof(asc_line)); |
| 471 | memset(hex_line, ' ', sizeof(hex_line)); |
andresp@webrtc.org | ff689be | 2015-02-12 11:54:26 +0000 | [diff] [blame] | 472 | size_t line_len = std::min(len, LINE_SIZE); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 473 | for (size_t i = 0; i < line_len; ++i) { |
| 474 | unsigned char ch = udata[i]; |
| 475 | asc_line[i] = isprint(ch) ? ch : '.'; |
| 476 | hex_line[i*2 + i/4] = hex_encode(ch >> 4); |
| 477 | hex_line[i*2 + i/4 + 1] = hex_encode(ch & 0xf); |
| 478 | } |
| 479 | asc_line[sizeof(asc_line)-1] = 0; |
| 480 | hex_line[sizeof(hex_line)-1] = 0; |
| 481 | LOG_V(level) << label << direction |
| 482 | << asc_line << " " << hex_line << " "; |
| 483 | udata += line_len; |
| 484 | len -= line_len; |
| 485 | } |
| 486 | return; |
| 487 | } |
| 488 | |
| 489 | size_t consecutive_unprintable = state ? state->unprintable_count_[input] : 0; |
| 490 | |
| 491 | const unsigned char* end = udata + len; |
| 492 | while (udata < end) { |
| 493 | const unsigned char* line = udata; |
| 494 | const unsigned char* end_of_line = strchrn<unsigned char>(udata, |
| 495 | end - udata, |
| 496 | '\n'); |
| 497 | if (!end_of_line) { |
| 498 | udata = end_of_line = end; |
| 499 | } else { |
| 500 | udata = end_of_line + 1; |
| 501 | } |
| 502 | |
| 503 | bool is_printable = true; |
| 504 | |
| 505 | // If we are in unprintable mode, we need to see a line of at least |
| 506 | // kMinPrintableLine characters before we'll switch back. |
| 507 | const ptrdiff_t kMinPrintableLine = 4; |
| 508 | if (consecutive_unprintable && ((end_of_line - line) < kMinPrintableLine)) { |
| 509 | is_printable = false; |
| 510 | } else { |
| 511 | // Determine if the line contains only whitespace and printable |
| 512 | // characters. |
| 513 | bool is_entirely_whitespace = true; |
| 514 | for (const unsigned char* pos = line; pos < end_of_line; ++pos) { |
| 515 | if (isspace(*pos)) |
| 516 | continue; |
| 517 | is_entirely_whitespace = false; |
| 518 | if (!isprint(*pos)) { |
| 519 | is_printable = false; |
| 520 | break; |
| 521 | } |
| 522 | } |
| 523 | // Treat an empty line following unprintable data as unprintable. |
| 524 | if (consecutive_unprintable && is_entirely_whitespace) { |
| 525 | is_printable = false; |
| 526 | } |
| 527 | } |
| 528 | if (!is_printable) { |
| 529 | consecutive_unprintable += (udata - line); |
| 530 | continue; |
| 531 | } |
| 532 | // Print out the current line, but prefix with a count of prior unprintable |
| 533 | // characters. |
| 534 | if (consecutive_unprintable) { |
| 535 | LOG_V(level) << label << direction << "## " << consecutive_unprintable |
| 536 | << " consecutive unprintable ##"; |
| 537 | consecutive_unprintable = 0; |
| 538 | } |
| 539 | // Strip off trailing whitespace. |
| 540 | while ((end_of_line > line) && isspace(*(end_of_line-1))) { |
| 541 | --end_of_line; |
| 542 | } |
| 543 | // Filter out any private data |
| 544 | std::string substr(reinterpret_cast<const char*>(line), end_of_line - line); |
| 545 | std::string::size_type pos_private = substr.find("Email"); |
| 546 | if (pos_private == std::string::npos) { |
| 547 | pos_private = substr.find("Passwd"); |
| 548 | } |
| 549 | if (pos_private == std::string::npos) { |
| 550 | LOG_V(level) << label << direction << substr; |
| 551 | } else { |
| 552 | LOG_V(level) << label << direction << "## omitted for privacy ##"; |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | if (state) { |
| 557 | state->unprintable_count_[input] = consecutive_unprintable; |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | ////////////////////////////////////////////////////////////////////// |
| 562 | |
| 563 | } // namespace rtc |