1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2009-2010 The FreeBSD Foundation 5 * Copyright (c) 2011 Pawel Jakub Dawidek <pjd@FreeBSD.org> 6 * All rights reserved. 7 * 8 * This software was developed by Pawel Jakub Dawidek under sponsorship from 9 * the FreeBSD Foundation. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * $FreeBSD$ 33 */ 34 35 #ifndef _PJDLOG_H_ 36 #define _PJDLOG_H_ 37 38 #include <sys/cdefs.h> 39 40 #include <errno.h> 41 #include <stdarg.h> 42 #include <sysexits.h> 43 #include <syslog.h> 44 45 #define PJDLOG_MODE_STD 0 46 #define PJDLOG_MODE_SYSLOG 1 47 #define PJDLOG_MODE_SOCK 2 48 49 void pjdlog_init(int mode); 50 void pjdlog_fini(void); 51 52 void pjdlog_mode_set(int mode); 53 int pjdlog_mode_get(void); 54 55 #ifdef notyet 56 void pjdlog_sock_set(int sock); 57 int pjdlog_sock_get(void); 58 #endif 59 60 void pjdlog_debug_set(int level); 61 int pjdlog_debug_get(void); 62 63 void pjdlog_prefix_set(const char *fmt, ...) __printflike(1, 2); 64 void pjdlogv_prefix_set(const char *fmt, va_list ap) __printflike(1, 0); 65 const char *pjdlog_prefix_get(void); 66 void pjdlog_prefix_push(const char *fmt, ...) __printflike(1, 2); 67 void pjdlogv_prefix_push(const char *fmt, va_list ap) __printflike(1, 0); 68 void pjdlog_prefix_pop(void); 69 70 void _pjdlogv_common(const char *func, const char *file, int line, int loglevel, 71 int debuglevel, int error, const char *fmt, va_list ap) __printflike(7, 0); 72 void _pjdlog_common(const char *func, const char *file, int line, int loglevel, 73 int debuglevel, int error, const char *fmt, ...) __printflike(7, 8); 74 75 void _pjdlogv_exit(const char *func, const char *file, int line, int exitcode, 76 int error, const char *fmt, va_list ap) __printflike(6, 0) __dead2; 77 void _pjdlog_exit(const char *func, const char *file, int line, int exitcode, 78 int error, const char *fmt, ...) __printflike(6, 7) __dead2; 79 80 void _pjdlog_abort(const char *func, const char *file, int line, int error, 81 const char *failedexpr, const char *fmt, ...) __printflike(6, 7) __dead2; 82 83 #ifdef notyet 84 int pjdlog_receive(int sock); 85 #endif 86 87 #define pjdlogv_common(loglevel, debuglevel, error, fmt, ap) \ 88 _pjdlogv_common(__func__, __FILE__, __LINE__, (loglevel), \ 89 (debuglevel), (error), (fmt), (ap)) 90 #define pjdlog_common(loglevel, debuglevel, error, ...) \ 91 _pjdlog_common(__func__, __FILE__, __LINE__, (loglevel), \ 92 (debuglevel), (error), __VA_ARGS__) 93 94 #define pjdlogv(loglevel, fmt, ap) \ 95 pjdlogv_common((loglevel), 0, -1, (fmt), (ap)) 96 #define pjdlog(loglevel, ...) \ 97 pjdlog_common((loglevel), 0, -1, __VA_ARGS__) 98 99 #define pjdlogv_emergency(fmt, ap) pjdlogv(LOG_EMERG, (fmt), (ap)) 100 #define pjdlog_emergency(...) pjdlog(LOG_EMERG, __VA_ARGS__) 101 #define pjdlogv_alert(fmt, ap) pjdlogv(LOG_ALERT, (fmt), (ap)) 102 #define pjdlog_alert(...) pjdlog(LOG_ALERT, __VA_ARGS__) 103 #define pjdlogv_critical(fmt, ap) pjdlogv(LOG_CRIT, (fmt), (ap)) 104 #define pjdlog_critical(...) pjdlog(LOG_CRIT, __VA_ARGS__) 105 #define pjdlogv_error(fmt, ap) pjdlogv(LOG_ERR, (fmt), (ap)) 106 #define pjdlog_error(...) pjdlog(LOG_ERR, __VA_ARGS__) 107 #define pjdlogv_warning(fmt, ap) pjdlogv(LOG_WARNING, (fmt), (ap)) 108 #define pjdlog_warning(...) pjdlog(LOG_WARNING, __VA_ARGS__) 109 #define pjdlogv_notice(fmt, ap) pjdlogv(LOG_NOTICE, (fmt), (ap)) 110 #define pjdlog_notice(...) pjdlog(LOG_NOTICE, __VA_ARGS__) 111 #define pjdlogv_info(fmt, ap) pjdlogv(LOG_INFO, (fmt), (ap)) 112 #define pjdlog_info(...) pjdlog(LOG_INFO, __VA_ARGS__) 113 114 #define pjdlog_debug(debuglevel, ...) \ 115 pjdlog_common(LOG_DEBUG, (debuglevel), -1, __VA_ARGS__) 116 #define pjdlogv_debug(debuglevel, fmt, ap) \ 117 pjdlogv_common(LOG_DEBUG, (debuglevel), -1, (fmt), (ap)) 118 119 #define pjdlog_errno(loglevel, ...) \ 120 pjdlog_common((loglevel), 0, (errno), __VA_ARGS__) 121 #define pjdlogv_errno(loglevel, fmt, ap) \ 122 pjdlogv_common((loglevel), 0, (errno), (fmt), (ap)) 123 124 #define pjdlogv_exit(exitcode, fmt, ap) \ 125 _pjdlogv_exit(__func__, __FILE__, __LINE__, (exitcode), \ 126 (errno), (fmt), (ap)) 127 #define pjdlog_exit(exitcode, ...) \ 128 _pjdlog_exit(__func__, __FILE__, __LINE__, (exitcode), (errno), \ 129 __VA_ARGS__) 130 131 #define pjdlogv_exitx(exitcode, fmt, ap) \ 132 _pjdlogv_exit(__func__, __FILE__, __LINE__, (exitcode), -1, \ 133 (fmt), (ap)) 134 #define pjdlog_exitx(exitcode, ...) \ 135 _pjdlog_exit(__func__, __FILE__, __LINE__, (exitcode), -1, \ 136 __VA_ARGS__) 137 138 #define PJDLOG_VERIFY(expr) do { \ 139 if (!(expr)) { \ 140 _pjdlog_abort(__func__, __FILE__, __LINE__, -1, #expr, \ 141 __func__); \ 142 } \ 143 } while (0) 144 #define PJDLOG_RVERIFY(expr, ...) do { \ 145 if (!(expr)) { \ 146 _pjdlog_abort(__func__, __FILE__, __LINE__, -1, #expr, \ 147 __VA_ARGS__); \ 148 } \ 149 } while (0) 150 #define PJDLOG_EVERIFY(expr) do { \ 151 if (!(expr)) { \ 152 _pjdlog_abort(__func__, __FILE__, __LINE__, errno, \ 153 #expr, __func__); \ 154 } \ 155 } while (0) 156 #define PJDLOG_ABORT(...) _pjdlog_abort(__func__, __FILE__, \ 157 __LINE__, -1, NULL, __VA_ARGS__) 158 #ifdef NDEBUG 159 #define PJDLOG_ASSERT(expr) do { } while (0) 160 #define PJDLOG_RASSERT(...) do { } while (0) 161 #else 162 #define PJDLOG_ASSERT(expr) do { \ 163 if (!(expr)) { \ 164 _pjdlog_abort(__func__, __FILE__, __LINE__, -1, #expr, \ 165 __func__); \ 166 } \ 167 } while (0) 168 #define PJDLOG_RASSERT(expr, ...) do { \ 169 if (!(expr)) { \ 170 _pjdlog_abort(__func__, __FILE__, __LINE__, -1, #expr, \ 171 __VA_ARGS__); \ 172 } \ 173 } while (0) 174 #endif 175 176 #endif /* !_PJDLOG_H_ */ 177