// this file is include huagao logging tools // // created: 2022-02-09 // #pragma once #include #ifdef WIN32 #define bzero(a, l) memset(a, 0, l) #endif enum hg_log_type { HG_LOG_TYPE_NONE = 0, // no logging HG_LOG_TYPE_CONSOLE, // print to console HG_LOG_TYPE_FILE, // write log into file HG_LOG_TYPE_CALLBACK, // invoke callback log_callback }; enum hg_log_level { HG_LOG_LEVEL_ALL = 0, HG_LOG_LEVEL_DEBUG_INFO, HG_LOG_LEVEL_WARNING, HG_LOG_LEVEL_FATAL, }; typedef void (*log_callback)(hg_log_level, const char* info); #ifdef EXPORT_AS_C extern "C" { #endif namespace hg_log { std::string format_ptr(void* ptr); std::string format_current_thread_id(void); std::string current_time(void); std::string format_size(unsigned long size); std::string u2utf8(const wchar_t* u); std::string pe_path(std::string* name = nullptr); unsigned long long available_memory(void); // Function: initialize the logging type and level // // Parameters: type - log type // // level - log level. only the information can be logged if it's level was not less than this level // // log_file - when type was 'HG_LOG_TYPE_FILE', specify the logging file path. default is "/tmp/hgscanner.log" // // Return: 0 - success, or -1 in HG_LOG_TYPE_FILE and log_file cannot be created int init(hg_log_type type = HG_LOG_TYPE_NONE, hg_log_level level = HG_LOG_LEVEL_DEBUG_INFO, char* log_file = 0); void log(hg_log_level level, const char* info); // 由于64-bits下,变参解析会出现错位现象,目前不建议使用该接口 !!! void vlog(hg_log_level level, const char* fmt, ...); } #ifdef EXPORT_AS_C } #endif // 为方便变参调试信息输出,定义几个参数的宏。宏中“bytes”参数意义为该条日志信息所需要的最小空间(字节) #if defined(WIN32) && !defined(EXPORT_SANE_API) #define HG_LOG(level, info) \ { \ unsigned int l = level; \ hg_scanner_mgr::ui_default_callback(NULL, SANE_EVENT_WIN_DEBUG_INFO, (void*)info, &l, NULL); \ } #define HG_VLOG_1(level, bytes, fmt, arg1) \ { \ char* msgbuf = (char*)malloc(bytes); \ sprintf(msgbuf, fmt, arg1); \ unsigned int l = level; \ hg_scanner_mgr::ui_default_callback(NULL, SANE_EVENT_WIN_DEBUG_INFO, msgbuf, &l, NULL); \ free(msgbuf); \ } #define HG_VLOG_MINI_1(level, fmt, arg1) HG_VLOG_1(level, 256, fmt, arg1) #define HG_VLOG_2(level, bytes, fmt, arg1, arg2) \ { \ char* msgbuf = (char*)malloc(bytes); \ sprintf(msgbuf, fmt, arg1, arg2); \ unsigned int l = level; \ hg_scanner_mgr::ui_default_callback(NULL, SANE_EVENT_WIN_DEBUG_INFO, msgbuf, &l, NULL); \ free(msgbuf); \ } #define HG_VLOG_MINI_2(level, fmt, arg1, arg2) HG_VLOG_2(level, 256, fmt, arg1, arg2) #define HG_VLOG_3(level, bytes, fmt, arg1, arg2, arg3) \ { \ char* msgbuf = (char*)malloc(bytes); \ sprintf(msgbuf, fmt, arg1, arg2, arg3); \ unsigned int l = level; \ hg_scanner_mgr::ui_default_callback(NULL, SANE_EVENT_WIN_DEBUG_INFO, msgbuf, &l, NULL); \ free(msgbuf); \ } #define HG_VLOG_MINI_3(level, fmt, arg1, arg2, arg3) \ HG_VLOG_3(level, 256, fmt, arg1, arg2, arg3) #define HG_VLOG_4(level, bytes, fmt, arg1, arg2, arg3, arg4) \ { \ char* msgbuf = (char*)malloc(bytes); \ sprintf(msgbuf, fmt, arg1, arg2, arg3, arg4); \ unsigned int l = level; \ hg_scanner_mgr::ui_default_callback(NULL, SANE_EVENT_WIN_DEBUG_INFO, msgbuf, &l, NULL); \ free(msgbuf); \ } #define HG_VLOG_MINI_4(level, fmt, arg1, arg2, arg3, arg4) \ HG_VLOG_4(level, 256, fmt, arg1, arg2, arg3, arg4) #define HG_VLOG_5(level, bytes, fmt, arg1, arg2, arg3, arg4, arg5) \ { \ char* msgbuf = (char*)malloc(bytes); \ sprintf(msgbuf, fmt, arg1, arg2, arg3, arg4, arg5); \ unsigned int l = level; \ hg_scanner_mgr::ui_default_callback(NULL, SANE_EVENT_WIN_DEBUG_INFO, msgbuf, &l, NULL); \ free(msgbuf); \ } #define HG_VLOG_MINI_5(level, fmt, arg1, arg2, arg3, arg4, arg5) \ HG_VLOG_5(level, 256, fmt, arg1, arg2, arg3, arg4, arg5) #define HG_VLOG_6(level, bytes, fmt, arg1, arg2, arg3, arg4, arg5, arg6)\ { \ char* msgbuf = (char*)malloc(bytes); \ sprintf(msgbuf, fmt, arg1, arg2, arg3, arg4, arg5, arg6); \ unsigned int l = level; \ hg_scanner_mgr::ui_default_callback(NULL, SANE_EVENT_WIN_DEBUG_INFO, msgbuf, &l, NULL); \ free(msgbuf); \ } #define HG_VLOG_MINI_6(level, fmt, arg1, arg2, arg3, arg4, arg5, arg6) \ HG_VLOG_6(level, 256, fmt, arg1, arg2, arg3, arg4, arg5, arg6) #else #define HG_LOG hg_log::log #define HG_VLOG_1(level, bytes, fmt, arg1) \ { \ char* msgbuf = (char*)malloc(bytes); \ sprintf(msgbuf, fmt, arg1); \ HG_LOG(level, msgbuf); \ free(msgbuf); \ } #define HG_VLOG_MINI_1(level, fmt, arg1) HG_VLOG_1(level, 256, fmt, arg1) #define HG_VLOG_2(level, bytes, fmt, arg1, arg2) \ { \ char* msgbuf = (char*)malloc(bytes); \ sprintf(msgbuf, fmt, arg1, arg2); \ HG_LOG(level, msgbuf); \ free(msgbuf); \ } #define HG_VLOG_MINI_2(level, fmt, arg1, arg2) HG_VLOG_2(level, 256, fmt, arg1, arg2) #define HG_VLOG_3(level, bytes, fmt, arg1, arg2, arg3) \ { \ char* msgbuf = (char*)malloc(bytes); \ sprintf(msgbuf, fmt, arg1, arg2, arg3); \ HG_LOG(level, msgbuf); \ free(msgbuf); \ } #define HG_VLOG_MINI_3(level, fmt, arg1, arg2, arg3) \ HG_VLOG_3(level, 256, fmt, arg1, arg2, arg3) #define HG_VLOG_4(level, bytes, fmt, arg1, arg2, arg3, arg4) \ { \ char* msgbuf = (char*)malloc(bytes); \ sprintf(msgbuf, fmt, arg1, arg2, arg3, arg4); \ HG_LOG(level, msgbuf); \ free(msgbuf); \ } #define HG_VLOG_MINI_4(level, fmt, arg1, arg2, arg3, arg4) \ HG_VLOG_4(level, 256, fmt, arg1, arg2, arg3, arg4) #define HG_VLOG_5(level, bytes, fmt, arg1, arg2, arg3, arg4, arg5) \ { \ char* msgbuf = (char*)malloc(bytes); \ sprintf(msgbuf, fmt, arg1, arg2, arg3, arg4, arg5); \ HG_LOG(level, msgbuf); \ free(msgbuf); \ } #define HG_VLOG_MINI_5(level, fmt, arg1, arg2, arg3, arg4, arg5) \ HG_VLOG_5(level, 256, fmt, arg1, arg2, arg3, arg4, arg5) #define HG_VLOG_6(level, bytes, fmt, arg1, arg2, arg3, arg4, arg5, arg6)\ { \ char* msgbuf = (char*)malloc(bytes); \ sprintf(msgbuf, fmt, arg1, arg2, arg3, arg4, arg5, arg6); \ HG_LOG(level, msgbuf); \ free(msgbuf); \ } #define HG_VLOG_MINI_6(level, fmt, arg1, arg2, arg3, arg4, arg5, arg6) \ HG_VLOG_6(level, 256, fmt, arg1, arg2, arg3, arg4, arg5, arg6) #endif