3 ms·
I do that a lot with the firmware I write in C. typedef struct { err_t error; int error_line; char *error_msg; ... ... } thing_t;
by Gibbon1 5y ago
I do that a lot with the firmware I write in C.
typedef struct
{
err_t error;
int error_line;
char *error_msg;
...
...
} thing_t;
// set out of range error
thing->error = THING_ERROR_OOR;
thing->error_line = __LINE__;
thing->error_msg = "outofrange"
You can grep on 'outofrange' and find where the error was set.
I originally started doing that to mark 'bad' analog readings in process control equipment. I wrote my filters and control loops to be able to 'eat' occasional bad readings without barfing. Worked very well.
- Const-me 5y agoI often using something like this in C++ if( nullptr != pfnErrorSink ) pfnErrorSink( "outofrange", __FILE__, __LINE__ ); return E_BOUNDS; // Or sometimes throw E_BOUNDS; Where pfnErrorSink is either global, thread_local or a field keeping C function pointer provided by whoever consumes the code.