123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704 |
- #ifndef BASE_LOGGING_H_
- #define BASE_LOGGING_H_
- #include <stddef.h>
- #include <cassert>
- #include <cstdint>
- #include <sstream>
- #include <string>
- #include "base/base_export.h"
- #include "base/callback_forward.h"
- #include "base/check.h"
- #include "base/check_op.h"
- #include "base/compiler_specific.h"
- #include "base/macros.h"
- #include "base/notreached.h"
- #include "base/scoped_clear_last_error.h"
- #include "base/strings/string_piece_forward.h"
- #if defined(OS_CHROMEOS)
- #include <cstdio>
- #endif
- namespace logging {
- #if defined(OS_WIN)
- typedef wchar_t PathChar;
- #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
- typedef char PathChar;
- #endif
- using LoggingDestination = uint32_t;
- enum : uint32_t {
- LOG_NONE = 0,
- LOG_TO_FILE = 1 << 0,
- LOG_TO_SYSTEM_DEBUG_LOG = 1 << 1,
- LOG_TO_STDERR = 1 << 2,
- LOG_TO_ALL = LOG_TO_FILE | LOG_TO_SYSTEM_DEBUG_LOG | LOG_TO_STDERR,
- #if defined(OS_FUCHSIA) || defined(OS_NACL)
- LOG_DEFAULT = LOG_TO_SYSTEM_DEBUG_LOG,
- #elif defined(OS_WIN)
- LOG_DEFAULT = LOG_TO_FILE,
- #elif defined(OS_POSIX)
- LOG_DEFAULT = LOG_TO_SYSTEM_DEBUG_LOG | LOG_TO_STDERR,
- #endif
- };
- enum LogLockingState { LOCK_LOG_FILE, DONT_LOCK_LOG_FILE };
- enum OldFileDeletionState { DELETE_OLD_LOG_FILE, APPEND_TO_OLD_LOG_FILE };
- struct BASE_EXPORT LoggingSettings {
-
-
- uint32_t logging_dest = LOG_DEFAULT;
-
-
- const PathChar* log_file_path = nullptr;
- LogLockingState lock_log = LOCK_LOG_FILE;
- OldFileDeletionState delete_old = APPEND_TO_OLD_LOG_FILE;
- #if defined(OS_CHROMEOS)
-
-
-
-
- FILE* log_file = nullptr;
- #endif
- };
- #if defined(NDEBUG)
- #define BaseInitLoggingImpl BaseInitLoggingImpl_built_with_NDEBUG
- #else
- #define BaseInitLoggingImpl BaseInitLoggingImpl_built_without_NDEBUG
- #endif
- BASE_EXPORT bool BaseInitLoggingImpl(const LoggingSettings& settings);
- inline bool InitLogging(const LoggingSettings& settings) {
- return BaseInitLoggingImpl(settings);
- }
- BASE_EXPORT void SetMinLogLevel(int level);
- BASE_EXPORT int GetMinLogLevel();
- BASE_EXPORT bool ShouldCreateLogMessage(int severity);
- BASE_EXPORT int GetVlogVerbosity();
- BASE_EXPORT int GetVlogLevelHelper(const char* file_start, size_t N);
- template <size_t N>
- int GetVlogLevel(const char (&file)[N]) {
- return GetVlogLevelHelper(file, N);
- }
- BASE_EXPORT void SetLogItems(bool enable_process_id, bool enable_thread_id,
- bool enable_timestamp, bool enable_tickcount);
- BASE_EXPORT void SetLogPrefix(const char* prefix);
- BASE_EXPORT void SetShowErrorDialogs(bool enable_dialogs);
- using LogAssertHandlerFunction =
- base::RepeatingCallback<void(const char* file,
- int line,
- const base::StringPiece message,
- const base::StringPiece stack_trace)>;
- class BASE_EXPORT ScopedLogAssertHandler {
- public:
- explicit ScopedLogAssertHandler(LogAssertHandlerFunction handler);
- ~ScopedLogAssertHandler();
- private:
- DISALLOW_COPY_AND_ASSIGN(ScopedLogAssertHandler);
- };
- typedef bool (*LogMessageHandlerFunction)(int severity,
- const char* file, int line, size_t message_start, const std::string& str);
- BASE_EXPORT void SetLogMessageHandler(LogMessageHandlerFunction handler);
- BASE_EXPORT LogMessageHandlerFunction GetLogMessageHandler();
- typedef int LogSeverity;
- const LogSeverity LOG_VERBOSE = -1;
- const LogSeverity LOG_INFO = 0;
- const LogSeverity LOG_WARNING = 1;
- const LogSeverity LOG_ERROR = 2;
- const LogSeverity LOG_FATAL = 3;
- const LogSeverity LOG_NUM_SEVERITIES = 4;
- #if defined(NDEBUG)
- const LogSeverity LOG_DFATAL = LOG_ERROR;
- #else
- const LogSeverity LOG_DFATAL = LOG_FATAL;
- #endif
- #define COMPACT_GOOGLE_LOG_EX_INFO(ClassName, ...) \
- ::logging::ClassName(__FILE__, __LINE__, ::logging::LOG_INFO, ##__VA_ARGS__)
- #define COMPACT_GOOGLE_LOG_EX_WARNING(ClassName, ...) \
- ::logging::ClassName(__FILE__, __LINE__, ::logging::LOG_WARNING, \
- ##__VA_ARGS__)
- #define COMPACT_GOOGLE_LOG_EX_ERROR(ClassName, ...) \
- ::logging::ClassName(__FILE__, __LINE__, ::logging::LOG_ERROR, ##__VA_ARGS__)
- #define COMPACT_GOOGLE_LOG_EX_FATAL(ClassName, ...) \
- ::logging::ClassName(__FILE__, __LINE__, ::logging::LOG_FATAL, ##__VA_ARGS__)
- #define COMPACT_GOOGLE_LOG_EX_DFATAL(ClassName, ...) \
- ::logging::ClassName(__FILE__, __LINE__, ::logging::LOG_DFATAL, ##__VA_ARGS__)
- #define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \
- ::logging::ClassName(__FILE__, __LINE__, ::logging::LOG_DCHECK, ##__VA_ARGS__)
- #define COMPACT_GOOGLE_LOG_INFO COMPACT_GOOGLE_LOG_EX_INFO(LogMessage)
- #define COMPACT_GOOGLE_LOG_WARNING COMPACT_GOOGLE_LOG_EX_WARNING(LogMessage)
- #define COMPACT_GOOGLE_LOG_ERROR COMPACT_GOOGLE_LOG_EX_ERROR(LogMessage)
- #define COMPACT_GOOGLE_LOG_FATAL COMPACT_GOOGLE_LOG_EX_FATAL(LogMessage)
- #define COMPACT_GOOGLE_LOG_DFATAL COMPACT_GOOGLE_LOG_EX_DFATAL(LogMessage)
- #define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_EX_DCHECK(LogMessage)
- #if defined(OS_WIN)
- #define ERROR 0
- #define COMPACT_GOOGLE_LOG_EX_0(ClassName, ...) \
- COMPACT_GOOGLE_LOG_EX_ERROR(ClassName , ##__VA_ARGS__)
- #define COMPACT_GOOGLE_LOG_0 COMPACT_GOOGLE_LOG_ERROR
- const LogSeverity LOG_0 = LOG_ERROR;
- #endif
- #define LOG_IS_ON(severity) \
- (::logging::ShouldCreateLogMessage(::logging::LOG_##severity))
- #define VLOG_IS_ON(verboselevel) \
- ((verboselevel) <= ::logging::GetVlogLevel(__FILE__))
- #define LAZY_STREAM(stream, condition) \
- !(condition) ? (void) 0 : ::logging::LogMessageVoidify() & (stream)
- #define LOG_STREAM(severity) COMPACT_GOOGLE_LOG_ ## severity.stream()
- #define LOG(severity) LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity))
- #define LOG_IF(severity, condition) \
- LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity) && (condition))
- #define VLOG_STREAM(verbose_level) \
- ::logging::LogMessage(__FILE__, __LINE__, -verbose_level).stream()
- #define VLOG(verbose_level) \
- LAZY_STREAM(VLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level))
- #define VLOG_IF(verbose_level, condition) \
- LAZY_STREAM(VLOG_STREAM(verbose_level), \
- VLOG_IS_ON(verbose_level) && (condition))
- #if defined (OS_WIN)
- #define VPLOG_STREAM(verbose_level) \
- ::logging::Win32ErrorLogMessage(__FILE__, __LINE__, -verbose_level, \
- ::logging::GetLastSystemErrorCode()).stream()
- #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
- #define VPLOG_STREAM(verbose_level) \
- ::logging::ErrnoLogMessage(__FILE__, __LINE__, -verbose_level, \
- ::logging::GetLastSystemErrorCode()).stream()
- #endif
- #define VPLOG(verbose_level) \
- LAZY_STREAM(VPLOG_STREAM(verbose_level), VLOG_IS_ON(verbose_level))
- #define VPLOG_IF(verbose_level, condition) \
- LAZY_STREAM(VPLOG_STREAM(verbose_level), \
- VLOG_IS_ON(verbose_level) && (condition))
- #define LOG_ASSERT(condition) \
- LOG_IF(FATAL, !(ANALYZER_ASSUME_TRUE(condition))) \
- << "Assert failed: " #condition ". "
- #if defined(OS_WIN)
- #define PLOG_STREAM(severity) \
- COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \
- ::logging::GetLastSystemErrorCode()).stream()
- #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
- #define PLOG_STREAM(severity) \
- COMPACT_GOOGLE_LOG_EX_ ## severity(ErrnoLogMessage, \
- ::logging::GetLastSystemErrorCode()).stream()
- #endif
- #define PLOG(severity) \
- LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity))
- #define PLOG_IF(severity, condition) \
- LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity) && (condition))
- BASE_EXPORT extern std::ostream* g_swallow_stream;
- #define EAT_STREAM_PARAMETERS \
- true ? (void)0 \
- : ::logging::LogMessageVoidify() & (*::logging::g_swallow_stream)
- #if DCHECK_IS_ON()
- #define DLOG_IS_ON(severity) LOG_IS_ON(severity)
- #define DLOG_IF(severity, condition) LOG_IF(severity, condition)
- #define DLOG_ASSERT(condition) LOG_ASSERT(condition)
- #define DPLOG_IF(severity, condition) PLOG_IF(severity, condition)
- #define DVLOG_IF(verboselevel, condition) VLOG_IF(verboselevel, condition)
- #define DVPLOG_IF(verboselevel, condition) VPLOG_IF(verboselevel, condition)
- #else
- #define DLOG_IS_ON(severity) false
- #define DLOG_IF(severity, condition) EAT_STREAM_PARAMETERS
- #define DLOG_ASSERT(condition) EAT_STREAM_PARAMETERS
- #define DPLOG_IF(severity, condition) EAT_STREAM_PARAMETERS
- #define DVLOG_IF(verboselevel, condition) EAT_STREAM_PARAMETERS
- #define DVPLOG_IF(verboselevel, condition) EAT_STREAM_PARAMETERS
- #endif
- #define DLOG(severity) \
- LAZY_STREAM(LOG_STREAM(severity), DLOG_IS_ON(severity))
- #define DPLOG(severity) \
- LAZY_STREAM(PLOG_STREAM(severity), DLOG_IS_ON(severity))
- #define DVLOG(verboselevel) DVLOG_IF(verboselevel, true)
- #define DVPLOG(verboselevel) DVPLOG_IF(verboselevel, true)
- #if DCHECK_IS_ON()
- #if defined(DCHECK_IS_CONFIGURABLE)
- BASE_EXPORT extern LogSeverity LOG_DCHECK;
- #else
- const LogSeverity LOG_DCHECK = LOG_FATAL;
- #endif
- #else
- const LogSeverity LOG_DCHECK = LOG_FATAL;
- #endif
- #undef assert
- #define assert(x) DLOG_ASSERT(x)
- class BASE_EXPORT LogMessage {
- public:
-
- LogMessage(const char* file, int line, LogSeverity severity);
-
- LogMessage(const char* file, int line, const char* condition);
- virtual ~LogMessage();
- std::ostream& stream() { return stream_; }
- LogSeverity severity() { return severity_; }
- std::string str() { return stream_.str(); }
- private:
- void Init(const char* file, int line);
- LogSeverity severity_;
- std::ostringstream stream_;
- size_t message_start_;
-
-
- const char* file_;
- const int line_;
- const char* file_basename_;
-
-
-
- base::ScopedClearLastError last_error_;
- DISALLOW_COPY_AND_ASSIGN(LogMessage);
- };
- class LogMessageVoidify {
- public:
- LogMessageVoidify() = default;
-
-
- void operator&(std::ostream&) { }
- };
- #if defined(OS_WIN)
- typedef unsigned long SystemErrorCode;
- #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
- typedef int SystemErrorCode;
- #endif
- BASE_EXPORT SystemErrorCode GetLastSystemErrorCode();
- BASE_EXPORT std::string SystemErrorCodeToString(SystemErrorCode error_code);
- #if defined(OS_WIN)
- class BASE_EXPORT Win32ErrorLogMessage : public LogMessage {
- public:
- Win32ErrorLogMessage(const char* file,
- int line,
- LogSeverity severity,
- SystemErrorCode err);
-
- ~Win32ErrorLogMessage() override;
- private:
- SystemErrorCode err_;
- DISALLOW_COPY_AND_ASSIGN(Win32ErrorLogMessage);
- };
- #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
- class BASE_EXPORT ErrnoLogMessage : public LogMessage {
- public:
- ErrnoLogMessage(const char* file,
- int line,
- LogSeverity severity,
- SystemErrorCode err);
-
- ~ErrnoLogMessage() override;
- private:
- SystemErrorCode err_;
- DISALLOW_COPY_AND_ASSIGN(ErrnoLogMessage);
- };
- #endif
- BASE_EXPORT void CloseLogFile();
- #if defined(OS_CHROMEOS)
- BASE_EXPORT FILE* DuplicateLogFILE();
- #endif
- BASE_EXPORT void RawLog(int level, const char* message);
- #define RAW_LOG(level, message) \
- ::logging::RawLog(::logging::LOG_##level, message)
- #if defined(OS_WIN)
- BASE_EXPORT bool IsLoggingToFileEnabled();
- BASE_EXPORT std::wstring GetLogFileFullPath();
- #endif
- }
- namespace std {
- BASE_EXPORT std::ostream& operator<<(std::ostream& out, const wchar_t* wstr);
- inline std::ostream& operator<<(std::ostream& out, const std::wstring& wstr) {
- return out << wstr.c_str();
- }
- }
- #endif
|