// Copyright 2018 Google LLC. All Rights Reserved. This file and proprietary // source code may only be used and distributed under the Widevine License // Agreement. // // Log - Platform independent interface for a Logging class // #ifndef WVCDM_UTIL_LOG_H_ #define WVCDM_UTIL_LOG_H_ #include #include #include #include #include #include "util_common.h" namespace wvutil { // Simple logging class. The implementation is platform dependent. typedef enum { // This log level should only be used for |g_cutoff|, in order to silence all // logging. It should never be passed to |Log()| as a log level. LOG_SILENT = -1, LOG_ERROR = 0, LOG_WARN = 1, LOG_INFO = 2, LOG_DEBUG = 3, LOG_VERBOSE = 4, } LogPriority; extern LogPriority g_cutoff; struct LogMessage { uint32_t uid_; int64_t time_ms_; LogPriority priority_; std::string message_; }; class LogBuffer { public: static const int MAX_CAPACITY = 100; void addLog(const LogMessage& log); std::vector getLogs(); private: std::deque buffer_; std::mutex mutex_; }; extern LogBuffer g_logbuf; static const uint32_t UNKNOWN_UID = ~0; #ifdef __ANDROID__ void SetLoggingUid(const uint32_t); void ClearLoggingUid(); uint32_t GetLoggingUid(); uint32_t GetIpcCallingUid(); #else static inline void SetLoggingUid(const uint32_t) {} static inline void ClearLoggingUid() {} static inline uint32_t GetLoggingUid() { return UNKNOWN_UID; } static inline uint32_t GetIpcCallingUid() { return UNKNOWN_UID; } #endif struct LoggingUidSetter { LoggingUidSetter() {} LoggingUidSetter(uint32_t uid) { SetLoggingUid(uid); } virtual ~LoggingUidSetter() { ClearLoggingUid(); } }; // Enable/disable verbose logging (LOGV). // This function is supplied for cases where the system layer does not // initialize logging. This is also needed to initialize logging in // unit tests. CORE_UTIL_EXPORT void InitLogging(); #ifdef __GNUC__ [[gnu::format(printf, 5, 6)]] CORE_UTIL_EXPORT void Log(const char* file, const char* function, int line, LogPriority level, const char* fmt, ...); #else CORE_UTIL_EXPORT void Log(const char* file, const char* function, int line, LogPriority level, const char* fmt, ...); #endif // Log APIs #ifndef LOGE # define LOGE(...) \ Log(__FILE__, __func__, __LINE__, wvutil::LOG_ERROR, __VA_ARGS__) # define LOGW(...) \ Log(__FILE__, __func__, __LINE__, wvutil::LOG_WARN, __VA_ARGS__) # define LOGI(...) \ Log(__FILE__, __func__, __LINE__, wvutil::LOG_INFO, __VA_ARGS__) # define LOGD(...) \ Log(__FILE__, __func__, __LINE__, wvutil::LOG_DEBUG, __VA_ARGS__) # define LOGV(...) \ Log(__FILE__, __func__, __LINE__, wvutil::LOG_VERBOSE, __VA_ARGS__) #endif } // namespace wvutil #endif // WVCDM_UTIL_LOG_H_