110 lines
2.1 KiB
C
110 lines
2.1 KiB
C
// #define PICO
|
|
|
|
#ifdef PICO
|
|
#include "pico/stdio.h"
|
|
#include "pico/stdlib.h"
|
|
#include "pico/time.h"
|
|
#else
|
|
#include <time.h>
|
|
#include <stdlib.h>
|
|
#endif
|
|
|
|
#include <stdio.h>
|
|
#include <stdarg.h>
|
|
|
|
#include "log.h"
|
|
|
|
char* _level_to_str(LogLevel level) {
|
|
switch (level) {
|
|
case TRACE:
|
|
return "TRACE";
|
|
case DEBUG:
|
|
return "DEBUG";
|
|
case INFO:
|
|
return "INFO";
|
|
case WARNING:
|
|
return "WARNING";
|
|
case ERROR:
|
|
return "ERROR";
|
|
case FATAL:
|
|
return "FATAL";
|
|
default:
|
|
return "INFO";
|
|
}
|
|
}
|
|
|
|
void _log(LogLevel level, const char *format, va_list args) {
|
|
time_t now;
|
|
time(&now);
|
|
char time_str[26];
|
|
ctime_r(&now, time_str);
|
|
time_str[24] = '\0';
|
|
|
|
FILE* output = stdout;
|
|
if (level == ERROR || level == FATAL)
|
|
output = stderr;
|
|
|
|
fprintf(output, "[%s] [%s] ", _level_to_str(level), time_str);
|
|
|
|
vfprintf(stdout, format, args);
|
|
fprintf(stdout, "\n");
|
|
}
|
|
|
|
void trace(const char *format, ...) {
|
|
if (LOG_LEVEL >= TRACE) {
|
|
va_list args;
|
|
va_start(args, format);
|
|
_log(TRACE, format, args);
|
|
va_end(args);
|
|
}
|
|
}
|
|
|
|
void debug(const char *format, ...) {
|
|
if (LOG_LEVEL >= DEBUG) {
|
|
va_list args;
|
|
va_start(args, format);
|
|
_log(DEBUG, format, args);
|
|
va_end(args);
|
|
}
|
|
}
|
|
|
|
void info(const char *format, ...) {
|
|
if (LOG_LEVEL >= INFO) {
|
|
va_list args;
|
|
va_start(args, format);
|
|
_log(INFO, format, args);
|
|
va_end(args);
|
|
}
|
|
}
|
|
|
|
void warning(const char *format, ...) {
|
|
if (LOG_LEVEL >= WARNING) {
|
|
va_list args;
|
|
va_start(args, format);
|
|
_log(WARNING, format, args);
|
|
va_end(args);
|
|
}
|
|
}
|
|
|
|
void error(const char *format, ...) {
|
|
if (LOG_LEVEL >= ERROR) {
|
|
va_list args;
|
|
va_start(args, format);
|
|
_log(ERROR, format, args);
|
|
va_end(args);
|
|
}
|
|
}
|
|
|
|
void fatal(void (*fallback)(void), const char *format, ...) {
|
|
if (LOG_LEVEL >= FATAL) {
|
|
va_list args;
|
|
va_start(args, format);
|
|
_log(FATAL, format, args);
|
|
va_end(args);
|
|
|
|
if (fallback != NULL)
|
|
fallback();
|
|
|
|
exit(1);
|
|
}
|
|
} |