blob: ec8cd7ff4a6f6d7d77f89576ca7a842874e2734f [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) {
Karl Wiberg881f1682018-03-08 15:03:23 +0100130 char tmp_buf[1024];
131 SimpleStringBuilder tmp(tmp_buf);
Tommifef05002018-02-27 13:51:08 +0100132 tmp.AppendFormat("[0x%08X]", err);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000133 switch (err_ctx) {
134 case ERRCTX_ERRNO:
135 tmp << " " << strerror(err);
136 break;
kwiberg77eab702016-09-28 17:42:01 -0700137#ifdef WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000138 case ERRCTX_HRESULT: {
139 char msgbuf[256];
Tommie51a0a82018-02-27 15:30:29 +0100140 DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM |
141 FORMAT_MESSAGE_IGNORE_INSERTS;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000142 if (DWORD len = FormatMessageA(
Tommie51a0a82018-02-27 15:30:29 +0100143 flags, nullptr, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
deadbeef37f5ecf2017-02-27 14:06:41 -0800144 msgbuf, sizeof(msgbuf) / sizeof(msgbuf[0]), nullptr)) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000145 while ((len > 0) &&
146 isspace(static_cast<unsigned char>(msgbuf[len-1]))) {
147 msgbuf[--len] = 0;
148 }
149 tmp << " " << msgbuf;
150 }
151 break;
152 }
Tommi0eefb4d2015-05-23 09:54:07 +0200153#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000154#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
155 case ERRCTX_OSSTATUS: {
Tommi09ca02e2016-04-24 17:32:48 +0200156 std::string desc(DescriptionFromOSStatus(err));
157 tmp << " " << (desc.empty() ? "Unknown error" : desc.c_str());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000158 break;
159 }
160#endif // WEBRTC_MAC && !defined(WEBRTC_IOS)
161 default:
162 break;
163 }
164 extra_ = tmp.str();
165 }
166}
167
Tommie51a0a82018-02-27 15:30:29 +0100168#if defined(WEBRTC_ANDROID)
jiayl66f0da22015-09-14 15:06:39 -0700169LogMessage::LogMessage(const char* file,
170 int line,
171 LoggingSeverity sev,
Tommie51a0a82018-02-27 15:30:29 +0100172 const char* tag)
deadbeef37f5ecf2017-02-27 14:06:41 -0800173 : LogMessage(file,
174 line,
175 sev,
176 ERRCTX_NONE,
Tommie51a0a82018-02-27 15:30:29 +0100177 0 /* err */) {
Tommifef05002018-02-27 13:51:08 +0100178 if (!is_noop_) {
179 tag_ = tag;
180 print_stream_ << tag << ": ";
181 }
jiayl66f0da22015-09-14 15:06:39 -0700182}
Tommie51a0a82018-02-27 15:30:29 +0100183#endif
184
185// DEPRECATED. Currently only used by downstream projects that use
186// implementation details of logging.h. Work is ongoing to remove those
187// dependencies.
188LogMessage::LogMessage(const char* file, int line, LoggingSeverity sev,
189 const std::string& tag)
190 : LogMessage(file, line, sev) {
191 if (!is_noop_)
192 print_stream_ << tag << ": ";
193}
jiayl66f0da22015-09-14 15:06:39 -0700194
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000195LogMessage::~LogMessage() {
Tommifef05002018-02-27 13:51:08 +0100196 if (is_noop_)
197 return;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000198
Tommifef05002018-02-27 13:51:08 +0100199 FinishPrintStream();
200
201 // TODO(tommi): Unfortunately |ostringstream::str()| always returns a copy
202 // of the constructed string. This means that we always end up creating
203 // two copies here (one owned by the stream, one by the return value of
204 // |str()|). It would be nice to switch to something else.
205 const std::string str = print_stream_.str();
206
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100207 if (severity_ >= g_dbg_sev) {
Tommie51a0a82018-02-27 15:30:29 +0100208#if defined(WEBRTC_ANDROID)
jiayl66f0da22015-09-14 15:06:39 -0700209 OutputToDebug(str, severity_, tag_);
Tommie51a0a82018-02-27 15:30:29 +0100210#else
211 OutputToDebug(str, severity_);
212#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000213 }
214
Peter Boström225789d2015-10-23 15:20:56 +0200215 CritScope cs(&g_log_crit);
216 for (auto& kv : streams_) {
217 if (severity_ >= kv.second) {
218 kv.first->OnLogMessage(str);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000219 }
220 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000221}
222
Tommifef05002018-02-27 13:51:08 +0100223std::ostream& LogMessage::stream() {
224 return is_noop_ ? GetNoopStream() : print_stream_;
225}
226
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100227bool LogMessage::Loggable(LoggingSeverity sev) {
228 return sev >= g_min_sev;
229}
230
231int LogMessage::GetMinLogSeverity() {
232 return g_min_sev;
233}
234
235LoggingSeverity LogMessage::GetLogToDebug() {
236 return g_dbg_sev;
237}
Honghai Zhang82d78622016-05-06 11:29:15 -0700238int64_t LogMessage::LogStartTime() {
Taylor Brandstetter4f0dfbd2016-06-15 17:15:23 -0700239 static const int64_t g_start = SystemTimeMillis();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000240 return g_start;
241}
242
Peter Boström0c4e06b2015-10-07 12:23:21 +0200243uint32_t LogMessage::WallClockStartTime() {
deadbeef37f5ecf2017-02-27 14:06:41 -0800244 static const uint32_t g_start_wallclock = time(nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000245 return g_start_wallclock;
246}
247
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000248void LogMessage::LogThreads(bool on) {
249 thread_ = on;
250}
251
252void LogMessage::LogTimestamps(bool on) {
253 timestamp_ = on;
254}
255
Tommi0eefb4d2015-05-23 09:54:07 +0200256void LogMessage::LogToDebug(LoggingSeverity min_sev) {
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100257 g_dbg_sev = min_sev;
Peter Boström225789d2015-10-23 15:20:56 +0200258 CritScope cs(&g_log_crit);
Tommi00aac5a2015-05-25 11:25:59 +0200259 UpdateMinLogSeverity();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000260}
261
andrew88703d72015-09-07 00:34:56 -0700262void LogMessage::SetLogToStderr(bool log_to_stderr) {
263 log_to_stderr_ = log_to_stderr;
264}
265
Tommi0eefb4d2015-05-23 09:54:07 +0200266int LogMessage::GetLogToStream(LogSink* stream) {
Peter Boström225789d2015-10-23 15:20:56 +0200267 CritScope cs(&g_log_crit);
Tommi0eefb4d2015-05-23 09:54:07 +0200268 LoggingSeverity sev = LS_NONE;
Peter Boström225789d2015-10-23 15:20:56 +0200269 for (auto& kv : streams_) {
270 if (!stream || stream == kv.first) {
271 sev = std::min(sev, kv.second);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000272 }
273 }
274 return sev;
275}
276
Tommi0eefb4d2015-05-23 09:54:07 +0200277void LogMessage::AddLogToStream(LogSink* stream, LoggingSeverity min_sev) {
Peter Boström225789d2015-10-23 15:20:56 +0200278 CritScope cs(&g_log_crit);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000279 streams_.push_back(std::make_pair(stream, min_sev));
280 UpdateMinLogSeverity();
281}
282
Tommi0eefb4d2015-05-23 09:54:07 +0200283void LogMessage::RemoveLogToStream(LogSink* stream) {
Peter Boström225789d2015-10-23 15:20:56 +0200284 CritScope cs(&g_log_crit);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000285 for (StreamList::iterator it = streams_.begin(); it != streams_.end(); ++it) {
286 if (stream == it->first) {
287 streams_.erase(it);
288 break;
289 }
290 }
291 UpdateMinLogSeverity();
292}
293
Tommi0eefb4d2015-05-23 09:54:07 +0200294void LogMessage::ConfigureLogging(const char* params) {
295 LoggingSeverity current_level = LS_VERBOSE;
296 LoggingSeverity debug_level = GetLogToDebug();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000297
298 std::vector<std::string> tokens;
299 tokenize(params, ' ', &tokens);
300
Tommi0eefb4d2015-05-23 09:54:07 +0200301 for (const std::string& token : tokens) {
302 if (token.empty())
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000303 continue;
304
305 // Logging features
Tommi0eefb4d2015-05-23 09:54:07 +0200306 if (token == "tstamp") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000307 LogTimestamps();
Tommi0eefb4d2015-05-23 09:54:07 +0200308 } else if (token == "thread") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000309 LogThreads();
310
311 // Logging levels
Tommi0eefb4d2015-05-23 09:54:07 +0200312 } else if (token == "sensitive") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000313 current_level = LS_SENSITIVE;
Tommi0eefb4d2015-05-23 09:54:07 +0200314 } else if (token == "verbose") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000315 current_level = LS_VERBOSE;
Tommi0eefb4d2015-05-23 09:54:07 +0200316 } else if (token == "info") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000317 current_level = LS_INFO;
Tommi0eefb4d2015-05-23 09:54:07 +0200318 } else if (token == "warning") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000319 current_level = LS_WARNING;
Tommi0eefb4d2015-05-23 09:54:07 +0200320 } else if (token == "error") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000321 current_level = LS_ERROR;
Tommi0eefb4d2015-05-23 09:54:07 +0200322 } else if (token == "none") {
323 current_level = LS_NONE;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000324
325 // Logging targets
Tommi0eefb4d2015-05-23 09:54:07 +0200326 } else if (token == "debug") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000327 debug_level = current_level;
328 }
329 }
330
331#if defined(WEBRTC_WIN)
Tommi0eefb4d2015-05-23 09:54:07 +0200332 if ((LS_NONE != debug_level) && !::IsDebuggerPresent()) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000333 // First, attempt to attach to our parent's console... so if you invoke
334 // from the command line, we'll see the output there. Otherwise, create
335 // our own console window.
336 // Note: These methods fail if a console already exists, which is fine.
Tommie51a0a82018-02-27 15:30:29 +0100337 if (!AttachConsole(ATTACH_PARENT_PROCESS))
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000338 ::AllocConsole();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000339 }
Tommi0eefb4d2015-05-23 09:54:07 +0200340#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000341
342 LogToDebug(debug_level);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000343}
344
danilchap3c6abd22017-09-06 05:46:29 -0700345void LogMessage::UpdateMinLogSeverity()
346 RTC_EXCLUSIVE_LOCKS_REQUIRED(g_log_crit) {
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100347 LoggingSeverity min_sev = g_dbg_sev;
Peter Boström225789d2015-10-23 15:20:56 +0200348 for (auto& kv : streams_) {
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100349 min_sev = std::min(g_dbg_sev, kv.second);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000350 }
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100351 g_min_sev = min_sev;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000352}
353
Tommie51a0a82018-02-27 15:30:29 +0100354#if defined(WEBRTC_ANDROID)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000355void LogMessage::OutputToDebug(const std::string& str,
jiayl66f0da22015-09-14 15:06:39 -0700356 LoggingSeverity severity,
Tommie51a0a82018-02-27 15:30:29 +0100357 const char* tag) {
358#else
359void LogMessage::OutputToDebug(const std::string& str,
360 LoggingSeverity severity) {
361#endif
andrew88703d72015-09-07 00:34:56 -0700362 bool log_to_stderr = log_to_stderr_;
tfarinaa41ab932015-10-30 16:08:48 -0700363#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) && defined(NDEBUG)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000364 // On the Mac, all stderr output goes to the Console log and causes clutter.
365 // So in opt builds, don't log to stderr unless the user specifically sets
366 // a preference to do so.
367 CFStringRef key = CFStringCreateWithCString(kCFAllocatorDefault,
368 "logToStdErr",
369 kCFStringEncodingUTF8);
370 CFStringRef domain = CFBundleGetIdentifier(CFBundleGetMainBundle());
deadbeef37f5ecf2017-02-27 14:06:41 -0800371 if (key != nullptr && domain != nullptr) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000372 Boolean exists_and_is_valid;
373 Boolean should_log =
374 CFPreferencesGetAppBooleanValue(key, domain, &exists_and_is_valid);
375 // If the key doesn't exist or is invalid or is false, we will not log to
376 // stderr.
377 log_to_stderr = exists_and_is_valid && should_log;
378 }
deadbeef37f5ecf2017-02-27 14:06:41 -0800379 if (key != nullptr) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000380 CFRelease(key);
381 }
Tommie51a0a82018-02-27 15:30:29 +0100382#endif // defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) && defined(NDEBUG)
383
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000384#if defined(WEBRTC_WIN)
385 // Always log to the debugger.
386 // Perhaps stderr should be controlled by a preference, as on Mac?
387 OutputDebugStringA(str.c_str());
388 if (log_to_stderr) {
389 // This handles dynamically allocated consoles, too.
390 if (HANDLE error_handle = ::GetStdHandle(STD_ERROR_HANDLE)) {
391 log_to_stderr = false;
392 DWORD written = 0;
393 ::WriteFile(error_handle, str.data(), static_cast<DWORD>(str.size()),
394 &written, 0);
395 }
396 }
Tommi0eefb4d2015-05-23 09:54:07 +0200397#endif // WEBRTC_WIN
Tommie51a0a82018-02-27 15:30:29 +0100398
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000399#if defined(WEBRTC_ANDROID)
400 // Android's logging facility uses severity to log messages but we
401 // need to map libjingle's severity levels to Android ones first.
402 // Also write to stderr which maybe available to executable started
403 // from the shell.
404 int prio;
405 switch (severity) {
406 case LS_SENSITIVE:
Tommie51a0a82018-02-27 15:30:29 +0100407 __android_log_write(ANDROID_LOG_INFO, tag, "SENSITIVE");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000408 if (log_to_stderr) {
409 fprintf(stderr, "SENSITIVE");
410 fflush(stderr);
411 }
412 return;
413 case LS_VERBOSE:
414 prio = ANDROID_LOG_VERBOSE;
415 break;
416 case LS_INFO:
417 prio = ANDROID_LOG_INFO;
418 break;
419 case LS_WARNING:
420 prio = ANDROID_LOG_WARN;
421 break;
422 case LS_ERROR:
423 prio = ANDROID_LOG_ERROR;
424 break;
425 default:
426 prio = ANDROID_LOG_UNKNOWN;
427 }
428
429 int size = str.size();
430 int line = 0;
431 int idx = 0;
432 const int max_lines = size / kMaxLogLineSize + 1;
433 if (max_lines == 1) {
Tommie51a0a82018-02-27 15:30:29 +0100434 __android_log_print(prio, tag, "%.*s", size, str.c_str());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000435 } else {
436 while (size > 0) {
437 const int len = std::min(size, kMaxLogLineSize);
438 // Use the size of the string in the format (str may have \0 in the
439 // middle).
Tommie51a0a82018-02-27 15:30:29 +0100440 __android_log_print(prio, tag, "[%d/%d] %.*s", line + 1, max_lines, len,
441 str.c_str() + idx);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000442 idx += len;
443 size -= len;
444 ++line;
445 }
446 }
447#endif // WEBRTC_ANDROID
448 if (log_to_stderr) {
449 fprintf(stderr, "%s", str.c_str());
450 fflush(stderr);
451 }
452}
453
Tommifef05002018-02-27 13:51:08 +0100454// static
455bool LogMessage::IsNoop(LoggingSeverity severity) {
456 if (severity >= g_dbg_sev)
457 return false;
458
459 // TODO(tommi): We're grabbing this lock for every LogMessage instance that
460 // is going to be logged. This introduces unnecessary synchronization for
461 // a feature that's mostly used for testing.
462 CritScope cs(&g_log_crit);
463 return streams_.size() == 0;
464}
465
466void LogMessage::FinishPrintStream() {
467 if (is_noop_)
468 return;
469 if (!extra_.empty())
470 print_stream_ << " : " << extra_;
471 print_stream_ << std::endl;
472}
473
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000474//////////////////////////////////////////////////////////////////////
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000475
476} // namespace rtc