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