1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2016 Michael Zhilin <mizhka@gmail.com> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer, 12 * without modification. 13 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 14 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any 15 * redistribution must be conditioned upon including a substantially 16 * similar Disclaimer requirement for further binary redistribution. 17 * 18 * NO WARRANTY 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY 22 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 23 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, 24 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 27 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 29 * THE POSSIBILITY OF SUCH DAMAGES. 30 */ 31 32 /* $FreeBSD$ */ 33 34 /* 35 * This file provides set of macros for logging: 36 * - BHND_<LEVEL> and 37 * - BHND_<LEVEL>_DEV 38 * where LEVEL = {ERROR,WARN,INFO,DEBUG} 39 * 40 * BHND_<LEVEL> macros is proxies to printf call and accept same parameters, 41 * for instance: 42 * BHND_INFO("register %d has value %d", reg, val); 43 * 44 * BHND_<LEVEL>_DEV macros is proxies to device_printf call and accept 45 * same parameters, for instance: 46 * BHND_INFO_DEV(dev, "register %d has value %d", reg, val); 47 * 48 * All macros contains newline char at the end of each call 49 * 50 * ERROR, WARN, INFO messages are printed only if: 51 * - log message level is lower than BHND_LOGGING (logging threshold) 52 * 53 * DEBUG, TRACE messages are printed only if: 54 * - bootverbose and 55 * - log message level is lower than BHND_LOGGING (logging threshold) 56 * 57 * In addition, for debugging purpose log message contains information about 58 * file name and line number if BHND_LOGGING is more than BHND_INFO_LEVEL 59 * 60 * NOTE: macros starting with underscore (_) are private and should be not used 61 * 62 * To override logging (for instance, force tracing), you can use: 63 * - "options BHND_LOGLEVEL=BHND_TRACE_LEVEL" in kernel configuration 64 * - "#define BHND_LOGGING BHND_TRACE_LEVEL" in source code file 65 * 66 * NOTE: kernel config option doesn't override log level defined on file level, 67 * so try to avoid "#define BHND_LOGGING" 68 */ 69 70 #ifndef _BHND_BHND_DEBUG_H_ 71 #define _BHND_BHND_DEBUG_H_ 72 73 #include <sys/systm.h> 74 75 #define BHND_ERROR_LEVEL 0x00 76 #define BHND_ERROR_MSG "ERROR" 77 #define BHND_WARN_LEVEL 0x10 78 #define BHND_WARN_MSG "!WARN" 79 #define BHND_INFO_LEVEL 0x20 80 #define BHND_INFO_MSG " info" 81 #define BHND_DEBUG_LEVEL 0x30 82 #define BHND_DEBUG_MSG "debug" 83 #define BHND_TRACE_LEVEL 0x40 84 #define BHND_TRACE_MSG "trace" 85 86 #if !(defined(BHND_LOGGING)) 87 #if !(defined(BHND_LOGLEVEL)) 88 /* By default logging will print only INFO+ message*/ 89 #define BHND_LOGGING BHND_INFO_LEVEL 90 #else /* defined(BHND_LOGLEVEL) */ 91 /* Kernel configuration specifies logging level */ 92 #define BHND_LOGGING BHND_LOGLEVEL 93 #endif /* !(defined(BHND_LOGLEVEL)) */ 94 #endif /* !(defined(BHND_LOGGING)) */ 95 96 #if BHND_LOGGING > BHND_INFO_LEVEL 97 #define _BHND_PRINT(fn, level, fmt, ...) \ 98 do { \ 99 if (level##LEVEL < BHND_DEBUG_LEVEL || bootverbose) \ 100 fn "[BHND " level##MSG "] %s:%d => " fmt "\n", \ 101 __func__, __LINE__, ## __VA_ARGS__); \ 102 } while(0); 103 #else /* BHND_LOGGING <= BHND_INFO_LEVEL */ 104 #define _BHND_PRINT(fn, level, fmt, ...) \ 105 do { \ 106 if (level##LEVEL < BHND_DEBUG_LEVEL || bootverbose) \ 107 fn "bhnd: " fmt "\n", ## __VA_ARGS__); \ 108 } while(0); 109 #endif /* BHND_LOGGING > BHND_INFO_LEVEL */ 110 111 #define _BHND_RAWPRINTFN printf( 112 #define _BHND_DEVPRINTFN(dev) device_printf(dev, 113 114 #define _BHND_LOGPRINT(level, fmt, ...) \ 115 _BHND_PRINT(_BHND_RAWPRINTFN, level, fmt, ## __VA_ARGS__) 116 #define _BHND_DEVPRINT(dev, level, fmt, ...) \ 117 _BHND_PRINT(_BHND_DEVPRINTFN(dev), level, fmt, ## __VA_ARGS__) 118 119 #define BHND_ERROR(fmt, ...) \ 120 _BHND_LOGPRINT(BHND_ERROR_, fmt, ## __VA_ARGS__); 121 #define BHND_ERROR_DEV(dev, fmt, ...) \ 122 _BHND_DEVPRINT(dev, BHND_ERROR_, fmt, ## __VA_ARGS__) 123 124 #if BHND_LOGGING >= BHND_WARN_LEVEL 125 #define BHND_WARN(fmt, ...) \ 126 _BHND_LOGPRINT(BHND_WARN_, fmt, ## __VA_ARGS__) 127 #define BHND_WARN_DEV(dev, fmt, ...) \ 128 _BHND_DEVPRINT(dev, BHND_WARN_, fmt, ## __VA_ARGS__) 129 130 #if BHND_LOGGING >= BHND_INFO_LEVEL 131 #define BHND_INFO(fmt, ...) \ 132 _BHND_LOGPRINT(BHND_INFO_, fmt, ## __VA_ARGS__) 133 #define BHND_INFO_DEV(dev, fmt, ...) \ 134 _BHND_DEVPRINT(dev, BHND_INFO_, fmt, ## __VA_ARGS__) 135 136 #if BHND_LOGGING >= BHND_DEBUG_LEVEL 137 #define BHND_DEBUG(fmt, ...) \ 138 _BHND_LOGPRINT(BHND_DEBUG_, fmt, ## __VA_ARGS__) 139 #define BHND_DEBUG_DEV(dev, fmt, ...) \ 140 _BHND_DEVPRINT(dev, BHND_DEBUG_, fmt, ## __VA_ARGS__) 141 142 #if BHND_LOGGING >= BHND_TRACE_LEVEL 143 #define BHND_TRACE(fmt, ...) \ 144 _BHND_LOGPRINT(BHND_TRACE_, fmt, ## __VA_ARGS__) 145 #define BHND_TRACE_DEV(dev, fmt, ...) \ 146 _BHND_DEVPRINT(dev, BHND_TRACE_, fmt, ## __VA_ARGS__) 147 148 #endif /* BHND_LOGGING >= BHND_TRACE_LEVEL */ 149 #endif /* BHND_LOGGING >= BHND_DEBUG_LEVEL */ 150 #endif /* BHND_LOGGING >= BHND_INFO_LEVEL */ 151 #endif /* BHND_LOGGING >= BHND_WARN_LEVEL */ 152 153 /* 154 * Empty defines without device context 155 */ 156 #if !(defined(BHND_WARN)) 157 #define BHND_WARN(fmt, ...); 158 #endif 159 160 #if !(defined(BHND_INFO)) 161 #define BHND_INFO(fmt, ...); 162 #endif 163 164 #if !(defined(BHND_DEBUG)) 165 #define BHND_DEBUG(fmt, ...); 166 #endif 167 168 #if !(defined(BHND_TRACE)) 169 #define BHND_TRACE(fmt, ...); 170 #endif 171 172 /* 173 * Empty defines with device context 174 */ 175 #if !(defined(BHND_WARN_DEV)) 176 #define BHND_WARN_DEV(dev, fmt, ...); 177 #endif 178 179 #if !(defined(BHND_INFO_DEV)) 180 #define BHND_INFO_DEV(dev, fmt, ...); 181 #endif 182 183 #if !(defined(BHND_DEBUG_DEV)) 184 #define BHND_DEBUG_DEV(dev, fmt, ...); 185 #endif 186 187 #if !(defined(BHND_TRACE_DEV)) 188 #define BHND_TRACE_DEV(dev, fmt, ...); 189 #endif 190 191 #endif /* _BHND_BHND_DEBUG_H_ */ 192