#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;
}
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
}
--- /dev/null
+#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
--- /dev/null
+#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