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 33 #ifndef _PJDLOG_H_ 34 #define _PJDLOG_H_ 35 36 #include <sys/cdefs.h> 37 38 #include <errno.h> 39 #include <stdarg.h> 40 #include <sysexits.h> 41 #include <syslog.h> 42 43 #define PJDLOG_MODE_STD 0 44 #define PJDLOG_MODE_SYSLOG 1 45 #define PJDLOG_MODE_SOCK 2 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 #ifdef notyet 54 void pjdlog_sock_set(int sock); 55 int pjdlog_sock_get(void); 56 #endif 57 58 void pjdlog_debug_set(int level); 59 int pjdlog_debug_get(void); 60 61 void pjdlog_prefix_set(const char *fmt, ...) __printflike(1, 2); 62 void pjdlogv_prefix_set(const char *fmt, va_list ap) __printflike(1, 0); 63 const char *pjdlog_prefix_get(void); 64 void pjdlog_prefix_push(const char *fmt, ...) __printflike(1, 2); 65 void pjdlogv_prefix_push(const char *fmt, va_list ap) __printflike(1, 0); 66 void pjdlog_prefix_pop(void); 67 68 void _pjdlogv_common(const char *func, const char *file, int line, int loglevel, 69 int debuglevel, int error, const char *fmt, va_list ap) __printflike(7, 0); 70 void _pjdlog_common(const char *func, const char *file, int line, int loglevel, 71 int debuglevel, int error, const char *fmt, ...) __printflike(7, 8); 72 73 void _pjdlogv_exit(const char *func, const char *file, int line, int exitcode, 74 int error, const char *fmt, va_list ap) __printflike(6, 0) __dead2; 75 void _pjdlog_exit(const char *func, const char *file, int line, int exitcode, 76 int error, const char *fmt, ...) __printflike(6, 7) __dead2; 77 78 void _pjdlog_abort(const char *func, const char *file, int line, int error, 79 const char *failedexpr, const char *fmt, ...) __printflike(6, 7) __dead2; 80 81 #ifdef notyet 82 int pjdlog_receive(int sock); 83 #endif 84 85 #define pjdlogv_common(loglevel, debuglevel, error, fmt, ap) \ 86 _pjdlogv_common(__func__, __FILE__, __LINE__, (loglevel), \ 87 (debuglevel), (error), (fmt), (ap)) 88 #define pjdlog_common(loglevel, debuglevel, error, ...) \ 89 _pjdlog_common(__func__, __FILE__, __LINE__, (loglevel), \ 90 (debuglevel), (error), __VA_ARGS__) 91 92 #define pjdlogv(loglevel, fmt, ap) \ 93 pjdlogv_common((loglevel), 0, -1, (fmt), (ap)) 94 #define pjdlog(loglevel, ...) \ 95 pjdlog_common((loglevel), 0, -1, __VA_ARGS__) 96 97 #define pjdlogv_emergency(fmt, ap) pjdlogv(LOG_EMERG, (fmt), (ap)) 98 #define pjdlog_emergency(...) pjdlog(LOG_EMERG, __VA_ARGS__) 99 #define pjdlogv_alert(fmt, ap) pjdlogv(LOG_ALERT, (fmt), (ap)) 100 #define pjdlog_alert(...) pjdlog(LOG_ALERT, __VA_ARGS__) 101 #define pjdlogv_critical(fmt, ap) pjdlogv(LOG_CRIT, (fmt), (ap)) 102 #define pjdlog_critical(...) pjdlog(LOG_CRIT, __VA_ARGS__) 103 #define pjdlogv_error(fmt, ap) pjdlogv(LOG_ERR, (fmt), (ap)) 104 #define pjdlog_error(...) pjdlog(LOG_ERR, __VA_ARGS__) 105 #define pjdlogv_warning(fmt, ap) pjdlogv(LOG_WARNING, (fmt), (ap)) 106 #define pjdlog_warning(...) pjdlog(LOG_WARNING, __VA_ARGS__) 107 #define pjdlogv_notice(fmt, ap) pjdlogv(LOG_NOTICE, (fmt), (ap)) 108 #define pjdlog_notice(...) pjdlog(LOG_NOTICE, __VA_ARGS__) 109 #define pjdlogv_info(fmt, ap) pjdlogv(LOG_INFO, (fmt), (ap)) 110 #define pjdlog_info(...) pjdlog(LOG_INFO, __VA_ARGS__) 111 112 #define pjdlog_debug(debuglevel, ...) \ 113 pjdlog_common(LOG_DEBUG, (debuglevel), -1, __VA_ARGS__) 114 #define pjdlogv_debug(debuglevel, fmt, ap) \ 115 pjdlogv_common(LOG_DEBUG, (debuglevel), -1, (fmt), (ap)) 116 117 #define pjdlog_errno(loglevel, ...) \ 118 pjdlog_common((loglevel), 0, (errno), __VA_ARGS__) 119 #define pjdlogv_errno(loglevel, fmt, ap) \ 120 pjdlogv_common((loglevel), 0, (errno), (fmt), (ap)) 121 122 #define pjdlogv_exit(exitcode, fmt, ap) \ 123 _pjdlogv_exit(__func__, __FILE__, __LINE__, (exitcode), \ 124 (errno), (fmt), (ap)) 125 #define pjdlog_exit(exitcode, ...) \ 126 _pjdlog_exit(__func__, __FILE__, __LINE__, (exitcode), (errno), \ 127 __VA_ARGS__) 128 129 #define pjdlogv_exitx(exitcode, fmt, ap) \ 130 _pjdlogv_exit(__func__, __FILE__, __LINE__, (exitcode), -1, \ 131 (fmt), (ap)) 132 #define pjdlog_exitx(exitcode, ...) \ 133 _pjdlog_exit(__func__, __FILE__, __LINE__, (exitcode), -1, \ 134 __VA_ARGS__) 135 136 #define PJDLOG_VERIFY(expr) do { \ 137 if (!(expr)) { \ 138 _pjdlog_abort(__func__, __FILE__, __LINE__, -1, #expr, \ 139 __func__); \ 140 } \ 141 } while (0) 142 #define PJDLOG_RVERIFY(expr, ...) do { \ 143 if (!(expr)) { \ 144 _pjdlog_abort(__func__, __FILE__, __LINE__, -1, #expr, \ 145 __VA_ARGS__); \ 146 } \ 147 } while (0) 148 #define PJDLOG_EVERIFY(expr) do { \ 149 if (!(expr)) { \ 150 _pjdlog_abort(__func__, __FILE__, __LINE__, errno, \ 151 #expr, __func__); \ 152 } \ 153 } while (0) 154 #define PJDLOG_ABORT(...) _pjdlog_abort(__func__, __FILE__, \ 155 __LINE__, -1, NULL, __VA_ARGS__) 156 #ifdef NDEBUG 157 #define PJDLOG_ASSERT(expr) do { } while (0) 158 #define PJDLOG_RASSERT(...) do { } while (0) 159 #else 160 #define PJDLOG_ASSERT(expr) do { \ 161 if (!(expr)) { \ 162 _pjdlog_abort(__func__, __FILE__, __LINE__, -1, #expr, \ 163 __func__); \ 164 } \ 165 } while (0) 166 #define PJDLOG_RASSERT(expr, ...) do { \ 167 if (!(expr)) { \ 168 _pjdlog_abort(__func__, __FILE__, __LINE__, -1, #expr, \ 169 __VA_ARGS__); \ 170 } \ 171 } while (0) 172 #endif 173 174 #endif /* !_PJDLOG_H_ */ 175