123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- #include "precompile.h"
- #include "sp_dbg_export.h"
- #include "sp_def.h"
- #include "sp_dbg.h"
- #include "sp_dir.h"
- #include "log.h"
- #include "sp_gui.h"
- #include "sp_checkEntity.h"
- #include "fileutil.h"
- spDbg::spDbg()
- : m_initialized(0), m_gui(NULL)
- {
- ZeroMemory(m_inst, sizeof(m_inst));
- ZeroMemory(m_logkey, sizeof(m_logkey));
- }
- int spDbg::sp_dbg_set_output_gui(void *gui)
- {
- m_gui = (sp_gui_t*)gui;
- return 0;
- }
- int spDbg::sp_dbg_init(const char *key) //Init log path
- {
- strcpy_s(m_logkey, sizeof(m_logkey), key);
- int rc;
- rc = xlog_init(NULL);
- if (rc == 0) {
- char t[MAX_PATH], drivePath[_MAX_DRIVE] = "";
- sp_dir_get_cur_drive(drivePath);
- strncpy(m_inst, key, sizeof(m_inst)-1);
- sprintf(t, "%s" SPLIT_SLASH_STR "rvc" SPLIT_SLASH_STR "dbg" SPLIT_SLASH_STR "%s" SPLIT_SLASH_STR "{yyyy}{MM}{dd}.log",
- drivePath, key);
- rc = xlog_add_logger(key,
- "periodic",
- "level", "All",
- "use_lock", "1",
- "file", t,
- NULL);
- }
- if (rc == 0) {
- m_initialized = 1;
- return rc;
- } else {
- return Error_Param;
- }
- }
- int spDbg::sp_dbg_term()
- {
- if (m_initialized) {
- m_initialized = 0;
- return xlog_term();
- } else {
- return Error_Unexpect;
- }
- }
- void spDbg::sp_dbg_log(int level, const char *str, va_list arg)
- {
- xlog_log_v(m_inst, level, str, arg); //输出日志
- if (level > XLOG_LEVEL_FATAL)
- {
- return;
- }
-
- // 只显示警告以上级别信息
- if (level >= XLOG_LEVEL_WARN && level <= XLOG_LEVEL_FATAL)
- {
- char buf[1024] = {};
- vsnprintf(buf, sizeof(buf)-1, str, arg);
- if (m_gui != NULL)
- {
- char msg[1056] = {};
- if (level == XLOG_LEVEL_INFO) // Log_Event
- sprintf_s(msg, 1056, "[%s] I:{%s}", m_logkey, buf);
- else if (level == XLOG_LEVEL_WARN) // Log_Warning
- sprintf_s(msg, 1056, "[%s] W:{%s}", m_logkey, buf);
- else if (level == XLOG_LEVEL_ERROR || level == XLOG_LEVEL_FATAL) // Log_Error
- sprintf_s(msg, 1056, "[%s] E:{%s}", m_logkey, buf);
- else if (level == XLOG_LEVEL_TRACE || level == XLOG_LEVEL_DEBUG) // Log_Debug
- sprintf_s(msg, 1056, "[%s] D:{%s}", m_logkey, buf);
- sp_gui_show_running_info((sp_gui_t *)m_gui, msg, 0);
- }
- else if (level >= XLOG_LEVEL_ERROR && stricmp(m_logkey, "spshell")==0)
- {
- assert(false);
- }
- }
- // output all dbg log to console
- // sp_dbg_console_v(level, str, arg);
- }
- void spDbg::sp_dbg_debug(const char *str, ...) //内部调用
- {
- if (m_initialized) {
- va_list arg;
- va_start(arg, str);
- sp_dbg_log(XLOG_LEVEL_DEBUG, str, arg);
- va_end(arg);
- }
- }
- void spDbg::sp_dbg_info(const char *str, ...)
- {
- if (m_initialized) {
- va_list arg;
- va_start(arg, str);
- sp_dbg_log(XLOG_LEVEL_INFO, str, arg);
- va_end(arg);
- }
- }
- void spDbg::sp_dbg_warn(const char *str, ...)
- {
- if (m_initialized) {
- va_list arg;
- va_start(arg, str);
- sp_dbg_log(XLOG_LEVEL_WARN, str, arg);
- va_end(arg);
- }
- }
- void spDbg::sp_dbg_error(const char *str, ...)
- {
- if (m_initialized) {
- va_list arg;
- va_start(arg, str);
- sp_dbg_log(XLOG_LEVEL_ERROR, str, arg);
- va_end(arg);
- }
- }
- void spDbg::sp_dbg_fatal(const char *str, ...)
- {
- if (m_initialized) {
- va_list arg;
- va_start(arg, str);
- sp_dbg_log(XLOG_LEVEL_FATAL, str, arg);
- va_end(arg);
- }
- }
- spDbg* spDbg::getInstance(){
- static spDbg *m_instance = NULL; //只能申请一个,提供给spshell.exe,sphost.exe使用
- if (NULL != m_instance)
- return m_instance;
- m_instance = new spDbg();
- return m_instance;
- }
- int sp_dbg_init(const char *key)
- {
- EntityGloabalResource *curResource = getEntityResource();
- if (NULL == curResource)
- {
- CreateModuleInfo(ENTITY_SINGLE_GROUPNAME);
- curResource = getEntityResource(); //for spshell
- }
- curResource->m_dbg = new spDbg();
- return ((spDbg *)curResource->m_dbg)->sp_dbg_init(key);
- }
- SPBASE_API int sp_dbg_set_output_gui(void *gui)
- {
- EntityGloabalResource *curResource = getEntityResource();
- return NULL == curResource ? spDbg::getInstance()->sp_dbg_set_output_gui(gui) : ((spDbg *)curResource->m_dbg)->sp_dbg_set_output_gui(gui);
- }
- SPBASE_API int sp_dbg_term()
- {
- EntityGloabalResource *curResource = getEntityResource();
- return NULL == curResource ? spDbg::getInstance()->sp_dbg_term() : ((spDbg *)curResource->m_dbg)->sp_dbg_term();
- }
- SPBASE_API void sp_dbg_debug(const char *str, ...)
- {
- va_list arg;
- va_start(arg, str);
- EntityGloabalResource *curResource = getEntityResource();
- if (NULL == curResource)
- spDbg::getInstance()->sp_dbg_log(XLOG_LEVEL_DEBUG, str, arg);
- else
- ((spDbg *)curResource->m_dbg)->sp_dbg_log(XLOG_LEVEL_DEBUG, str, arg);
- va_end(arg);
- }
- SPBASE_API void sp_dbg_info(const char *str, ...)
- {
- va_list arg;
- va_start(arg, str);
- EntityGloabalResource *curResource = getEntityResource();
- if (NULL == curResource)
- spDbg::getInstance()->sp_dbg_log(XLOG_LEVEL_INFO, str, arg);
- else
- ((spDbg *)curResource->m_dbg)->sp_dbg_log(XLOG_LEVEL_INFO, str, arg);
- va_end(arg);
- }
- SPBASE_API void sp_dbg_warn(const char *str, ...)
- {
- va_list arg;
- va_start(arg, str);
- EntityGloabalResource *curResource = getEntityResource();
- if (NULL == curResource)
- spDbg::getInstance()->sp_dbg_log(XLOG_LEVEL_WARN, str, arg);
- else
- ((spDbg *)curResource->m_dbg)->sp_dbg_log(XLOG_LEVEL_WARN, str, arg);
- va_end(arg);
- }
- SPBASE_API void sp_dbg_error(const char *str, ...)
- {
- va_list arg;
- va_start(arg, str);
- EntityGloabalResource *curResource = getEntityResource();
- if (NULL == curResource)
- spDbg::getInstance()->sp_dbg_log(XLOG_LEVEL_ERROR, str, arg);
- else
- ((spDbg *)curResource->m_dbg)->sp_dbg_log(XLOG_LEVEL_ERROR, str, arg);
- va_end(arg);
- }
- SPBASE_API void sp_dbg_fatal(const char *str, ...)
- {
- va_list arg;
- va_start(arg, str);
- EntityGloabalResource *curResource = getEntityResource();
- if (NULL == curResource)
- spDbg::getInstance()->sp_dbg_log(XLOG_LEVEL_FATAL, str, arg);
- else
- ((spDbg *)curResource->m_dbg)->sp_dbg_log(XLOG_LEVEL_FATAL, str, arg);
- va_end(arg);
- }
|