sp_dbg.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #include "precompile.h"
  2. #include "sp_dbg_export.h"
  3. #include "sp_gui_def.h"
  4. #include "sp_def.h"
  5. #include "sp_dir.h"
  6. #include "log.h"
  7. #include <stdarg.h>
  8. #include "fileutil.h"
  9. #include "memutil.h"
  10. #include <winpr/string.h>
  11. #include "dbgutil.h"
  12. static char g_inst[128] = {0};
  13. static int g_initialized = 0;
  14. static sp_gui_format_t* s_gui_inst = NULL;
  15. static inline void sp_dbg_buffer_with_level(const char* buf, int level)
  16. {
  17. //只有 SpShell 才会在界面上直接展示
  18. if (s_gui_inst != NULL)
  19. {
  20. char msg[1056] = {};
  21. int type = GUI_DISPLAY_ELEM_LOG_DEBUG;
  22. if (level == XLOG_LEVEL_INFO) {// Log_Event
  23. sprintf_s(msg, 1056, "[%s] I:{%s}", g_inst, buf);
  24. type = GUI_DISPLAY_ELEM_LOG_EVENT;
  25. }
  26. else if (level == XLOG_LEVEL_WARN) { // Log_Warning
  27. sprintf_s(msg, 1056, "[%s] W:{%s}", g_inst, buf);
  28. type = GUI_DISPLAY_ELEM_LOG_WARN;
  29. }
  30. else if (level == XLOG_LEVEL_ERROR) { // Log_Error
  31. sprintf_s(msg, 1056, "[%s] E:{%s}", g_inst, buf);
  32. type = GUI_DISPLAY_ELEM_LOG_ERROR;
  33. }
  34. else if (level == XLOG_LEVEL_FATAL) { // Log_Fatal
  35. sprintf_s(msg, 1056, "[%s] F:{%s}", g_inst, buf);
  36. type = GUI_DISPLAY_ELEM_LOG_FATAL;
  37. }
  38. else if (level == XLOG_LEVEL_TRACE || level == XLOG_LEVEL_DEBUG) { // Log_Debug
  39. sprintf_s(msg, 1056, "[%s] D:{%s}", g_inst, buf);
  40. }
  41. ((sp_gui_format_t*)s_gui_inst)->show_running_info(((sp_gui_format_t*)s_gui_inst)->gui_inst, msg, type);
  42. }
  43. else if (level >= XLOG_LEVEL_ERROR && stricmp(g_inst, "spshell")==0) {
  44. TOOLKIT_ASSERT(false);
  45. }
  46. }
  47. int sp_dbg_init(const char *key, int saveFile)
  48. {
  49. int rc;
  50. if (saveFile != 1)
  51. return Error_Succeed;
  52. rc = xlog_init(NULL);
  53. if (rc == 0) {
  54. char t[MAX_PATH] = {'\0'};
  55. char tmp[MAX_PATH];
  56. GetModuleFileNameA(NULL, tmp, MAX_PATH);
  57. *strrchr(tmp, SPLIT_SLASH) = 0;
  58. *strrchr(tmp, SPLIT_SLASH) = 0;
  59. *strrchr(tmp, SPLIT_SLASH) = 0;
  60. *strrchr(tmp, SPLIT_SLASH) = 0;
  61. *strrchr(tmp, SPLIT_SLASH) = 0;
  62. strncpy(g_inst, key, sizeof(g_inst) - 1);
  63. sprintf(t, "%s" SPLIT_SLASH_STR "rvc" SPLIT_SLASH_STR "dbg" SPLIT_SLASH_STR "%s" SPLIT_SLASH_STR "{yyyy}{MM}{dd}.log", tmp, key);
  64. rc = xlog_add_logger(key,
  65. "periodic",
  66. "level", "All",
  67. "use_lock", "1",
  68. "file", t,
  69. NULL);
  70. }
  71. if (rc == 0) {
  72. g_initialized = 1;
  73. return rc;
  74. } else {
  75. return Error_Param;
  76. }
  77. }
  78. int sp_dbg_term()
  79. {
  80. if (g_initialized) {
  81. g_initialized = 0;
  82. sp_dbg_set_output_gui(NULL);
  83. return xlog_term();
  84. } else {
  85. return Error_Unexpect;
  86. }
  87. }
  88. int sp_dbg_set_level(int level)
  89. {
  90. //return xlog_set_level(g_inst, level);
  91. return 0;
  92. }
  93. int sp_dbg_set_output_gui(void *gui)
  94. {
  95. s_gui_inst = (sp_gui_format_t*)gui;
  96. }
  97. void sp_dbg_log(int level, const char *str, va_list arg)
  98. {
  99. xlog_log_v(g_inst, level, str, arg);
  100. if (s_gui_inst != NULL && level >= XLOG_LEVEL_WARN && level <= XLOG_LEVEL_FATAL) {
  101. char buf[1024] = {};
  102. vsnprintf(buf, sizeof(buf)-1, str, arg);
  103. sp_dbg_buffer_with_level(buf, level);
  104. } else if (level >= XLOG_LEVEL_WARN && level <= XLOG_LEVEL_FATAL) {
  105. char buf[1024] = {};
  106. vsnprintf(buf, sizeof(buf) - 1, str, arg);
  107. sp_trace_append(buf);
  108. }
  109. }
  110. void sp_dbg_trace(const char *str, ...)
  111. {
  112. if (g_initialized) {
  113. va_list arg;
  114. va_start(arg, str);
  115. sp_dbg_log(XLOG_LEVEL_TRACE, str, arg);
  116. va_end(arg);
  117. }
  118. }
  119. void sp_dbg_debug(const char *str, ...)
  120. {
  121. if (g_initialized) {
  122. va_list arg;
  123. va_start(arg, str);
  124. sp_dbg_log(XLOG_LEVEL_DEBUG, str, arg);
  125. va_end(arg);
  126. }
  127. }
  128. void sp_dbg_info(const char *str, ...)
  129. {
  130. if (g_initialized) {
  131. va_list arg;
  132. va_start(arg, str);
  133. sp_dbg_log(XLOG_LEVEL_INFO, str, arg);
  134. va_end(arg);
  135. }
  136. }
  137. void sp_dbg_warn(const char *str, ...)
  138. {
  139. if (g_initialized) {
  140. va_list arg;
  141. va_start(arg, str);
  142. sp_dbg_log(XLOG_LEVEL_WARN, str, arg);
  143. va_end(arg);
  144. }
  145. }
  146. void sp_dbg_error(const char *str, ...)
  147. {
  148. if (g_initialized) {
  149. va_list arg;
  150. va_start(arg, str);
  151. sp_dbg_log(XLOG_LEVEL_ERROR, str, arg);
  152. va_end(arg);
  153. }
  154. }
  155. void sp_dbg_fatal(const char *str, ...)
  156. {
  157. if (g_initialized) {
  158. va_list arg;
  159. va_start(arg, str);
  160. sp_dbg_log(XLOG_LEVEL_FATAL, str, arg);
  161. va_end(arg);
  162. }
  163. }
  164. SPBASE_API void sp_dbg_debugNoOut(const char* str, ...)
  165. {
  166. if (g_initialized) {
  167. va_list arg;
  168. va_start(arg, str);
  169. sp_dbg_log(XLOG_LEVEL_DEBUG, str, arg);
  170. va_end(arg);
  171. }
  172. }
  173. SPBASE_API void sp_dbg_infoNoOut(const char* str, ...)
  174. {
  175. if (g_initialized) {
  176. va_list arg;
  177. va_start(arg, str);
  178. sp_dbg_log(XLOG_LEVEL_INFO, str, arg);
  179. va_end(arg);
  180. }
  181. }
  182. SPBASE_API void sp_dbg_warnNoOut(const char* str, ...)
  183. {
  184. if (g_initialized) {
  185. va_list arg;
  186. va_start(arg, str);
  187. sp_dbg_log(XLOG_LEVEL_WARN, str, arg);
  188. va_end(arg);
  189. }
  190. }
  191. SPBASE_API void sp_dbg_errorNoOut(const char* str, ...)
  192. {
  193. if (g_initialized) {
  194. va_list arg;
  195. va_start(arg, str);
  196. sp_dbg_log(XLOG_LEVEL_ERROR, str, arg);
  197. va_end(arg);
  198. }
  199. }
  200. SPBASE_API void sp_dbg_fatalNoOut(const char* str, ...)
  201. {
  202. if (g_initialized) {
  203. va_list arg;
  204. va_start(arg, str);
  205. sp_dbg_log(XLOG_LEVEL_FATAL, str, arg);
  206. va_end(arg);
  207. }
  208. }