1 | /* |
---|
2 | Copyright (C) 2016 Paul Brossier <piem@aubio.org> |
---|
3 | |
---|
4 | This file is part of aubio. |
---|
5 | |
---|
6 | aubio is free software: you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation, either version 3 of the License, or |
---|
9 | (at your option) any later version. |
---|
10 | |
---|
11 | aubio is distributed in the hope that it will be useful, |
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | GNU General Public License for more details. |
---|
15 | |
---|
16 | You should have received a copy of the GNU General Public License |
---|
17 | along with aubio. If not, see <http://www.gnu.org/licenses/>. |
---|
18 | |
---|
19 | */ |
---|
20 | |
---|
21 | #include "config.h" |
---|
22 | #include "aubio_priv.h" |
---|
23 | #include "log.h" |
---|
24 | |
---|
25 | /** array of pointers to logging functions, one per level */ |
---|
26 | static aubio_log_function_t aubio_log_function[AUBIO_LOG_LAST_LEVEL]; |
---|
27 | /** array of pointers to closure passed to logging functions, one per level */ |
---|
28 | static void* aubio_log_user_data[AUBIO_LOG_LAST_LEVEL]; |
---|
29 | /** buffer for logging messages */ |
---|
30 | static char aubio_log_buffer[512]; |
---|
31 | |
---|
32 | /** private function used by default by logging functions */ |
---|
33 | void |
---|
34 | aubio_default_log(sint_t level, const char_t *message, void * data UNUSED) |
---|
35 | { |
---|
36 | FILE *out; |
---|
37 | out = stdout; |
---|
38 | if (level == AUBIO_LOG_DBG || level == AUBIO_LOG_ERR) { |
---|
39 | out = stderr; |
---|
40 | } |
---|
41 | fprintf(out, "%s", message); |
---|
42 | //fflush(out); |
---|
43 | } |
---|
44 | |
---|
45 | uint_t |
---|
46 | aubio_log(sint_t level, const char_t *fmt, ...) |
---|
47 | { |
---|
48 | aubio_log_function_t fun = NULL; |
---|
49 | |
---|
50 | va_list args; |
---|
51 | va_start(args, fmt); |
---|
52 | vsnprintf(aubio_log_buffer, sizeof(aubio_log_buffer), fmt, args); |
---|
53 | va_end(args); |
---|
54 | |
---|
55 | if ((level >= 0) && (level < AUBIO_LOG_LAST_LEVEL)) { |
---|
56 | fun = aubio_log_function[level]; |
---|
57 | if (fun != NULL) { |
---|
58 | (*fun)(level, aubio_log_buffer, aubio_log_user_data[level]); |
---|
59 | } else { |
---|
60 | aubio_default_log(level, aubio_log_buffer, NULL); |
---|
61 | } |
---|
62 | } |
---|
63 | return AUBIO_FAIL; |
---|
64 | } |
---|
65 | |
---|
66 | void |
---|
67 | aubio_log_reset(void) |
---|
68 | { |
---|
69 | uint_t i = 0; |
---|
70 | for (i = 0; i < AUBIO_LOG_LAST_LEVEL; i++) { |
---|
71 | aubio_log_set_level_function(i, aubio_default_log, NULL); |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | aubio_log_function_t |
---|
76 | aubio_log_set_level_function(sint_t level, aubio_log_function_t fun, void * data) |
---|
77 | { |
---|
78 | aubio_log_function_t old = NULL; |
---|
79 | if ((level >= 0) && (level < AUBIO_LOG_LAST_LEVEL)) { |
---|
80 | old = aubio_log_function[level]; |
---|
81 | aubio_log_function[level] = fun; |
---|
82 | aubio_log_user_data[level] = data; |
---|
83 | } |
---|
84 | return old; |
---|
85 | } |
---|
86 | |
---|
87 | void |
---|
88 | aubio_log_set_function(aubio_log_function_t fun, void * data) { |
---|
89 | uint_t i = 0; |
---|
90 | for (i = 0; i < AUBIO_LOG_LAST_LEVEL; i++) { |
---|
91 | aubio_log_set_level_function(i, fun, data); |
---|
92 | } |
---|
93 | } |
---|