Add logging multilevel system.
[bertos.git] / bertos / cfg / debug.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2003, 2004, 2005 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief Simple debug facilities for hosted and embedded C/C++ applications.
34  *
35  * Debug output goes to stderr in hosted applications.
36  * Freestanding (AKA embedded) applications use \c drv/kdebug.c to output
37  * diagnostic messages to a serial terminal or a JTAG debugger.
38  *
39  * \version $Id$
40  * \author Bernardo Innocenti <bernie@develer.com>
41  */
42
43 #ifndef BERTOS_DEBUG_H
44 #define BERTOS_DEBUG_H
45
46 #include <cfg/os.h>
47 #include <cfg/compiler.h>
48
49
50 /*
51  * Defaults for rarely used config stuff.
52  */
53 #ifndef CONFIG_KDEBUG_DISABLE_TRACE
54 #define CONFIG_KDEBUG_DISABLE_TRACE  0
55 #endif
56
57 #ifndef CONFIG_KDEBUG_ASSERT_NO_TEXT
58 #define CONFIG_KDEBUG_ASSERT_NO_TEXT  0
59 #endif
60
61 #if defined(__doxygen__)
62         /**
63          * Preprocessor symbol defined only for debug builds.
64          *
65          * The build infrastructure must arrange for _DEBUG to
66          * be predefined for all the source files being compiled.
67          *
68          * This is compatible with the MSVC convention for the
69          * default Debug and Release project targets.
70          */
71         #define _DEBUG 1
72 #endif
73
74 #ifdef _DEBUG
75         // STLport specific: enable extra checks
76         #define __STL_DEBUG 1
77
78         // MSVC specific: Enable memory allocation debug
79         #if defined(_MSC_VER)
80                 #include <crtdbg.h>
81         #endif
82
83         /*
84          * On UNIX systems the extabilished practice is to define
85          * NDEBUG for release builds and nothing for debug builds.
86          */
87         #ifdef NDEBUG
88         #undef NDEBUG
89         #endif
90
91         /**
92          * This macro duplicates the old MSVC trick of redefining
93          * THIS_FILE locally to avoid the overhead of many duplicate
94          * strings in ASSERT().
95          */
96         #ifndef THIS_FILE
97                 #define THIS_FILE  __FILE__
98         #endif
99
100         /**
101          * This macro can be used to conditionally exclude one or more
102          * statements conditioned on \c _DEBUG, avoiding the clutter
103          * of ifdef/endif pairs.
104          *
105          * \code
106          *     struct FooBar
107          *     {
108          *         int foo;
109          *         bool bar;
110          *         DB(int ref_count;) // Track number of users
111          *
112          *         void release()
113          *         {
114          *             DB(--ref_count;)
115          *         }
116          *     };
117          * \endcode
118          */
119         #define DB(x) x
120
121         #include "cfg/cfg_debug.h"   /* CONFIG_KDEBUG_ASSERT_NO_TEXT */
122         #include <cpu/attr.h>        /* CPU_HARVARD */
123
124         /* These are implemented in drv/kdebug.c */
125         void kdbg_init(void);
126         void kputchar(char c);
127         int kputnum(int num);
128         void kdump(const void *buf, size_t len);
129         void __init_wall(long *wall, int size);
130
131         #if CPU_HARVARD
132                 #include <mware/pgm.h>
133                 void kputs_P(const char *PROGMEM str);
134                 void kprintf_P(const char *PROGMEM fmt, ...) FORMAT(__printf__, 1, 2);
135                 int __assert_P(const char *PROGMEM cond, const char *PROGMEM file, int line);
136                 void __trace_P(const char *func);
137                 void __tracemsg_P(const char *func, const char *PROGMEM fmt, ...);
138                 int __invalid_ptr_P(void *p, const char *PROGMEM name, const char *PROGMEM file, int line);
139                 int __check_wall_P(long *wall, int size, const char * PGM_ATTR name, const char * PGM_ATTR file, int line);
140                 #define kputs(str)  kputs_P(PSTR(str))
141                 #define kprintf(fmt, ...)  kprintf_P(PSTR(fmt) ,## __VA_ARGS__)
142                 #define __assert(cond, file, line)  __assert_P(PSTR(cond), PSTR(file), (line))
143                 #define __trace(func)  __trace_P(func)
144                 #define __tracemsg(func, fmt, ...)  __tracemsg_P(func, PSTR(fmt), ## __VA_ARGS__)
145                 #define __invalid_ptr(p, name, file, line)  __invalid_ptr_P((p), PSTR(name), PSTR(file), (line))
146                 #define __check_wall(wall, size, name, file, line)  __check_wall_P(wall, size, PSTR(name), PSTR(file), (line))
147         #else /* !CPU_HARVARD */
148                 void kputs(const char *str);
149                 void kprintf(const char *fmt, ...) FORMAT(__printf__, 1, 2);
150                 int __assert(const char *cond, const char *file, int line);
151                 void __trace(const char *func);
152                 void __tracemsg(const char *func, const char *fmt, ...) FORMAT(__printf__, 2, 3);
153                 int __invalid_ptr(void *p, const char *name, const char *file, int line);
154                 int __check_wall(long *wall, int size, const char *name, const char *file, int line);
155         #endif /* !CPU_HARVARD */
156
157         #if !CONFIG_KDEBUG_ASSERT_NO_TEXT
158                 #define ASSERT(x)         ((void)(LIKELY(x) ? 0 : __assert(#x, THIS_FILE, __LINE__)))
159                 #define ASSERT2(x, help)  ((void)(LIKELY(x) ? 0 : __assert(help " (" #x ")", THIS_FILE, __LINE__)))
160         #else
161                 #define ASSERT(x)         ((void)(LIKELY(x) ? 0 : __assert("", THIS_FILE, __LINE__)))
162                 #define ASSERT2(x, help)  ((void)ASSERT(x))
163         #endif
164
165         /**
166          * Check that the given pointer is either NULL or pointing to valid memory.
167          *
168          * The assumption here is that valid pointers never point to low
169          * memory regions.  This helps catching pointers taken from
170          * struct/class memebers when the struct pointer was NULL.
171          */
172         #define ASSERT_VALID_PTR(p)         ((void)(LIKELY((p) >= 0x200) ? 0 : __invalid_ptr(p, #p, THIS_FILE, __LINE__)))
173
174         /**
175          * Check that the given pointer is not pointing to invalid memory.
176          *
177          * \see ASSERT_VALID_PTR()
178          */
179         #define ASSERT_VALID_PTR_OR_NULL(p) ((void)(LIKELY((p == NULL) || ((p) >= 0x200)) ? 0 : __invalid_ptr((p), #p, THIS_FILE, __LINE__)))
180
181         #if !CONFIG_KDEBUG_DISABLE_TRACE
182                 #define TRACE  __trace(__func__)
183                 #define TRACEMSG(msg,...) __tracemsg(__func__, msg, ## __VA_ARGS__)
184         #else
185                 #define TRACE  do {} while(0)
186                 #define TRACEMSG(...)  do {} while(0)
187         #endif
188
189         /**
190          * Multi level logging system.
191          *
192          * You can use these macro directy or using the cfg/log.h module
193          * that provide a simple interface for using the logging multilevel system.
194          * The priority level is order form error messages (hight priority) to info messages
195          * (low priority), so if you choose a low level log message you can see also all message
196          * that have a hight priority.
197          *
198          * \{
199          */
200         /// Logging level definition
201         #define DBG_LOG_ERROR     0
202         #define DBG_LOG_WARNING   1
203         #define DBG_LOG_INFO      2
204
205         /// Logging verbose mode
206         #define DBG_LOG_VERBOSE   1
207         #define DBG_LOG_SILENT    0
208
209         #define DBG_ERROR(debug_level, mode, str,...) \
210         do { \
211                         if(debug_level >= DBG_LOG_ERROR) \
212                         { \
213                                 if (mode == DBG_LOG_VERBOSE)\
214                                         kprintf("Error(%s():%d): "str, __func__, __LINE__, ## __VA_ARGS__); \
215                                 else \
216                                         kprintf("Error: "str,  ## __VA_ARGS__); \
217                         } \
218                         else \
219                         { \
220                                 /* NONE */ \
221                         } \
222         } while (0)
223
224         #define DBG_WARNING(debug_level, mode, str,...) \
225         do { \
226                         if(debug_level >= DBG_LOG_WARNING) \
227                         { \
228                                 if (mode == DBG_LOG_VERBOSE) \
229                                         kprintf("Warning(%s():%d): "str, __func__, __LINE__, ## __VA_ARGS__); \
230                                 else \
231                                         kprintf("Warning: "str,  ## __VA_ARGS__); \
232                         } \
233                         else \
234                         { \
235                                 /* NONE */ \
236                         } \
237         } while (0)
238
239         #define DBG_INFO(debug_level, mode, str,...) \
240         do { \
241                         if(debug_level >= DBG_LOG_INFO) \
242                         { \
243                                 if (mode == DBG_LOG_VERBOSE) \
244                                         kprintf("Info(%s():%d): "str, __func__, __LINE__, ## __VA_ARGS__); \
245                                 else \
246                                         kprintf("Info: "str,  ## __VA_ARGS__); \
247                         } \
248                         else \
249                         { \
250                                 /* NONE */ \
251                         } \
252         } while (0)
253         /* \} */
254
255         /**
256          * \name Walls to detect data corruption
257          * \{
258          */
259         #define WALL_SIZE                    8
260         #define WALL_VALUE                   (long)0xABADCAFEL
261         #define DECLARE_WALL(name,size)      long name[(size) / sizeof(long)];
262         #define FWD_DECLARE_WALL(name,size)  extern long name[(size) / sizeof(long)];
263         #define INIT_WALL(name)              __init_wall((name), countof(name))
264         #define CHECK_WALL(name)             __check_wall((name), countof(name), #name, THIS_FILE, __LINE__)
265         /*\}*/
266
267         /**
268          * Check that the given pointer actually points to an object
269          * of the specified type.
270          */
271         #define ASSERT_VALID_OBJ(_t, _o) do { \
272                 ASSERT_VALID_PTR((_o)); \
273                 ASSERT(dynamic_cast<_t>((_o)) != NULL); \
274         }
275
276         /**
277          * \name Debug object creation and destruction.
278          *
279          * These macros help track some kinds of leaks in C++ programs.
280          * Usage is as follows:
281          *
282          * \code
283          *   class Foo
284          *   {
285          *       DECLARE_INSTANCE_TRACKING(Foo)
286          *
287          *       Foo()
288          *       {
289          *           NEW_INSTANCE(Foo);
290          *           // ...
291          *       }
292          *
293          *       ~Foo()
294          *       {
295          *           DELETE_INSTANCE(Foo);
296          *           // ...
297          *       }
298          *   };
299          *
300          *   // Put this in the implementation file of the class
301          *   IMPLEMENT_INSTANCE_TRACKING(Foo)
302          *
303          *   // Client code
304          *   int main(void)
305          *   {
306          *        Foo *foo = new Foo;
307          *        cout << GET_INSTANCE_COUNT(Foo) << endl; // prints "1"
308          *        delete foo;
309          *        ASSERT_ZERO_INSTANCES(Foo); // OK
310          *   }
311          * \endcode
312          * \{
313          */
314         #define NEW_INSTANCE(CLASS)                do { ++CLASS::__instances } while (0)
315         #define DELETE_INSTANCE(CLASS)             do { --CLASS::__instances } while (0)
316         #define ASSERT_ZERO_INSTANCES(CLASS)       ASSERT(CLASS::__instances == 0)
317         #define GET_INSTANCE_COUNT(CLASS)          (CLASS::__instances)
318         #define DECLARE_INSTANCE_TRACKING(CLASS)   static int __instances
319         #define IMPLEMENT_INSTANCE_TRACKING(CLASS) int CLASS::__instances = 0
320         /*\}*/
321
322 #else /* !_DEBUG */
323
324         /*
325          * On UNIX systems the extabilished practice is to define
326          * NDEBUG for release builds and nothing for debug builds.
327          */
328         #ifndef NDEBUG
329         #define NDEBUG 1
330         #endif
331
332         #define DB(x)  /* nothing */
333         #ifndef ASSERT
334                 #define ASSERT(x)  ((void)0)
335         #endif /* ASSERT */
336         #define ASSERT2(x, help)             ((void)0)
337         #define ASSERT_VALID_PTR(p)          ((void)0)
338         #define ASSERT_VALID_PTR_OR_NULL(p)  ((void)0)
339         #define ASSERT_VALID_OBJ(_t, _o)     ((void)0)
340         #define TRACE                        do {} while (0)
341         #if COMPILER_VARIADIC_MACROS
342                 #define TRACEMSG(x, ...)         do {} while (0)
343         #else
344                 INLINE void TRACEMSG(UNUSED_ARG(const char *, msg), ...)
345                 {
346                         /* NOP */
347                 }
348         #endif
349
350         #define DECLARE_WALL(name, size)     /* nothing */
351         #define FWD_DECLARE_WALL(name, size) /* nothing */
352         #define INIT_WALL(name)              do {} while (0)
353         #define CHECK_WALL(name)             do {} while (0)
354
355         #define NEW_INSTANCE(CLASS)                do {} while (0)
356         #define DELETE_INSTANCE(CLASS)             do {} while (0)
357         #define ASSERT_ZERO_INSTANCES(CLASS)       do {} while (0)
358         #define GET_INSTANCE_COUNT(CLASS)          ERROR_ONLY_FOR_DEBUG
359         #define DECLARE_INSTANCE_TRACKING(CLASS)
360         #define IMPLEMENT_INSTANCE_TRACKING(CLASS)
361
362         INLINE void kdbg_init(void) { /* nop */ }
363         INLINE void kputchar(UNUSED_ARG(char, c)) { /* nop */ }
364         INLINE int kputnum(UNUSED_ARG(int, num)) { return 0; }
365         INLINE void kputs(UNUSED_ARG(const char *, str)) { /* nop */ }
366         INLINE void kdump(UNUSED_ARG(const void *, buf), UNUSED_ARG(size_t, len)) { /* nop */ }
367
368         #if defined(__cplusplus) && COMPILER_VARIADIC_MACROS
369                 /* G++ can't inline functions with variable arguments... */
370                 #define kprintf(fmt,...) do { (void)(fmt); } while(0)
371         #else
372                 /* ...but GCC can. */
373                 INLINE void kprintf(UNUSED_ARG(const char *, fmt), ...) { /* nop */ }
374         #endif
375
376 #endif /* _DEBUG */
377
378 #endif /* BERTOS_DEBUG_H */