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
64 // Comment out these symbols to prevent compilation
65 //#define CREA_COMPILE_MESSAGES
66 //#define CREA_COMPILE_DEBUG_MESSAGES
67 //#define CREA_COMPILE_WARNING_MESSAGES
68 //#define CREA_COMPILE_ERROR_MESSAGES
71 #define creaOnMessageLevel(key,value) \
72 int __creaOnMessageLevelVariable = \
73 crea::MessageManager::GetMessageLevel(key); \
74 if ( __creaOnMessageLevelVariable<0) \
76 creaWarning("message type '"<<key<<"' unknown"); \
78 else if (value<= __creaOnMessageLevelVariable)
80 #ifdef CREA_PREPEND_MESSAGE_WITH_CODE
81 #define creaMessageCode \
82 key[0] << key[1] << key[2] << value << " "
84 #define creaMessageCode ""
87 #ifdef CREA_PREPEND_MESSAGE_WITH_TAB
88 #define creaMessageTab \
89 crea::MessageManager::GetTab()
91 #define creaMessageTab ""
94 //#define CREA_PREPEND_MESSAGE_WITH_SPACE
95 #ifdef CREA_PREPEND_MESSAGE_WITH_SPACE
96 #define creaMessageSpace(value) \
97 crea::MessageManager::GetSpace(value)
99 #define creaMessageSpace(value) ""
103 //===========================================================
104 #ifdef CREA_COMPILE_MESSAGES
106 // Macro for messages
107 #define creaMessage(key,value,MESSAGE) \
109 creaOnMessageLevel(key,value) \
111 std::ostringstream s; \
112 s << creaMessageCode \
114 << creaMessageSpace(value) \
116 crea::MessageManager::SendMessage(key,s.str()); \
121 // Macro for continuing a message (when one wants to split the macro
122 // call into multiple lines)
123 #define creaMessageCont(key,value,MESSAGE) \
126 creaOnMessageLevel(key,value) \
128 std::ostringstream s; \
130 crea::MessageManager::SendMessage(key,s.str()); \
135 #define creaMessageInc(key,value,MESSAGE) \
138 std::ostringstream s; \
139 s << creaMessageCode \
141 << creaMessageSpace(value) \
143 crea::MessageManager::SendMessage(key,s.str()); \
144 crea::MessageManager::IncTab(); \
149 #define creaMessageDec(key,value,MESSAGE) \
152 creaOnMessageLevel(key,value) \
154 crea::MessageManager::DecTab(); \
155 std::ostringstream s; \
156 s << creaMessageCode \
158 << creaMessageSpace(value) \
160 crea::MessageManager::SendMessage(key,s.str()); \
165 #define creaDecTab(key,value) \
168 creaOnMessageLevel(key,value) \
170 crea::MessageManager::DecTab(); \
175 #define creaIncTab(key,value) \
178 creaOnMessageLevel(key,value) \
180 crea::MessageManager::IncTab(); \
185 #define creaResetTab() \
188 crea::MessageManager::ResetTab(); \
193 #define creaMessage(key,value,MESSAGE)
194 #define creaMessageInc(key,value,MESSAGE)
195 #define creaMessageDec(key,value,MESSAGE)
196 #define creaMessageCont(key,value,MESSAGE)
197 #define creaDecTab(key,value)
198 #define creaIncTab(key,value)
199 #define creaResetTab()
201 //===========================================================
205 //===========================================================
206 // Macros for debug messages
207 #ifdef CREA_COMPILE_DEBUG_MESSAGES
208 #define creaDebugMessage(key,value,MESSAGE) creaMessage(key,value,MESSAGE)
209 #define creaDebugMessageCont(key,value,MESSAGE) creaMessageCont(key,value,MESSAGE)
210 #define creaDebugMessageInc(key,value,MESSAGE) creaMessageInc(key,value,MESSAGE)
211 #define creaDebugMessageDec(key,value,MESSAGE) creaMessageDec(key,value,MESSAGE)
212 #define creaDebugDecTab(key,value) creaDecTab(key,value)
213 #define creaDebugIncTab(key,value) creaIncTab(key,value)
214 #define creaDebugResetTab() creaResetTab()
216 #define creaDebugMessage(key,value,MESSAGE)
217 #define creaDebugMessageCont(key,value,MESSAGE)
218 #define creaDebugMessageInc(key,value,MESSAGE)
219 #define creaDebugMessageDec(key,value,MESSAGE)
220 #define creaDebugDecTab(key,value)
221 #define creaDebugIncTab(key,value)
223 //===========================================================
225 //===========================================================
226 #ifdef CREA_COMPILE_WARNING_MESSAGES
227 #define creaWarning(MESSAGE) \
230 int lev = crea::MessageManager::GetMessageLevel("warning"); \
233 std::cerr << "!! WARNING !! " << MESSAGE << std::endl; \
236 std::cerr << "!! WARNING !! In file '"<<__FILE__ \
237 <<"' ; Line "<<__LINE__<<std::endl; \
244 #define creaWarning(MESSAGE)
246 //===========================================================
249 //===========================================================
250 #ifdef CREA_COMPILE_ERROR_MESSAGES
251 //#include "creaWx.h"
252 #define creaError(MESSAGE) \
255 std::ostringstream s; \
257 std::ostringstream f; \
258 f << __FILE__ << " (l."<<__LINE__<<")"; \
259 crea::Exception e( CREA_GET_CURRENT_OBJECT_NAME, \
266 #define creaGlobalError(MESSAGE) \
269 std::ostringstream s; \
271 std::ostringstream f; \
272 f << __FILE__ << " (l."<<__LINE__<<")"; \
273 crea::Exception e( "global scope", \
280 #define CREA_INTERNAL_ERROR_MESSAGE \
281 "\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"
283 #define creaInternalError(MESSAGE) \
286 std::ostringstream s; \
287 s << MESSAGE << CREA_INTERNAL_ERROR_MESSAGE; \
288 std::ostringstream f; \
289 f << __FILE__ << " (l."<<__LINE__<<")"; \
290 crea::Exception e( CREA_GET_CURRENT_OBJECT_NAME, \
298 #define creaError(MESSAGE)
299 #define creaGlobalError(MESSAGE)
300 #define creaInternalError(MESSAGE)
302 //===========================================================
304 //===========================================================
305 #define creaendl std::endl
306 //===========================================================
308 // Signal/slot mechanism for message events
309 #include <boost/signal.hpp>
310 #include <boost/bind.hpp>
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"