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 "aubio_priv.h" |
---|
22 | #include "log.h" |
---|
23 | |
---|
24 | /** array of pointers to logging functions, one per level */ |
---|
25 | static aubio_log_function_t aubio_log_function[AUBIO_LOG_LAST_LEVEL]; |
---|
26 | /** array of pointers to closure passed to logging functions, one per level */ |
---|
27 | static void* aubio_log_user_data[AUBIO_LOG_LAST_LEVEL]; |
---|
28 | /** buffer for logging messages */ |
---|
29 | static char aubio_log_buffer[512]; |
---|
30 | |
---|
31 | /** private function used by default by logging functions */ |
---|
32 | void |
---|
33 | aubio_default_log(sint_t level, const char_t *message, void * data UNUSED) |
---|
34 | { |
---|
35 | FILE *out; |
---|
36 | out = stdout; |
---|
37 | if (level == AUBIO_LOG_ERR || level == AUBIO_LOG_DBG || level == AUBIO_LOG_WRN) { |
---|
38 | out = stderr; |
---|
39 | } |
---|
40 | fprintf(out, "%s", message); |
---|
41 | //fflush(out); |
---|
42 | } |
---|
43 | |
---|
44 | uint_t |
---|
45 | aubio_log(sint_t level, const char_t *fmt, ...) |
---|
46 | { |
---|
47 | aubio_log_function_t fun = NULL; |
---|
48 | |
---|
49 | va_list args; |
---|
50 | va_start(args, fmt); |
---|
51 | vsnprintf(aubio_log_buffer, sizeof(aubio_log_buffer), fmt, args); |
---|
52 | va_end(args); |
---|
53 | |
---|
54 | if ((level >= 0) && (level < AUBIO_LOG_LAST_LEVEL)) { |
---|
55 | fun = aubio_log_function[level]; |
---|
56 | if (fun != NULL) { |
---|
57 | (*fun)(level, aubio_log_buffer, aubio_log_user_data[level]); |
---|
58 | } else { |
---|
59 | aubio_default_log(level, aubio_log_buffer, NULL); |
---|
60 | } |
---|
61 | } |
---|
62 | return AUBIO_FAIL; |
---|
63 | } |
---|
64 | |
---|
65 | void |
---|
66 | aubio_log_reset(void) |
---|
67 | { |
---|
68 | uint_t i = 0; |
---|
69 | for (i = 0; i < AUBIO_LOG_LAST_LEVEL; i++) { |
---|
70 | aubio_log_set_level_function(i, aubio_default_log, NULL); |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | aubio_log_function_t |
---|
75 | aubio_log_set_level_function(sint_t level, aubio_log_function_t fun, void * data) |
---|
76 | { |
---|
77 | aubio_log_function_t old = NULL; |
---|
78 | if ((level >= 0) && (level < AUBIO_LOG_LAST_LEVEL)) { |
---|
79 | old = aubio_log_function[level]; |
---|
80 | aubio_log_function[level] = fun; |
---|
81 | aubio_log_user_data[level] = data; |
---|
82 | } |
---|
83 | return old; |
---|
84 | } |
---|
85 | |
---|
86 | void |
---|
87 | aubio_log_set_function(aubio_log_function_t fun, void * data) { |
---|
88 | uint_t i = 0; |
---|
89 | for (i = 0; i < AUBIO_LOG_LAST_LEVEL; i++) { |
---|
90 | aubio_log_set_level_function(i, fun, data); |
---|
91 | } |
---|
92 | } |
---|