source: src/utils/log.c @ c7844d8

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since c7844d8 was c7844d8, checked in by Paul Brossier <piem@piem.org>, 8 years ago

src/utils/log.h: add new aubio_log_set_function

  • Property mode set to 100644
File size: 1.9 KB
Line 
1#include "config.h"
2#include "aubio_priv.h"
3#include "log.h"
4
5/** array of pointers to logging functions, one per level */
6static aubio_log_function_t aubio_log_function[AUBIO_LOG_LAST_LEVEL];
7/** array of pointers to closure passed to logging functions, one per level */
8static void* aubio_log_user_data[AUBIO_LOG_LAST_LEVEL];
9/** buffer for logging messages */
10static char aubio_log_buffer[512];
11
12/** private function used by default by logging functions */
13void
14aubio_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
28uint_t
29aubio_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
49void
50aubio_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
58aubio_log_function_t
59aubio_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
70void
71aubio_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}
Note: See TracBrowser for help on using the repository browser.