54442b5c09d49ae0e24805707aa8f374876e854d
[platform/adaptation/emulator/libtbm-vigs.git] / src / tbm_emulator_log.c
1 #include "tbm_emulator_log.h"
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdarg.h>
5 #include <stdio.h>
6 #include <pthread.h>
7 #include <unistd.h>
8 #include <sys/time.h>
9
10 static const char *g_log_level_to_str[] =
11 {
12     "OFF",
13     "ERROR",
14     "INFO",
15     "DEBUG"
16 };
17
18 static pthread_once_t g_log_init = PTHREAD_ONCE_INIT;
19 static int g_debug_enabled = 0;
20
21 static void tbm_emulator_log_init_once(void)
22 {
23     char *debug_enabled_str = getenv("TBM_EMULATOR_DEBUG");
24     g_debug_enabled = debug_enabled_str ? atoi(debug_enabled_str) : 0;
25 }
26
27 static void tbm_emulator_log_init(void)
28 {
29     pthread_once(&g_log_init, tbm_emulator_log_init_once);
30 }
31
32 static void tbm_emulator_log_print_current_time(void)
33 {
34     char buff[128];
35     struct tm tm;
36     struct timeval tv = { 0, 0 };
37     time_t ti;
38
39     gettimeofday(&tv, NULL);
40
41     ti = tv.tv_sec;
42
43     localtime_r(&ti, &tm);
44     strftime(buff, sizeof(buff),
45              "%H:%M:%S", &tm);
46     fprintf(stderr, "%s", buff);
47 }
48
49 void tbm_emulator_log_event(tbm_emulator_log_level log_level,
50                             const char *func,
51                             int line,
52                             const char *format, ...)
53 {
54     va_list args;
55
56     tbm_emulator_log_init();
57
58     tbm_emulator_log_print_current_time();
59     fprintf(stderr,
60             " %-5s [%u] %s:%d",
61             g_log_level_to_str[log_level],
62             getpid(),
63             func,
64             line);
65     if (format) {
66         va_start(args, format);
67         fprintf(stderr, " - ");
68         vfprintf(stderr, format, args);
69         va_end(args);
70     }
71     fprintf(stderr, "\n");
72 }
73
74 int tbm_emulator_log_is_debug_enabled()
75 {
76     tbm_emulator_log_init();
77
78     return g_debug_enabled;
79 }