Based on the answers to a previous question, here's a design for a simple error reporting system for my embedded C app. I'd appreciate some feedback.
My app has code forming an overall program plus several layers of lower-level libraries. At the lowest level, the library gets a byte to use for error codes, so 255 total errors. They'll be encoded in an enum, like:
enum lib1ErrorCodes {
ERR_NO_ERROR,
ERR_NO_CONNECTION,
...
ERR_MISC
};
This is passed up the chain either through a global variable:
unsigned char lib1ErrNo;
The next higher level contains error codes for the libraries it uses:
enum lib2ErrorCodes {
ERR_NO_ERROR,
ERR_LIB1,
ERR_FILE_EXISTS,
...
ERR_MISC
}
Lib1's error is detected and is flagged in this level's error variable:
unsigned char lib2ErrNo = ERR_LIB1;
At the top level, when it becomes time to report all this to the user, these are detected:
if (lib3ErrNo == ERR_LIB2)
if (lib2ErrNo == ERR_LIB1)
printf("Error %d: %s", lib1ErrNo, lib1ErrDesc);
The only negatives I can think of in this scheme is the need to set aside a few error codes in each library to point to the libraries under it, and for the top-level program to include all these levels for their error codes.
What's the appropriate way to do this, if this isn't it?
I want:
- Unique error propagation all the way up to the top level so I can report it.
- Preferably not passing around large structures, as this is an embedded app.