sp_dbg.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 "SpEntity.h"
  10. #include "dbgutil.h"
  11. #include "fileutil.h"
  12. spDbg::spDbg()
  13. : m_initialized(0), m_gui(NULL)
  14. {
  15. ZeroMemory(m_inst, sizeof(m_inst));
  16. ZeroMemory(m_logkey, sizeof(m_logkey));
  17. }
  18. int spDbg::sp_dbg_set_output_gui(void *gui)
  19. {
  20. #if defined(_MSC_VER)
  21. m_gui = (sp_gui_t*)gui;
  22. #else
  23. m_gui = (sp_gui_format_t*)gui;
  24. #endif //_MSC_VER
  25. return 0;
  26. }
  27. int spDbg::sp_dbg_init(const char *key) //Init log path
  28. {
  29. strcpy_s(m_logkey, sizeof(m_logkey), key);
  30. int rc;
  31. rc = xlog_init(NULL);
  32. if (rc == 0) {
  33. char t[MAX_PATH] = "";
  34. strncpy(m_inst, key, sizeof(m_inst)-1);
  35. char tmp[MAX_PATH];
  36. GetModuleFileNameA(NULL, tmp, MAX_PATH);
  37. *strrchr(tmp, SPLIT_SLASH) = 0;
  38. *strrchr(tmp, SPLIT_SLASH) = 0;
  39. *strrchr(tmp, SPLIT_SLASH) = 0;
  40. *strrchr(tmp, SPLIT_SLASH) = 0;
  41. *strrchr(tmp, SPLIT_SLASH) = 0;
  42. sprintf(t, "%s" SPLIT_SLASH_STR "rvc" SPLIT_SLASH_STR "dbg" SPLIT_SLASH_STR "%s" SPLIT_SLASH_STR "{yyyy}{MM}{dd}.log",
  43. tmp, key);
  44. rc = xlog_add_logger(key,
  45. "periodic",
  46. "level", "All",
  47. "use_lock", "1",
  48. "file", t,
  49. NULL);
  50. }
  51. if (rc == 0) {
  52. m_initialized = 1;
  53. return rc;
  54. } else {
  55. return Error_Param;
  56. }
  57. }
  58. int spDbg::sp_dbg_set_level(int level)
  59. {
  60. return xlog_set_level(m_inst, level);
  61. }
  62. int spDbg::sp_dbg_term()
  63. {
  64. if (m_initialized) {
  65. m_initialized = 0;
  66. return xlog_term();
  67. } else {
  68. return Error_Unexpect;
  69. }
  70. }
  71. void spDbg::sp_dbg_log(int level, const char *str, va_list arg)
  72. {
  73. xlog_log_v(m_inst, level, str, arg);
  74. if (level >= XLOG_LEVEL_WARN && level <= XLOG_LEVEL_FATAL) {
  75. char buf[1024] = {};
  76. vsnprintf(buf, sizeof(buf)-1, str, arg);
  77. sp_dbg_buffer_with_level(buf, level);
  78. }
  79. // output all dbg log to console
  80. // sp_dbg_console_v(level, str, arg);
  81. }
  82. void spDbg::sp_dbg_trace(const char* str, ...)
  83. {
  84. if (m_initialized) {
  85. va_list arg;
  86. va_start(arg, str);
  87. sp_dbg_log(XLOG_LEVEL_TRACE, str, arg);
  88. va_end(arg);
  89. }
  90. }
  91. /** 输出到展示页面 */
  92. void spDbg::sp_dbg_buffer_with_level(const char* buf, int level)
  93. {
  94. if (m_gui != NULL) {
  95. char msg[1056] = {};
  96. if (level == XLOG_LEVEL_INFO) {// Log_Event
  97. sprintf_s(msg, 1056, "[%s] I:{%s}", m_logkey, buf);
  98. } else if (level == XLOG_LEVEL_WARN) { // Log_Warning
  99. sprintf_s(msg, 1056, "[%s] W:{%s}", m_logkey, buf);
  100. } else if (level == XLOG_LEVEL_ERROR) { // Log_Error
  101. sprintf_s(msg, 1056, "[%s] E:{%s}", m_logkey, buf);
  102. } else if (level == XLOG_LEVEL_FATAL) { // Log_Fatal
  103. sprintf_s(msg, 1056, "[%s] F:{%s}", m_logkey, buf);
  104. } else if (level == XLOG_LEVEL_TRACE || level == XLOG_LEVEL_DEBUG) { // Log_Debug
  105. sprintf_s(msg, 1056, "[%s] D:{%s}", m_logkey, buf);
  106. }
  107. sp_gui_show_running_info((sp_gui_t*)m_gui, msg, 0);
  108. } else if (level >= XLOG_LEVEL_ERROR && stricmp(m_logkey, "spshell") == 0) {
  109. #ifdef _WIN32
  110. MessageBoxA(NULL, buf, "SpShell 错误", MB_OK);
  111. #else
  112. TOOLKIT_ASSERT(false);
  113. #endif //_WIN32
  114. }
  115. }
  116. void spDbg::sp_dbg_logOutMsg(int level, const char* str, va_list arg)
  117. {
  118. xlog_log_v(m_inst, level, str, arg);
  119. if (level > XLOG_LEVEL_FATAL)
  120. return;
  121. char buf[1024] = {};
  122. vsnprintf(buf, sizeof(buf) - 1, str, arg);
  123. #ifdef _WIN32
  124. SpEntity* pEntity = (SpEntity*)(getEntityResource()->m_Entity);
  125. #else
  126. SpEntity* pEntity = NULL;//NGetThreadEntity();
  127. #endif //_WIN32
  128. if (pEntity != NULL) { //所以信创的设备压根就不会走到这里
  129. auto pEntCfg = pEntity->get_cfg_ent();
  130. if (pEntCfg != NULL) {
  131. pEntity->LogMessage(Log_Debug, Severity_None, 0, 0, buf);
  132. }
  133. }//output
  134. if (level >= XLOG_LEVEL_WARN && level <= XLOG_LEVEL_FATAL) {
  135. sp_dbg_buffer_with_level(buf, level);
  136. }
  137. }
  138. void spDbg::sp_dbg_debug(const char *str, ...)
  139. {
  140. if (m_initialized) {
  141. va_list arg;
  142. va_start(arg, str);
  143. sp_dbg_log(XLOG_LEVEL_DEBUG, str, arg);
  144. va_end(arg);
  145. }
  146. }
  147. void spDbg::sp_dbg_info(const char *str, ...)
  148. {
  149. if (m_initialized) {
  150. va_list arg;
  151. va_start(arg, str);
  152. sp_dbg_log(XLOG_LEVEL_INFO, str, arg);
  153. va_end(arg);
  154. }
  155. }
  156. void spDbg::sp_dbg_warn(const char *str, ...)
  157. {
  158. if (m_initialized) {
  159. va_list arg;
  160. va_start(arg, str);
  161. sp_dbg_log(XLOG_LEVEL_WARN, str, arg);
  162. va_end(arg);
  163. }
  164. }
  165. void spDbg::sp_dbg_error(const char *str, ...)
  166. {
  167. if (m_initialized) {
  168. va_list arg;
  169. va_start(arg, str);
  170. sp_dbg_log(XLOG_LEVEL_ERROR, str, arg);
  171. va_end(arg);
  172. }
  173. }
  174. void spDbg::sp_dbg_fatal(const char *str, ...)
  175. {
  176. if (m_initialized) {
  177. va_list arg;
  178. va_start(arg, str);
  179. sp_dbg_log(XLOG_LEVEL_FATAL, str, arg);
  180. va_end(arg);
  181. }
  182. }
  183. spDbg* spDbg::getInstance(){
  184. static spDbg *m_instance = NULL; //只能申请一个,提供给spshell.exe,sphost.exe使用
  185. if (NULL != m_instance)
  186. return m_instance;
  187. m_instance = new spDbg();
  188. return m_instance;
  189. }
  190. int sp_dbg_init(const char *key, int saveFile)
  191. {
  192. EntityGloabalResource *curResource = getEntityResource();
  193. if (NULL == curResource)
  194. {
  195. CreateModuleInfo(ENTITY_SINGLE_GROUPNAME);
  196. curResource = getEntityResource(); //for spshell
  197. }
  198. curResource->m_dbg = new spDbg();
  199. if (saveFile)
  200. return ((spDbg*)curResource->m_dbg)->sp_dbg_init(key);
  201. else
  202. return 0;
  203. }
  204. SPBASE_API int sp_dbg_set_output_gui(void *gui)
  205. {
  206. EntityGloabalResource *curResource = getEntityResource();
  207. return NULL == curResource ? spDbg::getInstance()->sp_dbg_set_output_gui(gui) : ((spDbg *)curResource->m_dbg)->sp_dbg_set_output_gui(gui);
  208. }
  209. SPBASE_API int sp_dbg_set_level(int level){
  210. EntityGloabalResource *curResource = getEntityResource();
  211. return NULL == curResource ? spDbg::getInstance()->sp_dbg_set_level(level) : ((spDbg *)curResource->m_dbg)->sp_dbg_set_level(level);
  212. }
  213. SPBASE_API int sp_dbg_term()
  214. {
  215. EntityGloabalResource *curResource = getEntityResource();
  216. return NULL == curResource ? spDbg::getInstance()->sp_dbg_term() : ((spDbg *)curResource->m_dbg)->sp_dbg_term();
  217. }
  218. static inline void sp_dbg_with_level(int level, const char* str, va_list arg)
  219. {
  220. EntityGloabalResource* curResource = getEntityResource();
  221. if (NULL == curResource)
  222. spDbg::getInstance()->sp_dbg_log(level, str, arg);
  223. else
  224. ((spDbg*)curResource->m_dbg)->sp_dbg_log(level, str, arg);
  225. }
  226. static inline void sp_dbg_with_level_ex(int level, const char* str, va_list arg)
  227. {
  228. EntityGloabalResource* curResource = getEntityResource();
  229. if (NULL == curResource)
  230. spDbg::getInstance()->sp_dbg_logOutMsg(level, str, arg);
  231. else
  232. ((spDbg*)curResource->m_dbg)->sp_dbg_logOutMsg(level, str, arg);
  233. }
  234. SPBASE_API void sp_dbg_trace(const char* str, ...)
  235. {
  236. if (str == nullptr)
  237. return;
  238. va_list arg;
  239. va_start(arg, str);
  240. sp_dbg_with_level(XLOG_LEVEL_TRACE, str, arg);
  241. va_end(arg);
  242. }
  243. SPBASE_API void sp_dbg_traceNoOut(const char* str, ...)
  244. {
  245. if (str == nullptr)
  246. return;
  247. va_list arg;
  248. va_start(arg, str);
  249. sp_dbg_with_level_ex(XLOG_LEVEL_TRACE, str, arg);
  250. va_end(arg);
  251. }
  252. SPBASE_API void sp_dbg_debug(const char *str, ...)
  253. {
  254. if (str == nullptr)
  255. return;
  256. va_list arg;
  257. va_start(arg, str);
  258. sp_dbg_with_level(XLOG_LEVEL_DEBUG, str, arg);
  259. va_end(arg);
  260. }
  261. SPBASE_API void sp_dbg_debugNoOut(const char* str, ...)
  262. {
  263. if (str == nullptr)
  264. return;
  265. va_list arg;
  266. va_start(arg, str);
  267. sp_dbg_with_level_ex(XLOG_LEVEL_DEBUG, str, arg);
  268. va_end(arg);
  269. }
  270. SPBASE_API void sp_dbg_info(const char *str, ...)
  271. {
  272. if (str == nullptr)
  273. return;
  274. va_list arg;
  275. va_start(arg, str);
  276. sp_dbg_with_level(XLOG_LEVEL_INFO, str, arg);
  277. va_end(arg);
  278. }
  279. SPBASE_API void sp_dbg_infoNoOut(const char* str, ...)
  280. {
  281. if (str == nullptr)
  282. return;
  283. va_list arg;
  284. va_start(arg, str);
  285. sp_dbg_with_level_ex(XLOG_LEVEL_INFO, str, arg);
  286. va_end(arg);
  287. }
  288. SPBASE_API void sp_dbg_warn(const char *str, ...)
  289. {
  290. if (str == nullptr)
  291. return;
  292. va_list arg;
  293. va_start(arg, str);
  294. sp_dbg_with_level(XLOG_LEVEL_WARN, str, arg);
  295. va_end(arg);
  296. }
  297. SPBASE_API void sp_dbg_warnNoOut(const char* str, ...)
  298. {
  299. if (str == nullptr)
  300. return;
  301. va_list arg;
  302. va_start(arg, str);
  303. sp_dbg_with_level_ex(XLOG_LEVEL_WARN, str, arg);
  304. va_end(arg);
  305. }
  306. SPBASE_API void sp_dbg_error(const char *str, ...)
  307. {
  308. if (str == nullptr)
  309. return;
  310. va_list arg;
  311. va_start(arg, str);
  312. sp_dbg_with_level(XLOG_LEVEL_ERROR, str, arg);
  313. va_end(arg);
  314. }
  315. SPBASE_API void sp_dbg_errorNoOut(const char* str, ...)
  316. {
  317. if (str == nullptr)
  318. return;
  319. va_list arg;
  320. va_start(arg, str);
  321. sp_dbg_with_level_ex(XLOG_LEVEL_ERROR, str, arg);
  322. va_end(arg);
  323. }
  324. SPBASE_API void sp_dbg_fatal(const char *str, ...)
  325. {
  326. if (str == nullptr)
  327. return;
  328. va_list arg;
  329. va_start(arg, str);
  330. sp_dbg_with_level(XLOG_LEVEL_FATAL, str, arg);
  331. va_end(arg);
  332. }
  333. SPBASE_API void sp_dbg_fatalNoOut(const char* str, ...)
  334. {
  335. if (str == nullptr)
  336. return;
  337. va_list arg;
  338. va_start(arg, str);
  339. sp_dbg_with_level_ex(XLOG_LEVEL_FATAL, str, arg);
  340. va_end(arg);
  341. }