我想在C中做一些错误处理。我喜欢C#异常,所以决定做类似的事情。目标是:
您认为这种方法有什么问题?
来电者功能:
EError activate_add(uint32_t key)
{
if (activate_bufferSize+1>=activate_bufferMax) THROW_ERROR("activate list full");
activate_buffer[activate_bufferSize].Id = activate_bufferSize+1;
activate_buffer[activate_bufferSize].StoredKey = key;
activate_bufferSize++;
TRY(activate_saveToEEProm());
NO_ERROR();
}
我的解决方案如下:
#ifndef ERROR_H_
#define ERROR_H_
typedef enum
{
Message_None,
Message_Error,
Message_Warning,
Message_Info
}EMessage_type;
typedef struct
{
EMessage_type message_type;
char* message;
const char* file;
uint32_t line;
}EErrorStruct,*EError;
#define THROW_ERROR(message, ...) { EError __err=GET_MESSAGE(Message_Error,(char*)__FILE__,__LINE__,message,##__VA_ARGS__); SEND_MESSAGE(__err); return __err;}
#define TRY(__x) { EError __err = __x; if (__err->message_type==Message_Error) { return __err;}}
#define TRYHAL(__x) { HAL_StatusTypeDef __res = __x; if (__res != HAL_OK) { THROW_ERROR("HAL problem: %u",__res);}}
#define TRYFAT(__x) { FRESULT __res = __x; if (__res != FR_OK) { THROW_ERROR("FAT problem: %u",__res);}}
#define NO_ERROR() { return GET_MESSAGE(Message_None,NULL,0,NULL,0);}
#define SEND_ERROR(message , ...) { SEND_MESSAGE(GET_MESSAGE(Message_Error,(char*)__FILE__,__LINE__,message,##__VA_ARGS__)); }
#define SEND_WARN(message , ...) { SEND_MESSAGE(GET_MESSAGE(Message_Warning,(char*)__FILE__,__LINE__,message,##__VA_ARGS__)); }
#define SEND_INFO(message , ...) { SEND_MESSAGE(GET_MESSAGE(Message_Info,(char*)__FILE__,__LINE__,message,##__VA_ARGS__)); }
EError GET_MESSAGE(EMessage_type messagetype,const char* file, uint32_t line,const char *format, ... );
void SEND_MESSAGE(EError err);
uint8_t ISFAILED(EError err);
#endif /* ERROR_H_ */
源文件
#include <error.h>
#include "stdio.h"
#include "stdarg.h"
static EErrorStruct error;
static const EError m = &error;
static char buffer[300];
EError GET_MESSAGE(EMessage_type messagetype,const char* file, uint32_t line,const char *format, ... )
{
va_list args;
va_start(args,format);
vsprintf(buffer, format, args);
va_end(args);
m->message = buffer;
m->message_type = messagetype;
m->file = file;
m->line = line;
return m;
}
void SEND_MESSAGE(EError err)
{
switch (err->message_type) {
case Message_Error:
printf("ERRO: %s \r\n\t\t\t\t\t\t\t\t\t\t at %s line: %u\r\n",err->message,err->file,(unsigned int)err->line);
break;
case Message_Warning:
printf("WARN: %s \r\n\t\t\t\t\t\t\t\t\t\t at %s line: %u\r\n",err->message,err->file,(unsigned int)err->line);
break;
case Message_Info:
printf("INFO: %s \r\n\t\t\t\t\t\t\t\t\t\t at %s line: %u\r\n",err->message,err->file,(unsigned int)err->line);
break;
default:
break;
}
}
uint8_t ISFAILED(EError err)
{
if (err->message_type == Message_Error) return 1;
return 0;
}