Qualia  0.2
error.h
Go to the documentation of this file.
1 /*
2  * error.h
3  *
4  * Error/warning/notification routines/macros.
5  *
6  * This file is part of Qualia https://github.com/sofian/qualia
7  *
8  * (c) 2011 Sofian Audry -- info(@)sofianaudry(.)com
9  * This file was originally part of Drone (http://drone.ws)
10  * (c) 2004 Mathieu Guindon, Julien Keable, Jean-Sebastien Senecal
11  * Error messages code adapted from Torch
12  * (c) 2004 Ronan Collobert (collober@idiap.ch)
13  *
14  * This program is free software: you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation, either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program. If not, see <http://www.gnu.org/licenses/>.
26  */
27 #ifndef ERROR_INCLUDED
28 #define ERROR_INCLUDED
29 
40 #include <qualia/core/common.h>
41 
42 // # Macro definitions #####################################################
43 
44 // Force DEBUG_LEVEL_NODEBUG when running in AVR/Arduino mode
45 #if (! is_computer())
46  #ifdef DEBUG_LEVEL
47  #warning "DEBUG_LEVEL is not supported on AVR/Arduino: forcing DEBUG_LEVEL_NODEBUG"
48  #undef DEBUG_LEVEL
49  #endif
50  #define DEBUG_LEVEL DEBUG_LEVEL_NODEBUG
51 #endif
52 
53 // Debug levels.
54 
56 #define DEBUG_LEVEL_NODEBUG -1
57 
59 #define DEBUG_LEVEL_ERROR 0
60 
62 #define DEBUG_LEVEL_WARNING 1
63 
65 #define DEBUG_LEVEL_NOTICE 2
66 
67 // Default debug level.
68 #ifndef DEBUG_LEVEL
69  #define DEBUG_LEVEL DEBUG_LEVEL_ERROR
70 #endif
71 
72 // Quick condition checkers.
73 #define DEBUG_ERROR (DEBUG_LEVEL >= DEBUG_LEVEL_ERROR)
74 #define DEBUG_WARNING (DEBUG_LEVEL >= DEBUG_LEVEL_WARNING)
75 #define DEBUG_NOTICE (DEBUG_LEVEL >= DEBUG_LEVEL_NOTICE)
76 
77 // Redefine __STRING just to make sure.
78 #ifndef __STRING
79 #define __STRING(x) #x
80 #endif
81 
82 // This macro is absolutely NOT SAFE! NEVER USE IT ALONE!!!
83 // Use ASSERT_ERROR, ASSERT_WARNING and ASSERT_NOTICE instead, below.
84 #define __DEBUG_TRIGGER_ASSERT(expr, func) \
85 ((expr) ? static_cast<void>(0) : func("Fail: " __STRING(expr)) );
86 
87 // Dummy instruction (does nothing).
88 #define __DEBUG_DUMMY_INSTRUCTION (static_cast<void>(0))
89 
90 //#define dummymsg static_cast<void>(0);
91 
92 // # Message functions #####################################################
93 
94 // # Assertions and messages functions #####################################
95 
96 #if DEBUG_ERROR
97 
99  void Q_ERROR(const char* msg, ...);
100 
102  void Q_ASSERT_ERROR_MESSAGE(bool expr, const char* msg, ...);
103 
105  #define Q_ASSERT_ERROR(expr) __DEBUG_TRIGGER_ASSERT(expr, Q_ERROR)
106 
107 #else
108 
109  #define Q_ERROR(...) __DEBUG_DUMMY_INSTRUCTION
110  #define Q_ASSERT_ERROR_MESSAGE(expr, ...) __DEBUG_DUMMY_INSTRUCTION
111  #define Q_ASSERT_ERROR(expr) __DEBUG_DUMMY_INSTRUCTION
112 
113 #endif
114 
115 #if DEBUG_WARNING
116 
118  void Q_WARNING(const char* msg, ...);
119 
121  void Q_ASSERT_WARNING_MESSAGE(bool expr, const char* msg, ...);
122 
124  #define Q_ASSERT_WARNING(expr) __DEBUG_TRIGGER_ASSERT(expr, Q_WARNING)
125 
126 #else
127 
128  #define Q_WARNING(...) __DEBUG_DUMMY_INSTRUCTION
129  #define Q_ASSERT_WARNING_MESSAGE(expr, ...) __DEBUG_DUMMY_INSTRUCTION
130  #define Q_ASSERT_WARNING(expr) __DEBUG_DUMMY_INSTRUCTION
131 
132 #endif
133 
134 #if DEBUG_NOTICE
135 
137  void Q_NOTICE(const char* msg, ...);
138 
140  void Q_ASSERT_NOTICE_MESSAGE(bool expr, const char* msg, ...);
141 
143  #define Q_ASSERT_NOTICE(expr) __DEBUG_TRIGGER_ASSERT(expr, Q_NOTICE)
144 
145 #else
146 
147  #define Q_NOTICE(...) __DEBUG_DUMMY_INSTRUCTION
148  #define Q_ASSERT_NOTICE_MESSAGE(expr, ...) __DEBUG_DUMMY_INSTRUCTION
149  #define Q_ASSERT_NOTICE(expr) __DEBUG_DUMMY_INSTRUCTION
150 
151 #endif
152 
153 #if (is_computer())
154 
155  void Q_PRINT(const char* msg, ...);
156 
158  void Q_MESSAGE(const char* msg, ...);
159 #else
160  #define Q_PRINT(...) __DEBUG_DUMMY_INSTRUCTION
161  #define Q_MESSAGE(...) __DEBUG_DUMMY_INSTRUCTION
162 #endif
163 
164 #endif