Skip to content

Commit

Permalink
update util_log
Browse files Browse the repository at this point in the history
  • Loading branch information
i-evi committed Apr 25, 2021
1 parent 926ef59 commit 787e43f
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 11 deletions.
89 changes: 79 additions & 10 deletions src/util_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,36 @@
#include <time.h>

#include "util_log.h"
#ifdef UTLOG_LINUX_API_TIME
#include <sys/time.h>
#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;
Expand Down Expand Up @@ -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
Expand All @@ -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, '%');
Expand Down
22 changes: 21 additions & 1 deletion src/util_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 787e43f

Please sign in to comment.