
Seems to me that the simplest approach would be just to build a giant switch-case statement pointing to the appropriate string, but maybe it's just because I can't think of a good data structure to simplify the mapping process. Printf("SomeModuleFunc returned %s\n", ErrToString(result)) Īnd it would print SOME_MODULE_ERR_NOT_FOUND, SOME_MODULE_ERR_BAD_CRC, SOME_MODULE_STATUS_BUSY, etc as appropriate. By refactoring the error message printing I will be able to refactor the logging system.īasically, I want this: result = SomeModuleFunc()
A non sequential process code#
I want to improve our logging system, but this stupid code is getting in the way. We currently have code to print the appropriate error string, but the process of conversion (from error code to string) is so bizarre that even adding new error codes is a chore. #define OTHER_MODULE_ERR_NOT_FOUND (OTHER_MODULE_ERR_BASE + 2) #define OTHER_MODULE_ERR_BAD_CRC (OTHER_MODULE_ERR_BASE + 1) #define SOME_MODULE_WARNING_INCOMPLETE (SOME_MODULE_ERR_BASE + WARNING_OFFSET + 1) #define SOME_MODULE_STATUS_BUSY (SOME_MODULE_ERR_BASE + STATUS_OFFSET + 1) #define SOME_MODULE_ERR_BAD_CRC (SOME_MODULE_ERR_BASE + 2) #define SOME_MODULE_ERR_NOT_FOUND (SOME_MODULE_ERR_BASE + 1) * Error codes as defined as needed, sequentially from the base value */ There are offsets for warnings and status messages: #define SOME_MODULE_ERR_BASE 0x120000 The error codes are built so that each code has a base value, specifying which module it belongs to.

The error codes are unique, and can't be used to compose new codes. I'm thinking about refactoring this, but I not sure of the best way of mapping a value to its corresponding string. I have a very contrived error code system (a few hundreds of non-sequential values, C language) with an equally contrived conversion to human-readable error messages.
