Implement logging in core library 93/195593/2
authorVyacheslav Cherkashin <v.cherkashin@samsung.com>
Wed, 12 Dec 2018 18:02:54 +0000 (21:02 +0300)
committerVyacheslav Cherkashin <v.cherkashin@samsung.com>
Fri, 28 Dec 2018 08:57:59 +0000 (11:57 +0300)
Change-Id: I3aa7e37017f3bef488c53566fd8fd18222017b82
Signed-off-by: Vyacheslav Cherkashin <v.cherkashin@samsung.com>
src/CMakeLists.txt
src/core/core.cpp
src/core/core.h
src/core/log.cpp [new file with mode: 0644]
src/core/log.h [new file with mode: 0644]

index 742d2ef..b17a80e 100644 (file)
@@ -13,6 +13,7 @@ include_directories(
 
 # setup sources
 set(SRC
+  core/log.cpp
   core/core.cpp
   core/injector.cpp
   core/internal_libc.cpp
index 6c930b5..bd473a8 100644 (file)
@@ -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;
 }
index 476639c..f640173 100644 (file)
@@ -5,7 +5,13 @@
 extern "C" {
 #endif
 
-int __swap_init();
+#include <stdarg.h>
+
+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 (file)
index 0000000..de7945d
--- /dev/null
@@ -0,0 +1,36 @@
+#include "log.h"
+#include <cassert>
+
+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 (file)
index 0000000..36e7749
--- /dev/null
@@ -0,0 +1,34 @@
+#ifndef LOG_H
+#define LOG_H
+
+#include <cstdarg>
+
+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