4 \brief Class creaMessageManager and Macros for outputing messages in crea
6 There are 4 kinds of messages :
7 - Messages (normal messages)
8 - Debug messages (not compiled in release)
11 There are also "types" of messages which are strings which identify the nature of the message
12 (for example : "Kernel" messages are generated by the core classes of the library, there can be a type of
13 message for each type of Node, and so on...)
14 A type of message must be declared by registering it into the MessageManager. This is done by a line like :
15 crea::MessageManager::RegisterMessageType("Kernel","Messages generated by the core classes of the library",5);
17 -The first string is the type of the message (the category which will be used to generate a message of this type)
18 -The second string is help string
19 -The integer is the initial level for the messages of this type (see below).
21 To generate a message of a known type then use one of the macros :
22 creaMessage, creaDebugMessage, creaWarning, creaError or their variants.
26 creaMessage("Kernel",4,"problem with "<<GetName()<<creaendl);
28 will push the 3rd argument in std::cout if the message level of "Kernel" messages is greater or equal to 4.
29 which means that it generates a message of level 4 (0 : very important/always displayed ... 9 : deep debug message).
31 At run time, one is able to change the level of the messages displayed by using a command like :
33 crea::MessageManager::SetMessageLevel("Kernel",5);
35 which tells the manager to display all Kernel messages of level up to 5.
39 crea*Cont : continues a previous creaMessage on the same line (without rewriting the type and level)
40 crea*Inc / Dec : displays the message and then increments/decrement the messages tabulation
43 //===========================================================
45 \class crea::MessageManager
46 \brief Manages the messages displayed by crea
50 #ifndef __creaMessageManager_h__
51 #define __creaMessageManager_h__
53 // The do { } while(0) statement in macros is made to "swallow the semicolon"
54 // see http://gcc.gnu.org/onlinedocs/cpp/Swallowing-the-Semicolon.html#Swallowing-the-Semicolon
56 #include "creaSystem.h"
57 #include "creaRTTI.h" // for CREA_GET_CURRENT_OBJECT_NAME
58 // Signal/slot mechanism for message events
59 #include <boost/signal.hpp>
60 #include <boost/bind.hpp>
67 // Comment out these symbols to prevent compilation
68 //#define CREA_COMPILE_MESSAGES
69 //#define CREA_COMPILE_DEBUG_MESSAGES
70 //#define CREA_COMPILE_WARNING_MESSAGES
71 //#define CREA_COMPILE_ERROR_MESSAGES
74 #define creaOnMessageLevel(key,value) \
75 int __creaOnMessageLevelVariable = \
76 crea::MessageManager::GetMessageLevel(key); \
77 if ( __creaOnMessageLevelVariable<0) \
79 creaWarning("message type '"<<key<<"' unknown"); \
81 else if (value<= __creaOnMessageLevelVariable)
83 #ifdef CREA_PREPEND_MESSAGE_WITH_CODE
84 #define creaMessageCode \
85 key[0] << key[1] << key[2] << value << " "
87 #define creaMessageCode ""
90 #ifdef CREA_PREPEND_MESSAGE_WITH_TAB
91 #define creaMessageTab \
92 crea::MessageManager::GetTab()
94 #define creaMessageTab ""
97 //#define CREA_PREPEND_MESSAGE_WITH_SPACE
98 #ifdef CREA_PREPEND_MESSAGE_WITH_SPACE
99 #define creaMessageSpace(value) \
100 crea::MessageManager::GetSpace(value)
102 #define creaMessageSpace(value) ""
106 //===========================================================
107 #ifdef CREA_COMPILE_MESSAGES
109 // Macro for messages
110 #define creaMessage(key,value,MESSAGE) \
112 creaOnMessageLevel(key,value) \
114 std::ostringstream s; \
115 s << creaMessageCode \
117 << creaMessageSpace(value) \
119 crea::MessageManager::SendMessage(key,s.str()); \
124 // Macro for continuing a message (when one wants to split the macro
125 // call into multiple lines)
126 #define creaMessageCont(key,value,MESSAGE) \
129 creaOnMessageLevel(key,value) \
131 std::ostringstream s; \
133 crea::MessageManager::SendMessage(key,s.str()); \
138 #define creaMessageInc(key,value,MESSAGE) \
141 std::ostringstream s; \
142 s << creaMessageCode \
144 << creaMessageSpace(value) \
146 crea::MessageManager::SendMessage(key,s.str()); \
147 crea::MessageManager::IncTab(); \
152 #define creaMessageDec(key,value,MESSAGE) \
155 creaOnMessageLevel(key,value) \
157 crea::MessageManager::DecTab(); \
158 std::ostringstream s; \
159 s << creaMessageCode \
161 << creaMessageSpace(value) \
163 crea::MessageManager::SendMessage(key,s.str()); \
168 #define creaDecTab(key,value) \
171 creaOnMessageLevel(key,value) \
173 crea::MessageManager::DecTab(); \
178 #define creaIncTab(key,value) \
181 creaOnMessageLevel(key,value) \
183 crea::MessageManager::IncTab(); \
188 #define creaResetTab() \
191 crea::MessageManager::ResetTab(); \
196 #define creaMessage(key,value,MESSAGE)
197 #define creaMessageInc(key,value,MESSAGE)
198 #define creaMessageDec(key,value,MESSAGE)
199 #define creaMessageCont(key,value,MESSAGE)
200 #define creaDecTab(key,value)
201 #define creaIncTab(key,value)
202 #define creaResetTab()
204 //===========================================================
208 //===========================================================
209 // Macros for debug messages
210 #ifdef CREA_COMPILE_DEBUG_MESSAGES
211 #define creaDebugMessage(key,value,MESSAGE) creaMessage(key,value,MESSAGE)
212 #define creaDebugMessageCont(key,value,MESSAGE) creaMessageCont(key,value,MESSAGE)
213 #define creaDebugMessageInc(key,value,MESSAGE) creaMessageInc(key,value,MESSAGE)
214 #define creaDebugMessageDec(key,value,MESSAGE) creaMessageDec(key,value,MESSAGE)
215 #define creaDebugDecTab(key,value) creaDecTab(key,value)
216 #define creaDebugIncTab(key,value) creaIncTab(key,value)
217 #define creaDebugResetTab() creaResetTab()
219 #define creaDebugMessage(key,value,MESSAGE)
220 #define creaDebugMessageCont(key,value,MESSAGE)
221 #define creaDebugMessageInc(key,value,MESSAGE)
222 #define creaDebugMessageDec(key,value,MESSAGE)
223 #define creaDebugDecTab(key,value)
224 #define creaDebugIncTab(key,value)
226 //===========================================================
228 //===========================================================
229 #ifdef CREA_COMPILE_WARNING_MESSAGES
230 #define creaWarning(MESSAGE) \
233 int lev = crea::MessageManager::GetMessageLevel("warning"); \
236 std::cerr << "!! WARNING !! " << MESSAGE << std::endl; \
239 std::cerr << "!! WARNING !! In file '"<<__FILE__ \
240 <<"' ; Line "<<__LINE__<<std::endl; \
247 #define creaWarning(MESSAGE)
249 //===========================================================
252 //===========================================================
253 #ifdef CREA_COMPILE_ERROR_MESSAGES
254 //#include "creaWx.h"
255 #define creaError(MESSAGE) \
258 std::ostringstream s; \
260 std::ostringstream f; \
261 f << __FILE__ << " (l."<<__LINE__<<")"; \
262 crea::Exception e( CREA_GET_CURRENT_OBJECT_NAME, \
269 #define creaGlobalError(MESSAGE) \
272 std::ostringstream s; \
274 std::ostringstream f; \
275 f << __FILE__ << " (l."<<__LINE__<<")"; \
276 crea::Exception e( "global scope", \
283 #define CREA_INTERNAL_ERROR_MESSAGE \
284 "\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"
286 #define creaInternalError(MESSAGE) \
289 std::ostringstream s; \
290 s << MESSAGE << CREA_INTERNAL_ERROR_MESSAGE; \
291 std::ostringstream f; \
292 f << __FILE__ << " (l."<<__LINE__<<")"; \
293 crea::Exception e( CREA_GET_CURRENT_OBJECT_NAME, \
301 #define creaError(MESSAGE)
302 #define creaGlobalError(MESSAGE)
303 #define creaInternalError(MESSAGE)
305 //===========================================================
307 //===========================================================
308 #define creaendl std::endl
309 //===========================================================
315 class CREA_EXPORT MessageManager
318 //=============================================
319 typedef boost::signal<void (const std::string&)> MessageSignalType;
320 typedef MessageSignalType::slot_function_type MessageCallbackType;
321 //=============================================
327 static MessageManager* GetInstance();
329 static void RegisterMessageType(const std::string& key,
330 const std::string& help,
331 unsigned char default_level = 9);
333 static void SetMessageLevel(const std::string& key, unsigned char level);
335 static int GetMessageLevel(const std::string& key);
337 static void SendMessage(const std::string& key, const std::string& mess);
339 static void AddMessageObserver(const std::string& key, MessageCallbackType callback );
341 static void SendMessagesToCout(bool v = true);
343 static std::string& GetTab() { static std::string s; return s; }
345 static std::string GetSpace(int n) {
346 std::string s; s.insert(0," ",n); return s; }
348 static void IncTab() { GetTab() += std::string(" "); }
350 static void DecTab() { GetTab() = GetTab().substr(0,GetTab().length()-1); }
352 static void ResetTab() { GetTab() = std::string(""); }
354 static void PrintInfo();
360 MessageType(int l, const std::string& h) : Level(l), Help(h) {}
363 MessageSignalType Signal;
365 typedef std::map<std::string,MessageType*> MessageMapType;
366 MessageMapType mMessageMap;
367 unsigned int mMaxMessageLength;
370 //===========================================================
374 #include "creaException.h"