2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
10 # This software is governed by the CeCILL-B license under French law and
11 # abiding by the rules of distribution of free software. You can use,
12 # modify and/ or redistribute the software under the terms of the CeCILL-B
13 # license as circulated by CEA, CNRS and INRIA at the following URL
14 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15 # or in the file LICENSE.txt.
17 # As a counterpart to the access to the source code and rights to copy,
18 # modify and redistribute granted by the license, users are provided only
19 # with a limited warranty and the software's author, the holder of the
20 # economic rights, and the successive licensors have only limited
23 # The fact that you are presently reading this means that you have had
24 # knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------
29 \brief Class creaMessageManager and Macros for outputing messages in crea
30 There are 4 kinds of messages :
31 - Messages (normal messages)
32 - Debug messages (not compiled in release)
35 There are also "types" of messages which are strings which identify the nature of the message
36 (for example : "Kernel" messages are generated by the core classes of the library, there can be a type of
37 message for each type of Node, and so on...)
38 A type of message must be declared by registering it into the MessageManager. This is done by a line like :
39 crea::MessageManager::RegisterMessageType("Kernel","Messages generated by the core classes of the library",5);
41 -The first string is the type of the message (the category which will be used to generate a message of this type)
42 -The second string is help string
43 -The integer is the initial level for the messages of this type (see below).
44 To generate a message of a known type then use one of the macros :
45 creaMessage, creaDebugMessage, creaWarning, creaError or their variants.
47 creaMessage("Kernel",4,"problem with "<<GetName()<<creaendl);
48 will push the 3rd argument in std::cout if the message level of "Kernel" messages is greater or equal to 4.
49 which means that it generates a message of level 4 (0 : very important/always displayed ... 9 : deep debug message).
50 At run time, one is able to change the level of the messages displayed by using a command like :
51 crea::MessageManager::SetMessageLevel("Kernel",5);
52 which tells the manager to display all Kernel messages of level up to 5.
54 crea*Cont : continues a previous creaMessage on the same line (without rewriting the type and level)
55 crea*Inc / Dec : displays the message and then increments/decrement the messages tabulation
58 //===========================================================
60 \class crea::MessageManager
61 \brief Manages the messages displayed by crea
64 #ifndef __creaMessageManager_h__
65 #define __creaMessageManager_h__
67 // The do { } while(0) statement in macros is made to "swallow the semicolon"
68 // see http://gcc.gnu.org/onlinedocs/cpp/Swallowing-the-Semicolon.html#Swallowing-the-Semicolon
69 #include "creaSystem.h"
70 #include "creaRTTI.h" // for CREA_GET_CURRENT_OBJECT_NAME
71 // Signal/slot mechanism for message events
72 #include <boost/signal.hpp>
73 #include <boost/bind.hpp>
78 // Comment out these symbols to prevent compilation
79 //#define CREA_COMPILE_MESSAGES
80 //#define CREA_COMPILE_DEBUG_MESSAGES
81 //#define CREA_COMPILE_WARNING_MESSAGES
82 //#define CREA_COMPILE_ERROR_MESSAGES
83 #define creaOnMessageLevel(key,value) \
84 int __creaOnMessageLevelVariable = \
85 crea::MessageManager::GetMessageLevel(key); \
86 if ( __creaOnMessageLevelVariable<0) \
88 creaWarning("message type '"<<key<<"' unknown"); \
90 else if (value<= __creaOnMessageLevelVariable)
92 #ifdef CREA_PREPEND_MESSAGE_WITH_CODE
93 #define creaMessageCode \
94 key[0] << key[1] << key[2] << value << " "
96 #define creaMessageCode ""
99 #ifdef CREA_PREPEND_MESSAGE_WITH_TAB
100 #define creaMessageTab \
101 crea::MessageManager::GetTab()
103 #define creaMessageTab ""
106 //#define CREA_PREPEND_MESSAGE_WITH_SPACE
107 #ifdef CREA_PREPEND_MESSAGE_WITH_SPACE
108 #define creaMessageSpace(value) \
109 crea::MessageManager::GetSpace(value)
111 #define creaMessageSpace(value) ""
118 //===========================================================
120 #ifdef CREA_COMPILE_MESSAGES
122 // Macro for messages
123 #define creaMessage(key,value,MESSAGE) \
125 creaOnMessageLevel(key,value) \
127 std::ostringstream s; \
128 s << creaMessageCode \
130 << creaMessageSpace(value) \
132 crea::MessageManager::SendMessage(key,s.str()); \
139 // Macro for continuing a message (when one wants to split the macro
140 // call into multiple lines)
141 #define creaMessageCont(key,value,MESSAGE) \
144 creaOnMessageLevel(key,value) \
146 std::ostringstream s; \
148 crea::MessageManager::SendMessage(key,s.str()); \
154 #define creaMessageInc(key,value,MESSAGE) \
157 std::ostringstream s; \
158 s << creaMessageCode \
160 << creaMessageSpace(value) \
162 crea::MessageManager::SendMessage(key,s.str()); \
163 crea::MessageManager::IncTab(); \
169 #define creaMessageDec(key,value,MESSAGE) \
172 creaOnMessageLevel(key,value) \
174 crea::MessageManager::DecTab(); \
175 std::ostringstream s; \
176 s << creaMessageCode \
178 << creaMessageSpace(value) \
180 crea::MessageManager::SendMessage(key,s.str()); \
186 #define creaDecTab(key,value) \
189 creaOnMessageLevel(key,value) \
191 crea::MessageManager::DecTab(); \
197 #define creaIncTab(key,value) \
200 creaOnMessageLevel(key,value) \
202 crea::MessageManager::IncTab(); \
208 #define creaResetTab() \
211 crea::MessageManager::ResetTab(); \
216 #define creaMessage(key,value,MESSAGE)
217 #define creaMessageInc(key,value,MESSAGE)
218 #define creaMessageDec(key,value,MESSAGE)
219 #define creaMessageCont(key,value,MESSAGE)
220 #define creaDecTab(key,value)
221 #define creaIncTab(key,value)
222 #define creaResetTab()
225 //===========================================================
233 //===========================================================
235 // Macros for debug messages
237 #ifdef CREA_COMPILE_DEBUG_MESSAGES
239 #define creaDebugMessage(key,value,MESSAGE) creaMessage(key,value,MESSAGE)
241 #define creaDebugMessageCont(key,value,MESSAGE) creaMessageCont(key,value,MESSAGE)
243 #define creaDebugMessageInc(key,value,MESSAGE) creaMessageInc(key,value,MESSAGE)
245 #define creaDebugMessageDec(key,value,MESSAGE) creaMessageDec(key,value,MESSAGE)
247 #define creaDebugDecTab(key,value) creaDecTab(key,value)
249 #define creaDebugIncTab(key,value) creaIncTab(key,value)
251 #define creaDebugResetTab() creaResetTab()
255 #define creaDebugMessage(key,value,MESSAGE)
257 #define creaDebugMessageCont(key,value,MESSAGE)
259 #define creaDebugMessageInc(key,value,MESSAGE)
261 #define creaDebugMessageDec(key,value,MESSAGE)
263 #define creaDebugDecTab(key,value)
265 #define creaDebugIncTab(key,value)
269 //===========================================================
273 //===========================================================
275 #ifdef CREA_COMPILE_WARNING_MESSAGES
276 #define creaWarning(MESSAGE) \
279 int lev = crea::MessageManager::GetMessageLevel("warning"); \
282 std::cerr << "!! WARNING !! " << MESSAGE << std::endl; \
285 std::cerr << "!! WARNING !! In file '"<<__FILE__ \
286 <<"' ; Line "<<__LINE__<<std::endl; \
293 #define creaWarning(MESSAGE)
296 //===========================================================
302 //===========================================================
304 #ifdef CREA_COMPILE_ERROR_MESSAGES
305 //#include "creaWx.h"
306 #define creaError(MESSAGE) \
309 std::ostringstream s; \
311 std::ostringstream f; \
312 f << __FILE__ << " (l."<<__LINE__<<")"; \
313 crea::Exception e( CREA_GET_CURRENT_OBJECT_NAME, \
322 #define creaGlobalError(MESSAGE) \
325 std::ostringstream s; \
327 std::ostringstream f; \
328 f << __FILE__ << " (l."<<__LINE__<<")"; \
329 crea::Exception e( "global scope", \
338 #define CREA_INTERNAL_ERROR_MESSAGE \
339 "\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"
341 #define creaInternalError(MESSAGE) \
344 std::ostringstream s; \
345 s << MESSAGE << CREA_INTERNAL_ERROR_MESSAGE; \
346 std::ostringstream f; \
347 f << __FILE__ << " (l."<<__LINE__<<")"; \
348 crea::Exception e( CREA_GET_CURRENT_OBJECT_NAME, \
358 #define creaError(MESSAGE)
359 #define creaGlobalError(MESSAGE)
360 #define creaInternalError(MESSAGE)
363 //===========================================================
367 //===========================================================
369 #define creaendl std::endl
371 //===========================================================
383 class CREA_EXPORT MessageManager
389 //=============================================
391 typedef boost::signal<void (const std::string&)> MessageSignalType;
393 typedef MessageSignalType::slot_function_type MessageCallbackType;
395 //=============================================
407 static MessageManager* GetInstance();
411 static void RegisterMessageType(const std::string& key,
413 const std::string& help,
415 unsigned char default_level = 9);
419 static void SetMessageLevel(const std::string& key, unsigned char level);
423 static int GetMessageLevel(const std::string& key);
427 static void SendMessage(const std::string& key, const std::string& mess);
431 static void AddMessageObserver(const std::string& key, MessageCallbackType callback );
435 static void SendMessagesToCout(bool v = true);
439 static std::string& GetTab() { static std::string s; return s; }
443 static std::string GetSpace(int n) {
445 std::string s; s.insert(0," ",n); return s; }
449 static void IncTab() { GetTab() += std::string(" "); }
453 static void DecTab() { GetTab() = GetTab().substr(0,GetTab().length()-1); }
457 static void ResetTab() { GetTab() = std::string(""); }
461 static void PrintInfo();
473 MessageType(int l, const std::string& h) : Level(l), Help(h) {}
479 MessageSignalType Signal;
483 typedef std::map<std::string,MessageType*> MessageMapType;
485 MessageMapType mMessageMap;
487 unsigned int mMaxMessageLength;
493 //===========================================================
501 #include "creaException.h"