1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "third_party/libjingle/overrides/talk/base/logging.h"
7 #if defined(OS_MACOSX) && !defined(OS_IOS)
8 #include <CoreServices/CoreServices.h>
13 #include "base/atomicops.h"
14 #include "base/strings/string_util.h"
15 #include "base/threading/platform_thread.h"
16 #include "third_party/libjingle/source/talk/base/ipaddress.h"
17 #include "third_party/libjingle/source/talk/base/stream.h"
18 #include "third_party/libjingle/source/talk/base/stringencode.h"
19 #include "third_party/libjingle/source/talk/base/stringutils.h"
20 #include "third_party/libjingle/source/talk/base/timeutils.h"
22 // From this file we can't use VLOG since it expands into usage of the __FILE__
23 // macro (for correct filtering). The actual logging call from DIAGNOSTIC_LOG in
24 // ~DiagnosticLogMessage. Note that the second parameter to the LAZY_STREAM
25 // macro is true since the filter check has already been done for
27 #define LOG_LAZY_STREAM_DIRECT(file_name, line_number, sev) \
28 LAZY_STREAM(logging::LogMessage(file_name, line_number, \
33 void (*g_logging_delegate_function)(const std::string&) = NULL;
34 void (*g_extra_logging_init_function)(
35 void (*logging_delegate_function)(const std::string&)) = NULL;
37 COMPILE_ASSERT(sizeof(base::subtle::Atomic32) == sizeof(base::PlatformThreadId),
38 atomic32_not_same_size_as_platformthreadid);
39 base::subtle::Atomic32 g_init_logging_delegate_thread_id = 0;
42 /////////////////////////////////////////////////////////////////////////////
44 /////////////////////////////////////////////////////////////////////////////
46 const char* FindLabel(int value, const ConstantLabel entries[]) {
47 for (int i = 0; entries[i].label; ++i) {
48 if (value == entries[i].value) return entries[i].label;
53 std::string ErrorName(int err, const ConstantLabel* err_table) {
58 if (const char * value = FindLabel(err, err_table))
63 base::snprintf(buffer, sizeof(buffer), "0x%08x", err);
67 /////////////////////////////////////////////////////////////////////////////
68 // Log helper functions
69 /////////////////////////////////////////////////////////////////////////////
71 // Generates extra information for LOG_E.
72 static std::string GenerateExtra(LogErrorContext err_ctx,
75 if (err_ctx != ERRCTX_NONE) {
76 std::ostringstream tmp;
78 tmp << "[0x" << std::setfill('0') << std::hex << std::setw(8) << err << "]";
81 tmp << " " << strerror(err);
84 case ERRCTX_HRESULT: {
86 DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM;
87 HMODULE hmod = GetModuleHandleA(module);
89 flags |= FORMAT_MESSAGE_FROM_HMODULE;
90 if (DWORD len = FormatMessageA(
92 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
93 msgbuf, sizeof(msgbuf) / sizeof(msgbuf[0]), NULL)) {
95 isspace(static_cast<unsigned char>(msgbuf[len-1]))) {
104 case ERRCTX_OSSTATUS:
105 tmp << " " << "Unknown LibJingle error: " << err;
107 #elif defined(OS_MACOSX)
108 case ERRCTX_OSSTATUS: {
109 tmp << " " << nonnull(GetMacOSStatusErrorString(err), "Unknown error");
110 if (const char* desc = GetMacOSStatusCommentString(err)) {
124 DiagnosticLogMessage::DiagnosticLogMessage(const char* file,
126 LoggingSeverity severity,
128 LogErrorContext err_ctx,
133 log_to_chrome_(log_to_chrome) {
134 extra_ = GenerateExtra(err_ctx, err, NULL);
137 DiagnosticLogMessage::DiagnosticLogMessage(const char* file,
139 LoggingSeverity severity,
141 LogErrorContext err_ctx,
147 log_to_chrome_(log_to_chrome) {
148 extra_ = GenerateExtra(err_ctx, err, module);
151 DiagnosticLogMessage::~DiagnosticLogMessage() {
152 print_stream_ << extra_;
153 const std::string& str = print_stream_.str();
155 LOG_LAZY_STREAM_DIRECT(file_name_, line_, severity_) << str;
156 if (g_logging_delegate_function && severity_ <= LS_INFO) {
157 g_logging_delegate_function(str);
161 // Note: this function is a copy from the overriden libjingle implementation.
162 void LogMultiline(LoggingSeverity level, const char* label, bool input,
163 const void* data, size_t len, bool hex_mode,
164 LogMultilineState* state) {
165 if (!LOG_CHECK_LEVEL_V(level))
168 const char * direction = (input ? " << " : " >> ");
170 // NULL data means to flush our count of unprintable characters.
172 if (state && state->unprintable_count_[input]) {
173 LOG_V(level) << label << direction << "## "
174 << state->unprintable_count_[input]
175 << " consecutive unprintable ##";
176 state->unprintable_count_[input] = 0;
181 // The ctype classification functions want unsigned chars.
182 const unsigned char* udata = static_cast<const unsigned char*>(data);
185 const size_t LINE_SIZE = 24;
186 char hex_line[LINE_SIZE * 9 / 4 + 2], asc_line[LINE_SIZE + 1];
188 memset(asc_line, ' ', sizeof(asc_line));
189 memset(hex_line, ' ', sizeof(hex_line));
190 size_t line_len = _min(len, LINE_SIZE);
191 for (size_t i = 0; i < line_len; ++i) {
192 unsigned char ch = udata[i];
193 asc_line[i] = isprint(ch) ? ch : '.';
194 hex_line[i*2 + i/4] = hex_encode(ch >> 4);
195 hex_line[i*2 + i/4 + 1] = hex_encode(ch & 0xf);
197 asc_line[sizeof(asc_line)-1] = 0;
198 hex_line[sizeof(hex_line)-1] = 0;
199 LOG_V(level) << label << direction
200 << asc_line << " " << hex_line << " ";
207 size_t consecutive_unprintable = state ? state->unprintable_count_[input] : 0;
209 const unsigned char* end = udata + len;
210 while (udata < end) {
211 const unsigned char* line = udata;
212 const unsigned char* end_of_line = strchrn<unsigned char>(udata,
216 udata = end_of_line = end;
218 udata = end_of_line + 1;
221 bool is_printable = true;
223 // If we are in unprintable mode, we need to see a line of at least
224 // kMinPrintableLine characters before we'll switch back.
225 const ptrdiff_t kMinPrintableLine = 4;
226 if (consecutive_unprintable && ((end_of_line - line) < kMinPrintableLine)) {
227 is_printable = false;
229 // Determine if the line contains only whitespace and printable
231 bool is_entirely_whitespace = true;
232 for (const unsigned char* pos = line; pos < end_of_line; ++pos) {
235 is_entirely_whitespace = false;
236 if (!isprint(*pos)) {
237 is_printable = false;
241 // Treat an empty line following unprintable data as unprintable.
242 if (consecutive_unprintable && is_entirely_whitespace) {
243 is_printable = false;
247 consecutive_unprintable += (udata - line);
250 // Print out the current line, but prefix with a count of prior unprintable
252 if (consecutive_unprintable) {
253 LOG_V(level) << label << direction << "## " << consecutive_unprintable
254 << " consecutive unprintable ##";
255 consecutive_unprintable = 0;
257 // Strip off trailing whitespace.
258 while ((end_of_line > line) && isspace(*(end_of_line-1))) {
261 // Filter out any private data
262 std::string substr(reinterpret_cast<const char*>(line), end_of_line - line);
263 std::string::size_type pos_private = substr.find("Email");
264 if (pos_private == std::string::npos) {
265 pos_private = substr.find("Passwd");
267 if (pos_private == std::string::npos) {
268 LOG_V(level) << label << direction << substr;
270 LOG_V(level) << label << direction << "## omitted for privacy ##";
275 state->unprintable_count_[input] = consecutive_unprintable;
279 void InitDiagnosticLoggingDelegateFunction(
280 void (*delegate)(const std::string&)) {
282 // Ensure that this function is always called from the same thread.
283 base::subtle::NoBarrier_CompareAndSwap(&g_init_logging_delegate_thread_id, 0,
284 static_cast<base::subtle::Atomic32>(base::PlatformThread::CurrentId()));
285 DCHECK_EQ(g_init_logging_delegate_thread_id,
286 base::PlatformThread::CurrentId());
289 // This function may be called with the same argument several times if the
290 // page is reloaded or there are several PeerConnections on one page with
291 // logging enabled. This is OK, we simply don't have to do anything.
292 if (delegate == g_logging_delegate_function)
294 CHECK(!g_logging_delegate_function);
296 IPAddress::set_strip_sensitive(true);
298 g_logging_delegate_function = delegate;
300 if (g_extra_logging_init_function)
301 g_extra_logging_init_function(delegate);
304 void SetExtraLoggingInit(
305 void (*function)(void (*delegate)(const std::string&))) {
307 CHECK(!g_extra_logging_init_function);
308 g_extra_logging_init_function = function;
311 } // namespace talk_base