1 #include <stdarg.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include "handle.h"
5 #include "debug.h"
6
7 /* Deprecated */
8 struct sepol_handle sepol_compat_handle = {
9 .msg_callback = sepol_msg_default_handler,
10 .msg_callback_arg = NULL,
11 };
12
sepol_debug(int on)13 void sepol_debug(int on)
14 {
15 sepol_compat_handle.msg_callback = (on) ?
16 sepol_msg_default_handler : NULL;
17 }
18
19 /* End deprecated */
20
sepol_msg_get_level(sepol_handle_t * handle)21 int sepol_msg_get_level(sepol_handle_t * handle)
22 {
23 return handle->msg_level;
24 }
25
hidden_def(sepol_msg_get_level)26 hidden_def(sepol_msg_get_level)
27
28 const char *sepol_msg_get_channel(sepol_handle_t * handle)
29 {
30 return handle->msg_channel;
31 }
32
hidden_def(sepol_msg_get_channel)33 hidden_def(sepol_msg_get_channel)
34
35 const char *sepol_msg_get_fname(sepol_handle_t * handle)
36 {
37 return handle->msg_fname;
38 }
39
hidden_def(sepol_msg_get_fname)40 hidden_def(sepol_msg_get_fname)
41 #ifdef __GNUC__
42 __attribute__ ((format(printf, 3, 4)))
43 #endif
44 void hidden sepol_msg_default_handler(void *varg __attribute__ ((unused)),
45 sepol_handle_t * handle,
46 const char *fmt, ...)
47 {
48
49 FILE *stream = NULL;
50
51 switch (sepol_msg_get_level(handle)) {
52
53 case SEPOL_MSG_ERR:
54 case SEPOL_MSG_WARN:
55 stream = stderr;
56 break;
57 case SEPOL_MSG_INFO:
58 default:
59 stream = stdout;
60 break;
61 }
62
63 fprintf(stream, "%s.%s: ",
64 sepol_msg_get_channel(handle), sepol_msg_get_fname(handle));
65
66 va_list ap;
67 va_start(ap, fmt);
68 vfprintf(stream, fmt, ap);
69 va_end(ap);
70
71 fprintf(stream, "\n");
72 }
73
sepol_msg_set_callback(sepol_handle_t * handle,void (* msg_callback)(void * varg,sepol_handle_t * handle,const char * fmt,...),void * msg_callback_arg)74 extern void sepol_msg_set_callback(sepol_handle_t * handle,
75 #ifdef __GNUC__
76 __attribute__ ((format(printf, 3, 4)))
77 #endif
78 void (*msg_callback) (void *varg,
79 sepol_handle_t *
80 handle,
81 const char *fmt, ...),
82 void *msg_callback_arg)
83 {
84
85 handle->msg_callback = msg_callback;
86 handle->msg_callback_arg = msg_callback_arg;
87 }
88