]>
Commit | Line | Data |
---|---|---|
6f58a222 JF |
1 | /* |
2 | * Copyright (c) 2019 Red Hat, Inc. | |
3 | * | |
4 | * All rights reserved. | |
5 | * | |
6 | * Author: Jan Friesse (jfriesse@redhat.com) | |
7 | * | |
8 | * This software licensed under BSD license, the text of which follows: | |
9 | * | |
10 | * Redistribution and use in source and binary forms, with or without | |
11 | * modification, are permitted provided that the following conditions are met: | |
12 | * | |
13 | * - Redistributions of source code must retain the above copyright notice, | |
14 | * this list of conditions and the following disclaimer. | |
15 | * - Redistributions in binary form must reproduce the above copyright notice, | |
16 | * this list of conditions and the following disclaimer in the documentation | |
17 | * and/or other materials provided with the distribution. | |
18 | * - Neither the name of the Red Hat, Inc. nor the names of its | |
19 | * contributors may be used to endorse or promote products derived from this | |
20 | * software without specific prior written permission. | |
21 | * | |
22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
23 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
26 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
28 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
31 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | |
32 | * THE POSSIBILITY OF SUCH DAMAGE. | |
33 | */ | |
34 | ||
35 | #include <stdio.h> | |
36 | #include <assert.h> | |
37 | #include <string.h> | |
38 | #include <errno.h> | |
39 | ||
40 | #include "log.h" | |
41 | ||
42 | #define MAX_LINE_LEN 512 | |
43 | ||
44 | static int openlog_called; | |
45 | static int openlog_facility; | |
46 | static char vsyslog_buf[MAX_LINE_LEN]; | |
47 | static int vsyslog_priority; | |
48 | static int vsyslog_called; | |
49 | static int closelog_called; | |
50 | ||
b82970cb JF |
51 | extern void __vsyslog_chk(int priority, int flag, const char *format, va_list ap) |
52 | __attribute__((__format__(__printf__, 3, 0))); | |
53 | ||
6f58a222 JF |
54 | void |
55 | openlog(const char *ident, int option, int facility) | |
56 | { | |
57 | ||
58 | openlog_called = 1; | |
59 | openlog_facility = facility; | |
60 | } | |
61 | ||
62 | void | |
63 | vsyslog(int priority, const char *format, va_list ap) | |
64 | { | |
65 | va_list ap_copy; | |
66 | int res; | |
67 | ||
68 | vsyslog_called = 1; | |
69 | vsyslog_priority = priority; | |
70 | ||
71 | va_copy(ap_copy, ap); | |
72 | res = vsnprintf(vsyslog_buf, MAX_LINE_LEN, format, ap_copy); | |
73 | assert(res < MAX_LINE_LEN && res != -1); | |
74 | va_end(ap_copy); | |
75 | } | |
76 | ||
563f870f JF |
77 | void |
78 | __vsyslog_chk(int priority, int flag, const char *format, va_list ap) | |
79 | { | |
80 | ||
81 | vsyslog(priority, format, ap); | |
82 | } | |
83 | ||
6f58a222 JF |
84 | void |
85 | closelog(void) | |
86 | { | |
87 | ||
88 | closelog_called = 1; | |
89 | } | |
90 | ||
91 | static void | |
92 | log_check(int priority, const char *msg, int should_be_called, int expected_priority) | |
93 | { | |
94 | ||
95 | vsyslog_called = 0; | |
96 | vsyslog_priority = -1; | |
97 | vsyslog_buf[0] = '\0'; | |
98 | ||
99 | log(priority, "%s", msg); | |
100 | ||
101 | if (should_be_called) { | |
102 | assert(vsyslog_called); | |
103 | assert(vsyslog_priority == expected_priority); | |
104 | assert(strcmp(vsyslog_buf, msg) == 0); | |
105 | } else { | |
106 | assert(!vsyslog_called); | |
107 | } | |
108 | } | |
109 | ||
110 | int | |
111 | main(void) | |
112 | { | |
113 | ||
114 | openlog_called = 0; | |
115 | assert(log_init("test", LOG_TARGET_SYSLOG, LOG_DAEMON) == 0); | |
116 | assert(openlog_called); | |
117 | assert(openlog_facility == LOG_DAEMON); | |
118 | ||
119 | log_check(LOG_INFO, "test log info", 1, LOG_INFO); | |
120 | log_check(LOG_ERR, "test log err", 1, LOG_ERR); | |
121 | log_check(LOG_DEBUG, "test log debug", 0, LOG_DEBUG); | |
122 | ||
123 | log_set_debug(1); | |
124 | log_check(LOG_DEBUG, "test log debug", 1, LOG_DEBUG); | |
125 | ||
126 | log_set_debug(0); | |
127 | log_check(LOG_DEBUG, "test log debug", 0, LOG_DEBUG); | |
128 | ||
129 | log_set_debug(1); | |
130 | log_set_priority_bump(1); | |
131 | log_check(LOG_ERR, "test log err", 1, LOG_ERR); | |
132 | log_check(LOG_DEBUG, "test log debug", 1, LOG_INFO); | |
133 | ||
134 | log_set_priority_bump(0); | |
135 | log_check(LOG_ERR, "test log err", 1, LOG_ERR); | |
136 | log_check(LOG_DEBUG, "test log debug", 1, LOG_DEBUG); | |
137 | ||
138 | closelog_called = 0; | |
139 | openlog_called = 0; | |
140 | log_set_target(LOG_TARGET_SYSLOG, LOG_USER); | |
141 | assert(openlog_called); | |
142 | assert(closelog_called); | |
143 | assert(openlog_facility == LOG_USER); | |
144 | ||
145 | closelog_called = 0; | |
146 | log_close(); | |
147 | assert(closelog_called); | |
148 | ||
149 | return (0); | |
150 | } |