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