123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490 |
- //Error_code是错误码的底层通用模块,
- //功能:用作故障分析和处理。
- //用法:所有的功能接口函数return错误管理类,
- //然后上层判断分析错误码,并进行故障处理。
- #include "error_code.h"
- /////////////////////////////////////////////
- //构造函数
- Error_manager::Error_manager()
- {
- m_error_code = SUCCESS;
- m_error_level = NORMAL;
- pm_error_description = 0;
- m_description_length = 0;
- return ;
- }
- //拷贝构造
- Error_manager::Error_manager(const Error_manager & error_manager)
- {
- this->m_error_code = error_manager.m_error_code;
- this->m_error_level = error_manager.m_error_level;
- pm_error_description = NULL;
- m_description_length = 0;
- reallocate_memory_and_copy_string(error_manager.pm_error_description, error_manager.m_description_length);
- return ;
- }
- //赋值构造
- Error_manager::Error_manager(Error_code error_code, Error_level error_level,
- char* p_error_description, int description_length)
- {
- pm_error_description=NULL;
- m_error_code = error_code;
- m_error_level = error_level;
- m_description_length = 0;
- reallocate_memory_and_copy_string(p_error_description, description_length);
- return ;
- }
- //赋值构造
- Error_manager::Error_manager(Error_code error_code, Error_level error_level , std::string & error_aggregate_string)
- {
- pm_error_description=NULL;
- m_error_code = error_code;
- m_error_level = error_level;
- m_description_length = 0;
- reallocate_memory_and_copy_string(error_aggregate_string);
- return ;
- }
- //析构函数
- Error_manager::~Error_manager()
- {
- free_description();
- }
- //初始化
- void Error_manager::error_manager_init()
- {
- error_manager_clear_all();
- return;
- }
- //初始化
- void Error_manager::error_manager_init(Error_code error_code, Error_level error_level, char* p_error_description, int description_length)
- {
- m_error_code = error_code;
- m_error_level = error_level;
- reallocate_memory_and_copy_string(p_error_description, description_length);
- return ;
- }
- //初始化
- void Error_manager::error_manager_init(Error_code error_code, Error_level error_level , std::string & error_aggregate_string)
- {
- m_error_code = error_code;
- m_error_level = error_level;
- reallocate_memory_and_copy_string(error_aggregate_string);
- return ;
- }
- //重置
- void Error_manager::error_manager_reset(Error_code error_code, Error_level error_level, char* p_error_description, int description_length)
- {
- m_error_code = error_code;
- m_error_level = error_level;
- reallocate_memory_and_copy_string(p_error_description, description_length);
- return ;
- }
- //重置
- void Error_manager::error_manager_reset(Error_code error_code, Error_level error_level , std::string & error_aggregate_string)
- {
- m_error_code = error_code;
- m_error_level = error_level;
- reallocate_memory_and_copy_string(error_aggregate_string);
- return ;
- }
- //重置
- void Error_manager::error_manager_reset(const Error_manager & error_manager)
- {
- this->m_error_code = error_manager.m_error_code;
- this->m_error_level = error_manager.m_error_level;
- reallocate_memory_and_copy_string(error_manager.pm_error_description, error_manager.m_description_length);
- return ;
- }
- //清除所有内容
- void Error_manager::error_manager_clear_all()
- {
- m_error_code = SUCCESS;
- m_error_level = NORMAL;
- free_description();
- }
- //重载=
- Error_manager& Error_manager::operator=(const Error_manager & error_manager)
- {
- error_manager_reset(error_manager);
- }
- //重载=,支持Error_manager和Error_code的直接转化,会清空错误等级和描述
- Error_manager& Error_manager::operator=(Error_code error_code)
- {
- error_manager_clear_all();
- set_error_code(error_code);
- }
- //重载==
- bool Error_manager::operator==(const Error_manager & error_manager)
- {
- is_equal_error_manager(error_manager);
- }
- //重载==,支持Error_manager和Error_code的直接比较
- bool Error_manager::operator==(Error_code error_code)
- {
- if(m_error_code == error_code)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- //重载!=
- bool Error_manager::operator!=(const Error_manager & error_manager)
- {
- ! is_equal_error_manager(error_manager);
- }
- //重载!=,支持Error_manager和Error_code的直接比较
- bool Error_manager::operator!=(Error_code error_code)
- {
- if(m_error_code != error_code)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- //获取错误码
- Error_code Error_manager::get_error_code()
- {
- return m_error_code;
- }
- //获取错误等级
- Error_level Error_manager::get_error_level()
- {
- return m_error_level;
- }
- //获取错误描述的指针,(浅拷贝)
- char* Error_manager::get_error_description()
- {
- return pm_error_description;
- }
- //复制错误描述,(深拷贝)
- //output:p_error_description 错误描述的字符串指针,不可以为NULL,必须要有实际的内存
- //output:description_length 错误描述的字符串长度,不可以为0,长度最好足够大,一般256即可。
- void Error_manager::copy_error_description(char* p_error_description, int description_length)
- {
- if(p_error_description != NULL && pm_error_description != NULL)
- {
- char *pt_source = p_error_description;
- char* pt_destination = pm_error_description;
- int t_length_min = m_description_length;
- if(m_description_length > description_length)
- {
- t_length_min = description_length;
- }
- for(int i=0;i<t_length_min; i++)
- {
- *pt_destination = *pt_source;
- pt_destination++;
- pt_source++;
- }
- }
- return;
- }
- //复制错误描述,(深拷贝)
- //output:error_description_string 错误描述的string
- void Error_manager::copy_error_description(std::string & error_description_string)
- {
- if( (!error_description_string.empty() ) && pm_error_description != NULL)
- {
- error_description_string = pm_error_description;
- }
- return;
- }
- //设置错误码
- void Error_manager::set_error_code(Error_code error_code)
- {
- m_error_code = error_code;
- return;
- }
- //比较错误等级并升级,取高等级的结果
- void Error_manager::set_error_level_up(Error_level error_level)
- {
- if(m_error_level < error_level)
- {
- m_error_level = error_level;
- }
- return;
- }
- //比较错误等级并降级,取低等级的结果
- void Error_manager::set_error_level_down(Error_level error_level)
- {
- if(m_error_level > error_level)
- {
- m_error_level = error_level;
- }
- return;
- }
- //错误等级,设定到固定值
- void Error_manager::set_error_level_location(Error_level error_level)
- {
- m_error_level = error_level;
- return;
- }
- //设置错误描述
- void Error_manager::set_error_description(char* p_error_description, int description_length)
- {
- reallocate_memory_and_copy_string(p_error_description, description_length);
- return ;
- }
- //设置错误描述
- void Error_manager::set_error_description(std::string & error_description_string)
- {
- reallocate_memory_and_copy_string(error_description_string);
- return ;
- }
- //尾部追加错误描述
- void Error_manager::add_error_description(char* p_error_description, int description_length)
- {
- if(p_error_description !=NULL)
- {
- char* pt_description_front = pm_error_description;
- int t_description_front_length = m_description_length;
- char* pt_description_back = p_error_description;
- int t_description_back_length = 0;
- if(description_length == 0)
- {
- t_description_back_length = 0;
- while (*pt_description_back != '\0')
- {
- t_description_back_length++;
- pt_description_back++;
- }
- t_description_back_length++;
- pt_description_back = p_error_description;
- }
- else
- {
- t_description_back_length = description_length;
- }
- int t_description_new_length = t_description_front_length + 5 + t_description_back_length - 1;
- char* pt_description_new = (char*) malloc(t_description_new_length );
- sprintf(pt_description_new, "%s ### %s", pt_description_front, pt_description_back);
- free_description();
- pm_error_description = pt_description_new;
- m_description_length = t_description_new_length;
- }
- return ;
- }
- //尾部追加错误描述
- void Error_manager::add_error_description(std::string & error_description_string)
- {
- if( ! error_description_string.empty() )
- {
- std::string t_description_string = pm_error_description ;
- t_description_string += (" ### "+ error_description_string);
- reallocate_memory_and_copy_string(t_description_string);
- }
- }
- //比较错误是否相同,
- // 注:只比较错误码和等级
- bool Error_manager::is_equal_error_manager(const Error_manager & error_manager)
- {
- if(this->m_error_code == error_manager.m_error_code
- && this->m_error_level == error_manager.m_error_level)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- //比较并覆盖错误,讲低级错误转为字符串存放于描述中,
- //如果错误相同,则保留this的,将输入参数转入描述。
- void Error_manager::compare_and_cover_error( Error_manager & error_manager)
- {
- if(this->m_error_code == SUCCESS)
- {
- error_manager_reset(error_manager);
- }
- else if (error_manager.m_error_code == SUCCESS)
- {
- //
- }
- else
- {
- Error_manager t_error_manager_new;
- char* pt_string_inside = NULL;
- int t_string_inside_length = 0;
- if(this->m_error_level < error_manager.m_error_level)
- {
- t_string_inside_length = ERROR_NAMAGER_TO_STRING_FRONT_LENGTH + this->m_description_length;
- pt_string_inside = (char*)malloc(t_string_inside_length);
- translate_error_to_string(pt_string_inside, t_string_inside_length);
- error_manager_reset(error_manager);
- add_error_description(pt_string_inside,t_string_inside_length);
- }
- else
- {
- t_string_inside_length = ERROR_NAMAGER_TO_STRING_FRONT_LENGTH + error_manager.m_description_length;
- pt_string_inside = (char*)malloc(t_string_inside_length);
- error_manager.translate_error_to_string(pt_string_inside, t_string_inside_length);
- add_error_description(pt_string_inside,t_string_inside_length);
- }
- }
- }
- //将所有的错误信息,格式化为字符串,用作日志打印。
- //output:p_error_description 错误汇总的字符串指针,不可以为NULL,必须要有实际的内存
- //output:description_length 错误汇总的字符串长度,不可以为0,长度最好足够大,一般256即可。
- void Error_manager::translate_error_to_string(char* p_error_aggregate, int aggregate_length )
- {
- char t_string_array[ERROR_NAMAGER_TO_STRING_FRONT_LENGTH] = {0};
- char* pt_index_front = t_string_array;
- char* pt_index_back = pm_error_description;
- char* pt_index = p_error_aggregate;
- sprintf(t_string_array, "error_code:0x%08x, error_level:%02d, error_description:", m_error_code , m_error_level );
- int t_length_min = m_description_length + ERROR_NAMAGER_TO_STRING_FRONT_LENGTH -1;
- if(t_length_min > aggregate_length)
- {
- t_length_min = aggregate_length;
- }
- for(int i=0;i<t_length_min; i++)
- {
- if(i < ERROR_NAMAGER_TO_STRING_FRONT_LENGTH -1)
- {
- *pt_index = *pt_index_front;
- pt_index++;
- pt_index_front++;
- }
- else
- {
- *pt_index = *pt_index_back;
- pt_index++;
- pt_index_back++;
- }
- }
- }
- //output:error_description_string 错误汇总的string
- void Error_manager::translate_error_to_string(std::string & error_aggregate_string)
- {
- char t_string_array[ERROR_NAMAGER_TO_STRING_FRONT_LENGTH] = {0};
- sprintf(t_string_array, "error_code:0x%08x, error_level:%02d, error_description:", m_error_code , m_error_level );
- error_aggregate_string = t_string_array ;
- if(pm_error_description != NULL)
- {
- error_aggregate_string += pm_error_description;
- }
- else
- {
- //error_aggregate_string += "NULL";
- }
- }
- //错误码转字符串的简易版,可支持cout<<
- //return 错误汇总的string
- std::string Error_manager::to_string()
- {
- std::string t_string;
- translate_error_to_string(t_string);
- return t_string;
- }
- //释放错误描述的内存,
- void Error_manager::free_description()
- {
- if(pm_error_description != NULL)
- {
- free (pm_error_description);
- pm_error_description = NULL;
- }
- m_description_length = 0;
- }
- //重新分配错误描述的内存,并从外部拷贝新的(深拷贝)
- //input:p_error_description 错误描述的字符串指针,可以为NULL,
- //input:description_length 错误描述的字符串长度,如果为0,则从p_error_description里面获取有效的长度
- void Error_manager::reallocate_memory_and_copy_string(char* p_error_description, int description_length)
- {
- free_description();
- if(p_error_description != NULL)
- {
- char* pt_source = p_error_description;
- char* pt_destination = NULL;
- if(description_length == 0)
- {
- m_description_length = 0;
- while (*pt_source != '\0')
- {
- m_description_length++;
- pt_source++;
- }
- m_description_length++;
- pt_source = p_error_description;
- }
- else
- {
- m_description_length = description_length;
- }
- pm_error_description = (char*) malloc(m_description_length );
- pt_destination = pm_error_description;
- for(int i=0;i<m_description_length; i++)
- {
- *pt_destination = *pt_source;
- pt_destination++;
- pt_source++;
- }
- }
- return;
- }
- //重新分配错误描述的内存,并从外部拷贝新的(深拷贝)
- //input:error_aggregate_string 错误描述的string
- void Error_manager::reallocate_memory_and_copy_string(std::string & error_aggregate_string)
- {
- free_description();
- if( ! error_aggregate_string.empty())
- {
- m_description_length = error_aggregate_string.length()+1;
- pm_error_description = (char*) malloc( m_description_length );
- strcpy(pm_error_description , error_aggregate_string.c_str() );
- }
- }
|