xref: /aosp_15_r20/external/selinux/libsepol/cil/src/cil_log.c (revision 2d543d20722ada2425b5bdab9d0d1d29470e7bba)
1*2d543d20SAndroid Build Coastguard Worker /*
2*2d543d20SAndroid Build Coastguard Worker  * Copyright 2011 Tresys Technology, LLC. All rights reserved.
3*2d543d20SAndroid Build Coastguard Worker  *
4*2d543d20SAndroid Build Coastguard Worker  * Redistribution and use in source and binary forms, with or without
5*2d543d20SAndroid Build Coastguard Worker  * modification, are permitted provided that the following conditions are met:
6*2d543d20SAndroid Build Coastguard Worker  *
7*2d543d20SAndroid Build Coastguard Worker  *    1. Redistributions of source code must retain the above copyright notice,
8*2d543d20SAndroid Build Coastguard Worker  *       this list of conditions and the following disclaimer.
9*2d543d20SAndroid Build Coastguard Worker  *
10*2d543d20SAndroid Build Coastguard Worker  *    2. Redistributions in binary form must reproduce the above copyright notice,
11*2d543d20SAndroid Build Coastguard Worker  *       this list of conditions and the following disclaimer in the documentation
12*2d543d20SAndroid Build Coastguard Worker  *       and/or other materials provided with the distribution.
13*2d543d20SAndroid Build Coastguard Worker  *
14*2d543d20SAndroid Build Coastguard Worker  * THIS SOFTWARE IS PROVIDED BY TRESYS TECHNOLOGY, LLC ``AS IS'' AND ANY EXPRESS
15*2d543d20SAndroid Build Coastguard Worker  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16*2d543d20SAndroid Build Coastguard Worker  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
17*2d543d20SAndroid Build Coastguard Worker  * EVENT SHALL TRESYS TECHNOLOGY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
18*2d543d20SAndroid Build Coastguard Worker  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19*2d543d20SAndroid Build Coastguard Worker  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20*2d543d20SAndroid Build Coastguard Worker  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21*2d543d20SAndroid Build Coastguard Worker  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
22*2d543d20SAndroid Build Coastguard Worker  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23*2d543d20SAndroid Build Coastguard Worker  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24*2d543d20SAndroid Build Coastguard Worker  *
25*2d543d20SAndroid Build Coastguard Worker  * The views and conclusions contained in the software and documentation are those
26*2d543d20SAndroid Build Coastguard Worker  * of the authors and should not be interpreted as representing official policies,
27*2d543d20SAndroid Build Coastguard Worker  * either expressed or implied, of Tresys Technology, LLC.
28*2d543d20SAndroid Build Coastguard Worker  */
29*2d543d20SAndroid Build Coastguard Worker 
30*2d543d20SAndroid Build Coastguard Worker #include <stdlib.h>
31*2d543d20SAndroid Build Coastguard Worker #include <stdio.h>
32*2d543d20SAndroid Build Coastguard Worker #include <stdarg.h>
33*2d543d20SAndroid Build Coastguard Worker #include <string.h>
34*2d543d20SAndroid Build Coastguard Worker 
35*2d543d20SAndroid Build Coastguard Worker #include <cil/cil.h>
36*2d543d20SAndroid Build Coastguard Worker #include "cil_log.h"
37*2d543d20SAndroid Build Coastguard Worker 
38*2d543d20SAndroid Build Coastguard Worker static enum cil_log_level cil_log_level = CIL_ERR;
39*2d543d20SAndroid Build Coastguard Worker 
cil_default_log_handler(int lvl,const char * msg)40*2d543d20SAndroid Build Coastguard Worker static void cil_default_log_handler(__attribute__((unused)) int lvl, const char *msg)
41*2d543d20SAndroid Build Coastguard Worker {
42*2d543d20SAndroid Build Coastguard Worker 	fprintf(stderr, "%s", msg);
43*2d543d20SAndroid Build Coastguard Worker }
44*2d543d20SAndroid Build Coastguard Worker 
45*2d543d20SAndroid Build Coastguard Worker static void (*cil_log_handler)(int lvl, const char *msg) = &cil_default_log_handler;
46*2d543d20SAndroid Build Coastguard Worker 
cil_set_log_handler(void (* handler)(int lvl,const char * msg))47*2d543d20SAndroid Build Coastguard Worker void cil_set_log_handler(void (*handler)(int lvl, const char *msg))
48*2d543d20SAndroid Build Coastguard Worker {
49*2d543d20SAndroid Build Coastguard Worker 	cil_log_handler = handler;
50*2d543d20SAndroid Build Coastguard Worker }
51*2d543d20SAndroid Build Coastguard Worker 
cil_vlog(enum cil_log_level lvl,const char * msg,va_list args)52*2d543d20SAndroid Build Coastguard Worker __attribute__ ((format (printf, 2, 0))) void cil_vlog(enum cil_log_level lvl, const char *msg, va_list args)
53*2d543d20SAndroid Build Coastguard Worker {
54*2d543d20SAndroid Build Coastguard Worker 	if (cil_log_level >= lvl) {
55*2d543d20SAndroid Build Coastguard Worker 		char buff[MAX_LOG_SIZE];
56*2d543d20SAndroid Build Coastguard Worker 		int n = vsnprintf(buff, MAX_LOG_SIZE, msg, args);
57*2d543d20SAndroid Build Coastguard Worker 		if (n > 0) {
58*2d543d20SAndroid Build Coastguard Worker 			(*cil_log_handler)(cil_log_level, buff);
59*2d543d20SAndroid Build Coastguard Worker 			if (n >= MAX_LOG_SIZE) {
60*2d543d20SAndroid Build Coastguard Worker 				(*cil_log_handler)(cil_log_level, " <LOG MESSAGE TRUNCATED>");
61*2d543d20SAndroid Build Coastguard Worker 			}
62*2d543d20SAndroid Build Coastguard Worker 		}
63*2d543d20SAndroid Build Coastguard Worker 	}
64*2d543d20SAndroid Build Coastguard Worker }
65*2d543d20SAndroid Build Coastguard Worker 
cil_log(enum cil_log_level lvl,const char * msg,...)66*2d543d20SAndroid Build Coastguard Worker __attribute__ ((format (printf, 2, 3))) void cil_log(enum cil_log_level lvl, const char *msg, ...)
67*2d543d20SAndroid Build Coastguard Worker {
68*2d543d20SAndroid Build Coastguard Worker     va_list args;
69*2d543d20SAndroid Build Coastguard Worker     va_start(args, msg);
70*2d543d20SAndroid Build Coastguard Worker     cil_vlog(lvl, msg, args);
71*2d543d20SAndroid Build Coastguard Worker     va_end(args);
72*2d543d20SAndroid Build Coastguard Worker }
73*2d543d20SAndroid Build Coastguard Worker 
cil_set_log_level(enum cil_log_level lvl)74*2d543d20SAndroid Build Coastguard Worker void cil_set_log_level(enum cil_log_level lvl)
75*2d543d20SAndroid Build Coastguard Worker {
76*2d543d20SAndroid Build Coastguard Worker 	cil_log_level = lvl;
77*2d543d20SAndroid Build Coastguard Worker }
78*2d543d20SAndroid Build Coastguard Worker 
cil_get_log_level(void)79*2d543d20SAndroid Build Coastguard Worker enum cil_log_level cil_get_log_level(void)
80*2d543d20SAndroid Build Coastguard Worker {
81*2d543d20SAndroid Build Coastguard Worker 	return cil_log_level;
82*2d543d20SAndroid Build Coastguard Worker }
83