error_code.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. //Error_code是错误码的底层通用模块,
  2. //功能:用作故障分析和处理。
  3. //用法:所有的功能接口函数return错误管理类,
  4. //然后上层判断分析错误码,并进行故障处理。
  5. #ifndef TEST_ERROR_ERROR_CODE_H
  6. #define TEST_ERROR_ERROR_CODE_H
  7. #include <string>
  8. #include <string.h>
  9. #include<iostream>
  10. //错误管理类转化为字符串 的前缀,固定长度为58
  11. //这个是由显示格式来确定的,如果要修改格式或者 Error_code长度超过8位,Error_level长度超过2位,折需要重新计算
  12. #define ERROR_NAMAGER_TO_STRING_FRONT_LENGTH 58
  13. //进程加锁的状态,
  14. enum Lock_status
  15. {
  16. UNLOCK = 0,
  17. LOCK = 1,
  18. };
  19. //设备使能状态,
  20. enum Able_status
  21. {
  22. UNABLE = 0,
  23. ENABLE = 1,
  24. };
  25. //数据是否为空
  26. enum Empty_status
  27. {
  28. NON_EMPTY = 0,
  29. EMPTY = 1,
  30. };
  31. //错误码的枚举,用来做故障分析
  32. enum Error_code
  33. {
  34. //成功,没有错误,默认值0
  35. SUCCESS = 0x00000000,
  36. //基本错误码,
  37. ERROR = 0x00000001,//错误
  38. PARTIAL_SUCCESS = 0x00000002,//部分成功
  39. WARNING = 0x00000003,//警告
  40. FAILED = 0x00000004,//失败
  41. NO_DATA = 0x00000010,//没有数据,传入参数容器内部没有数据时,
  42. POINTER_IS_NULL = 0x00000101,//空指针
  43. PARAMETER_ERROR = 0x00000102,//参数错误,传入参数不符合规范时,
  44. POINTER_MALLOC_FAIL = 0x00000103,//手动分配内存失败
  45. CLASS_BASE_FUNCTION_CANNOT_USE = 0x00000201,//基类函数不允许使用,必须使用子类的
  46. // 错误码的规范,
  47. // 错误码是int型,32位,十六进制。
  48. // 例如0x12345678
  49. // 12表示功能模块,例如:laser雷达模块 框架制定
  50. // 34表示文件名称,例如:laser_livox.cpp 框架制定
  51. // 56表示具体的类,例如:class laser_livox 个人制定
  52. // 78表示类的函数,例如:laser_livox::start(); 个人制定
  53. // 注:错误码的制定从1开始,不要从0开始,
  54. // 0用作错误码的基数,用来位运算,来判断错误码的范围。
  55. // nnxx client
  56. NNXX_CLIENT_ERROR = 0x01000000,
  57. NNXX_CLIENT_REQUEST_TIMEOUT,
  58. NNXX_CLIENT_REQUEST_UNKNOW,
  59. NNXX_CLIENT_UNCONNECT,
  60. //locate 错误
  61. LOCATE_RESPONSE_PARSE_ERROR = 0x02000000,
  62. };
  63. //错误等级,用来做故障处理
  64. enum Error_level
  65. {
  66. // 正常,没有错误,默认值0
  67. NORMAL = 0,
  68. // 轻微故障,可忽略的故障,NEGLIGIBLE_ERROR
  69. // 提示作用,不做任何处理,不影响代码的流程,
  70. // 用作一些不重要的事件,即使出错也不会影响到系统功能,
  71. // 例如:文件保存错误,等
  72. NEGLIGIBLE_ERROR = 1,
  73. // 一般故障,MINOR_ERROR
  74. // 用作底层功能函数的错误返回,表示该功能函数执行失败,
  75. // 返回给应用层之后,需要做故障分析和处理,
  76. // 例如:雷达数据传输失败,应用层就需要进行重新扫描,或者重连,或者重置参数等。
  77. MINOR_ERROR = 2,
  78. // 严重故障,MAJOR_ERROR
  79. // 用作应用层的任务事件的结果,表示该功能模块失败。
  80. // 通常是底层函数返回一般故障之后,应用层无法处理并解决故障,此时就要进行故障升级,
  81. // 从一般故障升级为严重故障,然后进行回退流程,回退已经执行的操作,最终回到故障待机状态。
  82. // 需要外部清除故障,并复位至正常待机状态,才能恢复功能的使用。
  83. // 例如:雷达扫描任务失败,且无法自动恢复。
  84. MAJOR_ERROR = 3,
  85. // 致命故障,CRITICAL_ERROR
  86. // 系统出现致命错误。导致系统无法正常运行,
  87. // 此时系统应该紧急停机,执行紧急流程,快速停机。
  88. // 此时不允许再执行任何函数和任务指令,防止系统故障更加严重。
  89. // 也不需要做任何错误处理了,快速执行紧急流程。
  90. // 例如:内存错误,进程挂死,关键设备失控,监控设备报警,等
  91. CRITICAL_ERROR = 4,
  92. };
  93. class Error_manager
  94. {
  95. public://外部接口函数
  96. //构造函数
  97. Error_manager();
  98. //拷贝构造
  99. Error_manager(const Error_manager & error_manager);
  100. //赋值构造
  101. Error_manager(Error_code error_code, Error_level error_level = NORMAL,
  102. const char* p_error_description = NULL, int description_length = 0);
  103. //赋值构造
  104. Error_manager(Error_code error_code, Error_level error_level , std::string & error_aggregate_string);
  105. //析构函数
  106. ~Error_manager();
  107. //初始化
  108. void error_manager_init();
  109. //初始化
  110. void error_manager_init(Error_code error_code, Error_level error_level = NORMAL,
  111. const char* p_error_description = NULL, int description_length = 0);
  112. //初始化
  113. void error_manager_init(Error_code error_code, Error_level error_level , std::string & error_aggregate_string);
  114. //重置
  115. void error_manager_reset(Error_code error_code, Error_level error_level = NORMAL,
  116. const char* p_error_description = NULL, int description_length = 0);
  117. //重置
  118. void error_manager_reset(Error_code error_code, Error_level error_level , std::string & error_aggregate_string);
  119. //重置
  120. void error_manager_reset(const Error_manager & error_manager);
  121. //清除所有内容
  122. void error_manager_clear_all();
  123. //重载=
  124. Error_manager& operator=(const Error_manager & error_manager);
  125. //重载=,支持Error_manager和Error_code的直接转化,会清空错误等级和描述
  126. Error_manager& operator=(Error_code error_code);
  127. //重载==
  128. bool operator==(const Error_manager & error_manager);
  129. //重载==,支持Error_manager和Error_code的直接比较
  130. bool operator==(Error_code error_code);
  131. //重载!=
  132. bool operator!=(const Error_manager & error_manager);
  133. //重载!=,支持Error_manager和Error_code的直接比较
  134. bool operator!=(Error_code error_code);
  135. //重载<<,支持cout<<
  136. friend std::ostream & operator<<(std::ostream &out, Error_manager &error_manager);
  137. //获取错误码
  138. Error_code get_error_code();
  139. //获取错误等级
  140. Error_level get_error_level();
  141. //获取错误描述的指针,(浅拷贝)
  142. char* get_error_description();
  143. //复制错误描述,(深拷贝)
  144. //output:p_error_description 错误描述的字符串指针,不可以为NULL,必须要有实际的内存
  145. //output:description_length 错误描述的字符串长度,不可以为0,长度最好足够大,一般256即可。
  146. void copy_error_description(const char* p_error_description, int description_length);
  147. //复制错误描述,(深拷贝)
  148. //output:error_description_string 错误描述的string
  149. void copy_error_description(std::string & error_description_string);
  150. //设置错误码
  151. void set_error_code(Error_code error_code);
  152. //比较错误等级并升级,取高等级的结果
  153. void set_error_level_up(Error_level error_level);
  154. //比较错误等级并降级,取低等级的结果
  155. void set_error_level_down(Error_level error_level);
  156. //错误等级,设定到固定值
  157. void set_error_level_location(Error_level error_level);
  158. //设置错误描述
  159. void set_error_description(const char* p_error_description, int description_length = 0);
  160. //设置错误描述
  161. void set_error_description(std::string & error_description_string);
  162. //尾部追加错误描述
  163. void add_error_description(const char* p_error_description, int description_length = 0);
  164. //尾部追加错误描述
  165. void add_error_description(std::string & error_description_string);
  166. //比较错误是否相同,
  167. // 注:只比较错误码和等级
  168. bool is_equal_error_manager(const Error_manager & error_manager);
  169. //比较并覆盖错误,讲低级错误转为字符串存放于描述中,
  170. //如果错误相同,则保留this的,将输入参数转入描述。
  171. void compare_and_cover_error(const Error_manager & error_manager);
  172. //比较并覆盖错误,讲低级错误转为字符串存放于描述中,
  173. //如果错误相同,则保留this的,将输入参数转入描述。
  174. void compare_and_cover_error( Error_manager * p_error_manager);
  175. //将所有的错误信息,格式化为字符串,用作日志打印。
  176. //output:p_error_description 错误汇总的字符串指针,不可以为NULL,必须要有实际的内存
  177. //output:description_length 错误汇总的字符串长度,不可以为0,长度最好足够大,一般256即可。
  178. void translate_error_to_string(char* p_error_aggregate, int aggregate_length);
  179. //output:error_description_string 错误汇总的string
  180. void translate_error_to_string(std::string & error_aggregate_string);
  181. //错误码转字符串的简易版,可支持cout<<
  182. //return 错误汇总的string
  183. std::string to_string();
  184. protected:
  185. Error_code m_error_code; //错误码
  186. Error_level m_error_level; //错误等级
  187. char* pm_error_description; //错误描述
  188. int m_description_length; //错误描述的字符长度
  189. protected://内部功能函数
  190. public:
  191. //释放错误描述的内存,
  192. void free_description();
  193. //重新分配错误描述的内存,并从外部拷贝新的(深拷贝)
  194. //input:p_error_description 错误描述的字符串指针,可以为NULL,
  195. //input:description_length 错误描述的字符串长度,如果为0,则从p_error_description里面获取有效的长度
  196. void reallocate_memory_and_copy_string(const char* p_error_description, int description_length = 0);
  197. //重新分配错误描述的内存,并从外部拷贝新的(深拷贝)
  198. //input:error_aggregate_string 错误描述的string
  199. void reallocate_memory_and_copy_string(std::string & error_aggregate_string);
  200. };
  201. #endif //TEST_ERROR_ERROR_CODE_H