1#include <stdarg.h>
2#include <stdlib.h>
3#include <stdio.h>
4#include "handle.h"
5#include "debug.h"
6
7/* Deprecated */
8struct sepol_handle sepol_compat_handle = {
9	.msg_callback = sepol_msg_default_handler,
10	.msg_callback_arg = NULL,
11};
12
13void sepol_debug(int on)
14{
15	sepol_compat_handle.msg_callback = (on) ?
16	    sepol_msg_default_handler : NULL;
17}
18
19/* End deprecated */
20
21int sepol_msg_get_level(sepol_handle_t * handle)
22{
23	return handle->msg_level;
24}
25
26hidden_def(sepol_msg_get_level)
27
28const char *sepol_msg_get_channel(sepol_handle_t * handle)
29{
30	return handle->msg_channel;
31}
32
33hidden_def(sepol_msg_get_channel)
34
35const char *sepol_msg_get_fname(sepol_handle_t * handle)
36{
37	return handle->msg_fname;
38}
39
40hidden_def(sepol_msg_get_fname)
41#ifdef __GNUC__
42    __attribute__ ((format(printf, 3, 4)))
43#endif
44void 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	varg = NULL;
74}
75
76extern void sepol_msg_set_callback(sepol_handle_t * handle,
77#ifdef __GNUC__
78				   __attribute__ ((format(printf, 3, 4)))
79#endif
80				   void (*msg_callback) (void *varg,
81							 sepol_handle_t *
82							 handle,
83							 const char *fmt, ...),
84				   void *msg_callback_arg)
85{
86
87	handle->msg_callback = msg_callback;
88	handle->msg_callback_arg = msg_callback_arg;
89}
90