udp2raw/log.cpp

58 lines
1.3 KiB
C++
Raw Normal View History

#include "log.h"
2017-09-24 08:14:08 +00:00
#include "misc.h"
2017-07-24 13:18:58 +00:00
2023-02-07 12:11:49 +00:00
int log_level = log_info;
2017-07-24 17:54:09 +00:00
2023-02-07 12:11:49 +00:00
int enable_log_position = 0;
int enable_log_color = 1;
2017-07-26 00:51:05 +00:00
2023-02-07 12:11:49 +00:00
void log0(const char* file, const char* function, int line, int level, const char* str, ...) {
if (level > log_level) return;
if (level > log_trace || level < 0) return;
2017-07-24 13:18:58 +00:00
2023-02-07 12:11:49 +00:00
time_t timer;
char buffer[100];
struct tm* tm_info;
2017-07-24 13:18:58 +00:00
2023-02-07 12:11:49 +00:00
time(&timer);
tm_info = localtime(&timer);
2017-07-24 13:18:58 +00:00
2023-02-07 12:11:49 +00:00
if (enable_log_color)
printf("%s", log_color[level]);
2017-07-24 13:18:58 +00:00
2023-02-07 12:11:49 +00:00
strftime(buffer, 100, "%Y-%m-%d %H:%M:%S", tm_info);
printf("[%s][%s]", buffer, log_text[level]);
2017-07-24 13:18:58 +00:00
2023-02-07 12:11:49 +00:00
if (enable_log_position) printf("[%s,func:%s,line:%d]", file, function, line);
2017-07-24 13:18:58 +00:00
2023-02-07 12:11:49 +00:00
va_list vlist;
va_start(vlist, str);
vfprintf(stdout, str, vlist);
va_end(vlist);
if (enable_log_color)
printf("%s", RESET);
2017-07-24 13:18:58 +00:00
2023-02-07 12:11:49 +00:00
// printf("\n");
// if(enable_log_color)
// printf(log_color[level]);
fflush(stdout);
2017-07-24 13:18:58 +00:00
2023-02-07 12:11:49 +00:00
if (log_level == log_fatal) {
about_to_exit = 1;
}
}
2023-02-07 12:11:49 +00:00
void log_bare(int level, const char* str, ...) {
if (level > log_level) return;
if (level > log_trace || level < 0) return;
if (enable_log_color)
printf("%s", log_color[level]);
va_list vlist;
va_start(vlist, str);
vfprintf(stdout, str, vlist);
va_end(vlist);
if (enable_log_color)
printf("%s", RESET);
fflush(stdout);
2017-07-24 13:18:58 +00:00
}