blob: 4798f4dbeccc7a0a2fd63280f799f910f23f49ce [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)
Tommi23edcff2015-05-25 10:45:43 +020012#if !defined(WIN32_LEAN_AND_MEAN)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013#define WIN32_LEAN_AND_MEAN
Tommi23edcff2015-05-25 10:45:43 +020014#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000015#include <windows.h>
conceptgenesis3f705622016-01-30 14:40:44 -080016#if _MSC_VER < 1900
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000017#define snprintf _snprintf
conceptgenesis3f705622016-01-30 14:40:44 -080018#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000019#undef ERROR // wingdi.h
20#endif
21
22#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
23#include <CoreServices/CoreServices.h>
24#elif defined(WEBRTC_ANDROID)
25#include <android/log.h>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000026// Android has a 1024 limit on log inputs. We use 60 chars as an
27// approx for the header/tag portion.
28// See android/system/core/liblog/logd_write.c
29static const int kMaxLogLineSize = 1024 - 60;
30#endif // WEBRTC_MAC && !defined(WEBRTC_IOS) || WEBRTC_ANDROID
31
32#include <time.h>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000033#include <limits.h>
andresp@webrtc.orgff689be2015-02-12 11:54:26 +000034
35#include <algorithm>
36#include <iomanip>
37#include <ostream>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000038#include <vector>
39
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020040#include "rtc_base/criticalsection.h"
41#include "rtc_base/logging.h"
Tommie51a0a82018-02-27 15:30:29 +010042#include "rtc_base/platform_thread_types.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020043#include "rtc_base/stringencode.h"
Tommifef05002018-02-27 13:51:08 +010044#include "rtc_base/strings/string_builder.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020045#include "rtc_base/stringutils.h"
46#include "rtc_base/timeutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000047
48namespace rtc {
andrew88703d72015-09-07 00:34:56 -070049namespace {
Jonas Olsson2b6f1352018-02-15 11:57:03 +010050// By default, release builds don't log, debug builds at info level
51#if !defined(NDEBUG)
52static LoggingSeverity g_min_sev = LS_INFO;
53static LoggingSeverity g_dbg_sev = LS_INFO;
54#else
55static LoggingSeverity g_min_sev = LS_NONE;
56static LoggingSeverity g_dbg_sev = LS_NONE;
57#endif
andrew88703d72015-09-07 00:34:56 -070058
59// Return the filename portion of the string (that following the last slash).
60const char* FilenameFromPath(const char* file) {
61 const char* end1 = ::strrchr(file, '/');
62 const char* end2 = ::strrchr(file, '\\');
63 if (!end1 && !end2)
64 return file;
65 else
66 return (end1 > end2) ? end1 + 1 : end2 + 1;
67}
68
Tommifef05002018-02-27 13:51:08 +010069std::ostream& GetNoopStream() {
70 class NoopStreamBuf : public std::streambuf {
71 public:
72 int overflow(int c) override { return c; }
73 };
74 static NoopStreamBuf noop_buffer;
75 static std::ostream noop_stream(&noop_buffer);
76 return noop_stream;
77}
78
Jonas Olsson2b6f1352018-02-15 11:57:03 +010079// Global lock for log subsystem, only needed to serialize access to streams_.
80CriticalSection g_log_crit;
andrew88703d72015-09-07 00:34:56 -070081} // namespace
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000082
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000083
84/////////////////////////////////////////////////////////////////////////////
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
Peter Boström225789d2015-10-23 15:20:56 +020099LogMessage::LogMessage(const char* file,
100 int line,
101 LoggingSeverity sev,
102 LogErrorContext err_ctx,
Tommie51a0a82018-02-27 15:30:29 +0100103 int err)
104 : severity_(sev), is_noop_(IsNoop(sev)) {
Tommifef05002018-02-27 13:51:08 +0100105 // If there's no need to do any work, let's not :)
106 if (is_noop_)
107 return;
108
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000109 if (timestamp_) {
Taylor Brandstetter4f0dfbd2016-06-15 17:15:23 -0700110 // Use SystemTimeMillis so that even if tests use fake clocks, the timestamp
111 // in log messages represents the real system time.
112 int64_t time = TimeDiff(SystemTimeMillis(), LogStartTime());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000113 // Also ensure WallClockStartTime is initialized, so that it matches
114 // LogStartTime.
115 WallClockStartTime();
116 print_stream_ << "[" << std::setfill('0') << std::setw(3) << (time / 1000)
117 << ":" << std::setw(3) << (time % 1000) << std::setfill(' ')
118 << "] ";
119 }
120
121 if (thread_) {
henrikaba35d052015-07-14 17:04:08 +0200122 PlatformThreadId id = CurrentThreadId();
123 print_stream_ << "[" << std::dec << id << "] ";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000124 }
125
deadbeef37f5ecf2017-02-27 14:06:41 -0800126 if (file != nullptr)
Alex Glaznevebed24d2015-09-15 11:05:24 -0700127 print_stream_ << "(" << FilenameFromPath(file) << ":" << line << "): ";
andrew88703d72015-09-07 00:34:56 -0700128
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000129 if (err_ctx != ERRCTX_NONE) {
Tommifef05002018-02-27 13:51:08 +0100130 SimpleStringBuilder<1024> tmp;
131 tmp.AppendFormat("[0x%08X]", err);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000132 switch (err_ctx) {
133 case ERRCTX_ERRNO:
134 tmp << " " << strerror(err);
135 break;
kwiberg77eab702016-09-28 17:42:01 -0700136#ifdef WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000137 case ERRCTX_HRESULT: {
138 char msgbuf[256];
Tommie51a0a82018-02-27 15:30:29 +0100139 DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM |
140 FORMAT_MESSAGE_IGNORE_INSERTS;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000141 if (DWORD len = FormatMessageA(
Tommie51a0a82018-02-27 15:30:29 +0100142 flags, nullptr, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
deadbeef37f5ecf2017-02-27 14:06:41 -0800143 msgbuf, sizeof(msgbuf) / sizeof(msgbuf[0]), nullptr)) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000144 while ((len > 0) &&
145 isspace(static_cast<unsigned char>(msgbuf[len-1]))) {
146 msgbuf[--len] = 0;
147 }
148 tmp << " " << msgbuf;
149 }
150 break;
151 }
Tommi0eefb4d2015-05-23 09:54:07 +0200152#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000153#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
154 case ERRCTX_OSSTATUS: {
Tommi09ca02e2016-04-24 17:32:48 +0200155 std::string desc(DescriptionFromOSStatus(err));
156 tmp << " " << (desc.empty() ? "Unknown error" : desc.c_str());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000157 break;
158 }
159#endif // WEBRTC_MAC && !defined(WEBRTC_IOS)
160 default:
161 break;
162 }
163 extra_ = tmp.str();
164 }
165}
166
Tommie51a0a82018-02-27 15:30:29 +0100167#if defined(WEBRTC_ANDROID)
jiayl66f0da22015-09-14 15:06:39 -0700168LogMessage::LogMessage(const char* file,
169 int line,
170 LoggingSeverity sev,
Tommie51a0a82018-02-27 15:30:29 +0100171 const char* tag)
deadbeef37f5ecf2017-02-27 14:06:41 -0800172 : LogMessage(file,
173 line,
174 sev,
175 ERRCTX_NONE,
Tommie51a0a82018-02-27 15:30:29 +0100176 0 /* err */) {
Tommifef05002018-02-27 13:51:08 +0100177 if (!is_noop_) {
178 tag_ = tag;
179 print_stream_ << tag << ": ";
180 }
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.
187LogMessage::LogMessage(const char* file, int line, LoggingSeverity sev,
188 const std::string& tag)
189 : LogMessage(file, line, sev) {
190 if (!is_noop_)
191 print_stream_ << tag << ": ";
192}
jiayl66f0da22015-09-14 15:06:39 -0700193
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000194LogMessage::~LogMessage() {
Tommifef05002018-02-27 13:51:08 +0100195 if (is_noop_)
196 return;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000197
Tommifef05002018-02-27 13:51:08 +0100198 FinishPrintStream();
199
200 // TODO(tommi): Unfortunately |ostringstream::str()| always returns a copy
201 // of the constructed string. This means that we always end up creating
202 // two copies here (one owned by the stream, one by the return value of
203 // |str()|). It would be nice to switch to something else.
204 const std::string str = print_stream_.str();
205
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100206 if (severity_ >= g_dbg_sev) {
Tommie51a0a82018-02-27 15:30:29 +0100207#if defined(WEBRTC_ANDROID)
jiayl66f0da22015-09-14 15:06:39 -0700208 OutputToDebug(str, severity_, tag_);
Tommie51a0a82018-02-27 15:30:29 +0100209#else
210 OutputToDebug(str, severity_);
211#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000212 }
213
Peter Boström225789d2015-10-23 15:20:56 +0200214 CritScope cs(&g_log_crit);
215 for (auto& kv : streams_) {
216 if (severity_ >= kv.second) {
217 kv.first->OnLogMessage(str);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000218 }
219 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000220}
221
Tommifef05002018-02-27 13:51:08 +0100222std::ostream& LogMessage::stream() {
223 return is_noop_ ? GetNoopStream() : print_stream_;
224}
225
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100226bool LogMessage::Loggable(LoggingSeverity sev) {
227 return sev >= g_min_sev;
228}
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
310 // Logging levels
Tommi0eefb4d2015-05-23 09:54:07 +0200311 } else if (token == "sensitive") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000312 current_level = LS_SENSITIVE;
Tommi0eefb4d2015-05-23 09:54:07 +0200313 } else if (token == "verbose") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000314 current_level = LS_VERBOSE;
Tommi0eefb4d2015-05-23 09:54:07 +0200315 } else if (token == "info") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000316 current_level = LS_INFO;
Tommi0eefb4d2015-05-23 09:54:07 +0200317 } else if (token == "warning") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000318 current_level = LS_WARNING;
Tommi0eefb4d2015-05-23 09:54:07 +0200319 } else if (token == "error") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000320 current_level = LS_ERROR;
Tommi0eefb4d2015-05-23 09:54:07 +0200321 } else if (token == "none") {
322 current_level = LS_NONE;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000323
324 // Logging targets
Tommi0eefb4d2015-05-23 09:54:07 +0200325 } else if (token == "debug") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000326 debug_level = current_level;
327 }
328 }
329
330#if defined(WEBRTC_WIN)
Tommi0eefb4d2015-05-23 09:54:07 +0200331 if ((LS_NONE != debug_level) && !::IsDebuggerPresent()) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000332 // First, attempt to attach to our parent's console... so if you invoke
333 // from the command line, we'll see the output there. Otherwise, create
334 // our own console window.
335 // Note: These methods fail if a console already exists, which is fine.
Tommie51a0a82018-02-27 15:30:29 +0100336 if (!AttachConsole(ATTACH_PARENT_PROCESS))
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000337 ::AllocConsole();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000338 }
Tommi0eefb4d2015-05-23 09:54:07 +0200339#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000340
341 LogToDebug(debug_level);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000342}
343
danilchap3c6abd22017-09-06 05:46:29 -0700344void LogMessage::UpdateMinLogSeverity()
345 RTC_EXCLUSIVE_LOCKS_REQUIRED(g_log_crit) {
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100346 LoggingSeverity min_sev = g_dbg_sev;
Peter Boström225789d2015-10-23 15:20:56 +0200347 for (auto& kv : streams_) {
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100348 min_sev = std::min(g_dbg_sev, kv.second);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000349 }
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100350 g_min_sev = min_sev;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000351}
352
Tommie51a0a82018-02-27 15:30:29 +0100353#if defined(WEBRTC_ANDROID)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000354void LogMessage::OutputToDebug(const std::string& str,
jiayl66f0da22015-09-14 15:06:39 -0700355 LoggingSeverity severity,
Tommie51a0a82018-02-27 15:30:29 +0100356 const char* tag) {
357#else
358void LogMessage::OutputToDebug(const std::string& str,
359 LoggingSeverity severity) {
360#endif
andrew88703d72015-09-07 00:34:56 -0700361 bool log_to_stderr = log_to_stderr_;
tfarinaa41ab932015-10-30 16:08:48 -0700362#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) && defined(NDEBUG)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000363 // On the Mac, all stderr output goes to the Console log and causes clutter.
364 // So in opt builds, don't log to stderr unless the user specifically sets
365 // a preference to do so.
366 CFStringRef key = CFStringCreateWithCString(kCFAllocatorDefault,
367 "logToStdErr",
368 kCFStringEncodingUTF8);
369 CFStringRef domain = CFBundleGetIdentifier(CFBundleGetMainBundle());
deadbeef37f5ecf2017-02-27 14:06:41 -0800370 if (key != nullptr && domain != nullptr) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000371 Boolean exists_and_is_valid;
372 Boolean should_log =
373 CFPreferencesGetAppBooleanValue(key, domain, &exists_and_is_valid);
374 // If the key doesn't exist or is invalid or is false, we will not log to
375 // stderr.
376 log_to_stderr = exists_and_is_valid && should_log;
377 }
deadbeef37f5ecf2017-02-27 14:06:41 -0800378 if (key != nullptr) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000379 CFRelease(key);
380 }
Tommie51a0a82018-02-27 15:30:29 +0100381#endif // defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) && defined(NDEBUG)
382
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000383#if defined(WEBRTC_WIN)
384 // Always log to the debugger.
385 // Perhaps stderr should be controlled by a preference, as on Mac?
386 OutputDebugStringA(str.c_str());
387 if (log_to_stderr) {
388 // This handles dynamically allocated consoles, too.
389 if (HANDLE error_handle = ::GetStdHandle(STD_ERROR_HANDLE)) {
390 log_to_stderr = false;
391 DWORD written = 0;
392 ::WriteFile(error_handle, str.data(), static_cast<DWORD>(str.size()),
393 &written, 0);
394 }
395 }
Tommi0eefb4d2015-05-23 09:54:07 +0200396#endif // WEBRTC_WIN
Tommie51a0a82018-02-27 15:30:29 +0100397
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000398#if defined(WEBRTC_ANDROID)
399 // Android's logging facility uses severity to log messages but we
400 // need to map libjingle's severity levels to Android ones first.
401 // Also write to stderr which maybe available to executable started
402 // from the shell.
403 int prio;
404 switch (severity) {
405 case LS_SENSITIVE:
Tommie51a0a82018-02-27 15:30:29 +0100406 __android_log_write(ANDROID_LOG_INFO, tag, "SENSITIVE");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000407 if (log_to_stderr) {
408 fprintf(stderr, "SENSITIVE");
409 fflush(stderr);
410 }
411 return;
412 case LS_VERBOSE:
413 prio = ANDROID_LOG_VERBOSE;
414 break;
415 case LS_INFO:
416 prio = ANDROID_LOG_INFO;
417 break;
418 case LS_WARNING:
419 prio = ANDROID_LOG_WARN;
420 break;
421 case LS_ERROR:
422 prio = ANDROID_LOG_ERROR;
423 break;
424 default:
425 prio = ANDROID_LOG_UNKNOWN;
426 }
427
428 int size = str.size();
429 int line = 0;
430 int idx = 0;
431 const int max_lines = size / kMaxLogLineSize + 1;
432 if (max_lines == 1) {
Tommie51a0a82018-02-27 15:30:29 +0100433 __android_log_print(prio, tag, "%.*s", size, str.c_str());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000434 } else {
435 while (size > 0) {
436 const int len = std::min(size, kMaxLogLineSize);
437 // Use the size of the string in the format (str may have \0 in the
438 // middle).
Tommie51a0a82018-02-27 15:30:29 +0100439 __android_log_print(prio, tag, "[%d/%d] %.*s", line + 1, max_lines, len,
440 str.c_str() + idx);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000441 idx += len;
442 size -= len;
443 ++line;
444 }
445 }
446#endif // WEBRTC_ANDROID
447 if (log_to_stderr) {
448 fprintf(stderr, "%s", str.c_str());
449 fflush(stderr);
450 }
451}
452
Tommifef05002018-02-27 13:51:08 +0100453// static
454bool LogMessage::IsNoop(LoggingSeverity severity) {
455 if (severity >= g_dbg_sev)
456 return false;
457
458 // TODO(tommi): We're grabbing this lock for every LogMessage instance that
459 // is going to be logged. This introduces unnecessary synchronization for
460 // a feature that's mostly used for testing.
461 CritScope cs(&g_log_crit);
462 return streams_.size() == 0;
463}
464
465void LogMessage::FinishPrintStream() {
466 if (is_noop_)
467 return;
468 if (!extra_.empty())
469 print_stream_ << " : " << extra_;
470 print_stream_ << std::endl;
471}
472
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000473//////////////////////////////////////////////////////////////////////
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000474
475} // namespace rtc