123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- #include "precompile.h"
- #include "sp_dbg_export.h"
- #include "sp_gui_def.h"
- #include "sp_def.h"
- #include "sp_dir.h"
- #include "log.h"
- #include <stdarg.h>
- #include "fileutil.h"
- #include "memutil.h"
- #include <winpr/string.h>
- #include "dbgutil.h"
- static char g_inst[128] = {0};
- static int g_initialized = 0;
- static sp_gui_format_t* s_gui_inst = NULL;
- static inline void sp_dbg_buffer_with_level(const char* buf, int level)
- {
- //只有 SpShell 才会在界面上直接展示
- if (s_gui_inst != NULL)
- {
- char msg[1056] = {};
- int type = GUI_DISPLAY_ELEM_LOG_DEBUG;
- if (level == XLOG_LEVEL_INFO) {// Log_Event
- sprintf_s(msg, 1056, "[%s] I:{%s}", g_inst, buf);
- type = GUI_DISPLAY_ELEM_LOG_EVENT;
- }
- else if (level == XLOG_LEVEL_WARN) { // Log_Warning
- sprintf_s(msg, 1056, "[%s] W:{%s}", g_inst, buf);
- type = GUI_DISPLAY_ELEM_LOG_WARN;
- }
- else if (level == XLOG_LEVEL_ERROR) { // Log_Error
- sprintf_s(msg, 1056, "[%s] E:{%s}", g_inst, buf);
- type = GUI_DISPLAY_ELEM_LOG_ERROR;
- }
- else if (level == XLOG_LEVEL_FATAL) { // Log_Fatal
- sprintf_s(msg, 1056, "[%s] F:{%s}", g_inst, buf);
- type = GUI_DISPLAY_ELEM_LOG_FATAL;
- }
- else if (level == XLOG_LEVEL_TRACE || level == XLOG_LEVEL_DEBUG) { // Log_Debug
- sprintf_s(msg, 1056, "[%s] D:{%s}", g_inst, buf);
- }
- ((sp_gui_format_t*)s_gui_inst)->show_running_info(((sp_gui_format_t*)s_gui_inst)->gui_inst, msg, type);
- }
- else if (level >= XLOG_LEVEL_ERROR && stricmp(g_inst, "spshell")==0) {
- TOOLKIT_ASSERT(false);
- }
- }
- int sp_dbg_init(const char *key, int saveFile)
- {
- int rc;
- if (saveFile != 1)
- return Error_Succeed;
- rc = xlog_init(NULL);
- if (rc == 0) {
- char t[MAX_PATH] = {'\0'};
- char tmp[MAX_PATH];
- GetModuleFileNameA(NULL, tmp, MAX_PATH);
- *strrchr(tmp, SPLIT_SLASH) = 0;
- *strrchr(tmp, SPLIT_SLASH) = 0;
- *strrchr(tmp, SPLIT_SLASH) = 0;
- *strrchr(tmp, SPLIT_SLASH) = 0;
- *strrchr(tmp, SPLIT_SLASH) = 0;
- strncpy(g_inst, key, sizeof(g_inst) - 1);
- sprintf(t, "%s" SPLIT_SLASH_STR "rvc" SPLIT_SLASH_STR "dbg" SPLIT_SLASH_STR "%s" SPLIT_SLASH_STR "{yyyy}{MM}{dd}.log", tmp, key);
- rc = xlog_add_logger(key,
- "periodic",
- "level", "All",
- "use_lock", "1",
- "file", t,
- NULL);
- }
- if (rc == 0) {
- g_initialized = 1;
- return rc;
- } else {
- return Error_Param;
- }
- }
- int sp_dbg_term()
- {
- if (g_initialized) {
- g_initialized = 0;
- sp_dbg_set_output_gui(NULL);
- return xlog_term();
- } else {
- return Error_Unexpect;
- }
- }
- int sp_dbg_set_level(int level)
- {
- //return xlog_set_level(g_inst, level);
- return 0;
- }
- int sp_dbg_set_output_gui(void *gui)
- {
- s_gui_inst = (sp_gui_format_t*)gui;
- }
- void sp_dbg_log(int level, const char *str, va_list arg)
- {
- xlog_log_v(g_inst, level, str, arg);
- if (s_gui_inst != NULL && level >= XLOG_LEVEL_WARN && level <= XLOG_LEVEL_FATAL) {
- char buf[1024] = {};
- vsnprintf(buf, sizeof(buf)-1, str, arg);
- sp_dbg_buffer_with_level(buf, level);
- } else if (level >= XLOG_LEVEL_WARN && level <= XLOG_LEVEL_FATAL) {
- char buf[1024] = {};
- vsnprintf(buf, sizeof(buf) - 1, str, arg);
- sp_trace_append(buf);
- }
- }
- void sp_dbg_trace(const char *str, ...)
- {
- if (g_initialized) {
- va_list arg;
- va_start(arg, str);
- sp_dbg_log(XLOG_LEVEL_TRACE, str, arg);
- va_end(arg);
- }
- }
- void sp_dbg_debug(const char *str, ...)
- {
- if (g_initialized) {
- va_list arg;
- va_start(arg, str);
- sp_dbg_log(XLOG_LEVEL_DEBUG, str, arg);
- va_end(arg);
- }
- }
- void sp_dbg_info(const char *str, ...)
- {
- if (g_initialized) {
- va_list arg;
- va_start(arg, str);
- sp_dbg_log(XLOG_LEVEL_INFO, str, arg);
- va_end(arg);
- }
- }
- void sp_dbg_warn(const char *str, ...)
- {
- if (g_initialized) {
- va_list arg;
- va_start(arg, str);
- sp_dbg_log(XLOG_LEVEL_WARN, str, arg);
- va_end(arg);
- }
- }
- void sp_dbg_error(const char *str, ...)
- {
- if (g_initialized) {
- va_list arg;
- va_start(arg, str);
- sp_dbg_log(XLOG_LEVEL_ERROR, str, arg);
- va_end(arg);
- }
- }
- void sp_dbg_fatal(const char *str, ...)
- {
- if (g_initialized) {
- va_list arg;
- va_start(arg, str);
- sp_dbg_log(XLOG_LEVEL_FATAL, str, arg);
- va_end(arg);
- }
- }
- SPBASE_API void sp_dbg_debugNoOut(const char* str, ...)
- {
- if (g_initialized) {
- va_list arg;
- va_start(arg, str);
- sp_dbg_log(XLOG_LEVEL_DEBUG, str, arg);
- va_end(arg);
- }
- }
- SPBASE_API void sp_dbg_infoNoOut(const char* str, ...)
- {
- if (g_initialized) {
- va_list arg;
- va_start(arg, str);
- sp_dbg_log(XLOG_LEVEL_INFO, str, arg);
- va_end(arg);
- }
- }
- SPBASE_API void sp_dbg_warnNoOut(const char* str, ...)
- {
- if (g_initialized) {
- va_list arg;
- va_start(arg, str);
- sp_dbg_log(XLOG_LEVEL_WARN, str, arg);
- va_end(arg);
- }
- }
- SPBASE_API void sp_dbg_errorNoOut(const char* str, ...)
- {
- if (g_initialized) {
- va_list arg;
- va_start(arg, str);
- sp_dbg_log(XLOG_LEVEL_ERROR, str, arg);
- va_end(arg);
- }
- }
- SPBASE_API void sp_dbg_fatalNoOut(const char* str, ...)
- {
- if (g_initialized) {
- va_list arg;
- va_start(arg, str);
- sp_dbg_log(XLOG_LEVEL_FATAL, str, arg);
- va_end(arg);
- }
- }
|