123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712 |
- #ifndef RTC_BASE_LOGGING_H_
- #define RTC_BASE_LOGGING_H_
- #include <errno.h>
- #include <atomic>
- #include <sstream> // no-presubmit-check TODO(webrtc:8982)
- #include <string>
- #include <utility>
- #include "absl/meta/type_traits.h"
- #include "absl/strings/string_view.h"
- #include "rtc_base/constructor_magic.h"
- #include "rtc_base/deprecation.h"
- #include "rtc_base/strings/string_builder.h"
- #include "rtc_base/system/inline.h"
- #if !defined(NDEBUG) || defined(DLOG_ALWAYS_ON)
- #define RTC_DLOG_IS_ON 1
- #else
- #define RTC_DLOG_IS_ON 0
- #endif
- #if defined(RTC_DISABLE_LOGGING)
- #define RTC_LOG_ENABLED() 0
- #else
- #define RTC_LOG_ENABLED() 1
- #endif
- namespace rtc {
- enum LoggingSeverity {
- LS_VERBOSE,
- LS_INFO,
- LS_WARNING,
- LS_ERROR,
- LS_NONE,
- INFO = LS_INFO,
- WARNING = LS_WARNING,
- LERROR = LS_ERROR
- };
- enum LogErrorContext {
- ERRCTX_NONE,
- ERRCTX_ERRNO,
- ERRCTX_HRESULT,
-
- ERRCTX_EN = ERRCTX_ERRNO,
- ERRCTX_HR = ERRCTX_HRESULT,
- };
- class LogMessage;
- class LogSink {
- public:
- LogSink() {}
- virtual ~LogSink() {}
- virtual void OnLogMessage(const std::string& msg,
- LoggingSeverity severity,
- const char* tag);
- virtual void OnLogMessage(const std::string& message,
- LoggingSeverity severity);
- virtual void OnLogMessage(const std::string& message) = 0;
- private:
- friend class ::rtc::LogMessage;
- #if RTC_LOG_ENABLED()
-
- LogSink* next_ = nullptr;
- LoggingSeverity min_severity_;
- #endif
- };
- namespace webrtc_logging_impl {
- class LogMetadata {
- public:
- LogMetadata(const char* file, int line, LoggingSeverity severity)
- : file_(file),
- line_and_sev_(static_cast<uint32_t>(line) << 3 | severity) {}
- LogMetadata() = default;
- const char* File() const { return file_; }
- int Line() const { return line_and_sev_ >> 3; }
- LoggingSeverity Severity() const {
- return static_cast<LoggingSeverity>(line_and_sev_ & 0x7);
- }
- private:
- const char* file_;
-
-
-
-
- uint32_t line_and_sev_;
- };
- static_assert(std::is_trivial<LogMetadata>::value, "");
- struct LogMetadataErr {
- LogMetadata meta;
- LogErrorContext err_ctx;
- int err;
- };
- #ifdef WEBRTC_ANDROID
- struct LogMetadataTag {
- LoggingSeverity severity;
- const char* tag;
- };
- #endif
- enum class LogArgType : int8_t {
- kEnd = 0,
- kInt,
- kLong,
- kLongLong,
- kUInt,
- kULong,
- kULongLong,
- kDouble,
- kLongDouble,
- kCharP,
- kStdString,
- kStringView,
- kVoidP,
- kLogMetadata,
- kLogMetadataErr,
- #ifdef WEBRTC_ANDROID
- kLogMetadataTag,
- #endif
- };
- template <LogArgType N, typename T>
- struct Val {
- static constexpr LogArgType Type() { return N; }
- T GetVal() const { return val; }
- T val;
- };
- struct ToStringVal {
- static constexpr LogArgType Type() { return LogArgType::kStdString; }
- const std::string* GetVal() const { return &val; }
- std::string val;
- };
- inline Val<LogArgType::kInt, int> MakeVal(int x) {
- return {x};
- }
- inline Val<LogArgType::kLong, long> MakeVal(long x) {
- return {x};
- }
- inline Val<LogArgType::kLongLong, long long> MakeVal(long long x) {
- return {x};
- }
- inline Val<LogArgType::kUInt, unsigned int> MakeVal(unsigned int x) {
- return {x};
- }
- inline Val<LogArgType::kULong, unsigned long> MakeVal(unsigned long x) {
- return {x};
- }
- inline Val<LogArgType::kULongLong, unsigned long long> MakeVal(
- unsigned long long x) {
- return {x};
- }
- inline Val<LogArgType::kDouble, double> MakeVal(double x) {
- return {x};
- }
- inline Val<LogArgType::kLongDouble, long double> MakeVal(long double x) {
- return {x};
- }
- inline Val<LogArgType::kCharP, const char*> MakeVal(const char* x) {
- return {x};
- }
- inline Val<LogArgType::kStdString, const std::string*> MakeVal(
- const std::string& x) {
- return {&x};
- }
- inline Val<LogArgType::kStringView, const absl::string_view*> MakeVal(
- const absl::string_view& x) {
- return {&x};
- }
- inline Val<LogArgType::kVoidP, const void*> MakeVal(const void* x) {
- return {x};
- }
- inline Val<LogArgType::kLogMetadata, LogMetadata> MakeVal(
- const LogMetadata& x) {
- return {x};
- }
- inline Val<LogArgType::kLogMetadataErr, LogMetadataErr> MakeVal(
- const LogMetadataErr& x) {
- return {x};
- }
- template <typename T,
- absl::enable_if_t<std::is_enum<T>::value &&
- !std::is_arithmetic<T>::value>* = nullptr>
- inline decltype(MakeVal(std::declval<absl::underlying_type_t<T>>())) MakeVal(
- T x) {
- return {static_cast<absl::underlying_type_t<T>>(x)};
- }
- #ifdef WEBRTC_ANDROID
- inline Val<LogArgType::kLogMetadataTag, LogMetadataTag> MakeVal(
- const LogMetadataTag& x) {
- return {x};
- }
- #endif
- template <typename T, class = void>
- struct has_to_log_string : std::false_type {};
- template <typename T>
- struct has_to_log_string<T, decltype(ToLogString(std::declval<T>()))>
- : std::true_type {};
- template <
- typename T,
- typename T1 = absl::decay_t<T>,
- absl::enable_if_t<std::is_class<T1>::value &&
- !std::is_same<T1, std::string>::value &&
- !std::is_same<T1, LogMetadata>::value &&
- !has_to_log_string<T1>::value &&
- #ifdef WEBRTC_ANDROID
- !std::is_same<T1, LogMetadataTag>::value &&
- #endif
- !std::is_same<T1, LogMetadataErr>::value>* = nullptr>
- ToStringVal MakeVal(const T& x) {
- std::ostringstream os;
- os << x;
- return {os.str()};
- }
- template <typename T, absl::enable_if_t<has_to_log_string<T>::value>* = nullptr>
- ToStringVal MakeVal(const T& x) {
- return {ToLogString(x)};
- }
- #if RTC_LOG_ENABLED()
- void Log(const LogArgType* fmt, ...);
- #else
- inline void Log(const LogArgType* fmt, ...) {
-
- }
- #endif
- template <typename... Ts>
- class LogStreamer;
- template <>
- class LogStreamer<> final {
- public:
- template <typename U,
- typename V = decltype(MakeVal(std::declval<U>())),
- absl::enable_if_t<std::is_arithmetic<U>::value ||
- std::is_enum<U>::value>* = nullptr>
- RTC_FORCE_INLINE LogStreamer<V> operator<<(U arg) const {
- return LogStreamer<V>(MakeVal(arg), this);
- }
- template <typename U,
- typename V = decltype(MakeVal(std::declval<U>())),
- absl::enable_if_t<!std::is_arithmetic<U>::value &&
- !std::is_enum<U>::value>* = nullptr>
- RTC_FORCE_INLINE LogStreamer<V> operator<<(const U& arg) const {
- return LogStreamer<V>(MakeVal(arg), this);
- }
- template <typename... Us>
- RTC_FORCE_INLINE static void Call(const Us&... args) {
- static constexpr LogArgType t[] = {Us::Type()..., LogArgType::kEnd};
- Log(t, args.GetVal()...);
- }
- };
- template <typename T, typename... Ts>
- class LogStreamer<T, Ts...> final {
- public:
- RTC_FORCE_INLINE LogStreamer(T arg, const LogStreamer<Ts...>* prior)
- : arg_(arg), prior_(prior) {}
- template <typename U,
- typename V = decltype(MakeVal(std::declval<U>())),
- absl::enable_if_t<std::is_arithmetic<U>::value ||
- std::is_enum<U>::value>* = nullptr>
- RTC_FORCE_INLINE LogStreamer<V, T, Ts...> operator<<(U arg) const {
- return LogStreamer<V, T, Ts...>(MakeVal(arg), this);
- }
- template <typename U,
- typename V = decltype(MakeVal(std::declval<U>())),
- absl::enable_if_t<!std::is_arithmetic<U>::value &&
- !std::is_enum<U>::value>* = nullptr>
- RTC_FORCE_INLINE LogStreamer<V, T, Ts...> operator<<(const U& arg) const {
- return LogStreamer<V, T, Ts...>(MakeVal(arg), this);
- }
- template <typename... Us>
- RTC_FORCE_INLINE void Call(const Us&... args) const {
- prior_->Call(arg_, args...);
- }
- private:
-
- T arg_;
-
- const LogStreamer<Ts...>* prior_;
- };
- class LogCall final {
- public:
-
-
-
- template <typename... Ts>
- RTC_FORCE_INLINE bool operator&(const LogStreamer<Ts...>& streamer) {
- streamer.Call();
- return true;
- }
- };
- class LogMessageVoidify {
- public:
- LogMessageVoidify() = default;
-
-
- template <typename... Ts>
- void operator&(LogStreamer<Ts...>&& streamer) {}
- };
- }
- class LogMessage {
- public:
-
-
-
- template <LoggingSeverity S>
- RTC_NO_INLINE LogMessage(const char* file,
- int line,
- std::integral_constant<LoggingSeverity, S>)
- : LogMessage(file, line, S) {}
- #if RTC_LOG_ENABLED()
- LogMessage(const char* file, int line, LoggingSeverity sev);
- LogMessage(const char* file,
- int line,
- LoggingSeverity sev,
- LogErrorContext err_ctx,
- int err);
- #if defined(WEBRTC_ANDROID)
- LogMessage(const char* file, int line, LoggingSeverity sev, const char* tag);
- #endif
-
-
-
- RTC_DEPRECATED
- LogMessage(const char* file,
- int line,
- LoggingSeverity sev,
- const std::string& tag);
- ~LogMessage();
- void AddTag(const char* tag);
- rtc::StringBuilder& stream();
-
-
-
-
-
- static int64_t LogStartTime();
-
-
- static uint32_t WallClockStartTime();
-
- static void LogThreads(bool on = true);
-
- static void LogTimestamps(bool on = true);
-
-
- static void LogToDebug(LoggingSeverity min_sev);
- static LoggingSeverity GetLogToDebug();
-
- static void SetLogToStderr(bool log_to_stderr);
-
-
-
-
-
-
- static void AddLogToStream(LogSink* stream, LoggingSeverity min_sev);
-
-
-
- static void RemoveLogToStream(LogSink* stream);
-
-
- static int GetLogToStream(LogSink* stream = nullptr);
-
-
- static int GetMinLogSeverity();
-
-
- static void ConfigureLogging(const char* params);
-
-
-
-
- static bool IsNoop(LoggingSeverity severity);
-
-
- template <LoggingSeverity S>
- RTC_NO_INLINE static bool IsNoop() {
- return IsNoop(S);
- }
- #else
-
- LogMessage(const char* file, int line, LoggingSeverity sev) {}
- LogMessage(const char* file,
- int line,
- LoggingSeverity sev,
- LogErrorContext err_ctx,
- int err) {}
- #if defined(WEBRTC_ANDROID)
- LogMessage(const char* file, int line, LoggingSeverity sev, const char* tag) {
- }
- #endif
-
-
-
- RTC_DEPRECATED
- LogMessage(const char* file,
- int line,
- LoggingSeverity sev,
- const std::string& tag) {}
- ~LogMessage() = default;
- inline void AddTag(const char* tag) {}
- inline rtc::StringBuilder& stream() { return print_stream_; }
- inline static int64_t LogStartTime() { return 0; }
- inline static uint32_t WallClockStartTime() { return 0; }
- inline static void LogThreads(bool on = true) {}
- inline static void LogTimestamps(bool on = true) {}
- inline static void LogToDebug(LoggingSeverity min_sev) {}
- inline static LoggingSeverity GetLogToDebug() {
- return LoggingSeverity::LS_INFO;
- }
- inline static void SetLogToStderr(bool log_to_stderr) {}
- inline static void AddLogToStream(LogSink* stream, LoggingSeverity min_sev) {}
- inline static void RemoveLogToStream(LogSink* stream) {}
- inline static int GetLogToStream(LogSink* stream = nullptr) { return 0; }
- inline static int GetMinLogSeverity() { return 0; }
- inline static void ConfigureLogging(const char* params) {}
- static constexpr bool IsNoop(LoggingSeverity severity) { return true; }
- template <LoggingSeverity S>
- static constexpr bool IsNoop() {
- return IsNoop(S);
- }
- #endif
- private:
- friend class LogMessageForTesting;
- #if RTC_LOG_ENABLED()
-
- static void UpdateMinLogSeverity();
- #if defined(WEBRTC_ANDROID)
- static void OutputToDebug(const std::string& msg,
- LoggingSeverity severity,
- const char* tag);
- #else
- static void OutputToDebug(const std::string& msg, LoggingSeverity severity);
- #endif
-
-
- void FinishPrintStream();
-
- LoggingSeverity severity_;
- #if defined(WEBRTC_ANDROID)
-
- const char* tag_ = "libjingle";
- #endif
-
-
- std::string extra_;
-
- static LogSink* streams_;
-
-
-
-
- static std::atomic<bool> streams_empty_;
-
- static bool thread_, timestamp_;
-
- static bool log_to_stderr_;
- #else
-
- inline static void UpdateMinLogSeverity() {}
- #if defined(WEBRTC_ANDROID)
- inline static void OutputToDebug(const std::string& msg,
- LoggingSeverity severity,
- const char* tag) {}
- #else
- inline static void OutputToDebug(const std::string& msg,
- LoggingSeverity severity) {}
- #endif
- inline void FinishPrintStream() {}
- #endif
-
- rtc::StringBuilder print_stream_;
- RTC_DISALLOW_COPY_AND_ASSIGN(LogMessage);
- };
- #define RTC_LOG_FILE_LINE(sev, file, line) \
- ::rtc::webrtc_logging_impl::LogCall() & \
- ::rtc::webrtc_logging_impl::LogStreamer<>() \
- << ::rtc::webrtc_logging_impl::LogMetadata(file, line, sev)
- #define RTC_LOG(sev) \
- !rtc::LogMessage::IsNoop<::rtc::sev>() && \
- RTC_LOG_FILE_LINE(::rtc::sev, __FILE__, __LINE__)
- #define RTC_LOG_V(sev) \
- !rtc::LogMessage::IsNoop(sev) && RTC_LOG_FILE_LINE(sev, __FILE__, __LINE__)
- #if (defined(__GNUC__) && !defined(NDEBUG)) || defined(WANT_PRETTY_LOG_F)
- #define RTC_LOG_F(sev) RTC_LOG(sev) << __PRETTY_FUNCTION__ << ": "
- #define RTC_LOG_T_F(sev) \
- RTC_LOG(sev) << this << ": " << __PRETTY_FUNCTION__ << ": "
- #else
- #define RTC_LOG_F(sev) RTC_LOG(sev) << __FUNCTION__ << ": "
- #define RTC_LOG_T_F(sev) RTC_LOG(sev) << this << ": " << __FUNCTION__ << ": "
- #endif
- #define RTC_LOG_CHECK_LEVEL(sev) ::rtc::LogCheckLevel(::rtc::sev)
- #define RTC_LOG_CHECK_LEVEL_V(sev) ::rtc::LogCheckLevel(sev)
- inline bool LogCheckLevel(LoggingSeverity sev) {
- return (LogMessage::GetMinLogSeverity() <= sev);
- }
- #define RTC_LOG_E(sev, ctx, err) \
- !rtc::LogMessage::IsNoop<::rtc::sev>() && \
- ::rtc::webrtc_logging_impl::LogCall() & \
- ::rtc::webrtc_logging_impl::LogStreamer<>() \
- << ::rtc::webrtc_logging_impl::LogMetadataErr { \
- {__FILE__, __LINE__, ::rtc::sev}, ::rtc::ERRCTX_##ctx, (err) \
- }
- #define RTC_LOG_T(sev) RTC_LOG(sev) << this << ": "
- #define RTC_LOG_ERRNO_EX(sev, err) RTC_LOG_E(sev, ERRNO, err)
- #define RTC_LOG_ERRNO(sev) RTC_LOG_ERRNO_EX(sev, errno)
- #if defined(WEBRTC_WIN)
- #define RTC_LOG_GLE_EX(sev, err) RTC_LOG_E(sev, HRESULT, err)
- #define RTC_LOG_GLE(sev) RTC_LOG_GLE_EX(sev, static_cast<int>(GetLastError()))
- #define RTC_LOG_ERR_EX(sev, err) RTC_LOG_GLE_EX(sev, err)
- #define RTC_LOG_ERR(sev) RTC_LOG_GLE(sev)
- #elif defined(__native_client__) && __native_client__
- #define RTC_LOG_ERR_EX(sev, err) RTC_LOG(sev)
- #define RTC_LOG_ERR(sev) RTC_LOG(sev)
- #elif defined(WEBRTC_POSIX)
- #define RTC_LOG_ERR_EX(sev, err) RTC_LOG_ERRNO_EX(sev, err)
- #define RTC_LOG_ERR(sev) RTC_LOG_ERRNO(sev)
- #endif
- #ifdef WEBRTC_ANDROID
- namespace webrtc_logging_impl {
- inline const char* AdaptString(const char* str) {
- return str;
- }
- inline const char* AdaptString(const std::string& str) {
- return str.c_str();
- }
- }
- #define RTC_LOG_TAG(sev, tag) \
- !rtc::LogMessage::IsNoop(sev) && \
- ::rtc::webrtc_logging_impl::LogCall() & \
- ::rtc::webrtc_logging_impl::LogStreamer<>() \
- << ::rtc::webrtc_logging_impl::LogMetadataTag { \
- sev, ::rtc::webrtc_logging_impl::AdaptString(tag) \
- }
- #else
- #define RTC_LOG_TAG(sev, tag) RTC_LOG_V(sev)
- #endif
- #if RTC_DLOG_IS_ON
- #define RTC_DLOG(sev) RTC_LOG(sev)
- #define RTC_DLOG_V(sev) RTC_LOG_V(sev)
- #define RTC_DLOG_F(sev) RTC_LOG_F(sev)
- #else
- #define RTC_DLOG_EAT_STREAM_PARAMS() \
- while (false) \
- ::rtc::webrtc_logging_impl::LogMessageVoidify() & \
- (::rtc::webrtc_logging_impl::LogStreamer<>())
- #define RTC_DLOG(sev) RTC_DLOG_EAT_STREAM_PARAMS()
- #define RTC_DLOG_V(sev) RTC_DLOG_EAT_STREAM_PARAMS()
- #define RTC_DLOG_F(sev) RTC_DLOG_EAT_STREAM_PARAMS()
- #endif
- }
- #endif
|