1 /*=========================================================================
3 Module: $RCSfile: bbtkMessageManager.h,v $
5 Date: $Date: 2009/05/28 08:12:06 $
6 Version: $Revision: 1.9 $
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 #define BBTK_PREPEND_MESSAGE_WITH_CODE
112 #ifdef BBTK_PREPEND_MESSAGE_WITH_CODE
113 #define bbtkMessageCode(key,value) \
114 bbtk::MessageManager::FormatKey(key,value)
116 #define bbtkMessageCode(key,value) ""
119 #ifdef BBTK_PREPEND_MESSAGE_WITH_TAB
120 #define bbtkMessageTab \
121 bbtk::MessageManager::GetTab()
123 #define bbtkMessageTab ""
126 //#define BBTK_PREPEND_MESSAGE_WITH_SPACE
127 #ifdef BBTK_PREPEND_MESSAGE_WITH_SPACE
128 #define bbtkMessageSpace(value) \
129 bbtk::MessageManager::GetSpace(value)
131 #define bbtkMessageSpace(value) ""
135 //===========================================================
136 #ifdef BBTK_COMPILE_MESSAGES
138 // Macro for messages
139 #define bbtkMessage(key,value,MESSAGE) \
141 bbtkOnMessageLevel(key,value) \
143 std::cout << bbtkMessageCode(key,value) \
145 << bbtkMessageSpace(value) \
151 // Macro for continuing a message (when one wants to split the macro
152 // call into multiple lines)
153 #define bbtkMessageCont(key,value,MESSAGE) \
156 bbtkOnMessageLevel(key,value) \
158 std::cout << MESSAGE; \
163 #define bbtkMessageInc(key,value,MESSAGE) \
166 bbtkOnMessageLevel(key,value) \
168 std::cout << bbtkMessageCode(key,value) \
170 << bbtkMessageSpace(value) \
172 bbtk::MessageManager::IncTab(); \
177 #define bbtkMessageDec(key,value,MESSAGE) \
180 bbtkOnMessageLevel(key,value) \
182 bbtk::MessageManager::DecTab(); \
183 std::cout << bbtkMessageCode(key,value) \
185 << bbtkMessageSpace(value) \
191 #define bbtkDecTab(key,value) \
194 bbtkOnMessageLevel(key,value) \
196 bbtk::MessageManager::DecTab(); \
201 #define bbtkIncTab(key,value) \
204 bbtkOnMessageLevel(key,value) \
206 bbtk::MessageManager::IncTab(); \
211 #define bbtkResetTab() \
214 bbtk::MessageManager::ResetTab(); \
219 #define bbtkMessage(key,value,MESSAGE)
220 #define bbtkMessageInc(key,value,MESSAGE)
221 #define bbtkMessageDec(key,value,MESSAGE)
222 #define bbtkMessageCont(key,value,MESSAGE)
223 #define bbtkDecTab(key,value)
224 #define bbtkIncTab(key,value)
225 #define bbtkResetTab()
227 //===========================================================
231 //===========================================================
232 #ifdef BBTK_COMPILE_DEBUG_MESSAGES
234 // Macro for debug messages
235 #define bbtkDebugMessage(key,value,MESSAGE) \
238 bbtkOnMessageLevel(key,value) \
240 std::cout << bbtkMessageCode(key,value) \
242 << bbtkMessageSpace(value) \
248 // Macro for continuing a debug message (when one wants to split the
249 // macro call into multiple lines)
250 #define bbtkDebugMessageCont(key,value,MESSAGE) \
253 bbtkOnMessageLevel(key,value) \
255 std::cout << MESSAGE; \
260 #define bbtkDebugMessageInc(key,value,MESSAGE) \
263 bbtkOnMessageLevel(key,value) \
265 std::cout << bbtkMessageCode(key,value) \
267 << bbtkMessageSpace(value) \
269 bbtk::MessageManager::IncTab(); \
274 #define bbtkDebugMessageDec(key,value,MESSAGE) \
277 bbtkOnMessageLevel(key,value) \
279 bbtk::MessageManager::DecTab(); \
280 std::cout << bbtkMessageCode(key,value) \
282 << bbtkMessageSpace(value) \
288 #define bbtkDebugDecTab(key,value) \
291 bbtkOnMessageLevel(key,value) \
293 bbtk::MessageManager::DecTab(); \
298 #define bbtkDebugIncTab(key,value) \
301 bbtkOnMessageLevel(key,value) \
303 bbtk::MessageManager::IncTab(); \
308 #define bbtkDebugResetTab() \
311 bbtk::MessageManager::ResetTab(); \
316 #define bbtkDebugMessage(key,value,MESSAGE)
317 #define bbtkDebugMessageCont(key,value,MESSAGE)
318 #define bbtkDebugMessageInc(key,value,MESSAGE)
319 #define bbtkDebugMessageDec(key,value,MESSAGE)
320 #define bbtkDebugDecTab(key,value)
321 #define bbtkDebugIncTab(key,value)
323 //===========================================================
325 //===========================================================
326 #ifdef BBTK_COMPILE_WARNING_MESSAGES
327 #define bbtkWarning(MESSAGE) \
330 int lev = bbtk::MessageManager::GetMessageLevel("Warning"); \
333 std::cerr << "!! WARNING !! " << MESSAGE << std::endl; \
336 std::cerr << "!! WARNING !! In file '"<<__FILE__ \
337 <<"' ; Line "<<__LINE__<<std::endl; \
344 #define bbtkWarning(MESSAGE)
346 //===========================================================
349 //===========================================================
350 #ifdef BBTK_COMPILE_ERROR_MESSAGES
351 //#include "bbtkWx.h"
352 #define bbtkError(MESSAGE) \
355 std::ostringstream s; \
357 std::ostringstream f; \
358 f << __FILE__ << " (l."<<__LINE__<<")"; \
359 bbtk::Exception e( BBTK_GET_CURRENT_OBJECT_NAME, \
366 #define bbtkGlobalError(MESSAGE) \
369 std::ostringstream s; \
371 std::ostringstream f; \
372 f << __FILE__ << " (l."<<__LINE__<<")"; \
373 bbtk::Exception e( "global scope", \
380 #define BBTK_INTERNAL_ERROR_MESSAGE \
381 "\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"
383 #define bbtkInternalError(MESSAGE) \
386 std::ostringstream s; \
387 s << MESSAGE << BBTK_INTERNAL_ERROR_MESSAGE; \
388 std::ostringstream f; \
389 f << __FILE__ << " (l."<<__LINE__<<")"; \
390 bbtk::Exception e( BBTK_GET_CURRENT_OBJECT_NAME, \
398 #define bbtkError(MESSAGE)
399 #define bbtkGlobalError(MESSAGE)
400 #define bbtkInternalError(MESSAGE)
402 //===========================================================
404 //===========================================================
405 #define bbtkendl std::endl
406 //===========================================================
412 class BBTK_EXPORT MessageManager
420 static MessageManager* GetInstance();
422 static bool RegisterMessageType(std::string key,
424 unsigned char default_level = 9);
426 static void SetMessageLevel(std::string key, unsigned char level);
428 static int GetMessageLevel(std::string key);
430 static std::string& GetTab() { static std::string s; return s; }
432 static std::string GetSpace(int n) {
433 std::string s; s.insert(0," ",n); return s; }
435 static void IncTab() { GetTab() += std::string(" "); }
437 static void DecTab() { GetTab() = GetTab().substr(0,GetTab().length()-1); }
439 static void ResetTab() { GetTab() = std::string(""); }
441 static void PrintInfo();
443 static std::string FormatKey(const std::string& key, int value);
446 std::map<std::string,int> mMessageLevel;
447 std::map<std::string,std::string> mMessageHelp;
448 unsigned int mMaxMessageLength;
450 //===========================================================
454 #include "bbtkException.h"