forked from hulu/statsd-router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sr-util.c
30 lines (26 loc) · 810 Bytes
/
sr-util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "sr-util.h"
// function to convert numeric values into strings
static char *log_level_name(enum log_level_e level) {
static char *name[] = { "TRACE", "DEBUG", "INFO", "WARN", "ERROR"};
return name[level];
}
// function to log message
void log_msg(int level, char *format, ...) {
va_list args;
time_t t;
struct tm *tinfo;
char buffer[LOG_BUF_SIZE];
int l = 0;
if (level < log_level) {
return;
}
va_start(args, format);
time(&t);
tinfo = localtime(&t);
l = strftime(buffer, LOG_BUF_SIZE, "%Y-%m-%d %H:%M:%S", tinfo);
l += sprintf(buffer + l, " %ld %s ", syscall(SYS_gettid), log_level_name(level));
vsnprintf(buffer + l, LOG_BUF_SIZE - l, format, args);
va_end(args);
fprintf(stdout, "%s\n", buffer);
fflush(stdout);
}