diff --git a/libwvdrmengine/cdm/util/include/log.h b/libwvdrmengine/cdm/util/include/log.h index 5d945e4e..da60c43c 100644 --- a/libwvdrmengine/cdm/util/include/log.h +++ b/libwvdrmengine/cdm/util/include/log.h @@ -7,6 +7,11 @@ #ifndef WVCDM_UTIL_LOG_H_ #define WVCDM_UTIL_LOG_H_ +#include +#include +#include +#include +#include #include "util_common.h" namespace wvcdm { @@ -27,6 +32,24 @@ typedef enum { extern LogPriority g_cutoff; +struct LogMessage { + 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; + // 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 diff --git a/libwvdrmengine/cdm/util/src/log.cpp b/libwvdrmengine/cdm/util/src/log.cpp index d6c22d44..690b0c18 100644 --- a/libwvdrmengine/cdm/util/src/log.cpp +++ b/libwvdrmengine/cdm/util/src/log.cpp @@ -26,7 +26,9 @@ #include #include +#include +#include #include /* @@ -38,8 +40,20 @@ namespace wvcdm { +namespace { +int64_t GetCurrentTimeMs() { + struct timeval tv{}; + gettimeofday(&tv, NULL); + auto msec1 = static_cast(tv.tv_sec) * 1000; + auto msec2 = static_cast(tv.tv_usec) / 1000; + return msec1 + msec2; +} +} + LogPriority g_cutoff = LOG_VERBOSE; +LogBuffer g_logbuf; + void InitLogging() {} void Log(const char* file, const char* function, int line, LogPriority level, @@ -86,6 +100,22 @@ void Log(const char* file, const char* function, int line, LogPriority level, } __android_log_write(prio, LOG_TAG, buf); + if (level <= LOG_INFO) { + g_logbuf.addLog({GetCurrentTimeMs(), level, buf}); + } +} + +void LogBuffer::addLog(const LogMessage& log) { + std::unique_lock lock(mutex_); + buffer_.push_back(log); + while (buffer_.size() > MAX_CAPACITY) { + buffer_.pop_front(); + } +} + +std::vector LogBuffer::getLogs() { + std::unique_lock lock(mutex_); + return {buffer_.begin(), buffer_.end()}; } } // namespace wvcdm