1 /*=========================================================================
3 Module: $RCSfile: bbtkMessageManager.h,v $
5 Date: $Date: 2008/11/29 21:41:34 $
6 Version: $Revision: 1.8 $
7 =========================================================================*/
9 /* ---------------------------------------------------------------------
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
14 * This software is governed by the CeCILL-B license under French law and
15 * abiding by the rules of distribution of free software. You can use,
16 * modify and/ or redistribute the software under the terms of the CeCILL-B
17 * license as circulated by CEA, CNRS and INRIA at the following URL
18 * http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
19 * or in the file LICENSE.txt.
21 * As a counterpart to the access to the source code and rights to copy,
22 * modify and redistribute granted by the license, users are provided only
23 * with a limited warranty and the software's author, the holder of the
24 * economic rights, and the successive licensors have only limited
27 * The fact that you are presently reading this means that you have had
28 * knowledge of the CeCILL-B license and that you accept its terms.
29 * ------------------------------------------------------------------------ */
35 \brief Class bbtkMessageManager and Macros for outputing messages in bbtk
37 There are 4 kinds of messages :
38 - Messages (normal messages)
39 - Debug messages (not compiled in release)
42 There are also "types" of messages which are strings which identify the nature of the message
43 (for example : "Kernel" messages are generated by the core classes of the library, there can be a type of
44 message for each type of Node, and so on...)
45 A type of message must be declared by registering it into the MessageManager. This is done by a line like :
46 bbtk::MessageManager::RegisterMessageType("Kernel","Messages generated by the core classes of the library",5);
48 -The first string is the type of the message (the category which will be used to generate a message of this type)
49 -The second string is help string
50 -The integer is the initial level for the messages of this type (see below).
52 To generate a message of a known type then use one of the macros :
53 bbtkMessage, bbtkDebugMessage, bbtkWarning, bbtkError or their variants.
57 bbtkMessage("Kernel",4,"problem with "<<GetName()<<bbtkendl);
59 will push the 3rd argument in std::cout if the message level of "Kernel" messages is greater or equal to 4.
60 which means that it generates a message of level 4 (0 : very important/always displayed ... 9 : deep debug message).
62 At run time, one is able to change the level of the messages displayed by using a command like :
64 bbtk::MessageManager::SetMessageLevel("Kernel",5);
66 which tells the manager to display all Kernel messages of level up to 5.
70 bbtk*Cont : continues a previous bbtkMessage on the same line (without rewriting the type and level)
71 bbtk*Inc / Dec : displays the message and then increments/decrement the messages tabulation
74 //===========================================================
76 \class bbtk::MessageManager
77 \brief Manages the messages displayed by bbtk
81 #ifndef __bbtkMessageManager_h__
82 #define __bbtkMessageManager_h__
84 // The do { } while(0) statement in macros is made to "swallow the semicolon"
85 // see http://gcc.gnu.org/onlinedocs/cpp/Swallowing-the-Semicolon.html#Swallowing-the-Semicolon
87 #include "bbtkSystem.h"
95 // Comment out these symbols to prevent compilation
96 //#define BBTK_COMPILE_MESSAGES
97 //#define BBTK_COMPILE_DEBUG_MESSAGES
98 //#define BBTK_COMPILE_WARNING_MESSAGES
99 //#define BBTK_COMPILE_ERROR_MESSAGES
102 #define bbtkOnMessageLevel(key,value) \
103 int __bbtkOnMessageLevelVariable = \
104 bbtk::MessageManager::GetMessageLevel(key); \
105 if ( __bbtkOnMessageLevelVariable<0) \
107 bbtkWarning("message type '"<<key<<"' unknown"); \
109 else if (value<= __bbtkOnMessageLevelVariable)
111 #ifdef BBTK_PREPEND_MESSAGE_WITH_CODE
112 #define bbtkMessageCode \
113 key[0] << key[1] << key[2] << value << " "
115 #define bbtkMessageCode ""
118 #ifdef BBTK_PREPEND_MESSAGE_WITH_TAB
119 #define bbtkMessageTab \
120 bbtk::MessageManager::GetTab()
122 #define bbtkMessageTab ""
125 #define BBTK_PREPEND_MESSAGE_WITH_SPACE
126 #ifdef BBTK_PREPEND_MESSAGE_WITH_SPACE
127 #define bbtkMessageSpace(value) \
128 bbtk::MessageManager::GetSpace(value)
130 #define bbtkMessageSpace(value) ""
134 //===========================================================
135 #ifdef BBTK_COMPILE_MESSAGES
137 // Macro for messages
138 #define bbtkMessage(key,value,MESSAGE) \
140 bbtkOnMessageLevel(key,value) \
142 std::cout << bbtkMessageCode \
144 << bbtkMessageSpace(value) \
150 // Macro for continuing a message (when one wants to split the macro
151 // call into multiple lines)
152 #define bbtkMessageCont(key,value,MESSAGE) \
155 bbtkOnMessageLevel(key,value) \
157 std::cout << MESSAGE; \
162 #define bbtkMessageInc(key,value,MESSAGE) \
165 bbtkOnMessageLevel(key,value) \
167 std::cout << bbtkMessageCode \
169 << bbtkMessageSpace(value) \
171 bbtk::MessageManager::IncTab(); \
176 #define bbtkMessageDec(key,value,MESSAGE) \
179 bbtkOnMessageLevel(key,value) \
181 bbtk::MessageManager::DecTab(); \
182 std::cout << bbtkMessageCode \
184 << bbtkMessageSpace(value) \
190 #define bbtkDecTab(key,value) \
193 bbtkOnMessageLevel(key,value) \
195 bbtk::MessageManager::DecTab(); \
200 #define bbtkIncTab(key,value) \
203 bbtkOnMessageLevel(key,value) \
205 bbtk::MessageManager::IncTab(); \
210 #define bbtkResetTab() \
213 bbtk::MessageManager::ResetTab(); \
218 #define bbtkMessage(key,value,MESSAGE)
219 #define bbtkMessageInc(key,value,MESSAGE)
220 #define bbtkMessageDec(key,value,MESSAGE)
221 #define bbtkMessageCont(key,value,MESSAGE)
222 #define bbtkDecTab(key,value)
223 #define bbtkIncTab(key,value)
224 #define bbtkResetTab()
226 //===========================================================
230 //===========================================================
231 #ifdef BBTK_COMPILE_DEBUG_MESSAGES
233 // Macro for debug messages
234 #define bbtkDebugMessage(key,value,MESSAGE) \
237 bbtkOnMessageLevel(key,value) \
239 std::cout << bbtkMessageCode \
241 << bbtkMessageSpace(value) \
247 // Macro for continuing a debug message (when one wants to split the
248 // macro call into multiple lines)
249 #define bbtkDebugMessageCont(key,value,MESSAGE) \
252 bbtkOnMessageLevel(key,value) \
254 std::cout << MESSAGE; \
259 #define bbtkDebugMessageInc(key,value,MESSAGE) \
262 bbtkOnMessageLevel(key,value) \
264 std::cout << bbtkMessageCode \
266 << bbtkMessageSpace(value) \
268 bbtk::MessageManager::IncTab(); \
273 #define bbtkDebugMessageDec(key,value,MESSAGE) \
276 bbtkOnMessageLevel(key,value) \
278 bbtk::MessageManager::DecTab(); \
279 std::cout << bbtkMessageCode \
281 << bbtkMessageSpace(value) \
287 #define bbtkDebugDecTab(key,value) \
290 bbtkOnMessageLevel(key,value) \
292 bbtk::MessageManager::DecTab(); \
297 #define bbtkDebugIncTab(key,value) \
300 bbtkOnMessageLevel(key,value) \
302 bbtk::MessageManager::IncTab(); \
307 #define bbtkDebugResetTab() \
310 bbtk::MessageManager::ResetTab(); \
315 #define bbtkDebugMessage(key,value,MESSAGE)
316 #define bbtkDebugMessageCont(key,value,MESSAGE)
317 #define bbtkDebugMessageInc(key,value,MESSAGE)
318 #define bbtkDebugMessageDec(key,value,MESSAGE)
319 #define bbtkDebugDecTab(key,value)
320 #define bbtkDebugIncTab(key,value)
322 //===========================================================
324 //===========================================================
325 #ifdef BBTK_COMPILE_WARNING_MESSAGES
326 #define bbtkWarning(MESSAGE) \
329 int lev = bbtk::MessageManager::GetMessageLevel("Warning"); \
332 std::cerr << "!! WARNING !! " << MESSAGE << std::endl; \
335 std::cerr << "!! WARNING !! In file '"<<__FILE__ \
336 <<"' ; Line "<<__LINE__<<std::endl; \
343 #define bbtkWarning(MESSAGE)
345 //===========================================================
348 //===========================================================
349 #ifdef BBTK_COMPILE_ERROR_MESSAGES
350 //#include "bbtkWx.h"
351 #define bbtkError(MESSAGE) \
354 std::ostringstream s; \
356 std::ostringstream f; \
357 f << __FILE__ << " (l."<<__LINE__<<")"; \
358 bbtk::Exception e( BBTK_GET_CURRENT_OBJECT_NAME, \
365 #define bbtkGlobalError(MESSAGE) \
368 std::ostringstream s; \
370 std::ostringstream f; \
371 f << __FILE__ << " (l."<<__LINE__<<")"; \
372 bbtk::Exception e( "global scope", \
379 #define BBTK_INTERNAL_ERROR_MESSAGE \
380 "\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"
382 #define bbtkInternalError(MESSAGE) \
385 std::ostringstream s; \
386 s << MESSAGE << BBTK_INTERNAL_ERROR_MESSAGE; \
387 std::ostringstream f; \
388 f << __FILE__ << " (l."<<__LINE__<<")"; \
389 bbtk::Exception e( BBTK_GET_CURRENT_OBJECT_NAME, \
397 #define bbtkError(MESSAGE)
398 #define bbtkGlobalError(MESSAGE)
399 #define bbtkInternalError(MESSAGE)
401 //===========================================================
403 //===========================================================
404 #define bbtkendl std::endl
405 //===========================================================
411 class BBTK_EXPORT MessageManager
419 static MessageManager* GetInstance();
421 static bool RegisterMessageType(std::string key,
423 unsigned char default_level = 9);
425 static void SetMessageLevel(std::string key, unsigned char level);
427 static int GetMessageLevel(std::string key);
429 static std::string& GetTab() { static std::string s; return s; }
431 static std::string GetSpace(int n) {
432 std::string s; s.insert(0," ",n); return s; }
434 static void IncTab() { GetTab() += std::string(" "); }
436 static void DecTab() { GetTab() = GetTab().substr(0,GetTab().length()-1); }
438 static void ResetTab() { GetTab() = std::string(""); }
440 static void PrintInfo();
443 std::map<std::string,int> mMessageLevel;
444 std::map<std::string,std::string> mMessageHelp;
445 unsigned int mMaxMessageLength;
447 //===========================================================
451 #include "bbtkException.h"