blob: 8eae4b7330c3f8bb0c18f20f21b3e721cb2155bf [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
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)
107 : severity_(sev), is_noop_(IsNoop(sev)) {
Tommifef05002018-02-27 13:51:08 +0100108 // If there's no need to do any work, let's not :)
109 if (is_noop_)
110 return;
111
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000112 if (timestamp_) {
Taylor Brandstetter4f0dfbd2016-06-15 17:15:23 -0700113 // Use SystemTimeMillis so that even if tests use fake clocks, the timestamp
114 // in log messages represents the real system time.
115 int64_t time = TimeDiff(SystemTimeMillis(), LogStartTime());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000116 // Also ensure WallClockStartTime is initialized, so that it matches
117 // LogStartTime.
118 WallClockStartTime();
119 print_stream_ << "[" << std::setfill('0') << std::setw(3) << (time / 1000)
120 << ":" << std::setw(3) << (time % 1000) << std::setfill(' ')
121 << "] ";
122 }
123
124 if (thread_) {
henrikaba35d052015-07-14 17:04:08 +0200125 PlatformThreadId id = CurrentThreadId();
126 print_stream_ << "[" << std::dec << id << "] ";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000127 }
128
deadbeef37f5ecf2017-02-27 14:06:41 -0800129 if (file != nullptr)
Alex Glaznevebed24d2015-09-15 11:05:24 -0700130 print_stream_ << "(" << FilenameFromPath(file) << ":" << line << "): ";
andrew88703d72015-09-07 00:34:56 -0700131
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000132 if (err_ctx != ERRCTX_NONE) {
Karl Wiberg881f1682018-03-08 15:03:23 +0100133 char tmp_buf[1024];
134 SimpleStringBuilder tmp(tmp_buf);
Tommifef05002018-02-27 13:51:08 +0100135 tmp.AppendFormat("[0x%08X]", err);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000136 switch (err_ctx) {
137 case ERRCTX_ERRNO:
138 tmp << " " << strerror(err);
139 break;
kwiberg77eab702016-09-28 17:42:01 -0700140#ifdef WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000141 case ERRCTX_HRESULT: {
142 char msgbuf[256];
Tommie51a0a82018-02-27 15:30:29 +0100143 DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM |
144 FORMAT_MESSAGE_IGNORE_INSERTS;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000145 if (DWORD len = FormatMessageA(
Tommie51a0a82018-02-27 15:30:29 +0100146 flags, nullptr, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
deadbeef37f5ecf2017-02-27 14:06:41 -0800147 msgbuf, sizeof(msgbuf) / sizeof(msgbuf[0]), nullptr)) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000148 while ((len > 0) &&
149 isspace(static_cast<unsigned char>(msgbuf[len-1]))) {
150 msgbuf[--len] = 0;
151 }
152 tmp << " " << msgbuf;
153 }
154 break;
155 }
Tommi0eefb4d2015-05-23 09:54:07 +0200156#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000157#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
158 case ERRCTX_OSSTATUS: {
Tommi09ca02e2016-04-24 17:32:48 +0200159 std::string desc(DescriptionFromOSStatus(err));
160 tmp << " " << (desc.empty() ? "Unknown error" : desc.c_str());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000161 break;
162 }
163#endif // WEBRTC_MAC && !defined(WEBRTC_IOS)
164 default:
165 break;
166 }
167 extra_ = tmp.str();
168 }
169}
170
Tommie51a0a82018-02-27 15:30:29 +0100171#if defined(WEBRTC_ANDROID)
jiayl66f0da22015-09-14 15:06:39 -0700172LogMessage::LogMessage(const char* file,
173 int line,
174 LoggingSeverity sev,
Tommie51a0a82018-02-27 15:30:29 +0100175 const char* tag)
deadbeef37f5ecf2017-02-27 14:06:41 -0800176 : LogMessage(file,
177 line,
178 sev,
179 ERRCTX_NONE,
Tommie51a0a82018-02-27 15:30:29 +0100180 0 /* err */) {
Tommifef05002018-02-27 13:51:08 +0100181 if (!is_noop_) {
182 tag_ = tag;
183 print_stream_ << tag << ": ";
184 }
jiayl66f0da22015-09-14 15:06:39 -0700185}
Tommie51a0a82018-02-27 15:30:29 +0100186#endif
187
188// DEPRECATED. Currently only used by downstream projects that use
189// implementation details of logging.h. Work is ongoing to remove those
190// dependencies.
191LogMessage::LogMessage(const char* file, int line, LoggingSeverity sev,
192 const std::string& tag)
193 : LogMessage(file, line, sev) {
194 if (!is_noop_)
195 print_stream_ << tag << ": ";
196}
jiayl66f0da22015-09-14 15:06:39 -0700197
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000198LogMessage::~LogMessage() {
Tommifef05002018-02-27 13:51:08 +0100199 if (is_noop_)
200 return;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000201
Tommifef05002018-02-27 13:51:08 +0100202 FinishPrintStream();
203
204 // TODO(tommi): Unfortunately |ostringstream::str()| always returns a copy
205 // of the constructed string. This means that we always end up creating
206 // two copies here (one owned by the stream, one by the return value of
207 // |str()|). It would be nice to switch to something else.
208 const std::string str = print_stream_.str();
209
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100210 if (severity_ >= g_dbg_sev) {
Tommie51a0a82018-02-27 15:30:29 +0100211#if defined(WEBRTC_ANDROID)
jiayl66f0da22015-09-14 15:06:39 -0700212 OutputToDebug(str, severity_, tag_);
Tommie51a0a82018-02-27 15:30:29 +0100213#else
214 OutputToDebug(str, severity_);
215#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000216 }
217
Peter Boström225789d2015-10-23 15:20:56 +0200218 CritScope cs(&g_log_crit);
219 for (auto& kv : streams_) {
220 if (severity_ >= kv.second) {
221 kv.first->OnLogMessage(str);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000222 }
223 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000224}
225
Tommifef05002018-02-27 13:51:08 +0100226std::ostream& LogMessage::stream() {
227 return is_noop_ ? GetNoopStream() : print_stream_;
228}
229
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100230bool LogMessage::Loggable(LoggingSeverity sev) {
231 return sev >= g_min_sev;
232}
233
234int LogMessage::GetMinLogSeverity() {
235 return g_min_sev;
236}
237
238LoggingSeverity LogMessage::GetLogToDebug() {
239 return g_dbg_sev;
240}
Honghai Zhang82d78622016-05-06 11:29:15 -0700241int64_t LogMessage::LogStartTime() {
Taylor Brandstetter4f0dfbd2016-06-15 17:15:23 -0700242 static const int64_t g_start = SystemTimeMillis();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000243 return g_start;
244}
245
Peter Boström0c4e06b2015-10-07 12:23:21 +0200246uint32_t LogMessage::WallClockStartTime() {
deadbeef37f5ecf2017-02-27 14:06:41 -0800247 static const uint32_t g_start_wallclock = time(nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000248 return g_start_wallclock;
249}
250
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000251void LogMessage::LogThreads(bool on) {
252 thread_ = on;
253}
254
255void LogMessage::LogTimestamps(bool on) {
256 timestamp_ = on;
257}
258
Tommi0eefb4d2015-05-23 09:54:07 +0200259void LogMessage::LogToDebug(LoggingSeverity min_sev) {
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100260 g_dbg_sev = min_sev;
Peter Boström225789d2015-10-23 15:20:56 +0200261 CritScope cs(&g_log_crit);
Tommi00aac5a2015-05-25 11:25:59 +0200262 UpdateMinLogSeverity();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000263}
264
andrew88703d72015-09-07 00:34:56 -0700265void LogMessage::SetLogToStderr(bool log_to_stderr) {
266 log_to_stderr_ = log_to_stderr;
267}
268
Tommi0eefb4d2015-05-23 09:54:07 +0200269int LogMessage::GetLogToStream(LogSink* stream) {
Peter Boström225789d2015-10-23 15:20:56 +0200270 CritScope cs(&g_log_crit);
Tommi0eefb4d2015-05-23 09:54:07 +0200271 LoggingSeverity sev = LS_NONE;
Peter Boström225789d2015-10-23 15:20:56 +0200272 for (auto& kv : streams_) {
273 if (!stream || stream == kv.first) {
274 sev = std::min(sev, kv.second);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000275 }
276 }
277 return sev;
278}
279
Tommi0eefb4d2015-05-23 09:54:07 +0200280void LogMessage::AddLogToStream(LogSink* stream, LoggingSeverity min_sev) {
Peter Boström225789d2015-10-23 15:20:56 +0200281 CritScope cs(&g_log_crit);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000282 streams_.push_back(std::make_pair(stream, min_sev));
283 UpdateMinLogSeverity();
284}
285
Tommi0eefb4d2015-05-23 09:54:07 +0200286void LogMessage::RemoveLogToStream(LogSink* stream) {
Peter Boström225789d2015-10-23 15:20:56 +0200287 CritScope cs(&g_log_crit);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000288 for (StreamList::iterator it = streams_.begin(); it != streams_.end(); ++it) {
289 if (stream == it->first) {
290 streams_.erase(it);
291 break;
292 }
293 }
294 UpdateMinLogSeverity();
295}
296
Tommi0eefb4d2015-05-23 09:54:07 +0200297void LogMessage::ConfigureLogging(const char* params) {
298 LoggingSeverity current_level = LS_VERBOSE;
299 LoggingSeverity debug_level = GetLogToDebug();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000300
301 std::vector<std::string> tokens;
302 tokenize(params, ' ', &tokens);
303
Tommi0eefb4d2015-05-23 09:54:07 +0200304 for (const std::string& token : tokens) {
305 if (token.empty())
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000306 continue;
307
308 // Logging features
Tommi0eefb4d2015-05-23 09:54:07 +0200309 if (token == "tstamp") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000310 LogTimestamps();
Tommi0eefb4d2015-05-23 09:54:07 +0200311 } else if (token == "thread") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000312 LogThreads();
313
314 // Logging levels
Tommi0eefb4d2015-05-23 09:54:07 +0200315 } else if (token == "sensitive") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000316 current_level = LS_SENSITIVE;
Tommi0eefb4d2015-05-23 09:54:07 +0200317 } else if (token == "verbose") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000318 current_level = LS_VERBOSE;
Tommi0eefb4d2015-05-23 09:54:07 +0200319 } else if (token == "info") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000320 current_level = LS_INFO;
Tommi0eefb4d2015-05-23 09:54:07 +0200321 } else if (token == "warning") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000322 current_level = LS_WARNING;
Tommi0eefb4d2015-05-23 09:54:07 +0200323 } else if (token == "error") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000324 current_level = LS_ERROR;
Tommi0eefb4d2015-05-23 09:54:07 +0200325 } else if (token == "none") {
326 current_level = LS_NONE;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000327
328 // Logging targets
Tommi0eefb4d2015-05-23 09:54:07 +0200329 } else if (token == "debug") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000330 debug_level = current_level;
331 }
332 }
333
334#if defined(WEBRTC_WIN)
Tommi0eefb4d2015-05-23 09:54:07 +0200335 if ((LS_NONE != debug_level) && !::IsDebuggerPresent()) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000336 // First, attempt to attach to our parent's console... so if you invoke
337 // from the command line, we'll see the output there. Otherwise, create
338 // our own console window.
339 // Note: These methods fail if a console already exists, which is fine.
Tommie51a0a82018-02-27 15:30:29 +0100340 if (!AttachConsole(ATTACH_PARENT_PROCESS))
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000341 ::AllocConsole();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000342 }
Tommi0eefb4d2015-05-23 09:54:07 +0200343#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000344
345 LogToDebug(debug_level);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000346}
347
danilchap3c6abd22017-09-06 05:46:29 -0700348void LogMessage::UpdateMinLogSeverity()
349 RTC_EXCLUSIVE_LOCKS_REQUIRED(g_log_crit) {
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100350 LoggingSeverity min_sev = g_dbg_sev;
Peter Boström225789d2015-10-23 15:20:56 +0200351 for (auto& kv : streams_) {
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100352 min_sev = std::min(g_dbg_sev, kv.second);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000353 }
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100354 g_min_sev = min_sev;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000355}
356
Tommie51a0a82018-02-27 15:30:29 +0100357#if defined(WEBRTC_ANDROID)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000358void LogMessage::OutputToDebug(const std::string& str,
jiayl66f0da22015-09-14 15:06:39 -0700359 LoggingSeverity severity,
Tommie51a0a82018-02-27 15:30:29 +0100360 const char* tag) {
361#else
362void LogMessage::OutputToDebug(const std::string& str,
363 LoggingSeverity severity) {
364#endif
andrew88703d72015-09-07 00:34:56 -0700365 bool log_to_stderr = log_to_stderr_;
tfarinaa41ab932015-10-30 16:08:48 -0700366#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) && defined(NDEBUG)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000367 // On the Mac, all stderr output goes to the Console log and causes clutter.
368 // So in opt builds, don't log to stderr unless the user specifically sets
369 // a preference to do so.
370 CFStringRef key = CFStringCreateWithCString(kCFAllocatorDefault,
371 "logToStdErr",
372 kCFStringEncodingUTF8);
373 CFStringRef domain = CFBundleGetIdentifier(CFBundleGetMainBundle());
deadbeef37f5ecf2017-02-27 14:06:41 -0800374 if (key != nullptr && domain != nullptr) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000375 Boolean exists_and_is_valid;
376 Boolean should_log =
377 CFPreferencesGetAppBooleanValue(key, domain, &exists_and_is_valid);
378 // If the key doesn't exist or is invalid or is false, we will not log to
379 // stderr.
380 log_to_stderr = exists_and_is_valid && should_log;
381 }
deadbeef37f5ecf2017-02-27 14:06:41 -0800382 if (key != nullptr) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000383 CFRelease(key);
384 }
Tommie51a0a82018-02-27 15:30:29 +0100385#endif // defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) && defined(NDEBUG)
386
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000387#if defined(WEBRTC_WIN)
388 // Always log to the debugger.
389 // Perhaps stderr should be controlled by a preference, as on Mac?
390 OutputDebugStringA(str.c_str());
391 if (log_to_stderr) {
392 // This handles dynamically allocated consoles, too.
393 if (HANDLE error_handle = ::GetStdHandle(STD_ERROR_HANDLE)) {
394 log_to_stderr = false;
395 DWORD written = 0;
396 ::WriteFile(error_handle, str.data(), static_cast<DWORD>(str.size()),
397 &written, 0);
398 }
399 }
Tommi0eefb4d2015-05-23 09:54:07 +0200400#endif // WEBRTC_WIN
Tommie51a0a82018-02-27 15:30:29 +0100401
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000402#if defined(WEBRTC_ANDROID)
403 // Android's logging facility uses severity to log messages but we
404 // need to map libjingle's severity levels to Android ones first.
405 // Also write to stderr which maybe available to executable started
406 // from the shell.
407 int prio;
408 switch (severity) {
409 case LS_SENSITIVE:
Tommie51a0a82018-02-27 15:30:29 +0100410 __android_log_write(ANDROID_LOG_INFO, tag, "SENSITIVE");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000411 if (log_to_stderr) {
412 fprintf(stderr, "SENSITIVE");
413 fflush(stderr);
414 }
415 return;
416 case LS_VERBOSE:
417 prio = ANDROID_LOG_VERBOSE;
418 break;
419 case LS_INFO:
420 prio = ANDROID_LOG_INFO;
421 break;
422 case LS_WARNING:
423 prio = ANDROID_LOG_WARN;
424 break;
425 case LS_ERROR:
426 prio = ANDROID_LOG_ERROR;
427 break;
428 default:
429 prio = ANDROID_LOG_UNKNOWN;
430 }
431
432 int size = str.size();
433 int line = 0;
434 int idx = 0;
435 const int max_lines = size / kMaxLogLineSize + 1;
436 if (max_lines == 1) {
Tommie51a0a82018-02-27 15:30:29 +0100437 __android_log_print(prio, tag, "%.*s", size, str.c_str());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000438 } else {
439 while (size > 0) {
440 const int len = std::min(size, kMaxLogLineSize);
441 // Use the size of the string in the format (str may have \0 in the
442 // middle).
Tommie51a0a82018-02-27 15:30:29 +0100443 __android_log_print(prio, tag, "[%d/%d] %.*s", line + 1, max_lines, len,
444 str.c_str() + idx);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000445 idx += len;
446 size -= len;
447 ++line;
448 }
449 }
450#endif // WEBRTC_ANDROID
451 if (log_to_stderr) {
452 fprintf(stderr, "%s", str.c_str());
453 fflush(stderr);
454 }
455}
456
Tommifef05002018-02-27 13:51:08 +0100457// static
458bool LogMessage::IsNoop(LoggingSeverity severity) {
459 if (severity >= g_dbg_sev)
460 return false;
461
462 // TODO(tommi): We're grabbing this lock for every LogMessage instance that
463 // is going to be logged. This introduces unnecessary synchronization for
464 // a feature that's mostly used for testing.
465 CritScope cs(&g_log_crit);
466 return streams_.size() == 0;
467}
468
469void LogMessage::FinishPrintStream() {
470 if (is_noop_)
471 return;
472 if (!extra_.empty())
473 print_stream_ << " : " << extra_;
474 print_stream_ << std::endl;
475}
476
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000477//////////////////////////////////////////////////////////////////////
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000478
479} // namespace rtc