Browse Source

update: logger module

master
Dnomd343 2 years ago
parent
commit
e79a636126
  1. 2
      include/common.h
  2. 8
      include/utils/logger.h
  3. 35
      src/utils/logger.c

2
include/common.h

@ -1,7 +1,7 @@
#ifndef COMMON_H_ #ifndef COMMON_H_
#define COMMON_H_ #define COMMON_H_
#define VERSION "1.0.0-beta" #define VERSION "1.0.1-beta"
#define RANDOM_PORT_START 41952 #define RANDOM_PORT_START 41952
#define RANDOM_PORT_END 65535 #define RANDOM_PORT_END 65535

8
include/utils/logger.h

@ -1,5 +1,7 @@
#ifndef LOG_H #ifndef LOGGER_H_
#define LOG_H #define LOGGER_H_
#define LOG_PREFIX "Bootstrap"
enum { enum {
LOG_DEBUG, LOG_DEBUG,
@ -16,7 +18,7 @@ enum {
#define log_fatal(...) log_printf(LOG_FATAL, __VA_ARGS__) #define log_fatal(...) log_printf(LOG_FATAL, __VA_ARGS__)
extern int LOG_LEVEL; extern int LOG_LEVEL;
void log_perror(char *prefix); void log_perror(const char *fmt, ...);
void log_printf(int level, const char *fmt, ...); void log_printf(int level, const char *fmt, ...);
#endif #endif

35
src/utils/logger.c

@ -2,6 +2,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdarg.h> #include <stdarg.h>
#include <sys/time.h>
#include "logger.h" #include "logger.h"
int LOG_LEVEL = LOG_INFO; // default log level int LOG_LEVEL = LOG_INFO; // default log level
@ -22,36 +23,40 @@ static const char *log_color[] = {
"\x1b[95m", // fatal "\x1b[95m", // fatal
}; };
void fprint_prefix() { // print log prefix and time info
time_t _time;
time(&_time);
struct timeval tv;
gettimeofday(&tv, NULL);
struct tm *t = localtime(&_time);
fprintf(stderr, "\x1b[36m[%s]\x1b[0m", LOG_PREFIX);
fprintf(stderr, " \x1b[90m%04d-%02d-%02d", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday);
fprintf(stderr, " %02d:%02d:%02d.%03ld\x1b[0m", t->tm_hour, t->tm_min, t->tm_sec, tv.tv_usec / 1000);
}
void log_printf(int level, const char *fmt, ...) { void log_printf(int level, const char *fmt, ...) {
if (level < LOG_LEVEL) { // skip low log level if (level < LOG_LEVEL) { // skip low log level
return; return;
} }
time_t t = time(NULL);
char time_str[20]; // YYYY-mm-dd HH:MM:SS (20 bytes)
time_str[strftime(time_str, 20, "%Y-%m-%d %H:%M:%S", localtime(&t))] = '\0'; // generate time str
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
fprintf(stderr, "\x1b[36m[Bootstrap]\x1b[0m \x1b[90m%s\x1b[0m", time_str); // show log prefix fprint_prefix();
fprintf(stderr, " %s%s\x1b[0m ", log_color[level], log_string[level]); // show log level fprintf(stderr, " %s%s\x1b[0m ", log_color[level], log_string[level]); // show log level
vfprintf(stderr, fmt, ap); // output log content vfprintf(stderr, fmt, ap); // output log content
fprintf(stderr, "\n"); // add LF after line fprintf(stderr, "\n"); // add LF after line
fflush(stderr); fflush(stderr);
va_end(ap); va_end(ap);
if (level == LOG_FATAL) { if (level == LOG_FATAL) {
exit(1); exit(1);
} }
} }
void log_perror(char *prefix) { void log_perror(const char *fmt, ...) {
time_t t; va_list ap;
time(&t); va_start(ap, fmt);
struct tm *lt = localtime(&t); fprint_prefix();
fprintf(stderr, "\x1b[36m[Bootstrap]\x1b[0m "); fprintf(stderr, " %s%s\x1b[0m ", log_color[LOG_ERROR], log_string[LOG_ERROR]);
fprintf(stderr, "\x1b[90m%04d-%02d-%02d %02d:%02d:%02d\x1b[0m", vfprintf(stderr, fmt, ap); // output log content
lt->tm_year + 1900, lt->tm_mon, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec);
fprintf(stderr, " %s%s\x1b[0m ", log_color[3], log_string[3]); // error level
fflush(stderr); fflush(stderr);
perror(prefix); perror("");
} }

Loading…
Cancel
Save