error_code.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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. const char* p_error_description, int description_length)
  29. {
  30. m_error_code = error_code;
  31. m_error_level = error_level;
  32. pm_error_description = NULL;
  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. m_error_code = error_code;
  41. m_error_level = error_level;
  42. pm_error_description = NULL;
  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, const 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, const 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. return;
  105. }
  106. //重载=
  107. Error_manager& Error_manager::operator=(const Error_manager & error_manager)
  108. {
  109. error_manager_reset(error_manager);
  110. return *this;
  111. }
  112. //重载=,支持Error_manager和Error_code的直接转化,会清空错误等级和描述
  113. Error_manager& Error_manager::operator=(Error_code error_code)
  114. {
  115. error_manager_clear_all();
  116. set_error_code(error_code);
  117. return *this;
  118. }
  119. //重载==
  120. bool Error_manager::operator==(const Error_manager & error_manager)
  121. {
  122. return is_equal_error_manager(error_manager);
  123. }
  124. //重载==,支持Error_manager和Error_code的直接比较
  125. bool Error_manager::operator==(Error_code error_code)
  126. {
  127. if(m_error_code == error_code)
  128. {
  129. return true;
  130. }
  131. else
  132. {
  133. return false;
  134. }
  135. }
  136. //重载!=
  137. bool Error_manager::operator!=(const Error_manager & error_manager)
  138. {
  139. return (! is_equal_error_manager(error_manager));
  140. }
  141. //重载!=,支持Error_manager和Error_code的直接比较
  142. bool Error_manager::operator!=(Error_code error_code)
  143. {
  144. if(m_error_code != error_code)
  145. {
  146. return true;
  147. }
  148. else
  149. {
  150. return false;
  151. }
  152. }
  153. //重载<<,支持cout<<
  154. std::ostream & operator<<(std::ostream &out, Error_manager &error_manager)
  155. {
  156. out << error_manager.to_string();
  157. return out;
  158. }
  159. //获取错误码
  160. Error_code Error_manager::get_error_code()
  161. {
  162. return m_error_code;
  163. }
  164. //获取错误等级
  165. Error_level Error_manager::get_error_level()
  166. {
  167. return m_error_level;
  168. }
  169. //获取错误描述的指针,(浅拷贝)
  170. char* Error_manager::get_error_description()
  171. {
  172. return pm_error_description;
  173. }
  174. int Error_manager::get_description_length()
  175. {
  176. return m_description_length;
  177. }
  178. //复制错误描述,(深拷贝)
  179. //output:p_error_description 错误描述的字符串指针,不可以为NULL,必须要有实际的内存
  180. //output:description_length 错误描述的字符串长度,不可以为0,长度最好足够大,一般256即可。
  181. void Error_manager::copy_error_description(const char* p_error_description, int description_length)
  182. {
  183. if(p_error_description != NULL && pm_error_description != NULL)
  184. {
  185. char *pt_source = (char *)p_error_description;
  186. char* pt_destination = pm_error_description;
  187. int t_length_min = m_description_length;
  188. if(m_description_length > description_length)
  189. {
  190. t_length_min = description_length;
  191. }
  192. for(int i=0;i<t_length_min; i++)
  193. {
  194. *pt_destination = *pt_source;
  195. pt_destination++;
  196. pt_source++;
  197. }
  198. }
  199. return;
  200. }
  201. //复制错误描述,(深拷贝)
  202. //output:error_description_string 错误描述的string
  203. void Error_manager::copy_error_description(std::string & error_description_string)
  204. {
  205. if( (!error_description_string.empty() ) && pm_error_description != NULL)
  206. {
  207. error_description_string = pm_error_description;
  208. }
  209. return;
  210. }
  211. //设置错误码
  212. void Error_manager::set_error_code(Error_code error_code)
  213. {
  214. m_error_code = error_code;
  215. return;
  216. }
  217. //比较错误等级并升级,取高等级的结果
  218. void Error_manager::set_error_level_up(Error_level error_level)
  219. {
  220. if(m_error_level < error_level)
  221. {
  222. m_error_level = error_level;
  223. }
  224. return;
  225. }
  226. //比较错误等级并降级,取低等级的结果
  227. void Error_manager::set_error_level_down(Error_level error_level)
  228. {
  229. if(m_error_level > error_level)
  230. {
  231. m_error_level = error_level;
  232. }
  233. return;
  234. }
  235. //错误等级,设定到固定值
  236. void Error_manager::set_error_level_location(Error_level error_level)
  237. {
  238. m_error_level = error_level;
  239. return;
  240. }
  241. //设置错误描述
  242. void Error_manager::set_error_description(const char* p_error_description, int description_length)
  243. {
  244. reallocate_memory_and_copy_string(p_error_description, description_length);
  245. return ;
  246. }
  247. //设置错误描述
  248. void Error_manager::set_error_description(std::string & error_description_string)
  249. {
  250. reallocate_memory_and_copy_string(error_description_string);
  251. return ;
  252. }
  253. //尾部追加错误描述
  254. void Error_manager::add_error_description(const char* p_error_description, int description_length)
  255. {
  256. if(p_error_description !=NULL)
  257. {
  258. char* pt_description_front = pm_error_description;
  259. int t_description_front_length = m_description_length;
  260. char* pt_description_back = (char *)p_error_description;
  261. int t_description_back_length = 0;
  262. if(description_length == 0)
  263. {
  264. t_description_back_length = 0;
  265. while (*pt_description_back != '\0')
  266. {
  267. t_description_back_length++;
  268. pt_description_back++;
  269. }
  270. t_description_back_length++;
  271. pt_description_back = (char *)p_error_description;
  272. }
  273. else
  274. {
  275. t_description_back_length = description_length;
  276. }
  277. int t_description_new_length = t_description_front_length + 5 + t_description_back_length - 1;
  278. char* pt_description_new = (char*) malloc(t_description_new_length );
  279. sprintf(pt_description_new, "%s ### %s", pt_description_front, pt_description_back);
  280. free_description();
  281. pm_error_description = pt_description_new;
  282. m_description_length = t_description_new_length;
  283. }
  284. return ;
  285. }
  286. //尾部追加错误描述
  287. void Error_manager::add_error_description(std::string & error_description_string)
  288. {
  289. if( ! error_description_string.empty() )
  290. {
  291. std::string t_description_string = pm_error_description ;
  292. t_description_string += (" ### "+ error_description_string);
  293. reallocate_memory_and_copy_string(t_description_string);
  294. }
  295. return;
  296. }
  297. //比较错误是否相同,
  298. // 注:只比较错误码和等级
  299. bool Error_manager::is_equal_error_manager(const Error_manager & error_manager)
  300. {
  301. if(this->m_error_code == error_manager.m_error_code
  302. && this->m_error_level == error_manager.m_error_level)
  303. {
  304. return true;
  305. }
  306. else
  307. {
  308. return false;
  309. }
  310. }
  311. //比较并覆盖错误,讲低级错误转为字符串存放于描述中,
  312. //如果错误相同,则保留this的,将输入参数转入描述。
  313. void Error_manager::compare_and_cover_error(const Error_manager & error_manager)
  314. {
  315. if(this->m_error_code == SUCCESS)
  316. {
  317. error_manager_reset(error_manager);
  318. }
  319. else if (error_manager.m_error_code == SUCCESS)
  320. {
  321. return;
  322. }
  323. else
  324. {
  325. Error_manager t_error_manager_new;
  326. char* pt_string_inside = NULL;
  327. int t_string_inside_length = 0;
  328. if(this->m_error_level < error_manager.m_error_level)
  329. {
  330. t_string_inside_length = ERROR_NAMAGER_TO_STRING_FRONT_LENGTH + this->m_description_length;
  331. pt_string_inside = (char*)malloc(t_string_inside_length);
  332. translate_error_to_string(pt_string_inside, t_string_inside_length);
  333. error_manager_reset(error_manager);
  334. add_error_description(pt_string_inside,t_string_inside_length);
  335. }
  336. else
  337. {
  338. t_string_inside_length = ERROR_NAMAGER_TO_STRING_FRONT_LENGTH + error_manager.m_description_length;
  339. pt_string_inside = (char*)malloc(t_string_inside_length);
  340. ((Error_manager & )error_manager).translate_error_to_string(pt_string_inside, t_string_inside_length);
  341. add_error_description(pt_string_inside,t_string_inside_length);
  342. }
  343. }
  344. return;
  345. }
  346. //比较并覆盖错误,讲低级错误转为字符串存放于描述中,
  347. //如果错误相同,则保留this的,将输入参数转入描述。
  348. void Error_manager::compare_and_cover_error( Error_manager * p_error_manager)
  349. {
  350. if(this->m_error_code == SUCCESS)
  351. {
  352. error_manager_reset(*p_error_manager);
  353. }
  354. else if (p_error_manager->m_error_code == SUCCESS)
  355. {
  356. //
  357. }
  358. else
  359. {
  360. Error_manager t_error_manager_new;
  361. char* pt_string_inside = NULL;
  362. int t_string_inside_length = 0;
  363. if(this->m_error_level < p_error_manager->m_error_level)
  364. {
  365. t_string_inside_length = ERROR_NAMAGER_TO_STRING_FRONT_LENGTH + this->m_description_length;
  366. pt_string_inside = (char*)malloc(t_string_inside_length);
  367. translate_error_to_string(pt_string_inside, t_string_inside_length);
  368. error_manager_reset(*p_error_manager);
  369. add_error_description(pt_string_inside,t_string_inside_length);
  370. }
  371. else
  372. {
  373. t_string_inside_length = ERROR_NAMAGER_TO_STRING_FRONT_LENGTH + p_error_manager->m_description_length;
  374. pt_string_inside = (char*)malloc(t_string_inside_length);
  375. p_error_manager->translate_error_to_string(pt_string_inside, t_string_inside_length);
  376. add_error_description(pt_string_inside,t_string_inside_length);
  377. }
  378. }
  379. return;
  380. }
  381. //将所有的错误信息,格式化为字符串,用作日志打印。
  382. //output:p_error_description 错误汇总的字符串指针,不可以为NULL,必须要有实际的内存
  383. //output:description_length 错误汇总的字符串长度,不可以为0,长度最好足够大,一般256即可。
  384. void Error_manager::translate_error_to_string(char* p_error_aggregate, int aggregate_length )
  385. {
  386. char t_string_array[ERROR_NAMAGER_TO_STRING_FRONT_LENGTH] = {0};
  387. char* pt_index_front = t_string_array;
  388. char* pt_index_back = pm_error_description;
  389. char* pt_index = p_error_aggregate;
  390. sprintf(t_string_array, "error_code:0x%08x, error_level:%02d, error_description:", m_error_code , m_error_level );
  391. int t_length_min = m_description_length + ERROR_NAMAGER_TO_STRING_FRONT_LENGTH -1;
  392. if(t_length_min > aggregate_length)
  393. {
  394. t_length_min = aggregate_length;
  395. }
  396. for(int i=0;i<t_length_min; i++)
  397. {
  398. if(i < ERROR_NAMAGER_TO_STRING_FRONT_LENGTH -1)
  399. {
  400. *pt_index = *pt_index_front;
  401. pt_index++;
  402. pt_index_front++;
  403. }
  404. else
  405. {
  406. *pt_index = *pt_index_back;
  407. pt_index++;
  408. pt_index_back++;
  409. }
  410. }
  411. return;
  412. }
  413. //output:error_description_string 错误汇总的string
  414. void Error_manager::translate_error_to_string(std::string & error_aggregate_string)
  415. {
  416. char t_string_array[ERROR_NAMAGER_TO_STRING_FRONT_LENGTH] = {0};
  417. sprintf(t_string_array, "error_code:0x%08x, error_level:%02d, error_description:", m_error_code , m_error_level );
  418. error_aggregate_string = t_string_array ;
  419. if(pm_error_description != NULL)
  420. {
  421. error_aggregate_string += pm_error_description;
  422. }
  423. else
  424. {
  425. //error_aggregate_string += "NULL";
  426. }
  427. return;
  428. }
  429. //错误码转字符串的简易版,可支持cout<<
  430. //return 错误汇总的string
  431. std::string Error_manager::to_string()
  432. {
  433. std::string t_string;
  434. translate_error_to_string(t_string);
  435. return t_string;
  436. }
  437. //释放错误描述的内存,
  438. void Error_manager::free_description()
  439. {
  440. if(pm_error_description != NULL)
  441. {
  442. free (pm_error_description);
  443. pm_error_description = NULL;
  444. }
  445. m_description_length = 0;
  446. return;
  447. }
  448. //重新分配错误描述的内存,并从外部拷贝新的(深拷贝)
  449. //input:p_error_description 错误描述的字符串指针,可以为NULL,
  450. //input:description_length 错误描述的字符串长度,如果为0,则从p_error_description里面获取有效的长度
  451. void Error_manager::reallocate_memory_and_copy_string(const char* p_error_description, int description_length)
  452. {
  453. free_description();
  454. if(p_error_description != NULL)
  455. {
  456. char* pt_source = (char *)p_error_description;
  457. char* pt_destination = NULL;
  458. if(description_length == 0)
  459. {
  460. m_description_length = 0;
  461. while (*pt_source != '\0')
  462. {
  463. m_description_length++;
  464. pt_source++;
  465. }
  466. m_description_length++;
  467. pt_source = (char *)p_error_description;
  468. }
  469. else
  470. {
  471. m_description_length = description_length;
  472. }
  473. pm_error_description = (char*) malloc(m_description_length );
  474. pt_destination = pm_error_description;
  475. for(int i=0;i<m_description_length; i++)
  476. {
  477. *pt_destination = *pt_source;
  478. pt_destination++;
  479. pt_source++;
  480. }
  481. }
  482. return;
  483. }
  484. //重新分配错误描述的内存,并从外部拷贝新的(深拷贝)
  485. //input:error_aggregate_string 错误描述的string
  486. void Error_manager::reallocate_memory_and_copy_string(std::string & error_aggregate_string)
  487. {
  488. free_description();
  489. if( ! error_aggregate_string.empty())
  490. {
  491. m_description_length = error_aggregate_string.length()+1;
  492. pm_error_description = (char*) malloc( m_description_length );
  493. strcpy(pm_error_description , error_aggregate_string.c_str() );
  494. }
  495. return;
  496. }