2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
8 # This software is governed by the CeCILL-B license under French law and
9 # abiding by the rules of distribution of free software. You can use,
10 # modify and/ or redistribute the software under the terms of the CeCILL-B
11 # license as circulated by CEA, CNRS and INRIA at the following URL
12 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
13 # or in the file LICENSE.txt.
15 # As a counterpart to the access to the source code and rights to copy,
16 # modify and redistribute granted by the license, users are provided only
17 # with a limited warranty and the software's author, the holder of the
18 # economic rights, and the successive licensors have only limited
21 # The fact that you are presently reading this means that you have had
22 # knowledge of the CeCILL-B license and that you accept its terms.
23 # ------------------------------------------------------------------------ */
26 \brief Class creaMessageManager and Macros for outputing messages in crea
27 There are 4 kinds of messages :
28 - Messages (normal messages)
29 - Debug messages (not compiled in release)
32 There are also "types" of messages which are strings which identify the nature of the message
33 (for example : "Kernel" messages are generated by the core classes of the library, there can be a type of
34 message for each type of Node, and so on...)
35 A type of message must be declared by registering it into the MessageManager. This is done by a line like :
36 crea::MessageManager::RegisterMessageType("Kernel","Messages generated by the core classes of the library",5);
38 -The first string is the type of the message (the category which will be used to generate a message of this type)
39 -The second string is help string
40 -The integer is the initial level for the messages of this type (see below).
41 To generate a message of a known type then use one of the macros :
42 creaMessage, creaDebugMessage, creaWarning, creaError or their variants.
44 creaMessage("Kernel",4,"problem with "<<GetName()<<creaendl);
45 will push the 3rd argument in std::cout if the message level of "Kernel" messages is greater or equal to 4.
46 which means that it generates a message of level 4 (0 : very important/always displayed ... 9 : deep debug message).
47 At run time, one is able to change the level of the messages displayed by using a command like :
48 crea::MessageManager::SetMessageLevel("Kernel",5);
49 which tells the manager to display all Kernel messages of level up to 5.
51 crea*Cont : continues a previous creaMessage on the same line (without rewriting the type and level)
52 crea*Inc / Dec : displays the message and then increments/decrement the messages tabulation
55 //===========================================================
57 \class crea::MessageManager
58 \brief Manages the messages displayed by crea
61 #ifndef __creaMessageManager_h__
62 #define __creaMessageManager_h__
64 // The do { } while(0) statement in macros is made to "swallow the semicolon"
65 // see http://gcc.gnu.org/onlinedocs/cpp/Swallowing-the-Semicolon.html#Swallowing-the-Semicolon
66 #include "creaSystem.h"
67 #include "creaRTTI.h" // for CREA_GET_CURRENT_OBJECT_NAME
68 // Signal/slot mechanism for message events
69 #include <boost/signal.hpp>
70 #include <boost/bind.hpp>
75 // Comment out these symbols to prevent compilation
76 //#define CREA_COMPILE_MESSAGES
77 //#define CREA_COMPILE_DEBUG_MESSAGES
78 //#define CREA_COMPILE_WARNING_MESSAGES
79 //#define CREA_COMPILE_ERROR_MESSAGES
80 #define creaOnMessageLevel(key,value) \
81 int __creaOnMessageLevelVariable = \
82 crea::MessageManager::GetMessageLevel(key); \
83 if ( __creaOnMessageLevelVariable<0) \
85 creaWarning("message type '"<<key<<"' unknown"); \
87 else if (value<= __creaOnMessageLevelVariable)
89 #ifdef CREA_PREPEND_MESSAGE_WITH_CODE
90 #define creaMessageCode \
91 key[0] << key[1] << key[2] << value << " "
93 #define creaMessageCode ""
96 #ifdef CREA_PREPEND_MESSAGE_WITH_TAB
97 #define creaMessageTab \
98 crea::MessageManager::GetTab()
100 #define creaMessageTab ""
103 //#define CREA_PREPEND_MESSAGE_WITH_SPACE
104 #ifdef CREA_PREPEND_MESSAGE_WITH_SPACE
105 #define creaMessageSpace(value) \
106 crea::MessageManager::GetSpace(value)
108 #define creaMessageSpace(value) ""
115 //===========================================================
117 #ifdef CREA_COMPILE_MESSAGES
119 // Macro for messages
120 #define creaMessage(key,value,MESSAGE) \
122 creaOnMessageLevel(key,value) \
124 std::ostringstream s; \
125 s << creaMessageCode \
127 << creaMessageSpace(value) \
129 crea::MessageManager::SendMessage(key,s.str()); \
136 // Macro for continuing a message (when one wants to split the macro
137 // call into multiple lines)
138 #define creaMessageCont(key,value,MESSAGE) \
141 creaOnMessageLevel(key,value) \
143 std::ostringstream s; \
145 crea::MessageManager::SendMessage(key,s.str()); \
151 #define creaMessageInc(key,value,MESSAGE) \
154 std::ostringstream s; \
155 s << creaMessageCode \
157 << creaMessageSpace(value) \
159 crea::MessageManager::SendMessage(key,s.str()); \
160 crea::MessageManager::IncTab(); \
166 #define creaMessageDec(key,value,MESSAGE) \
169 creaOnMessageLevel(key,value) \
171 crea::MessageManager::DecTab(); \
172 std::ostringstream s; \
173 s << creaMessageCode \
175 << creaMessageSpace(value) \
177 crea::MessageManager::SendMessage(key,s.str()); \
183 #define creaDecTab(key,value) \
186 creaOnMessageLevel(key,value) \
188 crea::MessageManager::DecTab(); \
194 #define creaIncTab(key,value) \
197 creaOnMessageLevel(key,value) \
199 crea::MessageManager::IncTab(); \
205 #define creaResetTab() \
208 crea::MessageManager::ResetTab(); \
213 #define creaMessage(key,value,MESSAGE)
214 #define creaMessageInc(key,value,MESSAGE)
215 #define creaMessageDec(key,value,MESSAGE)
216 #define creaMessageCont(key,value,MESSAGE)
217 #define creaDecTab(key,value)
218 #define creaIncTab(key,value)
219 #define creaResetTab()
222 //===========================================================
230 //===========================================================
232 // Macros for debug messages
234 #ifdef CREA_COMPILE_DEBUG_MESSAGES
236 #define creaDebugMessage(key,value,MESSAGE) creaMessage(key,value,MESSAGE)
238 #define creaDebugMessageCont(key,value,MESSAGE) creaMessageCont(key,value,MESSAGE)
240 #define creaDebugMessageInc(key,value,MESSAGE) creaMessageInc(key,value,MESSAGE)
242 #define creaDebugMessageDec(key,value,MESSAGE) creaMessageDec(key,value,MESSAGE)
244 #define creaDebugDecTab(key,value) creaDecTab(key,value)
246 #define creaDebugIncTab(key,value) creaIncTab(key,value)
248 #define creaDebugResetTab() creaResetTab()
252 #define creaDebugMessage(key,value,MESSAGE)
254 #define creaDebugMessageCont(key,value,MESSAGE)
256 #define creaDebugMessageInc(key,value,MESSAGE)
258 #define creaDebugMessageDec(key,value,MESSAGE)
260 #define creaDebugDecTab(key,value)
262 #define creaDebugIncTab(key,value)
266 //===========================================================
270 //===========================================================
272 #ifdef CREA_COMPILE_WARNING_MESSAGES
273 #define creaWarning(MESSAGE) \
276 int lev = crea::MessageManager::GetMessageLevel("warning"); \
279 std::cerr << "!! WARNING !! " << MESSAGE << std::endl; \
282 std::cerr << "!! WARNING !! In file '"<<__FILE__ \
283 <<"' ; Line "<<__LINE__<<std::endl; \
290 #define creaWarning(MESSAGE)
293 //===========================================================
299 //===========================================================
301 #ifdef CREA_COMPILE_ERROR_MESSAGES
302 //#include "creaWx.h"
303 #define creaError(MESSAGE) \
306 std::ostringstream s; \
308 std::ostringstream f; \
309 f << __FILE__ << " (l."<<__LINE__<<")"; \
310 crea::Exception e( CREA_GET_CURRENT_OBJECT_NAME, \
319 #define creaGlobalError(MESSAGE) \
322 std::ostringstream s; \
324 std::ostringstream f; \
325 f << __FILE__ << " (l."<<__LINE__<<")"; \
326 crea::Exception e( "global scope", \
335 #define CREA_INTERNAL_ERROR_MESSAGE \
336 "\n\n***********************************************\n**** THIS IS AN INTERNAL ERROR TO crea ****\n**** Please send a full bug report to : ****\n**** creatools@creatis.insa-lyon.fr ****\n***********************************************\n\n"
338 #define creaInternalError(MESSAGE) \
341 std::ostringstream s; \
342 s << MESSAGE << CREA_INTERNAL_ERROR_MESSAGE; \
343 std::ostringstream f; \
344 f << __FILE__ << " (l."<<__LINE__<<")"; \
345 crea::Exception e( CREA_GET_CURRENT_OBJECT_NAME, \
355 #define creaError(MESSAGE)
356 #define creaGlobalError(MESSAGE)
357 #define creaInternalError(MESSAGE)
360 //===========================================================
364 //===========================================================
366 #define creaendl std::endl
368 //===========================================================
380 class CREA_EXPORT MessageManager
386 //=============================================
388 typedef boost::signal<void (const std::string&)> MessageSignalType;
390 typedef MessageSignalType::slot_function_type MessageCallbackType;
392 //=============================================
404 static MessageManager* GetInstance();
408 static void RegisterMessageType(const std::string& key,
410 const std::string& help,
412 unsigned char default_level = 9);
416 static void SetMessageLevel(const std::string& key, unsigned char level);
420 static int GetMessageLevel(const std::string& key);
424 static void SendMessage(const std::string& key, const std::string& mess);
428 static void AddMessageObserver(const std::string& key, MessageCallbackType callback );
432 static void SendMessagesToCout(bool v = true);
436 static std::string& GetTab() { static std::string s; return s; }
440 static std::string GetSpace(int n) {
442 std::string s; s.insert(0," ",n); return s; }
446 static void IncTab() { GetTab() += std::string(" "); }
450 static void DecTab() { GetTab() = GetTab().substr(0,GetTab().length()-1); }
454 static void ResetTab() { GetTab() = std::string(""); }
458 static void PrintInfo();
470 MessageType(int l, const std::string& h) : Level(l), Help(h) {}
476 MessageSignalType Signal;
480 typedef std::map<std::string,MessageType*> MessageMapType;
482 MessageMapType mMessageMap;
484 unsigned int mMaxMessageLength;
490 //===========================================================
498 #include "creaException.h"