-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1cfc177
commit 194cdc1
Showing
3 changed files
with
141 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,4 @@ pro/*.c | |
|
||
/.ccls-cache | ||
|
||
log.* | ||
/log.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#ifndef LOG_H | ||
#define LOG_H | ||
|
||
#include <stdbool.h> | ||
|
||
#include "enum.h" | ||
|
||
void log_debug(const char *__restrict __format, ...); | ||
|
||
void log_info(const char *__restrict __format, ...); | ||
|
||
void log_warn(const char *__restrict __format, ...); | ||
|
||
void log_error(const char *__restrict __format, ...); | ||
|
||
void log_error_errno(const char *__restrict __format, ...); | ||
|
||
enum LogThreshold log_get_threshold(void); | ||
|
||
// false if not valid | ||
bool log_set_threshold(const char *s); | ||
|
||
#endif // LOG_H | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
#include <errno.h> | ||
#include <stdbool.h> | ||
#include <stdarg.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <time.h> | ||
|
||
#include "enum.h" | ||
|
||
#include "log.h" | ||
|
||
char THRESHOLD_CHAR[] = { | ||
'?', | ||
'D', | ||
'I', | ||
'W', | ||
'E', | ||
}; | ||
|
||
char *THRESHOLD_LABEL[] = { | ||
"", | ||
"", | ||
"", | ||
"WARNING: ", | ||
"ERROR: ", | ||
}; | ||
|
||
enum LogThreshold log_threshold = LOG_THRESHOLD_DEFAULT; | ||
|
||
bool log_prefix = true; | ||
|
||
void print_prefix(const enum LogThreshold threshold, FILE *__restrict __stream) { | ||
static char buf[16]; | ||
static time_t t; | ||
|
||
t = time(NULL); | ||
|
||
strftime(buf, sizeof(buf), "%H:%M:%S", localtime(&t)); | ||
|
||
fprintf(__stream, "%c [%s] ", THRESHOLD_CHAR[threshold], buf); | ||
} | ||
|
||
void log_(const enum LogThreshold threshold, const int eno, const char *__restrict __format, va_list __args) { | ||
if (threshold < log_threshold) { | ||
return; | ||
} | ||
|
||
static FILE *stream; | ||
|
||
stream = threshold == ERROR ? stderr : stdout; | ||
|
||
if (log_prefix) { | ||
print_prefix(threshold, stream); | ||
} | ||
|
||
fprintf(stream, "%s", THRESHOLD_LABEL[threshold]); | ||
|
||
vfprintf(stream, __format, __args); | ||
|
||
if (eno) { | ||
fprintf(stream, " %d: %s", eno, strerror(eno)); | ||
} | ||
|
||
fprintf(stream, "\n"); | ||
|
||
fflush(stream); | ||
} | ||
|
||
void log_debug(const char *__restrict __format, ...) { | ||
va_list args; | ||
va_start(args, __format); | ||
log_(DEBUG, 0, __format, args); | ||
va_end(args); | ||
} | ||
|
||
void log_info(const char *__restrict __format, ...) { | ||
va_list args; | ||
va_start(args, __format); | ||
log_(INFO, 0, __format, args); | ||
va_end(args); | ||
} | ||
|
||
void log_warn(const char *__restrict __format, ...) { | ||
va_list args; | ||
va_start(args, __format); | ||
log_(WARNING, 0, __format, args); | ||
va_end(args); | ||
} | ||
|
||
void log_error(const char *__restrict __format, ...) { | ||
va_list args; | ||
va_start(args, __format); | ||
log_(ERROR, 0, __format, args); | ||
va_end(args); | ||
} | ||
|
||
void log_error_errno(const char *__restrict __format, ...) { | ||
va_list args; | ||
va_start(args, __format); | ||
log_(ERROR, errno, __format, args); | ||
va_end(args); | ||
} | ||
|
||
enum LogThreshold log_get_threshold(void) { | ||
return log_threshold; | ||
} | ||
|
||
bool log_set_threshold(const char *s) { | ||
enum LogThreshold t = log_threshold_val(s); | ||
if (t) { | ||
log_threshold = t; | ||
return true; | ||
} | ||
return false; | ||
} | ||
|