Skip to content

Instantly share code, notes, and snippets.

@mofosyne
Created November 13, 2024 13:55
Show Gist options
  • Save mofosyne/a215ba12e1ebb5a3823dee3a3f90cb83 to your computer and use it in GitHub Desktop.
Save mofosyne/a215ba12e1ebb5a3823dee3a3f90cb83 to your computer and use it in GitHub Desktop.
Self Describing Error Code Enum Macro (Or other status tracking variables) In C
/*
Self Describing Error Code Enum Macro (Or other status tracking variables) In C
Author: Brian Khuu (2024)
This is an idea I got for easier management of error codes and other enums.
The benefit of this approach is that it provides a way of having self
documenting enumerated values which would be useful for debug loggers.
Ergo, your debug log could print the meaning of an error or status message.
*/
#include <stdio.h>
// You would add new entries here
#define ERROR_ENUM_ENTRIES()\
ENUM_ENTRY(ERROR_CODE_TEST1, "Test 1", "Error Description 1")\
ENUM_ENTRY(ERROR_CODE_TEST2, "Test 2", "Error Description 2")\
ENUM_ENTRY(ERROR_CODE_TEST3, "Test 3", "Error Description 3")\
ENUM_ENTRY(ERROR_CODE_TEST4, "Test 4", "Error Description 4")\
ENUM_ENTRY(ERROR_CODE_TEST5, "Test 5", "Error Description 5")\
ENUM_ENTRY(ERROR_CODE_TEST6, "Test 6", "Error Description 6")\
ENUM_ENTRY(ERROR_CODE_TEST7, "Test 7", "Error Description 7")\
ENUM_ENTRY(ERROR_CODE_TEST8, "Test 8", "Error Description 8")\
ENUM_ENTRY(ERROR_CODE_TEST9, "Test 9", "Error Description 9")\
// Enumerated List
#define ENUM_ENTRY(CODE, ...) CODE,
enum error_code
{
ERROR_ENUM_ENTRIES()
MAX
};
#undef ENUM_ENTRY
// Error code to string representation
#define ENUM_ENTRY(CODE, ...) case CODE: ret = #CODE; break;
const char * error_code_to_str(enum error_code code)
{
char * ret = NULL;
switch (code)
{
ERROR_ENUM_ENTRIES()
default:
break;
}
return ret ? ret : "?";
};
#undef ENUM_ENTRY
// Error code to error name representation
#define ENUM_ENTRY(CODE, NAME, ...) case CODE: ret = NAME; break;
const char * error_code_name_to_str(enum error_code code)
{
char * ret = NULL;
switch (code)
{
ERROR_ENUM_ENTRIES()
default:
break;
}
return ret ? ret : "?";
};
#undef ENUM_ENTRY
// Error code to description of error code meaning
#define ENUM_ENTRY(CODE, NAME, MSG, ...) case CODE: ret = MSG; break;
const char * error_code_description_to_str(enum error_code code)
{
char * ret = NULL;
switch (code)
{
ERROR_ENUM_ENTRIES()
default:
break;
}
return ret ? ret : "?";
};
#undef ENUM_ENTRY
// Demo of this macro usage
int main(void) {
for (enum error_code i = 0; i < MAX ; i++)
{
printf("%d | %s | %s | %s\n", i , error_code_to_str(i), error_code_name_to_str(i), error_code_description_to_str(i));
}
}
/*
0 | ERROR_CODE_TEST1 | Test 1 | Error Description 1
1 | ERROR_CODE_TEST2 | Test 2 | Error Description 2
2 | ERROR_CODE_TEST3 | Test 3 | Error Description 3
3 | ERROR_CODE_TEST4 | Test 4 | Error Description 4
4 | ERROR_CODE_TEST5 | Test 5 | Error Description 5
5 | ERROR_CODE_TEST6 | Test 6 | Error Description 6
6 | ERROR_CODE_TEST7 | Test 7 | Error Description 7
7 | ERROR_CODE_TEST8 | Test 8 | Error Description 8
8 | ERROR_CODE_TEST9 | Test 9 | Error Description 9
*/
@mofosyne
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment