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
31243730Srwatson#ifndef	_PJDLOG_H_
32243730Srwatson#define	_PJDLOG_H_
33243730Srwatson
34243730Srwatson#include <sys/cdefs.h>
35243730Srwatson
36243730Srwatson#include <stdarg.h>
37243730Srwatson#include <sysexits.h>
38243730Srwatson#include <syslog.h>
39243730Srwatson
40243730Srwatson#include <compat/compat.h>
41243730Srwatson
42243730Srwatson#define	PJDLOG_MODE_STD		0
43243730Srwatson#define	PJDLOG_MODE_SYSLOG	1
44243730Srwatson
45243730Srwatsonvoid pjdlog_init(int mode);
46243730Srwatsonvoid pjdlog_fini(void);
47243730Srwatson
48243730Srwatsonvoid pjdlog_mode_set(int mode);
49243730Srwatsonint pjdlog_mode_get(void);
50243730Srwatson
51243730Srwatsonvoid pjdlog_debug_set(int level);
52243730Srwatsonint pjdlog_debug_get(void);
53243730Srwatson
54243730Srwatsonvoid pjdlog_prefix_set(const char *fmt, ...) __printflike(1, 2);
55243730Srwatsonvoid pjdlogv_prefix_set(const char *fmt, va_list ap) __printflike(1, 0);
56243730Srwatson
57243730Srwatsonvoid pjdlog_common(int loglevel, int debuglevel, int error, const char *fmt,
58243730Srwatson    ...) __printflike(4, 5);
59243730Srwatsonvoid pjdlogv_common(int loglevel, int debuglevel, int error, const char *fmt,
60243730Srwatson    va_list ap) __printflike(4, 0);
61243730Srwatson
62243730Srwatsonvoid pjdlog(int loglevel, const char *fmt, ...) __printflike(2, 3);
63243730Srwatsonvoid pjdlogv(int loglevel, const char *fmt, va_list ap) __printflike(2, 0);
64243730Srwatson
65243730Srwatson#define	pjdlogv_emergency(fmt, ap)	pjdlogv(LOG_EMERG, (fmt), (ap))
66243730Srwatson#define	pjdlog_emergency(...)		pjdlog(LOG_EMERG, __VA_ARGS__)
67243730Srwatson#define	pjdlogv_alert(fmt, ap)		pjdlogv(LOG_ALERT, (fmt), (ap))
68243730Srwatson#define	pjdlog_alert(...)		pjdlog(LOG_ALERT, __VA_ARGS__)
69243730Srwatson#define	pjdlogv_critical(fmt, ap)	pjdlogv(LOG_CRIT, (fmt), (ap))
70243730Srwatson#define	pjdlog_critical(...)		pjdlog(LOG_CRIT, __VA_ARGS__)
71243730Srwatson#define	pjdlogv_error(fmt, ap)		pjdlogv(LOG_ERR, (fmt), (ap))
72243730Srwatson#define	pjdlog_error(...)		pjdlog(LOG_ERR, __VA_ARGS__)
73243730Srwatson#define	pjdlogv_warning(fmt, ap)	pjdlogv(LOG_WARNING, (fmt), (ap))
74243730Srwatson#define	pjdlog_warning(...)		pjdlog(LOG_WARNING, __VA_ARGS__)
75243730Srwatson#define	pjdlogv_notice(fmt, ap)		pjdlogv(LOG_NOTICE, (fmt), (ap))
76243730Srwatson#define	pjdlog_notice(...)		pjdlog(LOG_NOTICE, __VA_ARGS__)
77243730Srwatson#define	pjdlogv_info(fmt, ap)		pjdlogv(LOG_INFO, (fmt), (ap))
78243730Srwatson#define	pjdlog_info(...)		pjdlog(LOG_INFO, __VA_ARGS__)
79243730Srwatson
80243730Srwatsonvoid pjdlog_debug(int debuglevel, const char *fmt, ...) __printflike(2, 3);
81243730Srwatsonvoid pjdlogv_debug(int debuglevel, const char *fmt, va_list ap) __printflike(2, 0);
82243730Srwatson
83243730Srwatsonvoid pjdlog_errno(int loglevel, const char *fmt, ...) __printflike(2, 3);
84243730Srwatsonvoid pjdlogv_errno(int loglevel, const char *fmt, va_list ap) __printflike(2, 0);
85243730Srwatson
86243730Srwatsonvoid pjdlog_exit(int exitcode, const char *fmt, ...) __printflike(2, 3) __dead2;
87243730Srwatsonvoid pjdlogv_exit(int exitcode, const char *fmt, va_list ap) __printflike(2, 0) __dead2;
88243730Srwatson
89243730Srwatsonvoid pjdlog_exitx(int exitcode, const char *fmt, ...) __printflike(2, 3) __dead2;
90243730Srwatsonvoid pjdlogv_exitx(int exitcode, const char *fmt, va_list ap) __printflike(2, 0) __dead2;
91243730Srwatson
92243730Srwatsonvoid pjdlog_abort(const char *func, const char *file, int line,
93243730Srwatson    const char *failedexpr, const char *fmt, ...) __printflike(5, 6) __dead2;
94243730Srwatson
95243730Srwatson#define	PJDLOG_VERIFY(expr)	do {					\
96243730Srwatson	if (!(expr)) {							\
97243730Srwatson		pjdlog_abort(__func__, __FILE__, __LINE__, #expr,	\
98243730Srwatson		    "%s", __func__);					\
99243730Srwatson	}								\
100243730Srwatson} while (0)
101243730Srwatson#define	PJDLOG_RVERIFY(expr, ...)	do {				\
102243730Srwatson	if (!(expr)) {							\
103243730Srwatson		pjdlog_abort(__func__, __FILE__, __LINE__, #expr,	\
104243730Srwatson		    __VA_ARGS__);					\
105243730Srwatson	}								\
106243730Srwatson} while (0)
107243730Srwatson#define	PJDLOG_ABORT(...)	pjdlog_abort(__func__, __FILE__,	\
108243730Srwatson				    __LINE__, NULL, __VA_ARGS__)
109243730Srwatson#ifdef NDEBUG
110243730Srwatson#define	PJDLOG_ASSERT(expr)	do { } while (0)
111243730Srwatson#define	PJDLOG_RASSERT(...)	do { } while (0)
112243730Srwatson#else
113243730Srwatson#define	PJDLOG_ASSERT(expr)	PJDLOG_VERIFY(expr)
114243730Srwatson#define	PJDLOG_RASSERT(...)	PJDLOG_RVERIFY(__VA_ARGS__)
115243730Srwatson#endif
116243730Srwatson
117243730Srwatson#endif	/* !_PJDLOG_H_ */
118