xref: /third_party/selinux/libsepol/src/debug.c (revision 6cd6a6ac)
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
26
27const char *sepol_msg_get_channel(sepol_handle_t * handle)
28{
29	return handle->msg_channel;
30}
31
32
33const char *sepol_msg_get_fname(sepol_handle_t * handle)
34{
35	return handle->msg_fname;
36}
37
38#ifdef __GNUC__
39    __attribute__ ((format(printf, 3, 4)))
40#endif
41void sepol_msg_default_handler(void *varg __attribute__ ((unused)),
42				      sepol_handle_t * handle,
43				      const char *fmt, ...)
44{
45
46	FILE *stream = NULL;
47	va_list ap;
48
49	switch (sepol_msg_get_level(handle)) {
50
51	case SEPOL_MSG_ERR:
52	case SEPOL_MSG_WARN:
53		stream = stderr;
54		break;
55	case SEPOL_MSG_INFO:
56	default:
57		stream = stdout;
58		break;
59	}
60
61	fprintf(stream, "%s.%s: ",
62		sepol_msg_get_channel(handle), sepol_msg_get_fname(handle));
63
64	va_start(ap, fmt);
65	vfprintf(stream, fmt, ap);
66	va_end(ap);
67
68	fprintf(stream, "\n");
69}
70
71extern void sepol_msg_set_callback(sepol_handle_t * handle,
72#ifdef __GNUC__
73				   __attribute__ ((format(printf, 3, 4)))
74#endif
75				   void (*msg_callback) (void *varg,
76							 sepol_handle_t *
77							 handle,
78							 const char *fmt, ...),
79				   void *msg_callback_arg)
80{
81
82	handle->msg_callback = msg_callback;
83	handle->msg_callback_arg = msg_callback_arg;
84}
85