xref: /freebsd/sys/dev/bhnd/bhnd_debug.h (revision 525fe93dc7487a1e63a90f6a2b956abc601963c1)
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 
33 /*
34  * This file provides set of macros for logging:
35  *  - BHND_<LEVEL> and
36  *  - BHND_<LEVEL>_DEV
37  * where LEVEL = {ERROR,WARN,INFO,DEBUG}
38  *
39  * BHND_<LEVEL> macros is proxies to printf call and accept same parameters,
40  * for instance:
41  * 	BHND_INFO("register %d has value %d", reg, val);
42  *
43  * BHND_<LEVEL>_DEV macros is proxies to device_printf call and accept
44  * same parameters, for instance:
45  * 	BHND_INFO_DEV(dev, "register %d has value %d", reg, val);
46  *
47  * All macros contains newline char at the end of each call
48  *
49  * ERROR, WARN, INFO messages are printed only if:
50  * 	- log message level is lower than BHND_LOGGING (logging threshold)
51  *
52  * DEBUG, TRACE messages are printed only if:
53  * 	- bootverbose and
54  * 	- log message level is lower than BHND_LOGGING (logging threshold)
55  *
56  * In addition, for debugging purpose log message contains information about
57  * file name and line number if BHND_LOGGING is more than BHND_INFO_LEVEL
58  *
59  * NOTE: macros starting with underscore (_) are private and should be not used
60  *
61  * To override logging (for instance, force tracing), you can use:
62  *  - "options BHND_LOGLEVEL=BHND_TRACE_LEVEL" in kernel configuration
63  *  - "#define	BHND_LOGGING	BHND_TRACE_LEVEL" in source code file
64  *
65  * NOTE: kernel config option doesn't override log level defined on file level,
66  * so try to avoid "#define	BHND_LOGGING"
67  */
68 
69 #ifndef _BHND_BHND_DEBUG_H_
70 #define _BHND_BHND_DEBUG_H_
71 
72 #include <sys/systm.h>
73 
74 #define	BHND_ERROR_LEVEL	0x00
75 #define	BHND_ERROR_MSG		"ERROR"
76 #define	BHND_WARN_LEVEL		0x10
77 #define	BHND_WARN_MSG		"!WARN"
78 #define	BHND_INFO_LEVEL		0x20
79 #define	BHND_INFO_MSG		" info"
80 #define	BHND_DEBUG_LEVEL	0x30
81 #define	BHND_DEBUG_MSG		"debug"
82 #define	BHND_TRACE_LEVEL	0x40
83 #define	BHND_TRACE_MSG		"trace"
84 
85 #if !(defined(BHND_LOGGING))
86 #if !(defined(BHND_LOGLEVEL))
87 /* By default logging will print only INFO+ message*/
88 #define	BHND_LOGGING		BHND_INFO_LEVEL
89 #else /* defined(BHND_LOGLEVEL) */
90 /* Kernel configuration specifies logging level */
91 #define	BHND_LOGGING		BHND_LOGLEVEL
92 #endif /* !(defined(BHND_LOGLEVEL)) */
93 #endif /* !(defined(BHND_LOGGING)) */
94 
95 #if BHND_LOGGING > BHND_INFO_LEVEL
96 #define	_BHND_PRINT(fn, level, fmt, ...)				\
97 	do {								\
98 		if (level##LEVEL < BHND_DEBUG_LEVEL || bootverbose)	\
99 		    fn "[BHND " level##MSG "] %s:%d => " fmt "\n",	\
100 			__func__, __LINE__, ## __VA_ARGS__);		\
101 	} while(0);
102 #else /* BHND_LOGGING <= BHND_INFO_LEVEL */
103 #define	_BHND_PRINT(fn, level, fmt, ...)				\
104 	do {								\
105 		if (level##LEVEL < BHND_DEBUG_LEVEL || bootverbose)	\
106 		    fn "bhnd: " fmt "\n", ## __VA_ARGS__);		\
107 	} while(0);
108 #endif /* BHND_LOGGING > BHND_INFO_LEVEL */
109 
110 #define	_BHND_RAWPRINTFN	printf(
111 #define	_BHND_DEVPRINTFN(dev)	device_printf(dev,
112 
113 #define	_BHND_LOGPRINT(level, fmt, ...) 				\
114 	_BHND_PRINT(_BHND_RAWPRINTFN, level, fmt, ## __VA_ARGS__)
115 #define	_BHND_DEVPRINT(dev, level, fmt, ...)				\
116 	_BHND_PRINT(_BHND_DEVPRINTFN(dev), level, fmt, ## __VA_ARGS__)
117 
118 #define	BHND_ERROR(fmt, ...)						\
119 	_BHND_LOGPRINT(BHND_ERROR_, fmt, ## __VA_ARGS__);
120 #define	BHND_ERROR_DEV(dev, fmt, ...)					\
121 	_BHND_DEVPRINT(dev, BHND_ERROR_, fmt, ## __VA_ARGS__)
122 
123 #if BHND_LOGGING >= BHND_WARN_LEVEL
124 #define	BHND_WARN(fmt, ...)						\
125 	_BHND_LOGPRINT(BHND_WARN_, fmt, ## __VA_ARGS__)
126 #define	BHND_WARN_DEV(dev, fmt, ...)					\
127 	_BHND_DEVPRINT(dev, BHND_WARN_, fmt, ## __VA_ARGS__)
128 
129 #if BHND_LOGGING >= BHND_INFO_LEVEL
130 #define	BHND_INFO(fmt, ...)						\
131 	_BHND_LOGPRINT(BHND_INFO_, fmt, ## __VA_ARGS__)
132 #define	BHND_INFO_DEV(dev, fmt, ...)					\
133 	_BHND_DEVPRINT(dev, BHND_INFO_, fmt, ## __VA_ARGS__)
134 
135 #if BHND_LOGGING >= BHND_DEBUG_LEVEL
136 #define	BHND_DEBUG(fmt, ...)						\
137 	_BHND_LOGPRINT(BHND_DEBUG_, fmt, ## __VA_ARGS__)
138 #define	BHND_DEBUG_DEV(dev, fmt, ...)					\
139 	_BHND_DEVPRINT(dev, BHND_DEBUG_, fmt, ## __VA_ARGS__)
140 
141 #if BHND_LOGGING >= BHND_TRACE_LEVEL
142 #define	BHND_TRACE(fmt, ...)						\
143 	_BHND_LOGPRINT(BHND_TRACE_, fmt, ## __VA_ARGS__)
144 #define	BHND_TRACE_DEV(dev, fmt, ...)					\
145 	_BHND_DEVPRINT(dev, BHND_TRACE_, fmt, ## __VA_ARGS__)
146 
147 #endif /* BHND_LOGGING >= BHND_TRACE_LEVEL */
148 #endif /* BHND_LOGGING >= BHND_DEBUG_LEVEL */
149 #endif /* BHND_LOGGING >= BHND_INFO_LEVEL */
150 #endif /* BHND_LOGGING >= BHND_WARN_LEVEL */
151 
152 /*
153  * Empty defines without device context
154  */
155 #if !(defined(BHND_WARN))
156 #define	BHND_WARN(fmt, ...);
157 #endif
158 
159 #if !(defined(BHND_INFO))
160 #define	BHND_INFO(fmt, ...);
161 #endif
162 
163 #if !(defined(BHND_DEBUG))
164 #define	BHND_DEBUG(fmt, ...);
165 #endif
166 
167 #if !(defined(BHND_TRACE))
168 #define	BHND_TRACE(fmt, ...);
169 #endif
170 
171 /*
172  * Empty defines with device context
173  */
174 #if !(defined(BHND_WARN_DEV))
175 #define	BHND_WARN_DEV(dev, fmt, ...);
176 #endif
177 
178 #if !(defined(BHND_INFO_DEV))
179 #define	BHND_INFO_DEV(dev, fmt, ...);
180 #endif
181 
182 #if !(defined(BHND_DEBUG_DEV))
183 #define	BHND_DEBUG_DEV(dev, fmt, ...);
184 #endif
185 
186 #if !(defined(BHND_TRACE_DEV))
187 #define	BHND_TRACE_DEV(dev, fmt, ...);
188 #endif
189 
190 #endif /* _BHND_BHND_DEBUG_H_ */
191