136 lines
2.9 KiB
C
136 lines
2.9 KiB
C
#include "include/conf.h"
|
|
|
|
#include <time.h>
|
|
#include <stdio.h>
|
|
#include <stdarg.h>
|
|
#include <sys/time.h>
|
|
|
|
#include "include/def.h"
|
|
#include "include/log.h"
|
|
#include "include/clrs.h"
|
|
|
|
#ifndef NO_LOGGING
|
|
|
|
void vs_vlog_fprint(FILE *fp, const char *head, const char *clr, const char *fmt, va_list args) {
|
|
struct timeval tv;
|
|
struct tm *ptm = NULL;
|
|
|
|
char time_string[40];
|
|
long milliseconds = 0;
|
|
|
|
if (!fp) {
|
|
return;
|
|
}
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
ptm = localtime(&tv.tv_sec);
|
|
strftime(time_string, sizeof(time_string), "%Y-%m-%d %H:%M:%S", ptm);
|
|
|
|
milliseconds = tv.tv_usec / 1000;
|
|
|
|
fprintf(fp, "%s.%03ld | %s%s" VS_CLR_RESET " ", time_string, milliseconds, clr, head);
|
|
vfprintf(fp, fmt, args);
|
|
fputs(VS_CLR_RESET "\n", fp);
|
|
}
|
|
|
|
void vs_log_fprint(FILE *fp, const char *head, const char *clr, const char *fmt, ...) {
|
|
va_list args;
|
|
|
|
if (!fp) {
|
|
return;
|
|
}
|
|
|
|
va_start(args, fmt);
|
|
|
|
vs_vlog_fprint(fp, head, clr, fmt, args);
|
|
|
|
va_end(args);
|
|
}
|
|
|
|
void vs_log_normal(const vs_Logger *lg, const char *msg) { vs_flog_normal(lg, "%s", msg); }
|
|
|
|
void vs_log_info(const vs_Logger *lg, const char *msg) { vs_flog_info(lg, "%s", msg); }
|
|
|
|
void vs_log_error(const vs_Logger *lg, const char *msg) { vs_flog_error(lg, "%s", msg); }
|
|
|
|
void vs_log_warn(const vs_Logger *lg, const char *msg) { vs_flog_warn(lg, "%s", msg); }
|
|
|
|
#endif /* NO_LOGGING */
|
|
|
|
void vs_flog_normal(const vs_Logger *lg, const char *fmt, ...) {
|
|
va_list args;
|
|
|
|
if ((*lg & VS_LOG_NORMAL) == 0) {
|
|
return;
|
|
}
|
|
|
|
va_start(args, fmt);
|
|
vs_vlog_fprint(stdout, "log ", VS_CLR_BOLD, fmt, args);
|
|
va_end(args);
|
|
}
|
|
|
|
void vs_flog_info(const vs_Logger *lg, const char *fmt, ...) {
|
|
va_list args;
|
|
|
|
if (!lg || !fmt || (*lg & VS_LOG_INFO) == 0) {
|
|
return;
|
|
}
|
|
|
|
va_start(args, fmt);
|
|
vs_vlog_fprint(stdout, "info ", VS_CLR_BOLD VS_CLR_BLUE, fmt, args);
|
|
va_end(args);
|
|
}
|
|
|
|
void vs_flog_error(const vs_Logger *lg, const char *fmt, ...) {
|
|
va_list args;
|
|
|
|
if (!lg || !fmt || (*lg & VS_LOG_ERROR) == 0) {
|
|
return;
|
|
}
|
|
|
|
va_start(args, fmt);
|
|
vs_vlog_fprint(stderr, "error", VS_CLR_BOLD VS_CLR_RED, fmt, args);
|
|
va_end(args);
|
|
}
|
|
|
|
void vs_flog_warn(const vs_Logger *lg, const char *fmt, ...) {
|
|
va_list args;
|
|
|
|
if (!lg || !fmt || (*lg & VS_LOG_WARN) == 0) {
|
|
return;
|
|
}
|
|
|
|
va_start(args, fmt);
|
|
vs_vlog_fprint(stderr, "warn ", VS_CLR_BOLD VS_CLR_YELLOW, fmt, args);
|
|
va_end(args);
|
|
}
|
|
|
|
void vs_log_flush(void) {
|
|
fflush(stdout);
|
|
fflush(stderr);
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
void vs_flog_debug(const char *fmt, ...) {
|
|
# ifdef NO_LOGGING
|
|
(void)fmt;
|
|
# else
|
|
va_list args;
|
|
va_start(args, fmt);
|
|
|
|
vs_vlog_fprint(stderr, "[DEBUG]", VS_CLR_BOLD VS_CLR_MAGENTA, fmt, args);
|
|
fflush(stdout);
|
|
|
|
va_end(args);
|
|
# endif /* NO_LOGGING */
|
|
}
|
|
|
|
void vs_log_debug(const char *msg) {
|
|
# ifdef NO_LOGGING
|
|
(void)msg;
|
|
# else
|
|
vs_flog_debug("%s", msg);
|
|
# endif /* NO_LOGGING */
|
|
}
|
|
#endif /* DEBUG */
|