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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
#include "tbm_emulator_log.h"
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/time.h>
static const char *g_log_level_to_str[] =
{
"OFF",
"ERROR",
"INFO",
"DEBUG"
};
static pthread_once_t g_log_init = PTHREAD_ONCE_INIT;
static int g_debug_enabled = 0;
static void tbm_emulator_log_init_once(void)
{
char *debug_enabled_str = getenv("TBM_EMULATOR_DEBUG");
g_debug_enabled = debug_enabled_str ? atoi(debug_enabled_str) : 0;
}
static void tbm_emulator_log_init(void)
{
pthread_once(&g_log_init, tbm_emulator_log_init_once);
}
static void tbm_emulator_log_print_current_time(void)
{
char buff[128];
struct tm tm;
struct timeval tv = { 0, 0 };
time_t ti;
gettimeofday(&tv, NULL);
ti = tv.tv_sec;
localtime_r(&ti, &tm);
strftime(buff, sizeof(buff),
"%H:%M:%S", &tm);
fprintf(stderr, "%s", buff);
}
void tbm_emulator_log_event(tbm_emulator_log_level log_level,
const char *func,
int line,
const char *format, ...)
{
va_list args;
tbm_emulator_log_init();
tbm_emulator_log_print_current_time();
fprintf(stderr,
" %-5s [%u] %s:%d",
g_log_level_to_str[log_level],
getpid(),
func,
line);
if (format) {
va_start(args, format);
fprintf(stderr, " - ");
vfprintf(stderr, format, args);
va_end(args);
}
fprintf(stderr, "\n");
}
int tbm_emulator_log_is_debug_enabled()
{
tbm_emulator_log_init();
return g_debug_enabled;
}
|