feat: logging with functional overloading for different kind of logs
This commit is contained in:
@@ -3,9 +3,18 @@
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
void LOG_INFO(char *str) { printf("+ [INFO] %s\n", str); }
|
||||
#define NARGS(...) NARGS_(__VA_ARGS__, 5, 4, 3, 2, 1, 0)
|
||||
#define NARGS_(_5, _4, _3, _2, _1, N, ...) N
|
||||
|
||||
void LOG_LEVEL(char *str, char *level) {
|
||||
#define LOGG(A, B) LOGG_(A, B)
|
||||
#define LOGG_(A, B) A##B
|
||||
|
||||
#define LOG(...) LOGG(LOG, NARGS(__VA_ARGS__))(__VA_ARGS__)
|
||||
|
||||
|
||||
void LOG1(char *str) { printf("+ [INFO] %s\n", str); }
|
||||
|
||||
void LOG2(char *str, char *level) {
|
||||
if (strcmp(level, "warn") == 0) {
|
||||
printf("! [WARN] %s\n", str);
|
||||
return;
|
||||
@@ -13,14 +22,15 @@ void LOG_LEVEL(char *str, char *level) {
|
||||
|
||||
if (strcmp(level, "fatal") == 0) {
|
||||
printf("!! [FATAL] %s\n", str);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
LOG_INFO("This is a Log");
|
||||
LOG_LEVEL("This is a Warning", "warn");
|
||||
/* LOG("HELLO"); */
|
||||
LOG("This is a Log");
|
||||
LOG("This is a Warning", "warn");
|
||||
LOG("HELLO", "fatal");
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user