7 \brief Class creaMessageManager and Macros for outputing messages in crea
11 There are 4 kinds of messages :
13 - Messages (normal messages)
15 - Debug messages (not compiled in release)
21 There are also "types" of messages which are strings which identify the nature of the message
23 (for example : "Kernel" messages are generated by the core classes of the library, there can be a type of
25 message for each type of Node, and so on...)
27 A type of message must be declared by registering it into the MessageManager. This is done by a line like :
29 crea::MessageManager::RegisterMessageType("Kernel","Messages generated by the core classes of the library",5);
33 -The first string is the type of the message (the category which will be used to generate a message of this type)
35 -The second string is help string
37 -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 :
43 creaMessage, creaDebugMessage, creaWarning, creaError or their variants.
51 creaMessage("Kernel",4,"problem with "<<GetName()<<creaendl);
55 will push the 3rd argument in std::cout if the message level of "Kernel" messages is greater or equal to 4.
57 which means that it generates a message of level 4 (0 : very important/always displayed ... 9 : deep debug message).
61 At run time, one is able to change the level of the messages displayed by using a command like :
65 crea::MessageManager::SetMessageLevel("Kernel",5);
69 which tells the manager to display all Kernel messages of level up to 5.
77 crea*Cont : continues a previous creaMessage on the same line (without rewriting the type and level)
79 crea*Inc / Dec : displays the message and then increments/decrement the messages tabulation
85 //===========================================================
89 \class crea::MessageManager
91 \brief Manages the messages displayed by crea
99 #ifndef __creaMessageManager_h__
101 #define __creaMessageManager_h__
105 // The do { } while(0) statement in macros is made to "swallow the semicolon"
107 // see http://gcc.gnu.org/onlinedocs/cpp/Swallowing-the-Semicolon.html#Swallowing-the-Semicolon
111 #include "creaSystem.h"
113 #include "creaRTTI.h" // for CREA_GET_CURRENT_OBJECT_NAME
115 // Signal/slot mechanism for message events
117 #include <boost/signal.hpp>
119 #include <boost/bind.hpp>
133 // Comment out these symbols to prevent compilation
135 //#define CREA_COMPILE_MESSAGES
137 //#define CREA_COMPILE_DEBUG_MESSAGES
139 //#define CREA_COMPILE_WARNING_MESSAGES
141 //#define CREA_COMPILE_ERROR_MESSAGES
147 #define creaOnMessageLevel(key,value) \
148 int __creaOnMessageLevelVariable = \
149 crea::MessageManager::GetMessageLevel(key); \
150 if ( __creaOnMessageLevelVariable<0) \
152 creaWarning("message type '"<<key<<"' unknown"); \
154 else if (value<= __creaOnMessageLevelVariable)
158 #ifdef CREA_PREPEND_MESSAGE_WITH_CODE
159 #define creaMessageCode \
160 key[0] << key[1] << key[2] << value << " "
162 #define creaMessageCode ""
167 #ifdef CREA_PREPEND_MESSAGE_WITH_TAB
168 #define creaMessageTab \
169 crea::MessageManager::GetTab()
171 #define creaMessageTab ""
176 //#define CREA_PREPEND_MESSAGE_WITH_SPACE
177 #ifdef CREA_PREPEND_MESSAGE_WITH_SPACE
178 #define creaMessageSpace(value) \
179 crea::MessageManager::GetSpace(value)
181 #define creaMessageSpace(value) ""
188 //===========================================================
190 #ifdef CREA_COMPILE_MESSAGES
192 // Macro for messages
193 #define creaMessage(key,value,MESSAGE) \
195 creaOnMessageLevel(key,value) \
197 std::ostringstream s; \
198 s << creaMessageCode \
200 << creaMessageSpace(value) \
202 crea::MessageManager::SendMessage(key,s.str()); \
209 // Macro for continuing a message (when one wants to split the macro
210 // call into multiple lines)
211 #define creaMessageCont(key,value,MESSAGE) \
214 creaOnMessageLevel(key,value) \
216 std::ostringstream s; \
218 crea::MessageManager::SendMessage(key,s.str()); \
224 #define creaMessageInc(key,value,MESSAGE) \
227 std::ostringstream s; \
228 s << creaMessageCode \
230 << creaMessageSpace(value) \
232 crea::MessageManager::SendMessage(key,s.str()); \
233 crea::MessageManager::IncTab(); \
239 #define creaMessageDec(key,value,MESSAGE) \
242 creaOnMessageLevel(key,value) \
244 crea::MessageManager::DecTab(); \
245 std::ostringstream s; \
246 s << creaMessageCode \
248 << creaMessageSpace(value) \
250 crea::MessageManager::SendMessage(key,s.str()); \
256 #define creaDecTab(key,value) \
259 creaOnMessageLevel(key,value) \
261 crea::MessageManager::DecTab(); \
267 #define creaIncTab(key,value) \
270 creaOnMessageLevel(key,value) \
272 crea::MessageManager::IncTab(); \
278 #define creaResetTab() \
281 crea::MessageManager::ResetTab(); \
286 #define creaMessage(key,value,MESSAGE)
287 #define creaMessageInc(key,value,MESSAGE)
288 #define creaMessageDec(key,value,MESSAGE)
289 #define creaMessageCont(key,value,MESSAGE)
290 #define creaDecTab(key,value)
291 #define creaIncTab(key,value)
292 #define creaResetTab()
295 //===========================================================
303 //===========================================================
305 // Macros for debug messages
307 #ifdef CREA_COMPILE_DEBUG_MESSAGES
309 #define creaDebugMessage(key,value,MESSAGE) creaMessage(key,value,MESSAGE)
311 #define creaDebugMessageCont(key,value,MESSAGE) creaMessageCont(key,value,MESSAGE)
313 #define creaDebugMessageInc(key,value,MESSAGE) creaMessageInc(key,value,MESSAGE)
315 #define creaDebugMessageDec(key,value,MESSAGE) creaMessageDec(key,value,MESSAGE)
317 #define creaDebugDecTab(key,value) creaDecTab(key,value)
319 #define creaDebugIncTab(key,value) creaIncTab(key,value)
321 #define creaDebugResetTab() creaResetTab()
325 #define creaDebugMessage(key,value,MESSAGE)
327 #define creaDebugMessageCont(key,value,MESSAGE)
329 #define creaDebugMessageInc(key,value,MESSAGE)
331 #define creaDebugMessageDec(key,value,MESSAGE)
333 #define creaDebugDecTab(key,value)
335 #define creaDebugIncTab(key,value)
339 //===========================================================
343 //===========================================================
345 #ifdef CREA_COMPILE_WARNING_MESSAGES
346 #define creaWarning(MESSAGE) \
349 int lev = crea::MessageManager::GetMessageLevel("warning"); \
352 std::cerr << "!! WARNING !! " << MESSAGE << std::endl; \
355 std::cerr << "!! WARNING !! In file '"<<__FILE__ \
356 <<"' ; Line "<<__LINE__<<std::endl; \
363 #define creaWarning(MESSAGE)
366 //===========================================================
372 //===========================================================
374 #ifdef CREA_COMPILE_ERROR_MESSAGES
375 //#include "creaWx.h"
376 #define creaError(MESSAGE) \
379 std::ostringstream s; \
381 std::ostringstream f; \
382 f << __FILE__ << " (l."<<__LINE__<<")"; \
383 crea::Exception e( CREA_GET_CURRENT_OBJECT_NAME, \
392 #define creaGlobalError(MESSAGE) \
395 std::ostringstream s; \
397 std::ostringstream f; \
398 f << __FILE__ << " (l."<<__LINE__<<")"; \
399 crea::Exception e( "global scope", \
408 #define CREA_INTERNAL_ERROR_MESSAGE \
409 "\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"
411 #define creaInternalError(MESSAGE) \
414 std::ostringstream s; \
415 s << MESSAGE << CREA_INTERNAL_ERROR_MESSAGE; \
416 std::ostringstream f; \
417 f << __FILE__ << " (l."<<__LINE__<<")"; \
418 crea::Exception e( CREA_GET_CURRENT_OBJECT_NAME, \
428 #define creaError(MESSAGE)
429 #define creaGlobalError(MESSAGE)
430 #define creaInternalError(MESSAGE)
433 //===========================================================
437 //===========================================================
439 #define creaendl std::endl
441 //===========================================================
453 class CREA_EXPORT MessageManager
459 //=============================================
461 typedef boost::signal<void (const std::string&)> MessageSignalType;
463 typedef MessageSignalType::slot_function_type MessageCallbackType;
465 //=============================================
477 static MessageManager* GetInstance();
481 static void RegisterMessageType(const std::string& key,
483 const std::string& help,
485 unsigned char default_level = 9);
489 static void SetMessageLevel(const std::string& key, unsigned char level);
493 static int GetMessageLevel(const std::string& key);
497 static void SendMessage(const std::string& key, const std::string& mess);
501 static void AddMessageObserver(const std::string& key, MessageCallbackType callback );
505 static void SendMessagesToCout(bool v = true);
509 static std::string& GetTab() { static std::string s; return s; }
513 static std::string GetSpace(int n) {
515 std::string s; s.insert(0," ",n); return s; }
519 static void IncTab() { GetTab() += std::string(" "); }
523 static void DecTab() { GetTab() = GetTab().substr(0,GetTab().length()-1); }
527 static void ResetTab() { GetTab() = std::string(""); }
531 static void PrintInfo();
543 MessageType(int l, const std::string& h) : Level(l), Help(h) {}
549 MessageSignalType Signal;
553 typedef std::map<std::string,MessageType*> MessageMapType;
555 MessageMapType mMessageMap;
557 unsigned int mMaxMessageLength;
563 //===========================================================
571 #include "creaException.h"