xref: /freebsd/contrib/openbsm/bin/auditdistd/pjdlog.h (revision b626f5a73a48f44a31a200291b141e1da408a2ff)
1*aa772005SRobert Watson /*-
2*aa772005SRobert Watson  * Copyright (c) 2009-2010 The FreeBSD Foundation
3*aa772005SRobert Watson  * Copyright (c) 2011 Pawel Jakub Dawidek <pjd@FreeBSD.org>
4*aa772005SRobert Watson  * All rights reserved.
5*aa772005SRobert Watson  *
6*aa772005SRobert Watson  * This software was developed by Pawel Jakub Dawidek under sponsorship from
7*aa772005SRobert Watson  * the FreeBSD Foundation.
8*aa772005SRobert Watson  *
9*aa772005SRobert Watson  * Redistribution and use in source and binary forms, with or without
10*aa772005SRobert Watson  * modification, are permitted provided that the following conditions
11*aa772005SRobert Watson  * are met:
12*aa772005SRobert Watson  * 1. Redistributions of source code must retain the above copyright
13*aa772005SRobert Watson  *    notice, this list of conditions and the following disclaimer.
14*aa772005SRobert Watson  * 2. Redistributions in binary form must reproduce the above copyright
15*aa772005SRobert Watson  *    notice, this list of conditions and the following disclaimer in the
16*aa772005SRobert Watson  *    documentation and/or other materials provided with the distribution.
17*aa772005SRobert Watson  *
18*aa772005SRobert Watson  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
19*aa772005SRobert Watson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*aa772005SRobert Watson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*aa772005SRobert Watson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
22*aa772005SRobert Watson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23*aa772005SRobert Watson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24*aa772005SRobert Watson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25*aa772005SRobert Watson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26*aa772005SRobert Watson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27*aa772005SRobert Watson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28*aa772005SRobert Watson  * SUCH DAMAGE.
29*aa772005SRobert Watson  */
30*aa772005SRobert Watson 
31*aa772005SRobert Watson #ifndef	_PJDLOG_H_
32*aa772005SRobert Watson #define	_PJDLOG_H_
33*aa772005SRobert Watson 
34*aa772005SRobert Watson #include <sys/cdefs.h>
35*aa772005SRobert Watson 
36*aa772005SRobert Watson #include <stdarg.h>
37*aa772005SRobert Watson #include <sysexits.h>
38*aa772005SRobert Watson #include <syslog.h>
39*aa772005SRobert Watson 
40*aa772005SRobert Watson #include <compat/compat.h>
41*aa772005SRobert Watson 
42*aa772005SRobert Watson #define	PJDLOG_MODE_STD		0
43*aa772005SRobert Watson #define	PJDLOG_MODE_SYSLOG	1
44*aa772005SRobert Watson 
45*aa772005SRobert Watson void pjdlog_init(int mode);
46*aa772005SRobert Watson void pjdlog_fini(void);
47*aa772005SRobert Watson 
48*aa772005SRobert Watson void pjdlog_mode_set(int mode);
49*aa772005SRobert Watson int pjdlog_mode_get(void);
50*aa772005SRobert Watson 
51*aa772005SRobert Watson void pjdlog_debug_set(int level);
52*aa772005SRobert Watson int pjdlog_debug_get(void);
53*aa772005SRobert Watson 
54*aa772005SRobert Watson void pjdlog_prefix_set(const char *fmt, ...) __printflike(1, 2);
55*aa772005SRobert Watson void pjdlogv_prefix_set(const char *fmt, va_list ap) __printflike(1, 0);
56*aa772005SRobert Watson 
57*aa772005SRobert Watson void pjdlog_common(int loglevel, int debuglevel, int error, const char *fmt,
58*aa772005SRobert Watson     ...) __printflike(4, 5);
59*aa772005SRobert Watson void pjdlogv_common(int loglevel, int debuglevel, int error, const char *fmt,
60*aa772005SRobert Watson     va_list ap) __printflike(4, 0);
61*aa772005SRobert Watson 
62*aa772005SRobert Watson void pjdlog(int loglevel, const char *fmt, ...) __printflike(2, 3);
63*aa772005SRobert Watson void pjdlogv(int loglevel, const char *fmt, va_list ap) __printflike(2, 0);
64*aa772005SRobert Watson 
65*aa772005SRobert Watson #define	pjdlogv_emergency(fmt, ap)	pjdlogv(LOG_EMERG, (fmt), (ap))
66*aa772005SRobert Watson #define	pjdlog_emergency(...)		pjdlog(LOG_EMERG, __VA_ARGS__)
67*aa772005SRobert Watson #define	pjdlogv_alert(fmt, ap)		pjdlogv(LOG_ALERT, (fmt), (ap))
68*aa772005SRobert Watson #define	pjdlog_alert(...)		pjdlog(LOG_ALERT, __VA_ARGS__)
69*aa772005SRobert Watson #define	pjdlogv_critical(fmt, ap)	pjdlogv(LOG_CRIT, (fmt), (ap))
70*aa772005SRobert Watson #define	pjdlog_critical(...)		pjdlog(LOG_CRIT, __VA_ARGS__)
71*aa772005SRobert Watson #define	pjdlogv_error(fmt, ap)		pjdlogv(LOG_ERR, (fmt), (ap))
72*aa772005SRobert Watson #define	pjdlog_error(...)		pjdlog(LOG_ERR, __VA_ARGS__)
73*aa772005SRobert Watson #define	pjdlogv_warning(fmt, ap)	pjdlogv(LOG_WARNING, (fmt), (ap))
74*aa772005SRobert Watson #define	pjdlog_warning(...)		pjdlog(LOG_WARNING, __VA_ARGS__)
75*aa772005SRobert Watson #define	pjdlogv_notice(fmt, ap)		pjdlogv(LOG_NOTICE, (fmt), (ap))
76*aa772005SRobert Watson #define	pjdlog_notice(...)		pjdlog(LOG_NOTICE, __VA_ARGS__)
77*aa772005SRobert Watson #define	pjdlogv_info(fmt, ap)		pjdlogv(LOG_INFO, (fmt), (ap))
78*aa772005SRobert Watson #define	pjdlog_info(...)		pjdlog(LOG_INFO, __VA_ARGS__)
79*aa772005SRobert Watson 
80*aa772005SRobert Watson void pjdlog_debug(int debuglevel, const char *fmt, ...) __printflike(2, 3);
81*aa772005SRobert Watson void pjdlogv_debug(int debuglevel, const char *fmt, va_list ap) __printflike(2, 0);
82*aa772005SRobert Watson 
83*aa772005SRobert Watson void pjdlog_errno(int loglevel, const char *fmt, ...) __printflike(2, 3);
84*aa772005SRobert Watson void pjdlogv_errno(int loglevel, const char *fmt, va_list ap) __printflike(2, 0);
85*aa772005SRobert Watson 
86*aa772005SRobert Watson void pjdlog_exit(int exitcode, const char *fmt, ...) __printflike(2, 3) __dead2;
87*aa772005SRobert Watson void pjdlogv_exit(int exitcode, const char *fmt, va_list ap) __printflike(2, 0) __dead2;
88*aa772005SRobert Watson 
89*aa772005SRobert Watson void pjdlog_exitx(int exitcode, const char *fmt, ...) __printflike(2, 3) __dead2;
90*aa772005SRobert Watson void pjdlogv_exitx(int exitcode, const char *fmt, va_list ap) __printflike(2, 0) __dead2;
91*aa772005SRobert Watson 
92*aa772005SRobert Watson void pjdlog_abort(const char *func, const char *file, int line,
93*aa772005SRobert Watson     const char *failedexpr, const char *fmt, ...) __printflike(5, 6) __dead2;
94*aa772005SRobert Watson 
95*aa772005SRobert Watson #define	PJDLOG_VERIFY(expr)	do {					\
96*aa772005SRobert Watson 	if (!(expr)) {							\
97*aa772005SRobert Watson 		pjdlog_abort(__func__, __FILE__, __LINE__, #expr,	\
98*aa772005SRobert Watson 		    "%s", __func__);					\
99*aa772005SRobert Watson 	}								\
100*aa772005SRobert Watson } while (0)
101*aa772005SRobert Watson #define	PJDLOG_RVERIFY(expr, ...)	do {				\
102*aa772005SRobert Watson 	if (!(expr)) {							\
103*aa772005SRobert Watson 		pjdlog_abort(__func__, __FILE__, __LINE__, #expr,	\
104*aa772005SRobert Watson 		    __VA_ARGS__);					\
105*aa772005SRobert Watson 	}								\
106*aa772005SRobert Watson } while (0)
107*aa772005SRobert Watson #define	PJDLOG_ABORT(...)	pjdlog_abort(__func__, __FILE__,	\
108*aa772005SRobert Watson 				    __LINE__, NULL, __VA_ARGS__)
109*aa772005SRobert Watson #ifdef NDEBUG
110*aa772005SRobert Watson #define	PJDLOG_ASSERT(expr)	do { } while (0)
111*aa772005SRobert Watson #define	PJDLOG_RASSERT(...)	do { } while (0)
112*aa772005SRobert Watson #else
113*aa772005SRobert Watson #define	PJDLOG_ASSERT(expr)	PJDLOG_VERIFY(expr)
114*aa772005SRobert Watson #define	PJDLOG_RASSERT(...)	PJDLOG_RVERIFY(__VA_ARGS__)
115*aa772005SRobert Watson #endif
116*aa772005SRobert Watson 
117*aa772005SRobert Watson #endif	/* !_PJDLOG_H_ */
118