error_code.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. //Error_code是错误码的底层通用模块,
  2. //功能:用作故障分析和处理。
  3. //用法:所有的功能接口函数return错误管理类,
  4. //然后上层判断分析错误码,并进行故障处理。
  5. #include "error_code.h"
  6. /////////////////////////////////////////////
  7. //构造函数
  8. Error_manager::Error_manager()
  9. {
  10. m_error_code = SUCCESS;
  11. m_error_level = NORMAL;
  12. pm_error_description = 0;
  13. m_description_length = 0;
  14. return ;
  15. }
  16. //拷贝构造
  17. Error_manager::Error_manager(const Error_manager & error_manager)
  18. {
  19. this->m_error_code = error_manager.m_error_code;
  20. this->m_error_level = error_manager.m_error_level;
  21. pm_error_description = NULL;
  22. m_description_length = 0;
  23. reallocate_memory_and_copy_string(error_manager.pm_error_description, error_manager.m_description_length);
  24. return ;
  25. }
  26. //赋值构造
  27. Error_manager::Error_manager(Error_code error_code, Error_level error_level,
  28. char* p_error_description, int description_length)
  29. {
  30. pm_error_description=NULL;
  31. m_error_code = error_code;
  32. m_error_level = error_level;
  33. m_description_length = 0;
  34. reallocate_memory_and_copy_string(p_error_description, description_length);
  35. return ;
  36. }
  37. //赋值构造
  38. Error_manager::Error_manager(Error_code error_code, Error_level error_level , std::string & error_aggregate_string)
  39. {
  40. pm_error_description=NULL;
  41. m_error_code = error_code;
  42. m_error_level = error_level;
  43. m_description_length = 0;
  44. reallocate_memory_and_copy_string(error_aggregate_string);
  45. return ;
  46. }
  47. //析构函数
  48. Error_manager::~Error_manager()
  49. {
  50. free_description();
  51. }
  52. //初始化
  53. void Error_manager::error_manager_init()
  54. {
  55. error_manager_clear_all();
  56. return;
  57. }
  58. //初始化
  59. void Error_manager::error_manager_init(Error_code error_code, Error_level error_level, char* p_error_description, int description_length)
  60. {
  61. m_error_code = error_code;
  62. m_error_level = error_level;
  63. reallocate_memory_and_copy_string(p_error_description, description_length);
  64. return ;
  65. }
  66. //初始化
  67. void Error_manager::error_manager_init(Error_code error_code, Error_level error_level , std::string & error_aggregate_string)
  68. {
  69. m_error_code = error_code;
  70. m_error_level = error_level;
  71. reallocate_memory_and_copy_string(error_aggregate_string);
  72. return ;
  73. }
  74. //重置
  75. void Error_manager::error_manager_reset(Error_code error_code, Error_level error_level, char* p_error_description, int description_length)
  76. {
  77. m_error_code = error_code;
  78. m_error_level = error_level;
  79. reallocate_memory_and_copy_string(p_error_description, description_length);
  80. return ;
  81. }
  82. //重置
  83. void Error_manager::error_manager_reset(Error_code error_code, Error_level error_level , std::string & error_aggregate_string)
  84. {
  85. m_error_code = error_code;
  86. m_error_level = error_level;
  87. reallocate_memory_and_copy_string(error_aggregate_string);
  88. return ;
  89. }
  90. //重置
  91. void Error_manager::error_manager_reset(const Error_manager & error_manager)
  92. {
  93. this->m_error_code = error_manager.m_error_code;
  94. this->m_error_level = error_manager.m_error_level;
  95. reallocate_memory_and_copy_string(error_manager.pm_error_description, error_manager.m_description_length);
  96. return ;
  97. }
  98. //清除所有内容
  99. void Error_manager::error_manager_clear_all()
  100. {
  101. m_error_code = SUCCESS;
  102. m_error_level = NORMAL;
  103. free_description();
  104. }
  105. //重载=
  106. Error_manager& Error_manager::operator=(const Error_manager & error_manager)
  107. {
  108. error_manager_reset(error_manager);
  109. }
  110. //重载=,支持Error_manager和Error_code的直接转化,会清空错误等级和描述
  111. Error_manager& Error_manager::operator=(Error_code error_code)
  112. {
  113. error_manager_clear_all();
  114. set_error_code(error_code);
  115. }
  116. //重载==
  117. bool Error_manager::operator==(const Error_manager & error_manager)
  118. {
  119. is_equal_error_manager(error_manager);
  120. }
  121. //重载==,支持Error_manager和Error_code的直接比较
  122. bool Error_manager::operator==(Error_code error_code)
  123. {
  124. if(m_error_code == error_code)
  125. {
  126. return true;
  127. }
  128. else
  129. {
  130. return false;
  131. }
  132. }
  133. //重载!=
  134. bool Error_manager::operator!=(const Error_manager & error_manager)
  135. {
  136. ! is_equal_error_manager(error_manager);
  137. }
  138. //重载!=,支持Error_manager和Error_code的直接比较
  139. bool Error_manager::operator!=(Error_code error_code)
  140. {
  141. if(m_error_code != error_code)
  142. {
  143. return true;
  144. }
  145. else
  146. {
  147. return false;
  148. }
  149. }
  150. //获取错误码
  151. Error_code Error_manager::get_error_code()
  152. {
  153. return m_error_code;
  154. }
  155. //获取错误等级
  156. Error_level Error_manager::get_error_level()
  157. {
  158. return m_error_level;
  159. }
  160. //获取错误描述的指针,(浅拷贝)
  161. char* Error_manager::get_error_description()
  162. {
  163. return pm_error_description;
  164. }
  165. //复制错误描述,(深拷贝)
  166. //output:p_error_description 错误描述的字符串指针,不可以为NULL,必须要有实际的内存
  167. //output:description_length 错误描述的字符串长度,不可以为0,长度最好足够大,一般256即可。
  168. void Error_manager::copy_error_description(char* p_error_description, int description_length)
  169. {
  170. if(p_error_description != NULL && pm_error_description != NULL)
  171. {
  172. char *pt_source = p_error_description;
  173. char* pt_destination = pm_error_description;
  174. int t_length_min = m_description_length;
  175. if(m_description_length > description_length)
  176. {
  177. t_length_min = description_length;
  178. }
  179. for(int i=0;i<t_length_min; i++)
  180. {
  181. *pt_destination = *pt_source;
  182. pt_destination++;
  183. pt_source++;
  184. }
  185. }
  186. return;
  187. }
  188. //复制错误描述,(深拷贝)
  189. //output:error_description_string 错误描述的string
  190. void Error_manager::copy_error_description(std::string & error_description_string)
  191. {
  192. if( (!error_description_string.empty() ) && pm_error_description != NULL)
  193. {
  194. error_description_string = pm_error_description;
  195. }
  196. return;
  197. }
  198. //设置错误码
  199. void Error_manager::set_error_code(Error_code error_code)
  200. {
  201. m_error_code = error_code;
  202. return;
  203. }
  204. //比较错误等级并升级,取高等级的结果
  205. void Error_manager::set_error_level_up(Error_level error_level)
  206. {
  207. if(m_error_level < error_level)
  208. {
  209. m_error_level = error_level;
  210. }
  211. return;
  212. }
  213. //比较错误等级并降级,取低等级的结果
  214. void Error_manager::set_error_level_down(Error_level error_level)
  215. {
  216. if(m_error_level > error_level)
  217. {
  218. m_error_level = error_level;
  219. }
  220. return;
  221. }
  222. //错误等级,设定到固定值
  223. void Error_manager::set_error_level_location(Error_level error_level)
  224. {
  225. m_error_level = error_level;
  226. return;
  227. }
  228. //设置错误描述
  229. void Error_manager::set_error_description(char* p_error_description, int description_length)
  230. {
  231. reallocate_memory_and_copy_string(p_error_description, description_length);
  232. return ;
  233. }
  234. //设置错误描述
  235. void Error_manager::set_error_description(std::string & error_description_string)
  236. {
  237. reallocate_memory_and_copy_string(error_description_string);
  238. return ;
  239. }
  240. //尾部追加错误描述
  241. void Error_manager::add_error_description(char* p_error_description, int description_length)
  242. {
  243. if(p_error_description !=NULL)
  244. {
  245. char* pt_description_front = pm_error_description;
  246. int t_description_front_length = m_description_length;
  247. char* pt_description_back = p_error_description;
  248. int t_description_back_length = 0;
  249. if(description_length == 0)
  250. {
  251. t_description_back_length = 0;
  252. while (*pt_description_back != '\0')
  253. {
  254. t_description_back_length++;
  255. pt_description_back++;
  256. }
  257. t_description_back_length++;
  258. pt_description_back = p_error_description;
  259. }
  260. else
  261. {
  262. t_description_back_length = description_length;
  263. }
  264. int t_description_new_length = t_description_front_length + 5 + t_description_back_length - 1;
  265. char* pt_description_new = (char*) malloc(t_description_new_length );
  266. sprintf(pt_description_new, "%s ### %s", pt_description_front, pt_description_back);
  267. free_description();
  268. pm_error_description = pt_description_new;
  269. m_description_length = t_description_new_length;
  270. }
  271. return ;
  272. }
  273. //尾部追加错误描述
  274. void Error_manager::add_error_description(std::string & error_description_string)
  275. {
  276. if( ! error_description_string.empty() )
  277. {
  278. std::string t_description_string = pm_error_description ;
  279. t_description_string += (" ### "+ error_description_string);
  280. reallocate_memory_and_copy_string(t_description_string);
  281. }
  282. }
  283. //比较错误是否相同,
  284. // 注:只比较错误码和等级
  285. bool Error_manager::is_equal_error_manager(const Error_manager & error_manager)
  286. {
  287. if(this->m_error_code == error_manager.m_error_code
  288. && this->m_error_level == error_manager.m_error_level)
  289. {
  290. return true;
  291. }
  292. else
  293. {
  294. return false;
  295. }
  296. }
  297. //比较并覆盖错误,讲低级错误转为字符串存放于描述中,
  298. //如果错误相同,则保留this的,将输入参数转入描述。
  299. void Error_manager::compare_and_cover_error( Error_manager & error_manager)
  300. {
  301. if(this->m_error_code == SUCCESS)
  302. {
  303. error_manager_reset(error_manager);
  304. }
  305. else if (error_manager.m_error_code == SUCCESS)
  306. {
  307. //
  308. }
  309. else
  310. {
  311. Error_manager t_error_manager_new;
  312. char* pt_string_inside = NULL;
  313. int t_string_inside_length = 0;
  314. if(this->m_error_level < error_manager.m_error_level)
  315. {
  316. t_string_inside_length = ERROR_NAMAGER_TO_STRING_FRONT_LENGTH + this->m_description_length;
  317. pt_string_inside = (char*)malloc(t_string_inside_length);
  318. translate_error_to_string(pt_string_inside, t_string_inside_length);
  319. error_manager_reset(error_manager);
  320. add_error_description(pt_string_inside,t_string_inside_length);
  321. }
  322. else
  323. {
  324. t_string_inside_length = ERROR_NAMAGER_TO_STRING_FRONT_LENGTH + error_manager.m_description_length;
  325. pt_string_inside = (char*)malloc(t_string_inside_length);
  326. error_manager.translate_error_to_string(pt_string_inside, t_string_inside_length);
  327. add_error_description(pt_string_inside,t_string_inside_length);
  328. }
  329. }
  330. }
  331. //将所有的错误信息,格式化为字符串,用作日志打印。
  332. //output:p_error_description 错误汇总的字符串指针,不可以为NULL,必须要有实际的内存
  333. //output:description_length 错误汇总的字符串长度,不可以为0,长度最好足够大,一般256即可。
  334. void Error_manager::translate_error_to_string(char* p_error_aggregate, int aggregate_length )
  335. {
  336. char t_string_array[ERROR_NAMAGER_TO_STRING_FRONT_LENGTH] = {0};
  337. char* pt_index_front = t_string_array;
  338. char* pt_index_back = pm_error_description;
  339. char* pt_index = p_error_aggregate;
  340. sprintf(t_string_array, "error_code:0x%08x, error_level:%02d, error_description:", m_error_code , m_error_level );
  341. int t_length_min = m_description_length + ERROR_NAMAGER_TO_STRING_FRONT_LENGTH -1;
  342. if(t_length_min > aggregate_length)
  343. {
  344. t_length_min = aggregate_length;
  345. }
  346. for(int i=0;i<t_length_min; i++)
  347. {
  348. if(i < ERROR_NAMAGER_TO_STRING_FRONT_LENGTH -1)
  349. {
  350. *pt_index = *pt_index_front;
  351. pt_index++;
  352. pt_index_front++;
  353. }
  354. else
  355. {
  356. *pt_index = *pt_index_back;
  357. pt_index++;
  358. pt_index_back++;
  359. }
  360. }
  361. }
  362. //output:error_description_string 错误汇总的string
  363. void Error_manager::translate_error_to_string(std::string & error_aggregate_string)
  364. {
  365. char t_string_array[ERROR_NAMAGER_TO_STRING_FRONT_LENGTH] = {0};
  366. sprintf(t_string_array, "error_code:0x%08x, error_level:%02d, error_description:", m_error_code , m_error_level );
  367. error_aggregate_string = t_string_array ;
  368. if(pm_error_description != NULL)
  369. {
  370. error_aggregate_string += pm_error_description;
  371. }
  372. else
  373. {
  374. //error_aggregate_string += "NULL";
  375. }
  376. }
  377. //错误码转字符串的简易版,可支持cout<<
  378. //return 错误汇总的string
  379. std::string Error_manager::to_string()
  380. {
  381. std::string t_string;
  382. translate_error_to_string(t_string);
  383. return t_string;
  384. }
  385. //释放错误描述的内存,
  386. void Error_manager::free_description()
  387. {
  388. if(pm_error_description != NULL)
  389. {
  390. free (pm_error_description);
  391. pm_error_description = NULL;
  392. }
  393. m_description_length = 0;
  394. }
  395. //重新分配错误描述的内存,并从外部拷贝新的(深拷贝)
  396. //input:p_error_description 错误描述的字符串指针,可以为NULL,
  397. //input:description_length 错误描述的字符串长度,如果为0,则从p_error_description里面获取有效的长度
  398. void Error_manager::reallocate_memory_and_copy_string(char* p_error_description, int description_length)
  399. {
  400. free_description();
  401. if(p_error_description != NULL)
  402. {
  403. char* pt_source = p_error_description;
  404. char* pt_destination = NULL;
  405. if(description_length == 0)
  406. {
  407. m_description_length = 0;
  408. while (*pt_source != '\0')
  409. {
  410. m_description_length++;
  411. pt_source++;
  412. }
  413. m_description_length++;
  414. pt_source = p_error_description;
  415. }
  416. else
  417. {
  418. m_description_length = description_length;
  419. }
  420. pm_error_description = (char*) malloc(m_description_length );
  421. pt_destination = pm_error_description;
  422. for(int i=0;i<m_description_length; i++)
  423. {
  424. *pt_destination = *pt_source;
  425. pt_destination++;
  426. pt_source++;
  427. }
  428. }
  429. return;
  430. }
  431. //重新分配错误描述的内存,并从外部拷贝新的(深拷贝)
  432. //input:error_aggregate_string 错误描述的string
  433. void Error_manager::reallocate_memory_and_copy_string(std::string & error_aggregate_string)
  434. {
  435. free_description();
  436. if( ! error_aggregate_string.empty())
  437. {
  438. m_description_length = error_aggregate_string.length()+1;
  439. pm_error_description = (char*) malloc( m_description_length );
  440. strcpy(pm_error_description , error_aggregate_string.c_str() );
  441. }
  442. }