2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
10 # This software is governed by the CeCILL-B license under French law and
11 # abiding by the rules of distribution of free software. You can use,
12 # modify and/ or redistribute the software under the terms of the CeCILL-B
13 # license as circulated by CEA, CNRS and INRIA at the following URL
14 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15 # or in the file LICENSE.txt.
17 # As a counterpart to the access to the source code and rights to copy,
18 # modify and redistribute granted by the license, users are provided only
19 # with a limited warranty and the software's author, the holder of the
20 # economic rights, and the successive licensors have only limited
23 # The fact that you are presently reading this means that you have had
24 # knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------ */
28 /*=========================================================================
30 Module: $RCSfile: bbtkMessageManager.h,v $
32 Date: $Date: 2012/11/16 08:49:01 $
33 Version: $Revision: 1.10 $
34 =========================================================================*/
42 \brief Class bbtkMessageManager and Macros for outputing messages in bbtk
44 There are 4 kinds of messages :
45 - Messages (normal messages)
46 - Debug messages (not compiled in release)
49 There are also "types" of messages which are strings which identify the nature of the message
50 (for example : "Kernel" messages are generated by the core classes of the library, there can be a type of
51 message for each type of Node, and so on...)
52 A type of message must be declared by registering it into the MessageManager. This is done by a line like :
53 bbtk::MessageManager::RegisterMessageType("Kernel","Messages generated by the core classes of the library",5);
55 -The first string is the type of the message (the category which will be used to generate a message of this type)
56 -The second string is help string
57 -The integer is the initial level for the messages of this type (see below).
59 To generate a message of a known type then use one of the macros :
60 bbtkMessage, bbtkDebugMessage, bbtkWarning, bbtkError or their variants.
64 bbtkMessage("Kernel",4,"problem with "<<GetName()<<bbtkendl);
66 will push the 3rd argument in std::cout if the message level of "Kernel" messages is greater or equal to 4.
67 which means that it generates a message of level 4 (0 : very important/always displayed ... 9 : deep debug message).
69 At run time, one is able to change the level of the messages displayed by using a command like :
71 bbtk::MessageManager::SetMessageLevel("Kernel",5);
73 which tells the manager to display all Kernel messages of level up to 5.
77 bbtk*Cont : continues a previous bbtkMessage on the same line (without rewriting the type and level)
78 bbtk*Inc / Dec : displays the message and then increments/decrement the messages tabulation
81 //===========================================================
83 \class bbtk::MessageManager
84 \brief Manages the messages displayed by bbtk
88 #ifndef __bbtkMessageManager_h__
89 #define __bbtkMessageManager_h__
91 // The do { } while(0) statement in macros is made to "swallow the semicolon"
92 // see http://gcc.gnu.org/onlinedocs/cpp/Swallowing-the-Semicolon.html#Swallowing-the-Semicolon
94 #include "bbtkSystem.h"
102 // Comment out these symbols to prevent compilation
103 //#define BBTK_COMPILE_MESSAGES
104 //#define BBTK_COMPILE_DEBUG_MESSAGES
105 //#define BBTK_COMPILE_WARNING_MESSAGES
106 //#define BBTK_COMPILE_ERROR_MESSAGES
109 #define bbtkOnMessageLevel(key,value) \
110 int __bbtkOnMessageLevelVariable = \
111 bbtk::MessageManager::GetMessageLevel(key); \
112 if ( __bbtkOnMessageLevelVariable<0) \
114 bbtkWarning("message type '"<<key<<"' unknown"); \
116 else if (value<= __bbtkOnMessageLevelVariable)
118 #define BBTK_PREPEND_MESSAGE_WITH_CODE
119 #ifdef BBTK_PREPEND_MESSAGE_WITH_CODE
120 #define bbtkMessageCode(key,value) \
121 bbtk::MessageManager::FormatKey(key,value)
123 #define bbtkMessageCode(key,value) ""
126 #ifdef BBTK_PREPEND_MESSAGE_WITH_TAB
127 #define bbtkMessageTab \
128 bbtk::MessageManager::GetTab()
130 #define bbtkMessageTab ""
133 //#define BBTK_PREPEND_MESSAGE_WITH_SPACE
134 #ifdef BBTK_PREPEND_MESSAGE_WITH_SPACE
135 #define bbtkMessageSpace(value) \
136 bbtk::MessageManager::GetSpace(value)
138 #define bbtkMessageSpace(value) ""
142 //===========================================================
143 #ifdef BBTK_COMPILE_MESSAGES
145 // Macro for messages
146 #define bbtkMessage(key,value,MESSAGE) \
148 bbtkOnMessageLevel(key,value) \
150 std::cout << bbtkMessageCode(key,value) \
152 << bbtkMessageSpace(value) \
158 // Macro for continuing a message (when one wants to split the macro
159 // call into multiple lines)
160 #define bbtkMessageCont(key,value,MESSAGE) \
163 bbtkOnMessageLevel(key,value) \
165 std::cout << MESSAGE; \
170 #define bbtkMessageInc(key,value,MESSAGE) \
173 bbtkOnMessageLevel(key,value) \
175 std::cout << bbtkMessageCode(key,value) \
177 << bbtkMessageSpace(value) \
179 bbtk::MessageManager::IncTab(); \
184 #define bbtkMessageDec(key,value,MESSAGE) \
187 bbtkOnMessageLevel(key,value) \
189 bbtk::MessageManager::DecTab(); \
190 std::cout << bbtkMessageCode(key,value) \
192 << bbtkMessageSpace(value) \
198 #define bbtkDecTab(key,value) \
201 bbtkOnMessageLevel(key,value) \
203 bbtk::MessageManager::DecTab(); \
208 #define bbtkIncTab(key,value) \
211 bbtkOnMessageLevel(key,value) \
213 bbtk::MessageManager::IncTab(); \
218 #define bbtkResetTab() \
221 bbtk::MessageManager::ResetTab(); \
226 #define bbtkMessage(key,value,MESSAGE)
227 #define bbtkMessageInc(key,value,MESSAGE)
228 #define bbtkMessageDec(key,value,MESSAGE)
229 #define bbtkMessageCont(key,value,MESSAGE)
230 #define bbtkDecTab(key,value)
231 #define bbtkIncTab(key,value)
232 #define bbtkResetTab()
234 //===========================================================
238 //===========================================================
239 #ifdef BBTK_COMPILE_DEBUG_MESSAGES
241 // Macro for debug messages
242 #define bbtkDebugMessage(key,value,MESSAGE) \
245 bbtkOnMessageLevel(key,value) \
247 std::cout << bbtkMessageCode(key,value) \
249 << bbtkMessageSpace(value) \
255 // Macro for continuing a debug message (when one wants to split the
256 // macro call into multiple lines)
257 #define bbtkDebugMessageCont(key,value,MESSAGE) \
260 bbtkOnMessageLevel(key,value) \
262 std::cout << MESSAGE; \
267 #define bbtkDebugMessageInc(key,value,MESSAGE) \
270 bbtkOnMessageLevel(key,value) \
272 std::cout << bbtkMessageCode(key,value) \
274 << bbtkMessageSpace(value) \
276 bbtk::MessageManager::IncTab(); \
281 #define bbtkDebugMessageDec(key,value,MESSAGE) \
284 bbtkOnMessageLevel(key,value) \
286 bbtk::MessageManager::DecTab(); \
287 std::cout << bbtkMessageCode(key,value) \
289 << bbtkMessageSpace(value) \
295 #define bbtkDebugDecTab(key,value) \
298 bbtkOnMessageLevel(key,value) \
300 bbtk::MessageManager::DecTab(); \
305 #define bbtkDebugIncTab(key,value) \
308 bbtkOnMessageLevel(key,value) \
310 bbtk::MessageManager::IncTab(); \
315 #define bbtkDebugResetTab() \
318 bbtk::MessageManager::ResetTab(); \
323 #define bbtkDebugMessage(key,value,MESSAGE)
324 #define bbtkDebugMessageCont(key,value,MESSAGE)
325 #define bbtkDebugMessageInc(key,value,MESSAGE)
326 #define bbtkDebugMessageDec(key,value,MESSAGE)
327 #define bbtkDebugDecTab(key,value)
328 #define bbtkDebugIncTab(key,value)
330 //===========================================================
332 //===========================================================
333 #ifdef BBTK_COMPILE_WARNING_MESSAGES
334 #define bbtkWarning(MESSAGE) \
337 int lev = bbtk::MessageManager::GetMessageLevel("Warning"); \
340 std::cerr << "!! WARNING !! " << MESSAGE << std::endl; \
343 std::cerr << "!! WARNING !! In file '"<<__FILE__ \
344 <<"' ; Line "<<__LINE__<<std::endl; \
351 #define bbtkWarning(MESSAGE)
353 //===========================================================
356 //===========================================================
357 #ifdef BBTK_COMPILE_ERROR_MESSAGES
358 //#include "bbtkWx.h"
359 #define bbtkError(MESSAGE) \
362 std::ostringstream s; \
364 std::ostringstream f; \
365 f << __FILE__ << " (l."<<__LINE__<<")"; \
366 bbtk::Exception e( BBTK_GET_CURRENT_OBJECT_NAME, \
373 #define bbtkGlobalError(MESSAGE) \
376 std::ostringstream s; \
378 std::ostringstream f; \
379 f << __FILE__ << " (l."<<__LINE__<<")"; \
380 bbtk::Exception e( "global scope", \
387 #define BBTK_INTERNAL_ERROR_MESSAGE \
388 "\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"
390 #define bbtkInternalError(MESSAGE) \
393 std::ostringstream s; \
394 s << MESSAGE << BBTK_INTERNAL_ERROR_MESSAGE; \
395 std::ostringstream f; \
396 f << __FILE__ << " (l."<<__LINE__<<")"; \
397 bbtk::Exception e( BBTK_GET_CURRENT_OBJECT_NAME, \
405 #define bbtkError(MESSAGE)
406 #define bbtkGlobalError(MESSAGE)
407 #define bbtkInternalError(MESSAGE)
409 //===========================================================
411 //===========================================================
412 #define bbtkendl std::endl
413 //===========================================================
419 class BBTK_EXPORT MessageManager
427 static MessageManager* GetInstance();
429 static bool RegisterMessageType(std::string key,
431 unsigned char default_level = 9);
433 static void SetMessageLevel(std::string key, unsigned char level);
435 static int GetMessageLevel(std::string key);
437 static std::string& GetTab() { static std::string s; return s; }
439 static std::string GetSpace(int n) {
440 std::string s; s.insert(0," ",n); return s; }
442 static void IncTab() { GetTab() += std::string(" "); }
444 static void DecTab() { GetTab() = GetTab().substr(0,GetTab().length()-1); }
446 static void ResetTab() { GetTab() = std::string(""); }
448 static void PrintInfo();
450 static std::string FormatKey(const std::string& key, int value);
453 std::map<std::string,int> mMessageLevel;
454 std::map<std::string,std::string> mMessageHelp;
455 unsigned int mMaxMessageLength;
457 //===========================================================
461 #include "bbtkException.h"