1243730Srwatson/*-
2243730Srwatson * Copyright (c) 2009-2010 The FreeBSD Foundation
3243730Srwatson * Copyright (c) 2011 Pawel Jakub Dawidek <pjd@FreeBSD.org>
4243730Srwatson * All rights reserved.
5243730Srwatson *
6243730Srwatson * This software was developed by Pawel Jakub Dawidek under sponsorship from
7243730Srwatson * the FreeBSD Foundation.
8243730Srwatson *
9243730Srwatson * Redistribution and use in source and binary forms, with or without
10243730Srwatson * modification, are permitted provided that the following conditions
11243730Srwatson * are met:
12243730Srwatson * 1. Redistributions of source code must retain the above copyright
13243730Srwatson *    notice, this list of conditions and the following disclaimer.
14243730Srwatson * 2. Redistributions in binary form must reproduce the above copyright
15243730Srwatson *    notice, this list of conditions and the following disclaimer in the
16243730Srwatson *    documentation and/or other materials provided with the distribution.
17243730Srwatson *
18243730Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
19243730Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20243730Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21243730Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
22243730Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23243730Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24243730Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25243730Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26243730Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27243730Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28243730Srwatson * SUCH DAMAGE.
29243730Srwatson *
30243730Srwatson * $P4: //depot/projects/trustedbsd/openbsm/bin/auditdistd/pjdlog.h#1 $
31243730Srwatson */
32243730Srwatson
33243730Srwatson#ifndef	_PJDLOG_H_
34243730Srwatson#define	_PJDLOG_H_
35243730Srwatson
36243730Srwatson#include <sys/cdefs.h>
37243730Srwatson
38243730Srwatson#include <stdarg.h>
39243730Srwatson#include <sysexits.h>
40243730Srwatson#include <syslog.h>
41243730Srwatson
42243730Srwatson#include <compat/compat.h>
43243730Srwatson
44243730Srwatson#define	PJDLOG_MODE_STD		0
45243730Srwatson#define	PJDLOG_MODE_SYSLOG	1
46243730Srwatson
47243730Srwatsonvoid pjdlog_init(int mode);
48243730Srwatsonvoid pjdlog_fini(void);
49243730Srwatson
50243730Srwatsonvoid pjdlog_mode_set(int mode);
51243730Srwatsonint pjdlog_mode_get(void);
52243730Srwatson
53243730Srwatsonvoid pjdlog_debug_set(int level);
54243730Srwatsonint pjdlog_debug_get(void);
55243730Srwatson
56243730Srwatsonvoid pjdlog_prefix_set(const char *fmt, ...) __printflike(1, 2);
57243730Srwatsonvoid pjdlogv_prefix_set(const char *fmt, va_list ap) __printflike(1, 0);
58243730Srwatson
59243730Srwatsonvoid pjdlog_common(int loglevel, int debuglevel, int error, const char *fmt,
60243730Srwatson    ...) __printflike(4, 5);
61243730Srwatsonvoid pjdlogv_common(int loglevel, int debuglevel, int error, const char *fmt,
62243730Srwatson    va_list ap) __printflike(4, 0);
63243730Srwatson
64243730Srwatsonvoid pjdlog(int loglevel, const char *fmt, ...) __printflike(2, 3);
65243730Srwatsonvoid pjdlogv(int loglevel, const char *fmt, va_list ap) __printflike(2, 0);
66243730Srwatson
67243730Srwatson#define	pjdlogv_emergency(fmt, ap)	pjdlogv(LOG_EMERG, (fmt), (ap))
68243730Srwatson#define	pjdlog_emergency(...)		pjdlog(LOG_EMERG, __VA_ARGS__)
69243730Srwatson#define	pjdlogv_alert(fmt, ap)		pjdlogv(LOG_ALERT, (fmt), (ap))
70243730Srwatson#define	pjdlog_alert(...)		pjdlog(LOG_ALERT, __VA_ARGS__)
71243730Srwatson#define	pjdlogv_critical(fmt, ap)	pjdlogv(LOG_CRIT, (fmt), (ap))
72243730Srwatson#define	pjdlog_critical(...)		pjdlog(LOG_CRIT, __VA_ARGS__)
73243730Srwatson#define	pjdlogv_error(fmt, ap)		pjdlogv(LOG_ERR, (fmt), (ap))
74243730Srwatson#define	pjdlog_error(...)		pjdlog(LOG_ERR, __VA_ARGS__)
75243730Srwatson#define	pjdlogv_warning(fmt, ap)	pjdlogv(LOG_WARNING, (fmt), (ap))
76243730Srwatson#define	pjdlog_warning(...)		pjdlog(LOG_WARNING, __VA_ARGS__)
77243730Srwatson#define	pjdlogv_notice(fmt, ap)		pjdlogv(LOG_NOTICE, (fmt), (ap))
78243730Srwatson#define	pjdlog_notice(...)		pjdlog(LOG_NOTICE, __VA_ARGS__)
79243730Srwatson#define	pjdlogv_info(fmt, ap)		pjdlogv(LOG_INFO, (fmt), (ap))
80243730Srwatson#define	pjdlog_info(...)		pjdlog(LOG_INFO, __VA_ARGS__)
81243730Srwatson
82243730Srwatsonvoid pjdlog_debug(int debuglevel, const char *fmt, ...) __printflike(2, 3);
83243730Srwatsonvoid pjdlogv_debug(int debuglevel, const char *fmt, va_list ap) __printflike(2, 0);
84243730Srwatson
85243730Srwatsonvoid pjdlog_errno(int loglevel, const char *fmt, ...) __printflike(2, 3);
86243730Srwatsonvoid pjdlogv_errno(int loglevel, const char *fmt, va_list ap) __printflike(2, 0);
87243730Srwatson
88243730Srwatsonvoid pjdlog_exit(int exitcode, const char *fmt, ...) __printflike(2, 3) __dead2;
89243730Srwatsonvoid pjdlogv_exit(int exitcode, const char *fmt, va_list ap) __printflike(2, 0) __dead2;
90243730Srwatson
91243730Srwatsonvoid pjdlog_exitx(int exitcode, const char *fmt, ...) __printflike(2, 3) __dead2;
92243730Srwatsonvoid pjdlogv_exitx(int exitcode, const char *fmt, va_list ap) __printflike(2, 0) __dead2;
93243730Srwatson
94243730Srwatsonvoid pjdlog_abort(const char *func, const char *file, int line,
95243730Srwatson    const char *failedexpr, const char *fmt, ...) __printflike(5, 6) __dead2;
96243730Srwatson
97243730Srwatson#define	PJDLOG_VERIFY(expr)	do {					\
98243730Srwatson	if (!(expr)) {							\
99243730Srwatson		pjdlog_abort(__func__, __FILE__, __LINE__, #expr,	\
100243730Srwatson		    "%s", __func__);					\
101243730Srwatson	}								\
102243730Srwatson} while (0)
103243730Srwatson#define	PJDLOG_RVERIFY(expr, ...)	do {				\
104243730Srwatson	if (!(expr)) {							\
105243730Srwatson		pjdlog_abort(__func__, __FILE__, __LINE__, #expr,	\
106243730Srwatson		    __VA_ARGS__);					\
107243730Srwatson	}								\
108243730Srwatson} while (0)
109243730Srwatson#define	PJDLOG_ABORT(...)	pjdlog_abort(__func__, __FILE__,	\
110243730Srwatson				    __LINE__, NULL, __VA_ARGS__)
111243730Srwatson#ifdef NDEBUG
112243730Srwatson#define	PJDLOG_ASSERT(expr)	do { } while (0)
113243730Srwatson#define	PJDLOG_RASSERT(...)	do { } while (0)
114243730Srwatson#else
115243730Srwatson#define	PJDLOG_ASSERT(expr)	PJDLOG_VERIFY(expr)
116243730Srwatson#define	PJDLOG_RASSERT(...)	PJDLOG_RVERIFY(__VA_ARGS__)
117243730Srwatson#endif
118243730Srwatson
119243730Srwatson#endif	/* !_PJDLOG_H_ */
120