blob: 74128fb8d64340843cb4e50de800707abce3537d [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000012#include <windows.h>
conceptgenesis3f705622016-01-30 14:40:44 -080013#if _MSC_VER < 1900
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000014#define snprintf _snprintf
conceptgenesis3f705622016-01-30 14:40:44 -080015#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000016#undef ERROR // wingdi.h
17#endif
18
19#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
20#include <CoreServices/CoreServices.h>
21#elif defined(WEBRTC_ANDROID)
22#include <android/log.h>
Yves Gerey988cc082018-10-23 12:03:01 +020023
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000024// 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
27static const int kMaxLogLineSize = 1024 - 60;
28#endif // WEBRTC_MAC && !defined(WEBRTC_IOS) || WEBRTC_ANDROID
29
Yves Gerey988cc082018-10-23 12:03:01 +020030#include <stdio.h>
31#include <string.h>
Yves Gerey665174f2018-06-19 15:03:05 +020032#include <time.h>
andresp@webrtc.orgff689be2015-02-12 11:54:26 +000033#include <algorithm>
Karl Wibergcefc4652018-05-23 23:20:38 +020034#include <cstdarg>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000035#include <vector>
36
Yves Gerey988cc082018-10-23 12:03:01 +020037#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080038#include "rtc_base/critical_section.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020039#include "rtc_base/logging.h"
Tommie51a0a82018-02-27 15:30:29 +010040#include "rtc_base/platform_thread_types.h"
Steve Anton10542f22019-01-11 09:11:00 -080041#include "rtc_base/string_encode.h"
42#include "rtc_base/string_utils.h"
Tommifef05002018-02-27 13:51:08 +010043#include "rtc_base/strings/string_builder.h"
Yves Gerey988cc082018-10-23 12:03:01 +020044#include "rtc_base/thread_annotations.h"
Steve Anton10542f22019-01-11 09:11:00 -080045#include "rtc_base/time_utils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000046
47namespace rtc {
andrew88703d72015-09-07 00:34:56 -070048namespace {
Jonas Olsson2b6f1352018-02-15 11:57:03 +010049// By default, release builds don't log, debug builds at info level
50#if !defined(NDEBUG)
51static LoggingSeverity g_min_sev = LS_INFO;
52static LoggingSeverity g_dbg_sev = LS_INFO;
53#else
54static LoggingSeverity g_min_sev = LS_NONE;
55static LoggingSeverity g_dbg_sev = LS_NONE;
56#endif
andrew88703d72015-09-07 00:34:56 -070057
58// Return the filename portion of the string (that following the last slash).
59const char* FilenameFromPath(const char* file) {
60 const char* end1 = ::strrchr(file, '/');
61 const char* end2 = ::strrchr(file, '\\');
62 if (!end1 && !end2)
63 return file;
64 else
65 return (end1 > end2) ? end1 + 1 : end2 + 1;
66}
67
Jonas Olsson2b6f1352018-02-15 11:57:03 +010068// Global lock for log subsystem, only needed to serialize access to streams_.
69CriticalSection g_log_crit;
andrew88703d72015-09-07 00:34:56 -070070} // namespace
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000071
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +020072// Inefficient default implementation, override is recommended.
73void LogSink::OnLogMessage(const std::string& msg,
74 LoggingSeverity severity,
75 const char* tag) {
Jiawei Ou3ea18782018-10-31 23:14:24 -070076 OnLogMessage(tag + (": " + msg), severity);
77}
78
79void LogSink::OnLogMessage(const std::string& msg,
80 LoggingSeverity /* severity */) {
81 OnLogMessage(msg);
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +020082}
83
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000084/////////////////////////////////////////////////////////////////////////////
85// LogMessage
86/////////////////////////////////////////////////////////////////////////////
87
andrew88703d72015-09-07 00:34:56 -070088bool LogMessage::log_to_stderr_ = true;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000089
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000090// The list of logging streams currently configured.
91// Note: we explicitly do not clean this up, because of the uncertain ordering
92// of destructors at program exit. Let the person who sets the stream trigger
deadbeef37f5ecf2017-02-27 14:06:41 -080093// cleanup by setting to null, or let it leak (safe at program exit).
danilchap3c6abd22017-09-06 05:46:29 -070094LogMessage::StreamList LogMessage::streams_ RTC_GUARDED_BY(g_log_crit);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000095
96// Boolean options default to false (0)
97bool LogMessage::thread_, LogMessage::timestamp_;
98
Karl Wibergab4f1c12018-05-04 10:42:28 +020099LogMessage::LogMessage(const char* file, int line, LoggingSeverity sev)
100 : LogMessage(file, line, sev, ERRCTX_NONE, 0) {}
101
Peter Boström225789d2015-10-23 15:20:56 +0200102LogMessage::LogMessage(const char* file,
103 int line,
104 LoggingSeverity sev,
105 LogErrorContext err_ctx,
Tommie51a0a82018-02-27 15:30:29 +0100106 int err)
Jonas Olssond8c50782018-09-07 11:21:28 +0200107 : severity_(sev) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000108 if (timestamp_) {
Taylor Brandstetter4f0dfbd2016-06-15 17:15:23 -0700109 // Use SystemTimeMillis so that even if tests use fake clocks, the timestamp
110 // in log messages represents the real system time.
111 int64_t time = TimeDiff(SystemTimeMillis(), LogStartTime());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000112 // Also ensure WallClockStartTime is initialized, so that it matches
113 // LogStartTime.
114 WallClockStartTime();
Jonas Olssond8c50782018-09-07 11:21:28 +0200115 print_stream_ << "[" << rtc::LeftPad('0', 3, rtc::ToString(time / 1000))
116 << ":" << rtc::LeftPad('0', 3, rtc::ToString(time % 1000))
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000117 << "] ";
118 }
119
120 if (thread_) {
henrikaba35d052015-07-14 17:04:08 +0200121 PlatformThreadId id = CurrentThreadId();
Jonas Olssond8c50782018-09-07 11:21:28 +0200122 print_stream_ << "[" << id << "] ";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000123 }
124
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200125 if (file != nullptr) {
126#if defined(WEBRTC_ANDROID)
127 tag_ = FilenameFromPath(file);
128 print_stream_ << "(line " << line << "): ";
129#else
Yves Gerey665174f2018-06-19 15:03:05 +0200130 print_stream_ << "(" << FilenameFromPath(file) << ":" << line << "): ";
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200131#endif
132 }
andrew88703d72015-09-07 00:34:56 -0700133
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000134 if (err_ctx != ERRCTX_NONE) {
Karl Wiberg881f1682018-03-08 15:03:23 +0100135 char tmp_buf[1024];
136 SimpleStringBuilder tmp(tmp_buf);
Tommifef05002018-02-27 13:51:08 +0100137 tmp.AppendFormat("[0x%08X]", err);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000138 switch (err_ctx) {
139 case ERRCTX_ERRNO:
140 tmp << " " << strerror(err);
141 break;
kwiberg77eab702016-09-28 17:42:01 -0700142#ifdef WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000143 case ERRCTX_HRESULT: {
144 char msgbuf[256];
Yves Gerey665174f2018-06-19 15:03:05 +0200145 DWORD flags =
146 FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000147 if (DWORD len = FormatMessageA(
Tommie51a0a82018-02-27 15:30:29 +0100148 flags, nullptr, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
deadbeef37f5ecf2017-02-27 14:06:41 -0800149 msgbuf, sizeof(msgbuf) / sizeof(msgbuf[0]), nullptr)) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000150 while ((len > 0) &&
Yves Gerey665174f2018-06-19 15:03:05 +0200151 isspace(static_cast<unsigned char>(msgbuf[len - 1]))) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000152 msgbuf[--len] = 0;
153 }
154 tmp << " " << msgbuf;
155 }
156 break;
157 }
Tommi0eefb4d2015-05-23 09:54:07 +0200158#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000159#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
160 case ERRCTX_OSSTATUS: {
Tommi09ca02e2016-04-24 17:32:48 +0200161 std::string desc(DescriptionFromOSStatus(err));
162 tmp << " " << (desc.empty() ? "Unknown error" : desc.c_str());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000163 break;
164 }
165#endif // WEBRTC_MAC && !defined(WEBRTC_IOS)
166 default:
167 break;
168 }
169 extra_ = tmp.str();
170 }
171}
172
Tommie51a0a82018-02-27 15:30:29 +0100173#if defined(WEBRTC_ANDROID)
jiayl66f0da22015-09-14 15:06:39 -0700174LogMessage::LogMessage(const char* file,
175 int line,
176 LoggingSeverity sev,
Tommie51a0a82018-02-27 15:30:29 +0100177 const char* tag)
Yves Gerey665174f2018-06-19 15:03:05 +0200178 : LogMessage(file, line, sev, ERRCTX_NONE, 0 /* err */) {
Jonas Olssond8c50782018-09-07 11:21:28 +0200179 tag_ = tag;
180 print_stream_ << tag << ": ";
jiayl66f0da22015-09-14 15:06:39 -0700181}
Tommie51a0a82018-02-27 15:30:29 +0100182#endif
183
184// DEPRECATED. Currently only used by downstream projects that use
185// implementation details of logging.h. Work is ongoing to remove those
186// dependencies.
Yves Gerey665174f2018-06-19 15:03:05 +0200187LogMessage::LogMessage(const char* file,
188 int line,
189 LoggingSeverity sev,
Tommie51a0a82018-02-27 15:30:29 +0100190 const std::string& tag)
191 : LogMessage(file, line, sev) {
Jonas Olssond8c50782018-09-07 11:21:28 +0200192 print_stream_ << tag << ": ";
Tommie51a0a82018-02-27 15:30:29 +0100193}
jiayl66f0da22015-09-14 15:06:39 -0700194
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000195LogMessage::~LogMessage() {
Tommifef05002018-02-27 13:51:08 +0100196 FinishPrintStream();
197
Jonas Olssond8c50782018-09-07 11:21:28 +0200198 const std::string str = print_stream_.Release();
Tommifef05002018-02-27 13:51:08 +0100199
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100200 if (severity_ >= g_dbg_sev) {
Tommie51a0a82018-02-27 15:30:29 +0100201#if defined(WEBRTC_ANDROID)
jiayl66f0da22015-09-14 15:06:39 -0700202 OutputToDebug(str, severity_, tag_);
Tommie51a0a82018-02-27 15:30:29 +0100203#else
204 OutputToDebug(str, severity_);
205#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000206 }
207
Peter Boström225789d2015-10-23 15:20:56 +0200208 CritScope cs(&g_log_crit);
209 for (auto& kv : streams_) {
210 if (severity_ >= kv.second) {
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200211#if defined(WEBRTC_ANDROID)
212 kv.first->OnLogMessage(str, severity_, tag_);
213#else
Jiawei Ou3ea18782018-10-31 23:14:24 -0700214 kv.first->OnLogMessage(str, severity_);
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200215#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000216 }
217 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000218}
219
Karl Wibergcefc4652018-05-23 23:20:38 +0200220void LogMessage::AddTag(const char* tag) {
221#ifdef WEBRTC_ANDROID
Jonas Olssond8c50782018-09-07 11:21:28 +0200222 tag_ = tag;
Karl Wibergcefc4652018-05-23 23:20:38 +0200223#endif
224}
225
Jonas Olssond8c50782018-09-07 11:21:28 +0200226rtc::StringBuilder& LogMessage::stream() {
227 return print_stream_;
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100228}
229
230int LogMessage::GetMinLogSeverity() {
231 return g_min_sev;
232}
233
234LoggingSeverity LogMessage::GetLogToDebug() {
235 return g_dbg_sev;
236}
Honghai Zhang82d78622016-05-06 11:29:15 -0700237int64_t LogMessage::LogStartTime() {
Taylor Brandstetter4f0dfbd2016-06-15 17:15:23 -0700238 static const int64_t g_start = SystemTimeMillis();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000239 return g_start;
240}
241
Peter Boström0c4e06b2015-10-07 12:23:21 +0200242uint32_t LogMessage::WallClockStartTime() {
deadbeef37f5ecf2017-02-27 14:06:41 -0800243 static const uint32_t g_start_wallclock = time(nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000244 return g_start_wallclock;
245}
246
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000247void LogMessage::LogThreads(bool on) {
248 thread_ = on;
249}
250
251void LogMessage::LogTimestamps(bool on) {
252 timestamp_ = on;
253}
254
Tommi0eefb4d2015-05-23 09:54:07 +0200255void LogMessage::LogToDebug(LoggingSeverity min_sev) {
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100256 g_dbg_sev = min_sev;
Peter Boström225789d2015-10-23 15:20:56 +0200257 CritScope cs(&g_log_crit);
Tommi00aac5a2015-05-25 11:25:59 +0200258 UpdateMinLogSeverity();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000259}
260
andrew88703d72015-09-07 00:34:56 -0700261void LogMessage::SetLogToStderr(bool log_to_stderr) {
262 log_to_stderr_ = log_to_stderr;
263}
264
Tommi0eefb4d2015-05-23 09:54:07 +0200265int LogMessage::GetLogToStream(LogSink* stream) {
Peter Boström225789d2015-10-23 15:20:56 +0200266 CritScope cs(&g_log_crit);
Tommi0eefb4d2015-05-23 09:54:07 +0200267 LoggingSeverity sev = LS_NONE;
Peter Boström225789d2015-10-23 15:20:56 +0200268 for (auto& kv : streams_) {
269 if (!stream || stream == kv.first) {
270 sev = std::min(sev, kv.second);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000271 }
272 }
273 return sev;
274}
275
Tommi0eefb4d2015-05-23 09:54:07 +0200276void LogMessage::AddLogToStream(LogSink* stream, LoggingSeverity min_sev) {
Peter Boström225789d2015-10-23 15:20:56 +0200277 CritScope cs(&g_log_crit);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000278 streams_.push_back(std::make_pair(stream, min_sev));
279 UpdateMinLogSeverity();
280}
281
Tommi0eefb4d2015-05-23 09:54:07 +0200282void LogMessage::RemoveLogToStream(LogSink* stream) {
Peter Boström225789d2015-10-23 15:20:56 +0200283 CritScope cs(&g_log_crit);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000284 for (StreamList::iterator it = streams_.begin(); it != streams_.end(); ++it) {
285 if (stream == it->first) {
286 streams_.erase(it);
287 break;
288 }
289 }
290 UpdateMinLogSeverity();
291}
292
Tommi0eefb4d2015-05-23 09:54:07 +0200293void LogMessage::ConfigureLogging(const char* params) {
294 LoggingSeverity current_level = LS_VERBOSE;
295 LoggingSeverity debug_level = GetLogToDebug();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000296
297 std::vector<std::string> tokens;
298 tokenize(params, ' ', &tokens);
299
Tommi0eefb4d2015-05-23 09:54:07 +0200300 for (const std::string& token : tokens) {
301 if (token.empty())
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000302 continue;
303
304 // Logging features
Tommi0eefb4d2015-05-23 09:54:07 +0200305 if (token == "tstamp") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000306 LogTimestamps();
Tommi0eefb4d2015-05-23 09:54:07 +0200307 } else if (token == "thread") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000308 LogThreads();
309
Yves Gerey665174f2018-06-19 15:03:05 +0200310 // Logging levels
Tommi0eefb4d2015-05-23 09:54:07 +0200311 } else if (token == "verbose") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000312 current_level = LS_VERBOSE;
Tommi0eefb4d2015-05-23 09:54:07 +0200313 } else if (token == "info") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000314 current_level = LS_INFO;
Tommi0eefb4d2015-05-23 09:54:07 +0200315 } else if (token == "warning") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000316 current_level = LS_WARNING;
Tommi0eefb4d2015-05-23 09:54:07 +0200317 } else if (token == "error") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000318 current_level = LS_ERROR;
Tommi0eefb4d2015-05-23 09:54:07 +0200319 } else if (token == "none") {
320 current_level = LS_NONE;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000321
Yves Gerey665174f2018-06-19 15:03:05 +0200322 // Logging targets
Tommi0eefb4d2015-05-23 09:54:07 +0200323 } else if (token == "debug") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000324 debug_level = current_level;
325 }
326 }
327
Robin Raymondce1b1402018-11-22 20:10:11 -0500328#if defined(WEBRTC_WIN) && !defined(WINUWP)
Tommi0eefb4d2015-05-23 09:54:07 +0200329 if ((LS_NONE != debug_level) && !::IsDebuggerPresent()) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000330 // First, attempt to attach to our parent's console... so if you invoke
331 // from the command line, we'll see the output there. Otherwise, create
332 // our own console window.
333 // Note: These methods fail if a console already exists, which is fine.
Tommie51a0a82018-02-27 15:30:29 +0100334 if (!AttachConsole(ATTACH_PARENT_PROCESS))
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000335 ::AllocConsole();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000336 }
Robin Raymondce1b1402018-11-22 20:10:11 -0500337#endif // defined(WEBRTC_WIN) && !defined(WINUWP)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000338
339 LogToDebug(debug_level);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000340}
341
danilchap3c6abd22017-09-06 05:46:29 -0700342void LogMessage::UpdateMinLogSeverity()
343 RTC_EXCLUSIVE_LOCKS_REQUIRED(g_log_crit) {
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100344 LoggingSeverity min_sev = g_dbg_sev;
Karl Wibergd294c852018-06-12 11:31:06 +0200345 for (const auto& kv : streams_) {
346 const LoggingSeverity sev = kv.second;
347 min_sev = std::min(min_sev, sev);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000348 }
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100349 g_min_sev = min_sev;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000350}
351
Tommie51a0a82018-02-27 15:30:29 +0100352#if defined(WEBRTC_ANDROID)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000353void LogMessage::OutputToDebug(const std::string& str,
jiayl66f0da22015-09-14 15:06:39 -0700354 LoggingSeverity severity,
Tommie51a0a82018-02-27 15:30:29 +0100355 const char* tag) {
356#else
357void LogMessage::OutputToDebug(const std::string& str,
358 LoggingSeverity severity) {
359#endif
andrew88703d72015-09-07 00:34:56 -0700360 bool log_to_stderr = log_to_stderr_;
tfarinaa41ab932015-10-30 16:08:48 -0700361#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) && defined(NDEBUG)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000362 // On the Mac, all stderr output goes to the Console log and causes clutter.
363 // So in opt builds, don't log to stderr unless the user specifically sets
364 // a preference to do so.
Yves Gerey665174f2018-06-19 15:03:05 +0200365 CFStringRef key = CFStringCreateWithCString(
366 kCFAllocatorDefault, "logToStdErr", kCFStringEncodingUTF8);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000367 CFStringRef domain = CFBundleGetIdentifier(CFBundleGetMainBundle());
deadbeef37f5ecf2017-02-27 14:06:41 -0800368 if (key != nullptr && domain != nullptr) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000369 Boolean exists_and_is_valid;
370 Boolean should_log =
371 CFPreferencesGetAppBooleanValue(key, domain, &exists_and_is_valid);
372 // If the key doesn't exist or is invalid or is false, we will not log to
373 // stderr.
374 log_to_stderr = exists_and_is_valid && should_log;
375 }
deadbeef37f5ecf2017-02-27 14:06:41 -0800376 if (key != nullptr) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000377 CFRelease(key);
378 }
Tommie51a0a82018-02-27 15:30:29 +0100379#endif // defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) && defined(NDEBUG)
380
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000381#if defined(WEBRTC_WIN)
382 // Always log to the debugger.
383 // Perhaps stderr should be controlled by a preference, as on Mac?
384 OutputDebugStringA(str.c_str());
385 if (log_to_stderr) {
386 // This handles dynamically allocated consoles, too.
387 if (HANDLE error_handle = ::GetStdHandle(STD_ERROR_HANDLE)) {
388 log_to_stderr = false;
389 DWORD written = 0;
390 ::WriteFile(error_handle, str.data(), static_cast<DWORD>(str.size()),
391 &written, 0);
392 }
393 }
Tommi0eefb4d2015-05-23 09:54:07 +0200394#endif // WEBRTC_WIN
Tommie51a0a82018-02-27 15:30:29 +0100395
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000396#if defined(WEBRTC_ANDROID)
397 // Android's logging facility uses severity to log messages but we
398 // need to map libjingle's severity levels to Android ones first.
399 // Also write to stderr which maybe available to executable started
400 // from the shell.
401 int prio;
402 switch (severity) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000403 case LS_VERBOSE:
404 prio = ANDROID_LOG_VERBOSE;
405 break;
406 case LS_INFO:
407 prio = ANDROID_LOG_INFO;
408 break;
409 case LS_WARNING:
410 prio = ANDROID_LOG_WARN;
411 break;
412 case LS_ERROR:
413 prio = ANDROID_LOG_ERROR;
414 break;
415 default:
416 prio = ANDROID_LOG_UNKNOWN;
417 }
418
419 int size = str.size();
420 int line = 0;
421 int idx = 0;
422 const int max_lines = size / kMaxLogLineSize + 1;
423 if (max_lines == 1) {
Tommie51a0a82018-02-27 15:30:29 +0100424 __android_log_print(prio, tag, "%.*s", size, str.c_str());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000425 } else {
426 while (size > 0) {
427 const int len = std::min(size, kMaxLogLineSize);
428 // Use the size of the string in the format (str may have \0 in the
429 // middle).
Tommie51a0a82018-02-27 15:30:29 +0100430 __android_log_print(prio, tag, "[%d/%d] %.*s", line + 1, max_lines, len,
431 str.c_str() + idx);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000432 idx += len;
433 size -= len;
434 ++line;
435 }
436 }
437#endif // WEBRTC_ANDROID
438 if (log_to_stderr) {
439 fprintf(stderr, "%s", str.c_str());
440 fflush(stderr);
441 }
442}
443
Tommifef05002018-02-27 13:51:08 +0100444// static
445bool LogMessage::IsNoop(LoggingSeverity severity) {
Jonas Olssond8c50782018-09-07 11:21:28 +0200446 if (severity >= g_dbg_sev || severity >= g_min_sev)
Tommifef05002018-02-27 13:51:08 +0100447 return false;
448
449 // TODO(tommi): We're grabbing this lock for every LogMessage instance that
450 // is going to be logged. This introduces unnecessary synchronization for
451 // a feature that's mostly used for testing.
452 CritScope cs(&g_log_crit);
Jonas Olssond8c50782018-09-07 11:21:28 +0200453 if (streams_.size() > 0)
454 return false;
455
456 return true;
Tommifef05002018-02-27 13:51:08 +0100457}
458
459void LogMessage::FinishPrintStream() {
Tommifef05002018-02-27 13:51:08 +0100460 if (!extra_.empty())
461 print_stream_ << " : " << extra_;
Jonas Olssond8c50782018-09-07 11:21:28 +0200462 print_stream_ << "\n";
Tommifef05002018-02-27 13:51:08 +0100463}
464
Karl Wibergcefc4652018-05-23 23:20:38 +0200465namespace webrtc_logging_impl {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000466
Karl Wibergcefc4652018-05-23 23:20:38 +0200467void Log(const LogArgType* fmt, ...) {
468 va_list args;
469 va_start(args, fmt);
470
471 LogMetadataErr meta;
472 const char* tag = nullptr;
473 switch (*fmt) {
474 case LogArgType::kLogMetadata: {
475 meta = {va_arg(args, LogMetadata), ERRCTX_NONE, 0};
476 break;
477 }
478 case LogArgType::kLogMetadataErr: {
479 meta = va_arg(args, LogMetadataErr);
480 break;
481 }
482#ifdef WEBRTC_ANDROID
483 case LogArgType::kLogMetadataTag: {
484 const LogMetadataTag tag_meta = va_arg(args, LogMetadataTag);
485 meta = {{nullptr, 0, tag_meta.severity}, ERRCTX_NONE, 0};
486 tag = tag_meta.tag;
487 break;
488 }
489#endif
490 default: {
491 RTC_NOTREACHED();
492 va_end(args);
493 return;
494 }
495 }
Jonas Olssond8c50782018-09-07 11:21:28 +0200496
497 if (LogMessage::IsNoop(meta.meta.Severity())) {
498 va_end(args);
499 return;
500 }
501
Karl Wibergcefc4652018-05-23 23:20:38 +0200502 LogMessage log_message(meta.meta.File(), meta.meta.Line(),
503 meta.meta.Severity(), meta.err_ctx, meta.err);
504 if (tag) {
505 log_message.AddTag(tag);
506 }
507
508 for (++fmt; *fmt != LogArgType::kEnd; ++fmt) {
509 switch (*fmt) {
510 case LogArgType::kInt:
511 log_message.stream() << va_arg(args, int);
512 break;
513 case LogArgType::kLong:
514 log_message.stream() << va_arg(args, long);
515 break;
516 case LogArgType::kLongLong:
517 log_message.stream() << va_arg(args, long long);
518 break;
519 case LogArgType::kUInt:
520 log_message.stream() << va_arg(args, unsigned);
521 break;
522 case LogArgType::kULong:
523 log_message.stream() << va_arg(args, unsigned long);
524 break;
525 case LogArgType::kULongLong:
526 log_message.stream() << va_arg(args, unsigned long long);
527 break;
528 case LogArgType::kDouble:
529 log_message.stream() << va_arg(args, double);
530 break;
531 case LogArgType::kLongDouble:
532 log_message.stream() << va_arg(args, long double);
533 break;
534 case LogArgType::kCharP:
535 log_message.stream() << va_arg(args, const char*);
536 break;
537 case LogArgType::kStdString:
538 log_message.stream() << *va_arg(args, const std::string*);
539 break;
Jonas Olssonf2ce37c2018-09-12 15:32:47 +0200540 case LogArgType::kStringView:
541 log_message.stream() << *va_arg(args, const absl::string_view*);
542 break;
Karl Wibergcefc4652018-05-23 23:20:38 +0200543 case LogArgType::kVoidP:
Jonas Olssond8c50782018-09-07 11:21:28 +0200544 log_message.stream() << rtc::ToHex(
545 reinterpret_cast<uintptr_t>(va_arg(args, const void*)));
Karl Wibergcefc4652018-05-23 23:20:38 +0200546 break;
547 default:
548 RTC_NOTREACHED();
549 va_end(args);
550 return;
551 }
552 }
553
554 va_end(args);
555}
556
557} // namespace webrtc_logging_impl
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000558} // namespace rtc