From 7052a767a42bc721bdb658ddf61ead10f53daead Mon Sep 17 00:00:00 2001 From: Vyacheslav Cherkashin Date: Wed, 12 Dec 2018 21:02:54 +0300 Subject: [PATCH] Implement logging in core library Change-Id: I3aa7e37017f3bef488c53566fd8fd18222017b82 Signed-off-by: Vyacheslav Cherkashin --- src/CMakeLists.txt | 1 + src/core/core.cpp | 6 +++++- src/core/core.h | 8 +++++++- src/core/log.cpp | 36 ++++++++++++++++++++++++++++++++++++ src/core/log.h | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 src/core/log.cpp create mode 100644 src/core/log.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 742d2ef..b17a80e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -13,6 +13,7 @@ include_directories( # setup sources set(SRC + core/log.cpp core/core.cpp core/injector.cpp core/internal_libc.cpp diff --git a/src/core/core.cpp b/src/core/core.cpp index 6c930b5..bd473a8 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -1,8 +1,12 @@ #include "core.h" #include "internal_deps.h" +#include "log.h" extern "C" SWAP_INTERFACE_ATTRIBUTE -int __swap_init() +int __swap_init(const struct core_init_info *init_info) { + __swap::Log::set_hook(init_info->vraw_log); + + LOGI("Initialize CORE library"); return 0; } diff --git a/src/core/core.h b/src/core/core.h index 476639c..f640173 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -5,7 +5,13 @@ extern "C" { #endif -int __swap_init(); +#include + +struct core_init_info { + void (*vraw_log)(int log_level, const char *format, va_list args); +}; + +int __swap_init(const struct core_init_info *init_info); #ifdef __cplusplus } diff --git a/src/core/log.cpp b/src/core/log.cpp new file mode 100644 index 0000000..de7945d --- /dev/null +++ b/src/core/log.cpp @@ -0,0 +1,36 @@ +#include "log.h" +#include + +namespace __swap { + +void (*Log::vraw_hook_)(int log_level, const char *format, va_list args) = nullptr; + +void Log::set_hook(void (*vraw_hook)(int log_level, const char *format, va_list args)) +{ + Log::vraw_hook_ = vraw_hook; +} + +void Log::raw(enum Level level, const char *format, ...) +{ + assert(Log::vraw_hook_); + + int log_level = 2; // set ERROR type by default + switch (level) { + case Level::INFO: + log_level = 0; + break; + case Level::WARN: + log_level = 1; + break; + case Level::ERROR: + log_level = 2; + break; + } + + va_list args; + va_start(args, format); + Log::vraw_hook_(log_level, format, args); + va_end(args); +} + +} // namespace __swap diff --git a/src/core/log.h b/src/core/log.h new file mode 100644 index 0000000..36e7749 --- /dev/null +++ b/src/core/log.h @@ -0,0 +1,34 @@ +#ifndef LOG_H +#define LOG_H + +#include + +namespace __swap { + +class Log +{ +public: + enum Level { + INFO, + ERROR, + WARN, + }; + + static void raw(enum Level level, const char *format, ...); + static void set_hook(void (*vraw_hook)(int log_level, const char *format, va_list args)); + +private: + static void (*vraw_hook_)(int log_level, const char *format, va_list args); +}; + +} // namespace __swap + +#define DO_RAW_LOG(LOG_LEVEL, FORMAT, ...) \ + __swap::Log::raw(__swap::Log::LOG_LEVEL, \ + "LIBCORE[%s:%d] " FORMAT, __func__, __LINE__, ##__VA_ARGS__) + +#define LOGI(FORMAT, ...) DO_RAW_LOG(Log::INFO, FORMAT, ##__VA_ARGS__) +#define LOGW(FORMAT, ...) DO_RAW_LOG(Log::ERROR, FORMAT, ##__VA_ARGS__) +#define LOGE(FORMAT, ...) DO_RAW_LOG(Log::WARN, FORMAT, ##__VA_ARGS__) + +#endif // LOG_H -- 2.7.4