xref: /freebsd/sbin/hastd/pjdlog.h (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
132115b10SPawel Jakub Dawidek /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni  *
432115b10SPawel Jakub Dawidek  * Copyright (c) 2009-2010 The FreeBSD Foundation
5eeb3cd67SPawel Jakub Dawidek  * Copyright (c) 2011 Pawel Jakub Dawidek <pjd@FreeBSD.org>
632115b10SPawel Jakub Dawidek  * All rights reserved.
732115b10SPawel Jakub Dawidek  *
832115b10SPawel Jakub Dawidek  * This software was developed by Pawel Jakub Dawidek under sponsorship from
932115b10SPawel Jakub Dawidek  * the FreeBSD Foundation.
1032115b10SPawel Jakub Dawidek  *
1132115b10SPawel Jakub Dawidek  * Redistribution and use in source and binary forms, with or without
1232115b10SPawel Jakub Dawidek  * modification, are permitted provided that the following conditions
1332115b10SPawel Jakub Dawidek  * are met:
1432115b10SPawel Jakub Dawidek  * 1. Redistributions of source code must retain the above copyright
1532115b10SPawel Jakub Dawidek  *    notice, this list of conditions and the following disclaimer.
1632115b10SPawel Jakub Dawidek  * 2. Redistributions in binary form must reproduce the above copyright
1732115b10SPawel Jakub Dawidek  *    notice, this list of conditions and the following disclaimer in the
1832115b10SPawel Jakub Dawidek  *    documentation and/or other materials provided with the distribution.
1932115b10SPawel Jakub Dawidek  *
2032115b10SPawel Jakub Dawidek  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
2132115b10SPawel Jakub Dawidek  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2232115b10SPawel Jakub Dawidek  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2332115b10SPawel Jakub Dawidek  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
2432115b10SPawel Jakub Dawidek  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2532115b10SPawel Jakub Dawidek  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2632115b10SPawel Jakub Dawidek  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2732115b10SPawel Jakub Dawidek  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2832115b10SPawel Jakub Dawidek  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2932115b10SPawel Jakub Dawidek  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3032115b10SPawel Jakub Dawidek  * SUCH DAMAGE.
3132115b10SPawel Jakub Dawidek  */
3232115b10SPawel Jakub Dawidek 
3332115b10SPawel Jakub Dawidek #ifndef	_PJDLOG_H_
3432115b10SPawel Jakub Dawidek #define	_PJDLOG_H_
3532115b10SPawel Jakub Dawidek 
3632115b10SPawel Jakub Dawidek #include <sys/cdefs.h>
3732115b10SPawel Jakub Dawidek 
3832115b10SPawel Jakub Dawidek #include <stdarg.h>
3932115b10SPawel Jakub Dawidek #include <sysexits.h>
4032115b10SPawel Jakub Dawidek #include <syslog.h>
4132115b10SPawel Jakub Dawidek 
4232115b10SPawel Jakub Dawidek #define	PJDLOG_MODE_STD		0
4332115b10SPawel Jakub Dawidek #define	PJDLOG_MODE_SYSLOG	1
4432115b10SPawel Jakub Dawidek 
45eeb3cd67SPawel Jakub Dawidek void pjdlog_init(int mode);
46eeb3cd67SPawel Jakub Dawidek void pjdlog_fini(void);
47eeb3cd67SPawel Jakub Dawidek 
4832115b10SPawel Jakub Dawidek void pjdlog_mode_set(int mode);
4932115b10SPawel Jakub Dawidek int pjdlog_mode_get(void);
5032115b10SPawel Jakub Dawidek 
5132115b10SPawel Jakub Dawidek void pjdlog_debug_set(int level);
5232115b10SPawel Jakub Dawidek int pjdlog_debug_get(void);
5332115b10SPawel Jakub Dawidek 
5432115b10SPawel Jakub Dawidek void pjdlog_prefix_set(const char *fmt, ...) __printflike(1, 2);
5509d6ae1bSPawel Jakub Dawidek void pjdlogv_prefix_set(const char *fmt, va_list ap) __printflike(1, 0);
5632115b10SPawel Jakub Dawidek 
5732115b10SPawel Jakub Dawidek void pjdlog_common(int loglevel, int debuglevel, int error, const char *fmt,
5832115b10SPawel Jakub Dawidek     ...) __printflike(4, 5);
5932115b10SPawel Jakub Dawidek void pjdlogv_common(int loglevel, int debuglevel, int error, const char *fmt,
6032115b10SPawel Jakub Dawidek     va_list ap) __printflike(4, 0);
6132115b10SPawel Jakub Dawidek 
6232115b10SPawel Jakub Dawidek void pjdlog(int loglevel, const char *fmt, ...) __printflike(2, 3);
6332115b10SPawel Jakub Dawidek void pjdlogv(int loglevel, const char *fmt, va_list ap) __printflike(2, 0);
6432115b10SPawel Jakub Dawidek 
6532115b10SPawel Jakub Dawidek #define	pjdlogv_emergency(fmt, ap)	pjdlogv(LOG_EMERG, (fmt), (ap))
6632115b10SPawel Jakub Dawidek #define	pjdlog_emergency(...)		pjdlog(LOG_EMERG, __VA_ARGS__)
6732115b10SPawel Jakub Dawidek #define	pjdlogv_alert(fmt, ap)		pjdlogv(LOG_ALERT, (fmt), (ap))
6832115b10SPawel Jakub Dawidek #define	pjdlog_alert(...)		pjdlog(LOG_ALERT, __VA_ARGS__)
6932115b10SPawel Jakub Dawidek #define	pjdlogv_critical(fmt, ap)	pjdlogv(LOG_CRIT, (fmt), (ap))
7032115b10SPawel Jakub Dawidek #define	pjdlog_critical(...)		pjdlog(LOG_CRIT, __VA_ARGS__)
7132115b10SPawel Jakub Dawidek #define	pjdlogv_error(fmt, ap)		pjdlogv(LOG_ERR, (fmt), (ap))
7232115b10SPawel Jakub Dawidek #define	pjdlog_error(...)		pjdlog(LOG_ERR, __VA_ARGS__)
7332115b10SPawel Jakub Dawidek #define	pjdlogv_warning(fmt, ap)	pjdlogv(LOG_WARNING, (fmt), (ap))
7432115b10SPawel Jakub Dawidek #define	pjdlog_warning(...)		pjdlog(LOG_WARNING, __VA_ARGS__)
7532115b10SPawel Jakub Dawidek #define	pjdlogv_notice(fmt, ap)		pjdlogv(LOG_NOTICE, (fmt), (ap))
7632115b10SPawel Jakub Dawidek #define	pjdlog_notice(...)		pjdlog(LOG_NOTICE, __VA_ARGS__)
7732115b10SPawel Jakub Dawidek #define	pjdlogv_info(fmt, ap)		pjdlogv(LOG_INFO, (fmt), (ap))
7832115b10SPawel Jakub Dawidek #define	pjdlog_info(...)		pjdlog(LOG_INFO, __VA_ARGS__)
7932115b10SPawel Jakub Dawidek 
8032115b10SPawel Jakub Dawidek void pjdlog_debug(int debuglevel, const char *fmt, ...) __printflike(2, 3);
8132115b10SPawel Jakub Dawidek void pjdlogv_debug(int debuglevel, const char *fmt, va_list ap) __printflike(2, 0);
8232115b10SPawel Jakub Dawidek 
8332115b10SPawel Jakub Dawidek void pjdlog_errno(int loglevel, const char *fmt, ...) __printflike(2, 3);
8432115b10SPawel Jakub Dawidek void pjdlogv_errno(int loglevel, const char *fmt, va_list ap) __printflike(2, 0);
8532115b10SPawel Jakub Dawidek 
8632115b10SPawel Jakub Dawidek void pjdlog_exit(int exitcode, const char *fmt, ...) __printflike(2, 3) __dead2;
8732115b10SPawel Jakub Dawidek void pjdlogv_exit(int exitcode, const char *fmt, va_list ap) __printflike(2, 0) __dead2;
8832115b10SPawel Jakub Dawidek 
8932115b10SPawel Jakub Dawidek void pjdlog_exitx(int exitcode, const char *fmt, ...) __printflike(2, 3) __dead2;
9032115b10SPawel Jakub Dawidek void pjdlogv_exitx(int exitcode, const char *fmt, va_list ap) __printflike(2, 0) __dead2;
9132115b10SPawel Jakub Dawidek 
9205a6b8deSPawel Jakub Dawidek void pjdlog_abort(const char *func, const char *file, int line,
9305a6b8deSPawel Jakub Dawidek     const char *failedexpr, const char *fmt, ...) __printflike(5, 6) __dead2;
94524840d8SPawel Jakub Dawidek 
95524840d8SPawel Jakub Dawidek #define	PJDLOG_VERIFY(expr)	do {					\
9694bf851dSPawel Jakub Dawidek 	if (!(expr)) {							\
9705a6b8deSPawel Jakub Dawidek 		pjdlog_abort(__func__, __FILE__, __LINE__, #expr,	\
98771a6733SPawel Jakub Dawidek 		    __func__);						\
9994bf851dSPawel Jakub Dawidek 	}								\
100524840d8SPawel Jakub Dawidek } while (0)
10194bf851dSPawel Jakub Dawidek #define	PJDLOG_RVERIFY(expr, ...)	do {				\
10294bf851dSPawel Jakub Dawidek 	if (!(expr)) {							\
10305a6b8deSPawel Jakub Dawidek 		pjdlog_abort(__func__, __FILE__, __LINE__, #expr,	\
10494bf851dSPawel Jakub Dawidek 		    __VA_ARGS__);					\
10594bf851dSPawel Jakub Dawidek 	}								\
10694bf851dSPawel Jakub Dawidek } while (0)
10705a6b8deSPawel Jakub Dawidek #define	PJDLOG_ABORT(...)	pjdlog_abort(__func__, __FILE__,	\
10894bf851dSPawel Jakub Dawidek 				    __LINE__, NULL, __VA_ARGS__)
109524840d8SPawel Jakub Dawidek #ifdef NDEBUG
110524840d8SPawel Jakub Dawidek #define	PJDLOG_ASSERT(expr)	do { } while (0)
11194bf851dSPawel Jakub Dawidek #define	PJDLOG_RASSERT(...)	do { } while (0)
112524840d8SPawel Jakub Dawidek #else
113524840d8SPawel Jakub Dawidek #define	PJDLOG_ASSERT(expr)	PJDLOG_VERIFY(expr)
11494bf851dSPawel Jakub Dawidek #define	PJDLOG_RASSERT(...)	PJDLOG_RVERIFY(__VA_ARGS__)
115524840d8SPawel Jakub Dawidek #endif
116524840d8SPawel Jakub Dawidek 
11732115b10SPawel Jakub Dawidek #endif	/* !_PJDLOG_H_ */
118