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