sp_dbg.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #include "precompile.h"
  2. #include "sp_dbg_export.h"
  3. #include "sp_def.h"
  4. #include "sp_dbg.h"
  5. #include "sp_dir.h"
  6. #include "log.h"
  7. #include "sp_gui.h"
  8. #include "sp_checkEntity.h"
  9. #include "fileutil.h"
  10. spDbg::spDbg()
  11. : m_initialized(0), m_gui(NULL)
  12. {
  13. ZeroMemory(m_inst, sizeof(m_inst));
  14. ZeroMemory(m_logkey, sizeof(m_logkey));
  15. }
  16. int spDbg::sp_dbg_set_output_gui(void *gui)
  17. {
  18. m_gui = (sp_gui_t*)gui;
  19. return 0;
  20. }
  21. int spDbg::sp_dbg_init(const char *key) //Init log path
  22. {
  23. strcpy_s(m_logkey, sizeof(m_logkey), key);
  24. int rc;
  25. rc = xlog_init(NULL);
  26. if (rc == 0) {
  27. char t[MAX_PATH], drivePath[_MAX_DRIVE] = "";
  28. sp_dir_get_cur_drive(drivePath);
  29. strncpy(m_inst, key, sizeof(m_inst)-1);
  30. sprintf(t, "%s" SPLIT_SLASH_STR "rvc" SPLIT_SLASH_STR "dbg" SPLIT_SLASH_STR "%s" SPLIT_SLASH_STR "{yyyy}{MM}{dd}.log",
  31. drivePath, key);
  32. rc = xlog_add_logger(key,
  33. "periodic",
  34. "level", "All",
  35. "use_lock", "1",
  36. "file", t,
  37. NULL);
  38. }
  39. if (rc == 0) {
  40. m_initialized = 1;
  41. return rc;
  42. } else {
  43. return Error_Param;
  44. }
  45. }
  46. int spDbg::sp_dbg_term()
  47. {
  48. if (m_initialized) {
  49. m_initialized = 0;
  50. return xlog_term();
  51. } else {
  52. return Error_Unexpect;
  53. }
  54. }
  55. void spDbg::sp_dbg_log(int level, const char *str, va_list arg)
  56. {
  57. xlog_log_v(m_inst, level, str, arg); //输出日志
  58. if (level > XLOG_LEVEL_FATAL)
  59. {
  60. return;
  61. }
  62. // 只显示警告以上级别信息
  63. if (level >= XLOG_LEVEL_WARN && level <= XLOG_LEVEL_FATAL)
  64. {
  65. char buf[1024] = {};
  66. vsnprintf(buf, sizeof(buf)-1, str, arg);
  67. if (m_gui != NULL)
  68. {
  69. char msg[1056] = {};
  70. if (level == XLOG_LEVEL_INFO) // Log_Event
  71. sprintf_s(msg, 1056, "[%s] I:{%s}", m_logkey, buf);
  72. else if (level == XLOG_LEVEL_WARN) // Log_Warning
  73. sprintf_s(msg, 1056, "[%s] W:{%s}", m_logkey, buf);
  74. else if (level == XLOG_LEVEL_ERROR || level == XLOG_LEVEL_FATAL) // Log_Error
  75. sprintf_s(msg, 1056, "[%s] E:{%s}", m_logkey, buf);
  76. else if (level == XLOG_LEVEL_TRACE || level == XLOG_LEVEL_DEBUG) // Log_Debug
  77. sprintf_s(msg, 1056, "[%s] D:{%s}", m_logkey, buf);
  78. sp_gui_show_running_info((sp_gui_t *)m_gui, msg, 0);
  79. }
  80. else if (level >= XLOG_LEVEL_ERROR && stricmp(m_logkey, "spshell")==0)
  81. {
  82. assert(false);
  83. }
  84. }
  85. // output all dbg log to console
  86. // sp_dbg_console_v(level, str, arg);
  87. }
  88. void spDbg::sp_dbg_debug(const char *str, ...) //内部调用
  89. {
  90. if (m_initialized) {
  91. va_list arg;
  92. va_start(arg, str);
  93. sp_dbg_log(XLOG_LEVEL_DEBUG, str, arg);
  94. va_end(arg);
  95. }
  96. }
  97. void spDbg::sp_dbg_info(const char *str, ...)
  98. {
  99. if (m_initialized) {
  100. va_list arg;
  101. va_start(arg, str);
  102. sp_dbg_log(XLOG_LEVEL_INFO, str, arg);
  103. va_end(arg);
  104. }
  105. }
  106. void spDbg::sp_dbg_warn(const char *str, ...)
  107. {
  108. if (m_initialized) {
  109. va_list arg;
  110. va_start(arg, str);
  111. sp_dbg_log(XLOG_LEVEL_WARN, str, arg);
  112. va_end(arg);
  113. }
  114. }
  115. void spDbg::sp_dbg_error(const char *str, ...)
  116. {
  117. if (m_initialized) {
  118. va_list arg;
  119. va_start(arg, str);
  120. sp_dbg_log(XLOG_LEVEL_ERROR, str, arg);
  121. va_end(arg);
  122. }
  123. }
  124. void spDbg::sp_dbg_fatal(const char *str, ...)
  125. {
  126. if (m_initialized) {
  127. va_list arg;
  128. va_start(arg, str);
  129. sp_dbg_log(XLOG_LEVEL_FATAL, str, arg);
  130. va_end(arg);
  131. }
  132. }
  133. spDbg* spDbg::getInstance(){
  134. static spDbg *m_instance = NULL; //只能申请一个,提供给spshell.exe,sphost.exe使用
  135. if (NULL != m_instance)
  136. return m_instance;
  137. m_instance = new spDbg();
  138. return m_instance;
  139. }
  140. int sp_dbg_init(const char *key)
  141. {
  142. EntityGloabalResource *curResource = getEntityResource();
  143. if (NULL == curResource)
  144. {
  145. CreateModuleInfo(ENTITY_SINGLE_GROUPNAME);
  146. curResource = getEntityResource(); //for spshell
  147. }
  148. curResource->m_dbg = new spDbg();
  149. return ((spDbg *)curResource->m_dbg)->sp_dbg_init(key);
  150. }
  151. SPBASE_API int sp_dbg_set_output_gui(void *gui)
  152. {
  153. EntityGloabalResource *curResource = getEntityResource();
  154. return NULL == curResource ? spDbg::getInstance()->sp_dbg_set_output_gui(gui) : ((spDbg *)curResource->m_dbg)->sp_dbg_set_output_gui(gui);
  155. }
  156. SPBASE_API int sp_dbg_term()
  157. {
  158. EntityGloabalResource *curResource = getEntityResource();
  159. return NULL == curResource ? spDbg::getInstance()->sp_dbg_term() : ((spDbg *)curResource->m_dbg)->sp_dbg_term();
  160. }
  161. SPBASE_API void sp_dbg_debug(const char *str, ...)
  162. {
  163. va_list arg;
  164. va_start(arg, str);
  165. EntityGloabalResource *curResource = getEntityResource();
  166. if (NULL == curResource)
  167. spDbg::getInstance()->sp_dbg_log(XLOG_LEVEL_DEBUG, str, arg);
  168. else
  169. ((spDbg *)curResource->m_dbg)->sp_dbg_log(XLOG_LEVEL_DEBUG, str, arg);
  170. va_end(arg);
  171. }
  172. SPBASE_API void sp_dbg_info(const char *str, ...)
  173. {
  174. va_list arg;
  175. va_start(arg, str);
  176. EntityGloabalResource *curResource = getEntityResource();
  177. if (NULL == curResource)
  178. spDbg::getInstance()->sp_dbg_log(XLOG_LEVEL_INFO, str, arg);
  179. else
  180. ((spDbg *)curResource->m_dbg)->sp_dbg_log(XLOG_LEVEL_INFO, str, arg);
  181. va_end(arg);
  182. }
  183. SPBASE_API void sp_dbg_warn(const char *str, ...)
  184. {
  185. va_list arg;
  186. va_start(arg, str);
  187. EntityGloabalResource *curResource = getEntityResource();
  188. if (NULL == curResource)
  189. spDbg::getInstance()->sp_dbg_log(XLOG_LEVEL_WARN, str, arg);
  190. else
  191. ((spDbg *)curResource->m_dbg)->sp_dbg_log(XLOG_LEVEL_WARN, str, arg);
  192. va_end(arg);
  193. }
  194. SPBASE_API void sp_dbg_error(const char *str, ...)
  195. {
  196. va_list arg;
  197. va_start(arg, str);
  198. EntityGloabalResource *curResource = getEntityResource();
  199. if (NULL == curResource)
  200. spDbg::getInstance()->sp_dbg_log(XLOG_LEVEL_ERROR, str, arg);
  201. else
  202. ((spDbg *)curResource->m_dbg)->sp_dbg_log(XLOG_LEVEL_ERROR, str, arg);
  203. va_end(arg);
  204. }
  205. SPBASE_API void sp_dbg_fatal(const char *str, ...)
  206. {
  207. va_list arg;
  208. va_start(arg, str);
  209. EntityGloabalResource *curResource = getEntityResource();
  210. if (NULL == curResource)
  211. spDbg::getInstance()->sp_dbg_log(XLOG_LEVEL_FATAL, str, arg);
  212. else
  213. ((spDbg *)curResource->m_dbg)->sp_dbg_log(XLOG_LEVEL_FATAL, str, arg);
  214. va_end(arg);
  215. }