/*** @file error_code.c* @author mengplus (chengmeng_2@outlook.com)* @brief* @version 0.1* @date 2023年12月13日** @copyright Copyright (c) 2023**/#include "error_code.h"#include <time.h>// 全局的错误码链表头部static error_code_t error_code_list_head;// 在 error_code.c 文件中static int is_duplicate_code(uint32_t code){error_code_t *current_error = error_code_list_head.next;while (current_error != NULL){if (current_error->code == code){return 1; // 错误码重复}current_error = current_error->next;}return 0; // 错误码唯一}void error_code_init(){// 初始化错误码链表头部error_code_list_head.prev = NULL;error_code_list_head.next = NULL;error_code_list_head.code = 0;}uint16_t error_code_register(error_code_t *obj){// 检查是否已存在相同错误码if (is_duplicate_code(obj->code)){return 1; // 错误码重复,注册失败}// 在链表头部插入新的错误码对象obj->prev = NULL;obj->next = error_code_list_head.next;if (error_code_list_head.next != NULL){error_code_list_head.next->prev = obj;}error_code_list_head.next = obj;obj->cnt = 0;obj->err_cnt = 0;obj->err_st = 0;obj->err_ed = 0;return 0; // 注册成功}// 在 error_code.c 源文件中添加移除接口的实现uint16_t error_code_unregister(error_code_t *obj){if (obj == NULL){return 1; // 无效的错误码对象}// 从链表中移除该错误码对象if (obj->prev != NULL){obj->prev->next = obj->next;}else{// obj 是链表头部error_code_list_head.next = obj->next;}if (obj->next != NULL){obj->next->prev = obj->prev;}// 释放可能的资源,比如动态分配的内存return 0; // 移除成功}error_code_t *error_code_get_list(){return error_code_list_head.next;}error_code_t *error_code_get_next_list(const error_code_t *item){return item->next;}uint8_t error_code_set_flag(error_code_t *obj){if (obj->cnt == 0){obj->err_st = time(NULL);}// 设置错误标志if (!error_code_is_error(obj)){obj->cnt++;}return 0; // 操作成功}void error_code_clear_flag(error_code_t *obj){if (error_code_is_error(obj)){obj->err_cnt++;}// 清除错误标志obj->cnt = 0; // 设置错误结束标志}uint8_t error_code_is_error(error_code_t *obj){// 检查是否发生错误return obj->cnt_max <= obj->cnt;}error_code_t *error_code_find(uint32_t code){error_code_t *current_error = error_code_list_head.next;while (current_error != NULL){if (current_error->code == code){return current_error; // 找到对应的错误码对象}current_error = current_error->next;}return NULL; // 未找到对应的错误码对象}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。