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