From 787e43ff35c0a52fdf7a150072f034eeec3e8867 Mon Sep 17 00:00:00 2001 From: Evidence John Date: Sun, 25 Apr 2021 17:31:33 +0800 Subject: [PATCH] update util_log --- src/util_log.c | 89 ++++++++++++++++++++++++++++++++++++++++++++------ src/util_log.h | 22 ++++++++++++- 2 files changed, 100 insertions(+), 11 deletions(-) diff --git a/src/util_log.c b/src/util_log.c index 7add0c5..89bf5f4 100644 --- a/src/util_log.c +++ b/src/util_log.c @@ -5,11 +5,36 @@ #include #include "util_log.h" +#ifdef UTLOG_LINUX_API_TIME +#include +#endif -static int highlight_flag = UTLOG_HIGHLIGHT_ON; +static utlog_time_t start_time; static FILE *user_log_ostream = NULL; +static int time_mode_flag_m = UTLOG_USE_SYS_TIME; +static int time_mode_flag_o = UTLOG_USE_RUN_TIME; +static int highlight_flag = UTLOG_HIGHLIGHT_ON; static int error_action_flag = UTLOG_ERR_ACT_ABORT; +#if defined (__GNUC__) + void utlog_set_start_time(void) __attribute__((constructor)); +#elif defined (_MSC_VER) + #pragma data_seg(".CRT$XIU") + void utlog_set_start_time(void); + void (*$)(void) = utlog_set_start_time; + #pragma data_seg +#else + #define UTLOG_START_TIME_NOT_SET + #warning "Unknown compiler, start_time may not be initialized" +#endif + +#ifndef UTLOG_START_TIME_NOT_SET +void utlog_set_start_time(void) +{ + start_time = utlog_gettime(); +} +#endif + void utlog_highlight_on(void) { highlight_flag = UTLOG_HIGHLIGHT_ON; @@ -86,16 +111,62 @@ if (logtype == UTLOG_ERR) { \ } \ return; +void utlog_use_clk_time(void) +{ + time_mode_flag_m = UTLOG_USE_CLK_TIME; +} + +void utlog_use_sys_time(void) +{ + time_mode_flag_m = UTLOG_USE_SYS_TIME; +} + +void utlog_use_abs_time(void) +{ + time_mode_flag_o = UTLOG_USE_ABS_TIME; +} + +void utlog_use_run_time(void) +{ + time_mode_flag_o = UTLOG_USE_RUN_TIME; +} + +utlog_time_t utlog_gettime(void) +{ + utlog_time_t timestamp; +#ifdef UTLOG_LINUX_API_TIME + struct timeval t; +#endif + if (time_mode_flag_m == UTLOG_USE_SYS_TIME) { +#ifdef UTLOG_LINUX_API_TIME + gettimeofday(&t, NULL); + timestamp = t.tv_usec; + timestamp = (timestamp / 1e6) + t.tv_sec; +#else + timestamp = time(NULL); +#endif + } else { + timestamp = clock(); + timestamp /= UTLOG_CLK_FRAC; + } + return timestamp; +} + void utlog_format(int logtype, const char *fmt, ...) { int n; char *p, *f, *pf; - /* time_t t; - * time(&t); - */ FILE *log_ostream; - clock_t clk = clock(); va_list ap; + utlog_time_t timestamp; + if (time_mode_flag_o == UTLOG_USE_ABS_TIME) { + timestamp = utlog_gettime(); + } else { + if (time_mode_flag_m == UTLOG_USE_SYS_TIME) + timestamp = utlog_gettime() - start_time; + else + timestamp = utlog_gettime(); + } if (user_log_ostream) log_ostream = user_log_ostream; else @@ -105,12 +176,10 @@ void utlog_format(int logtype, const char *fmt, ...) /* fprintf(log_ostream, "[%ld:%ld] : ", t, clk); */ if (getenv("TERM") && highlight_flag == UTLOG_HIGHLIGHT_ON) { - fprintf(log_ostream, "[%s%08ld\033[0m]: ", - _log_clock_style_str(logtype), - clk / UTLOG_CLK_FRAC); + fprintf(log_ostream, "[%s%08lf\033[0m]: ", + _log_clock_style_str(logtype), timestamp); } else { - fprintf(log_ostream, "[%08ld]: ", - clk / UTLOG_CLK_FRAC); + fprintf(log_ostream, "[%08lf]: ", timestamp); } va_start(ap, fmt); n = _account_symbol(f, '%'); diff --git a/src/util_log.h b/src/util_log.h index 8726ae2..ed1a861 100644 --- a/src/util_log.h +++ b/src/util_log.h @@ -5,7 +5,13 @@ extern "C" { #endif -#define UTLOG_CLK_FRAC (CLOCKS_PER_SEC / 1000) +#ifdef __linux__ + #define UTLOG_LINUX_API_TIME +#endif + +typedef double utlog_time_t; + +#define UTLOG_CLK_FRAC CLOCKS_PER_SEC #define UTLOG_DEFAULT_OSTREAM stderr @@ -20,6 +26,13 @@ enum utlog_err_act { UTLOG_ERR_ACT_WARNING }; +enum utlog_time_mode { + UTLOG_USE_SYS_TIME, + UTLOG_USE_CLK_TIME, + UTLOG_USE_ABS_TIME, + UTLOG_USE_RUN_TIME +}; + enum utlog_highlight { UTLOG_HIGHLIGHT_ON, UTLOG_HIGHLIGHT_OFF @@ -33,6 +46,13 @@ void utlog_set_error_action(int act); void utlog_highlight_on(void); void utlog_highlight_off(void); +void utlog_use_clk_time(void); +void utlog_use_sys_time(void); +void utlog_use_abs_time(void); +void utlog_use_run_time(void); + +utlog_time_t utlog_gettime(void); + void utlog_format(int logtype, const char *fmt, ...); #ifdef __cplusplus