2 \brief Class creaMessageManager and Macros for outputing messages in crea
3 There are 4 kinds of messages :
4 - Messages (normal messages)
5 - Debug messages (not compiled in release)
8 There are also "types" of messages which are strings which identify the nature of the message
9 (for example : "Kernel" messages are generated by the core classes of the library, there can be a type of
10 message for each type of Node, and so on...)
11 A type of message must be declared by registering it into the MessageManager. This is done by a line like :
12 crea::MessageManager::RegisterMessageType("Kernel","Messages generated by the core classes of the library",5);
14 -The first string is the type of the message (the category which will be used to generate a message of this type)
15 -The second string is help string
16 -The integer is the initial level for the messages of this type (see below).
17 To generate a message of a known type then use one of the macros :
18 creaMessage, creaDebugMessage, creaWarning, creaError or their variants.
20 creaMessage("Kernel",4,"problem with "<<GetName()<<creaendl);
21 will push the 3rd argument in std::cout if the message level of "Kernel" messages is greater or equal to 4.
22 which means that it generates a message of level 4 (0 : very important/always displayed ... 9 : deep debug message).
23 At run time, one is able to change the level of the messages displayed by using a command like :
24 crea::MessageManager::SetMessageLevel("Kernel",5);
25 which tells the manager to display all Kernel messages of level up to 5.
27 crea*Cont : continues a previous creaMessage on the same line (without rewriting the type and level)
28 crea*Inc / Dec : displays the message and then increments/decrement the messages tabulation
31 //===========================================================
33 \class crea::MessageManager
34 \brief Manages the messages displayed by crea
37 #ifndef __creaMessageManager_h__
38 #define __creaMessageManager_h__
40 // The do { } while(0) statement in macros is made to "swallow the semicolon"
41 // see http://gcc.gnu.org/onlinedocs/cpp/Swallowing-the-Semicolon.html#Swallowing-the-Semicolon
42 #include "creaSystem.h"
43 #include "creaRTTI.h" // for CREA_GET_CURRENT_OBJECT_NAME
44 // Signal/slot mechanism for message events
45 #include <boost/signal.hpp>
46 #include <boost/bind.hpp>
51 // Comment out these symbols to prevent compilation
52 //#define CREA_COMPILE_MESSAGES
53 //#define CREA_COMPILE_DEBUG_MESSAGES
54 //#define CREA_COMPILE_WARNING_MESSAGES
55 //#define CREA_COMPILE_ERROR_MESSAGES
56 #define creaOnMessageLevel(key,value) \
57 int __creaOnMessageLevelVariable = \
58 crea::MessageManager::GetMessageLevel(key); \
59 if ( __creaOnMessageLevelVariable<0) \
61 creaWarning("message type '"<<key<<"' unknown"); \
63 else if (value<= __creaOnMessageLevelVariable)
65 #ifdef CREA_PREPEND_MESSAGE_WITH_CODE
66 #define creaMessageCode \
67 key[0] << key[1] << key[2] << value << " "
69 #define creaMessageCode ""
72 #ifdef CREA_PREPEND_MESSAGE_WITH_TAB
73 #define creaMessageTab \
74 crea::MessageManager::GetTab()
76 #define creaMessageTab ""
79 //#define CREA_PREPEND_MESSAGE_WITH_SPACE
80 #ifdef CREA_PREPEND_MESSAGE_WITH_SPACE
81 #define creaMessageSpace(value) \
82 crea::MessageManager::GetSpace(value)
84 #define creaMessageSpace(value) ""
91 //===========================================================
93 #ifdef CREA_COMPILE_MESSAGES
96 #define creaMessage(key,value,MESSAGE) \
98 creaOnMessageLevel(key,value) \
100 std::ostringstream s; \
101 s << creaMessageCode \
103 << creaMessageSpace(value) \
105 crea::MessageManager::SendMessage(key,s.str()); \
112 // Macro for continuing a message (when one wants to split the macro
113 // call into multiple lines)
114 #define creaMessageCont(key,value,MESSAGE) \
117 creaOnMessageLevel(key,value) \
119 std::ostringstream s; \
121 crea::MessageManager::SendMessage(key,s.str()); \
127 #define creaMessageInc(key,value,MESSAGE) \
130 std::ostringstream s; \
131 s << creaMessageCode \
133 << creaMessageSpace(value) \
135 crea::MessageManager::SendMessage(key,s.str()); \
136 crea::MessageManager::IncTab(); \
142 #define creaMessageDec(key,value,MESSAGE) \
145 creaOnMessageLevel(key,value) \
147 crea::MessageManager::DecTab(); \
148 std::ostringstream s; \
149 s << creaMessageCode \
151 << creaMessageSpace(value) \
153 crea::MessageManager::SendMessage(key,s.str()); \
159 #define creaDecTab(key,value) \
162 creaOnMessageLevel(key,value) \
164 crea::MessageManager::DecTab(); \
170 #define creaIncTab(key,value) \
173 creaOnMessageLevel(key,value) \
175 crea::MessageManager::IncTab(); \
181 #define creaResetTab() \
184 crea::MessageManager::ResetTab(); \
189 #define creaMessage(key,value,MESSAGE)
190 #define creaMessageInc(key,value,MESSAGE)
191 #define creaMessageDec(key,value,MESSAGE)
192 #define creaMessageCont(key,value,MESSAGE)
193 #define creaDecTab(key,value)
194 #define creaIncTab(key,value)
195 #define creaResetTab()
198 //===========================================================
206 //===========================================================
208 // Macros for debug messages
210 #ifdef CREA_COMPILE_DEBUG_MESSAGES
212 #define creaDebugMessage(key,value,MESSAGE) creaMessage(key,value,MESSAGE)
214 #define creaDebugMessageCont(key,value,MESSAGE) creaMessageCont(key,value,MESSAGE)
216 #define creaDebugMessageInc(key,value,MESSAGE) creaMessageInc(key,value,MESSAGE)
218 #define creaDebugMessageDec(key,value,MESSAGE) creaMessageDec(key,value,MESSAGE)
220 #define creaDebugDecTab(key,value) creaDecTab(key,value)
222 #define creaDebugIncTab(key,value) creaIncTab(key,value)
224 #define creaDebugResetTab() creaResetTab()
228 #define creaDebugMessage(key,value,MESSAGE)
230 #define creaDebugMessageCont(key,value,MESSAGE)
232 #define creaDebugMessageInc(key,value,MESSAGE)
234 #define creaDebugMessageDec(key,value,MESSAGE)
236 #define creaDebugDecTab(key,value)
238 #define creaDebugIncTab(key,value)
242 //===========================================================
246 //===========================================================
248 #ifdef CREA_COMPILE_WARNING_MESSAGES
249 #define creaWarning(MESSAGE) \
252 int lev = crea::MessageManager::GetMessageLevel("warning"); \
255 std::cerr << "!! WARNING !! " << MESSAGE << std::endl; \
258 std::cerr << "!! WARNING !! In file '"<<__FILE__ \
259 <<"' ; Line "<<__LINE__<<std::endl; \
266 #define creaWarning(MESSAGE)
269 //===========================================================
275 //===========================================================
277 #ifdef CREA_COMPILE_ERROR_MESSAGES
278 //#include "creaWx.h"
279 #define creaError(MESSAGE) \
282 std::ostringstream s; \
284 std::ostringstream f; \
285 f << __FILE__ << " (l."<<__LINE__<<")"; \
286 crea::Exception e( CREA_GET_CURRENT_OBJECT_NAME, \
295 #define creaGlobalError(MESSAGE) \
298 std::ostringstream s; \
300 std::ostringstream f; \
301 f << __FILE__ << " (l."<<__LINE__<<")"; \
302 crea::Exception e( "global scope", \
311 #define CREA_INTERNAL_ERROR_MESSAGE \
312 "\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"
314 #define creaInternalError(MESSAGE) \
317 std::ostringstream s; \
318 s << MESSAGE << CREA_INTERNAL_ERROR_MESSAGE; \
319 std::ostringstream f; \
320 f << __FILE__ << " (l."<<__LINE__<<")"; \
321 crea::Exception e( CREA_GET_CURRENT_OBJECT_NAME, \
331 #define creaError(MESSAGE)
332 #define creaGlobalError(MESSAGE)
333 #define creaInternalError(MESSAGE)
336 //===========================================================
340 //===========================================================
342 #define creaendl std::endl
344 //===========================================================
356 class CREA_EXPORT MessageManager
362 //=============================================
364 typedef boost::signal<void (const std::string&)> MessageSignalType;
366 typedef MessageSignalType::slot_function_type MessageCallbackType;
368 //=============================================
380 static MessageManager* GetInstance();
384 static void RegisterMessageType(const std::string& key,
386 const std::string& help,
388 unsigned char default_level = 9);
392 static void SetMessageLevel(const std::string& key, unsigned char level);
396 static int GetMessageLevel(const std::string& key);
400 static void SendMessage(const std::string& key, const std::string& mess);
404 static void AddMessageObserver(const std::string& key, MessageCallbackType callback );
408 static void SendMessagesToCout(bool v = true);
412 static std::string& GetTab() { static std::string s; return s; }
416 static std::string GetSpace(int n) {
418 std::string s; s.insert(0," ",n); return s; }
422 static void IncTab() { GetTab() += std::string(" "); }
426 static void DecTab() { GetTab() = GetTab().substr(0,GetTab().length()-1); }
430 static void ResetTab() { GetTab() = std::string(""); }
434 static void PrintInfo();
446 MessageType(int l, const std::string& h) : Level(l), Help(h) {}
452 MessageSignalType Signal;
456 typedef std::map<std::string,MessageType*> MessageMapType;
458 MessageMapType mMessageMap;
460 unsigned int mMaxMessageLength;
466 //===========================================================
474 #include "creaException.h"