1 | #include "config.h" |
---|
2 | #include "aubio_priv.h" |
---|
3 | #include "log.h" |
---|
4 | |
---|
5 | /** array of pointers to logging functions, one per level */ |
---|
6 | static aubio_log_function_t aubio_log_function[AUBIO_LOG_LAST_LEVEL]; |
---|
7 | /** array of pointers to closure passed to logging functions, one per level */ |
---|
8 | static void* aubio_log_user_data[AUBIO_LOG_LAST_LEVEL]; |
---|
9 | /** buffer for logging messages */ |
---|
10 | static char aubio_log_buffer[512]; |
---|
11 | |
---|
12 | /** private function used by default by logging functions */ |
---|
13 | void |
---|
14 | aubio_default_log(sint_t level, const char_t *message, void * data UNUSED) |
---|
15 | { |
---|
16 | FILE *out; |
---|
17 | out = stdout; |
---|
18 | if (level == AUBIO_LOG_DBG || level == AUBIO_LOG_ERR) { |
---|
19 | out = stderr; |
---|
20 | } |
---|
21 | if (data != NULL) { |
---|
22 | fprintf(out, "%s", (char_t *)data); |
---|
23 | } |
---|
24 | fprintf(out, "%s", message); |
---|
25 | //fflush(out); |
---|
26 | } |
---|
27 | |
---|
28 | uint_t |
---|
29 | aubio_log(sint_t level, const char_t *fmt, ...) |
---|
30 | { |
---|
31 | aubio_log_function_t fun = NULL; |
---|
32 | |
---|
33 | va_list args; |
---|
34 | va_start(args, fmt); |
---|
35 | vsnprintf(aubio_log_buffer, sizeof(aubio_log_buffer), fmt, args); |
---|
36 | va_end(args); |
---|
37 | |
---|
38 | if ((level >= 0) && (level < AUBIO_LOG_LAST_LEVEL)) { |
---|
39 | fun = aubio_log_function[level]; |
---|
40 | if (fun != NULL) { |
---|
41 | (*fun)(level, aubio_log_buffer, aubio_log_user_data[level]); |
---|
42 | } else { |
---|
43 | aubio_default_log(level, aubio_log_buffer, NULL); |
---|
44 | } |
---|
45 | } |
---|
46 | return AUBIO_FAIL; |
---|
47 | } |
---|
48 | |
---|
49 | void |
---|
50 | aubio_log_reset(void) |
---|
51 | { |
---|
52 | uint_t i = 0; |
---|
53 | for (i = 0; i < AUBIO_LOG_LAST_LEVEL; i++) { |
---|
54 | aubio_log_set_level_function(i, aubio_default_log, NULL); |
---|
55 | } |
---|
56 | } |
---|
57 | |
---|
58 | aubio_log_function_t |
---|
59 | aubio_log_set_level_function(sint_t level, aubio_log_function_t fun, void * data) |
---|
60 | { |
---|
61 | aubio_log_function_t old = NULL; |
---|
62 | if ((level >= 0) && (level < AUBIO_LOG_LAST_LEVEL)) { |
---|
63 | old = aubio_log_function[level]; |
---|
64 | aubio_log_function[level] = fun; |
---|
65 | aubio_log_user_data[level] = data; |
---|
66 | } |
---|
67 | return old; |
---|
68 | } |
---|
69 | |
---|
70 | void |
---|
71 | aubio_log_set_function(aubio_log_function_t fun, void * data) { |
---|
72 | uint_t i = 0; |
---|
73 | for (i = 0; i < AUBIO_LOG_LAST_LEVEL; i++) { |
---|
74 | aubio_log_set_level_function(i, fun, data); |
---|
75 | } |
---|
76 | } |
---|