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