26cde813ac5369583e2ef0d87bf80dd5273d4b33
[aubio.git] / tests / src / utils / test-log.c
1 #include <aubio.h>
2 #include <stdio.h>
3 #include "aubio_priv.h"
4
5 const char_t *hdr = "CUSTOM HEADER: ";
6 const char_t *hdr2 = "OTHER HEADER: ";
7
8 /* an example of logging function that adds a custom header and prints
9  * aubio debug messages on stdout instead of stderr */
10 void logging(int level, const char_t *message, void *data) {
11   FILE *out;
12   //fprintf(stdout, "using custom logging function\n");
13   if (level == AUBIO_LOG_ERR) {
14     out = stderr;
15   } else {
16     out = stdout;
17   }
18   if ((level >= 0) && (data != NULL)) {
19     fprintf(out, "%s", (const char_t *)data);
20   }
21   fprintf(out, "%s", message);
22 }
23
24 int main (void)
25 {
26   aubio_init();
27
28   fprintf(stdout, "### testing normal logging\n");
29   AUBIO_ERR("testing normal AUBIO_LOG_ERR\n");
30   AUBIO_INF("testing normal AUBIO_LOG_INF\n");
31   AUBIO_WRN("testing normal AUBIO_LOG_WRN\n");
32   AUBIO_MSG("testing normal AUBIO_LOG_MSG\n");
33   AUBIO_DBG("testing normal AUBIO_LOG_DBG\n");
34
35   fprintf(stdout, "### testing with one custom function\n");
36   aubio_log_set_function(logging, (void *)hdr);
37   AUBIO_ERR("testing custom set_function AUBIO_LOG_ERR\n");
38   AUBIO_INF("testing custom set_function AUBIO_LOG_INF\n");
39   AUBIO_WRN("testing custom set_function AUBIO_LOG_WRN\n");
40   AUBIO_MSG("testing custom set_function AUBIO_LOG_MSG\n");
41   AUBIO_DBG("testing custom set_function AUBIO_LOG_DBG\n");
42
43   fprintf(stdout, "### testing resetted logging\n");
44   aubio_log_reset();
45   AUBIO_ERR("testing again normal AUBIO_LOG_ERR\n");
46   AUBIO_INF("testing again normal AUBIO_LOG_INF\n");
47   AUBIO_WRN("testing again normal AUBIO_LOG_WRN\n");
48   AUBIO_MSG("testing again normal AUBIO_LOG_MSG\n");
49   AUBIO_DBG("testing again normal AUBIO_LOG_DBG\n");
50
51   fprintf(stdout, "### testing per level customization\n");
52   aubio_log_set_level_function(AUBIO_LOG_ERR, logging, (void *)hdr2);
53   aubio_log_set_level_function(AUBIO_LOG_WRN, logging, NULL);
54   aubio_log_set_level_function(AUBIO_LOG_MSG, logging, (void *)hdr);
55   AUBIO_ERR("testing custom set_level_function AUBIO_LOG_ERR\n");
56   AUBIO_INF("testing again normal AUBIO_LOG_INF\n");
57   AUBIO_WRN("testing custom set_level_function AUBIO_LOG_WRN with data=NULL\n");
58   AUBIO_MSG("testing custom set_level_function AUBIO_LOG_MSG\n");
59   AUBIO_DBG("testing again normal AUBIO_LOG_DBG\n");
60
61   aubio_cleanup();
62   
63   return 0;
64 }