logging.cpp (3662B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 // This is a stripped down version of the Chromium source file base/logging.cc 8 // This prevents dependency on the Chromium logging and dependency creep in 9 // general. 10 // At some point we should find a way to hook this into our own logging see 11 // bug 1013988. 12 // The formatting in this file matches the original Chromium file to aid future 13 // merging. 14 15 #include "base/logging.h" 16 17 #if defined(OS_WIN) 18 #include <windows.h> 19 #endif 20 21 #if defined(OS_POSIX) 22 #include <errno.h> 23 #include <string.h> 24 #endif 25 26 #include "base/strings/stringprintf.h" 27 28 #if defined(OS_WIN) 29 #include "base/strings/utf_string_conversions.h" 30 #endif 31 32 #include <algorithm> 33 34 #include "mozilla/Assertions.h" 35 36 namespace logging { 37 38 namespace { 39 40 int g_min_log_level = 0; 41 42 LoggingDestination g_logging_destination = LOG_DEFAULT; 43 44 // For LOG_ERROR and above, always print to stderr. 45 const int kAlwaysPrintErrorLevel = LOG_ERROR; 46 47 // A log message handler that gets notified of every log message we process. 48 LogMessageHandlerFunction log_message_handler = nullptr; 49 50 } // namespace 51 52 // This is never instantiated, it's just used for EAT_STREAM_PARAMETERS to have 53 // an object of the correct type on the LHS of the unused part of the ternary 54 // operator. 55 std::ostream* g_swallow_stream; 56 57 void SetMinLogLevel(int level) { 58 g_min_log_level = std::min(LOG_FATAL, level); 59 } 60 61 int GetMinLogLevel() { 62 return g_min_log_level; 63 } 64 65 bool ShouldCreateLogMessage(int severity) { 66 if (severity < g_min_log_level) 67 return false; 68 69 // Return true here unless we know ~LogMessage won't do anything. Note that 70 // ~LogMessage writes to stderr if severity_ >= kAlwaysPrintErrorLevel, even 71 // when g_logging_destination is LOG_NONE. 72 return g_logging_destination != LOG_NONE || log_message_handler || 73 severity >= kAlwaysPrintErrorLevel; 74 } 75 76 int GetVlogLevelHelper(const char* file, size_t N) { 77 return 0; 78 } 79 80 LogMessage::LogMessage(const char* file, int line, LogSeverity severity) 81 : severity_(severity), file_(file), line_(line) { 82 } 83 84 LogMessage::LogMessage(const char* file, int line, const char* condition) 85 : severity_(LOG_FATAL), file_(file), line_(line) { 86 } 87 88 LogMessage::~LogMessage() { 89 if (severity_ == LOG_FATAL) { 90 MOZ_CRASH("Hit fatal chromium sandbox condition."); 91 } 92 } 93 94 SystemErrorCode GetLastSystemErrorCode() { 95 #if defined(OS_WIN) 96 return ::GetLastError(); 97 #elif defined(OS_POSIX) 98 return errno; 99 #else 100 #error Not implemented 101 #endif 102 } 103 104 #if BUILDFLAG(IS_WIN) 105 Win32ErrorLogMessage::Win32ErrorLogMessage(const char* file, int line, 106 LogSeverity severity, 107 SystemErrorCode err) 108 : LogMessage(file, line, severity), err_(err) { 109 (void)err_; 110 } 111 112 Win32ErrorLogMessage::~Win32ErrorLogMessage() {} 113 #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) 114 ErrnoLogMessage::ErrnoLogMessage(const char* file, 115 int line, 116 LogSeverity severity, 117 SystemErrorCode err) 118 : LogMessage(file, line, severity), err_(err) { 119 (void)err_; 120 } 121 122 ErrnoLogMessage::~ErrnoLogMessage() { 123 } 124 #endif // BUILDFLAG(IS_WIN)) 125 126 void RawLog(int level, const char* message) { 127 } 128 129 #if !BUILDFLAG(USE_RUNTIME_VLOG) 130 int GetDisableAllVLogLevel() { 131 return -1; 132 } 133 #endif // !BUILDFLAG(USE_RUNTIME_VLOG) 134 135 } // namespace logging