123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383 |
- #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 "SpEntity.h"
- #include "dbgutil.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)
- {
- #if defined(_MSC_VER)
- m_gui = (sp_gui_t*)gui;
- #else
- m_gui = (sp_gui_format_t*)gui;
- #endif //_MSC_VER
- 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] = "";
- strncpy(m_inst, key, sizeof(m_inst)-1);
- 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;
- 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) {
- m_initialized = 1;
- return rc;
- } else {
- return Error_Param;
- }
- }
- int spDbg::sp_dbg_set_level(int level)
- {
- return xlog_set_level(m_inst, level);
- }
- 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_WARN && level <= XLOG_LEVEL_FATAL) {
- char buf[1024] = {};
- vsnprintf(buf, sizeof(buf)-1, str, arg);
- sp_dbg_buffer_with_level(buf, level);
- }
- // output all dbg log to console
- // sp_dbg_console_v(level, str, arg);
- }
- void spDbg::sp_dbg_trace(const char* str, ...)
- {
- if (m_initialized) {
- va_list arg;
- va_start(arg, str);
- sp_dbg_log(XLOG_LEVEL_TRACE, str, arg);
- va_end(arg);
- }
- }
- /** 输出到展示页面 */
- void spDbg::sp_dbg_buffer_with_level(const char* buf, int level)
- {
- 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) { // Log_Error
- sprintf_s(msg, 1056, "[%s] E:{%s}", m_logkey, buf);
- } else if (level == XLOG_LEVEL_FATAL) { // Log_Fatal
- sprintf_s(msg, 1056, "[%s] F:{%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) {
- #ifdef _WIN32
- MessageBoxA(NULL, buf, "SpShell 错误", MB_OK);
- #else
- TOOLKIT_ASSERT(false);
- #endif //_WIN32
- }
- }
- void spDbg::sp_dbg_logOutMsg(int level, const char* str, va_list arg)
- {
- xlog_log_v(m_inst, level, str, arg);
- if (level > XLOG_LEVEL_FATAL)
- return;
- char buf[1024] = {};
- vsnprintf(buf, sizeof(buf) - 1, str, arg);
- #ifdef _WIN32
- SpEntity* pEntity = (SpEntity*)(getEntityResource()->m_Entity);
- #else
- SpEntity* pEntity = NULL;//NGetThreadEntity();
- #endif //_WIN32
- if (pEntity != NULL) { //所以信创的设备压根就不会走到这里
- auto pEntCfg = pEntity->get_cfg_ent();
- if (pEntCfg != NULL) {
- pEntity->LogMessage(Log_Debug, Severity_None, 0, 0, buf);
- }
- }//output
- if (level >= XLOG_LEVEL_WARN && level <= XLOG_LEVEL_FATAL) {
- sp_dbg_buffer_with_level(buf, level);
- }
- }
- 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, int saveFile)
- {
- EntityGloabalResource *curResource = getEntityResource();
- if (NULL == curResource)
- {
- CreateModuleInfo(ENTITY_SINGLE_GROUPNAME);
- curResource = getEntityResource(); //for spshell
- }
- curResource->m_dbg = new spDbg();
- if (saveFile)
- return ((spDbg*)curResource->m_dbg)->sp_dbg_init(key);
- else
- return 0;
- }
- 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_set_level(int level){
- EntityGloabalResource *curResource = getEntityResource();
- return NULL == curResource ? spDbg::getInstance()->sp_dbg_set_level(level) : ((spDbg *)curResource->m_dbg)->sp_dbg_set_level(level);
- }
- SPBASE_API int sp_dbg_term()
- {
- EntityGloabalResource *curResource = getEntityResource();
- return NULL == curResource ? spDbg::getInstance()->sp_dbg_term() : ((spDbg *)curResource->m_dbg)->sp_dbg_term();
- }
- static inline void sp_dbg_with_level(int level, const char* str, va_list arg)
- {
- EntityGloabalResource* curResource = getEntityResource();
- if (NULL == curResource)
- spDbg::getInstance()->sp_dbg_log(level, str, arg);
- else
- ((spDbg*)curResource->m_dbg)->sp_dbg_log(level, str, arg);
- }
- static inline void sp_dbg_with_level_ex(int level, const char* str, va_list arg)
- {
- EntityGloabalResource* curResource = getEntityResource();
- if (NULL == curResource)
- spDbg::getInstance()->sp_dbg_logOutMsg(level, str, arg);
- else
- ((spDbg*)curResource->m_dbg)->sp_dbg_logOutMsg(level, str, arg);
- }
- SPBASE_API void sp_dbg_trace(const char* str, ...)
- {
- if (str == nullptr)
- return;
- va_list arg;
- va_start(arg, str);
- sp_dbg_with_level(XLOG_LEVEL_TRACE, str, arg);
- va_end(arg);
- }
- SPBASE_API void sp_dbg_traceNoOut(const char* str, ...)
- {
- if (str == nullptr)
- return;
- va_list arg;
- va_start(arg, str);
- sp_dbg_with_level_ex(XLOG_LEVEL_TRACE, str, arg);
- va_end(arg);
- }
- SPBASE_API void sp_dbg_debug(const char *str, ...)
- {
- if (str == nullptr)
- return;
- va_list arg;
- va_start(arg, str);
- sp_dbg_with_level(XLOG_LEVEL_DEBUG, str, arg);
- va_end(arg);
- }
- SPBASE_API void sp_dbg_debugNoOut(const char* str, ...)
- {
- if (str == nullptr)
- return;
- va_list arg;
- va_start(arg, str);
- sp_dbg_with_level_ex(XLOG_LEVEL_DEBUG, str, arg);
- va_end(arg);
- }
- SPBASE_API void sp_dbg_info(const char *str, ...)
- {
- if (str == nullptr)
- return;
- va_list arg;
- va_start(arg, str);
- sp_dbg_with_level(XLOG_LEVEL_INFO, str, arg);
- va_end(arg);
- }
- SPBASE_API void sp_dbg_infoNoOut(const char* str, ...)
- {
- if (str == nullptr)
- return;
- va_list arg;
- va_start(arg, str);
- sp_dbg_with_level_ex(XLOG_LEVEL_INFO, str, arg);
- va_end(arg);
- }
- SPBASE_API void sp_dbg_warn(const char *str, ...)
- {
- if (str == nullptr)
- return;
- va_list arg;
- va_start(arg, str);
- sp_dbg_with_level(XLOG_LEVEL_WARN, str, arg);
- va_end(arg);
- }
- SPBASE_API void sp_dbg_warnNoOut(const char* str, ...)
- {
- if (str == nullptr)
- return;
- va_list arg;
- va_start(arg, str);
- sp_dbg_with_level_ex(XLOG_LEVEL_WARN, str, arg);
- va_end(arg);
- }
- SPBASE_API void sp_dbg_error(const char *str, ...)
- {
- if (str == nullptr)
- return;
- va_list arg;
- va_start(arg, str);
- sp_dbg_with_level(XLOG_LEVEL_ERROR, str, arg);
- va_end(arg);
- }
- SPBASE_API void sp_dbg_errorNoOut(const char* str, ...)
- {
- if (str == nullptr)
- return;
- va_list arg;
- va_start(arg, str);
- sp_dbg_with_level_ex(XLOG_LEVEL_ERROR, str, arg);
- va_end(arg);
- }
- SPBASE_API void sp_dbg_fatal(const char *str, ...)
- {
- if (str == nullptr)
- return;
- va_list arg;
- va_start(arg, str);
- sp_dbg_with_level(XLOG_LEVEL_FATAL, str, arg);
- va_end(arg);
- }
- SPBASE_API void sp_dbg_fatalNoOut(const char* str, ...)
- {
- if (str == nullptr)
- return;
- va_list arg;
- va_start(arg, str);
- sp_dbg_with_level_ex(XLOG_LEVEL_FATAL, str, arg);
- va_end(arg);
- }
|