4 \brief Class bbtkMessageManager and Macros for outputing messages in bbtk
6 There are 4 kinds of messages :
7 - Messages (normal messages)
8 - Debug messages (not compiled in release)
11 There are also "types" of messages which are strings which identify the nature of the message
12 (for example : "Core" messages are generated by the core classes of the librairy, there can be a type of
13 message for each type of Node, and so on...)
14 A type of message must be declared by registering it into the MessageManager. This is done by a line like :
15 bbtk::MessageManager::RegisterMessageType("Core","Messages generated by the core classes of the library",5);
17 -The first string is the type of the message (the keyword which will be used to generate a message of this type)
18 -The second string is help string
19 -The integer is the initial level for the messages of this type (see below).
21 To generate a message of a known type then use one of the macros :
22 bbtkMessage, bbtkDebugMessage, bbtkWarning, bbtkError or their variants.
26 bbtkMessage("Core",4,"problem with "<<GetName()<<bbtkendl);
28 will push the 3rd argument in std::cout if the message level of "Core" messages is greater or equal to 4.
29 which means that it generates a message of level 4 (0 : very important/always displayed ... 9 : deep debug message).
31 At run time, one is able to change the level of the messages displayed by using a command like :
33 bbtk::MessageManager::SetMessageLevel("Core",5);
35 which tells the manager to display all Core messages of level up to 5.
39 bbtk*Cont : continues a previous bbtkMessage on the same line (without rewriting the type and level)
40 bbtk*Inc / Dec : displays the message and then increments/decrement the messages tabulation
43 //===========================================================
45 \class bbtk::MessageManager
46 \brief Manages the messages displayed by bbtk
50 #ifndef __bbtkMessageManager_h__
51 #define __bbtkMessageManager_h__
53 // The do { } while(0) statement in macros is made to "swallow the semicolon"
54 // see http://gcc.gnu.org/onlinedocs/cpp/Swallowing-the-Semicolon.html#Swallowing-the-Semicolon
56 #include "bbtkSystem.h"
64 // Comment out these symbols to prevent compilation
65 //#define BBTK_COMPILE_MESSAGES
66 //#define BBTK_COMPILE_DEBUG_MESSAGES
67 //#define BBTK_COMPILE_WARNING_MESSAGES
68 //#define BBTK_COMPILE_ERROR_MESSAGES
71 #define bbtkOnMessageLevel(key,value) \
72 int __bbtkOnMessageLevelVariable = \
73 bbtk::MessageManager::GetMessageLevel(key); \
74 if ( __bbtkOnMessageLevelVariable<0) \
76 bbtkWarning("message type '"<<key<<"' unknown"); \
78 else if (value<= __bbtkOnMessageLevelVariable)
80 #ifdef BBTK_PREPEND_MESSAGE_WITH_CODE
81 #define bbtkMessageCode \
82 key[0] << key[1] << key[2] << value << " "
84 #define bbtkMessageCode ""
87 #ifdef BBTK_PREPEND_MESSAGE_WITH_TAB
88 #define bbtkMessageTab \
89 bbtk::MessageManager::GetTab()
91 #define bbtkMessageTab ""
94 #define BBTK_PREPEND_MESSAGE_WITH_SPACE
95 #ifdef BBTK_PREPEND_MESSAGE_WITH_SPACE
96 #define bbtkMessageSpace(value) \
97 bbtk::MessageManager::GetSpace(value)
99 #define bbtkMessageSpace(value) ""
103 //===========================================================
104 #ifdef BBTK_COMPILE_MESSAGES
106 // Macro for messages
107 #define bbtkMessage(key,value,MESSAGE) \
109 bbtkOnMessageLevel(key,value) \
111 std::cout << bbtkMessageCode \
113 << bbtkMessageSpace(value) \
119 // Macro for continuing a message (when one wants to split the macro
120 // call into multiple lines)
121 #define bbtkMessageCont(key,value,MESSAGE) \
124 bbtkOnMessageLevel(key,value) \
126 std::cout << MESSAGE; \
131 #define bbtkMessageInc(key,value,MESSAGE) \
134 bbtkOnMessageLevel(key,value) \
136 std::cout << bbtkMessageCode \
138 << bbtkMessageSpace(value) \
140 bbtk::MessageManager::IncTab(); \
145 #define bbtkMessageDec(key,value,MESSAGE) \
148 bbtkOnMessageLevel(key,value) \
150 bbtk::MessageManager::DecTab(); \
151 std::cout << bbtkMessageCode \
153 << bbtkMessageSpace(value) \
159 #define bbtkDecTab(key,value) \
162 bbtkOnMessageLevel(key,value) \
164 bbtk::MessageManager::DecTab(); \
169 #define bbtkIncTab(key,value) \
172 bbtkOnMessageLevel(key,value) \
174 bbtk::MessageManager::IncTab(); \
179 #define bbtkResetTab() \
182 bbtk::MessageManager::ResetTab(); \
187 #define bbtkMessage(key,value,MESSAGE)
188 #define bbtkMessageInc(key,value,MESSAGE)
189 #define bbtkMessageDec(key,value,MESSAGE)
190 #define bbtkMessageCont(key,value,MESSAGE)
191 #define bbtkDecTab(key,value)
192 #define bbtkIncTab(key,value)
193 #define bbtkResetTab()
195 //===========================================================
199 //===========================================================
200 #ifdef BBTK_COMPILE_DEBUG_MESSAGES
202 // Macro for debug messages
203 #define bbtkDebugMessage(key,value,MESSAGE) \
206 bbtkOnMessageLevel(key,value) \
208 std::cout << bbtkMessageCode \
210 << bbtkMessageSpace(value) \
216 // Macro for continuing a debug message (when one wants to split the
217 // macro call into multiple lines)
218 #define bbtkDebugMessageCont(key,value,MESSAGE) \
221 bbtkOnMessageLevel(key,value) \
223 std::cout << MESSAGE; \
228 #define bbtkDebugMessageInc(key,value,MESSAGE) \
231 bbtkOnMessageLevel(key,value) \
233 std::cout << bbtkMessageCode \
235 << bbtkMessageSpace(value) \
237 bbtk::MessageManager::IncTab(); \
242 #define bbtkDebugMessageDec(key,value,MESSAGE) \
245 bbtkOnMessageLevel(key,value) \
247 bbtk::MessageManager::DecTab(); \
248 std::cout << bbtkMessageCode \
250 << bbtkMessageSpace(value) \
256 #define bbtkDebugDecTab(key,value) \
259 bbtkOnMessageLevel(key,value) \
261 bbtk::MessageManager::DecTab(); \
266 #define bbtkDebugIncTab(key,value) \
269 bbtkOnMessageLevel(key,value) \
271 bbtk::MessageManager::IncTab(); \
276 #define bbtkDebugResetTab() \
279 bbtk::MessageManager::ResetTab(); \
284 #define bbtkDebugMessage(key,value,MESSAGE)
285 #define bbtkDebugMessageCont(key,value,MESSAGE)
286 #define bbtkDebugMessageInc(key,value,MESSAGE)
287 #define bbtkDebugMessageDec(key,value,MESSAGE)
288 #define bbtkDebugDecTab(key,value)
289 #define bbtkDebugIncTab(key,value)
291 //===========================================================
293 //===========================================================
294 #ifdef BBTK_COMPILE_WARNING_MESSAGES
295 #define bbtkWarning(MESSAGE) \
298 int lev = bbtk::MessageManager::GetMessageLevel("Warning"); \
301 std::cerr << "!! WARNING !! " << MESSAGE << std::endl; \
304 std::cerr << "!! WARNING !! In file '"<<__FILE__ \
305 <<"' ; Line "<<__LINE__<<std::endl; \
312 #define bbtkWarning(MESSAGE)
314 //===========================================================
317 //===========================================================
318 #ifdef BBTK_COMPILE_ERROR_MESSAGES
320 #define bbtkError(MESSAGE) \
323 bbtk::wx::ResetCursor(); \
324 std::ostringstream s; \
326 std::ostringstream f; \
327 f << __FILE__ << " (l."<<__LINE__<<")"; \
328 bbtk::Exception e( BBTK_GET_CURRENT_OBJECT_NAME, \
335 #define bbtkGlobalError(MESSAGE) \
338 std::ostringstream s; \
340 std::ostringstream f; \
341 f << __FILE__ << " (l."<<__LINE__<<")"; \
342 bbtk::Exception e( "global scope", \
349 #define BBTK_INTERNAL_ERROR_MESSAGE \
350 "\n\n***********************************************\n**** THIS IS AN INTERNAL ERROR TO BBTK ****\n**** Please send a full bug report to : ****\n**** bbtk-developers@creatis.insa-lyon.fr ****\n***********************************************\n\n"
352 #define bbtkInternalError(MESSAGE) \
355 std::ostringstream s; \
356 s << MESSAGE << BBTK_INTERNAL_ERROR_MESSAGE; \
357 std::ostringstream f; \
358 f << __FILE__ << " (l."<<__LINE__<<")"; \
359 bbtk::Exception e( BBTK_GET_CURRENT_OBJECT_NAME, \
367 #define bbtkError(MESSAGE)
368 #define bbtkGlobalError(MESSAGE)
369 #define bbtkInternalError(MESSAGE)
371 //===========================================================
373 //===========================================================
374 #define bbtkendl std::endl
375 //===========================================================
381 class BBTK_EXPORT MessageManager
389 static MessageManager* GetInstance();
391 static void RegisterMessageType(std::string key,
393 unsigned char default_level = 9);
395 static void SetMessageLevel(std::string key, unsigned char level);
397 static int GetMessageLevel(std::string key);
399 static std::string& GetTab() { static std::string s; return s; }
401 static std::string GetSpace(int n) {
402 std::string s; s.insert(0," ",n); return s; }
404 static void IncTab() { GetTab() += std::string(" "); }
406 static void DecTab() { GetTab() = GetTab().substr(0,GetTab().length()-1); }
408 static void ResetTab() { GetTab() = std::string(""); }
410 static void PrintInfo();
413 std::map<std::string,int> mMessageLevel;
414 std::map<std::string,std::string> mMessageHelp;
415 unsigned int mMaxMessageLength;
418 //===========================================================
422 #include "bbtkException.h"