status/error handling in C
Hello all,
I'm trying to find an efficient way to capture errors, report it and act on it. At the moment I have an enum of error codes and a function which prints out switching on the error code. This is oK as I don't have to act on every type of error returned by a function, but is inefficient in the sence I would like to be able to report more specifically what the error is.
e.g. at the moment my code can have something like
ERROR_STATUS status = STATUS_OK;
status = some_function_call();
if (status == STATUS_OK)
do_something;
else
report_status(status);
So status may be DEVICE_ERROR and report_status(status) may print out "ERROR: Device error". This is OK in the sense that I can group a bunch of error conditions to a single error code, however I would like to be more specific in what report_status returns, e.g. "ERROR: Device not found" or "ERROR: Device not ready" or "ERROR: Device not supported" etc.
So I'm looking for an efficient way of avoiding lots of if statement clutters when essentially I will be acting on anything to do with a device error with the same action, and yet be able to report more precicely what the error was. On a more advanced stage I would like to be able to pass in arguments in the printf. E.g "Error: Invalid device header. Found X. Expected Y" where X and Y are passed in.
I know C (not C++ or C#) does not have any specific error or exception handling functions and clever routines have to be written. Unfortunately, as a newbie I'm not clever yet :(
Any suggestions and poiters are most welcome.
Thanks in advance,
Monty