X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=src%2FcreaMessageManager.h;h=5aaa6ec7526db1998699a29100fad4d8d2736468;hb=refs%2Fheads%2FcreaDevManager;hp=246d7e1b768fc4182ddfdff90c7382357deff46b;hpb=596488a9529ffbedea2e6693e32e9b19f042b7e7;p=crea.git diff --git a/src/creaMessageManager.h b/src/creaMessageManager.h index 246d7e1..5aaa6ec 100644 --- a/src/creaMessageManager.h +++ b/src/creaMessageManager.h @@ -1,8 +1,32 @@ +/* +# --------------------------------------------------------------------- +# +# Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image +# pour la Santé) +# Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton +# Previous Authors : Laurent Guigues, Jean-Pierre Roux +# CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil +# +# This software is governed by the CeCILL-B license under French law and +# abiding by the rules of distribution of free software. You can use, +# modify and/ or redistribute the software under the terms of the CeCILL-B +# license as circulated by CEA, CNRS and INRIA at the following URL +# http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html +# or in the file LICENSE.txt. +# +# As a counterpart to the access to the source code and rights to copy, +# modify and redistribute granted by the license, users are provided only +# with a limited warranty and the software's author, the holder of the +# economic rights, and the successive licensors have only limited +# liability. +# +# The fact that you are presently reading this means that you have had +# knowledge of the CeCILL-B license and that you accept its terms. +# ------------------------------------------------------------------------ +*/ /*! \file - \brief Class creaMessageManager and Macros for outputing messages in crea - There are 4 kinds of messages : - Messages (normal messages) - Debug messages (not compiled in release) @@ -17,57 +41,45 @@ -The first string is the type of the message (the category which will be used to generate a message of this type) -The second string is help string -The integer is the initial level for the messages of this type (see below). - To generate a message of a known type then use one of the macros : creaMessage, creaDebugMessage, creaWarning, creaError or their variants. - example : - creaMessage("Kernel",4,"problem with "< +#include #include #include #include #include - // Comment out these symbols to prevent compilation -//#define crea_COMPILE_MESSAGES -//#define crea_COMPILE_DEBUG_MESSAGES -//#define crea_COMPILE_WARNING_MESSAGES -//#define crea_COMPILE_ERROR_MESSAGES - - +//#define CREA_COMPILE_MESSAGES +//#define CREA_COMPILE_DEBUG_MESSAGES +//#define CREA_COMPILE_WARNING_MESSAGES +//#define CREA_COMPILE_ERROR_MESSAGES #define creaOnMessageLevel(key,value) \ int __creaOnMessageLevelVariable = \ crea::MessageManager::GetMessageLevel(key); \ @@ -77,85 +89,100 @@ } \ else if (value<= __creaOnMessageLevelVariable) -#ifdef crea_PREPEND_MESSAGE_WITH_CODE +#ifdef CREA_PREPEND_MESSAGE_WITH_CODE #define creaMessageCode \ key[0] << key[1] << key[2] << value << " " #else #define creaMessageCode "" #endif -#ifdef crea_PREPEND_MESSAGE_WITH_TAB +#ifdef CREA_PREPEND_MESSAGE_WITH_TAB #define creaMessageTab \ crea::MessageManager::GetTab() #else #define creaMessageTab "" #endif -#define crea_PREPEND_MESSAGE_WITH_SPACE -#ifdef crea_PREPEND_MESSAGE_WITH_SPACE +//#define CREA_PREPEND_MESSAGE_WITH_SPACE +#ifdef CREA_PREPEND_MESSAGE_WITH_SPACE #define creaMessageSpace(value) \ crea::MessageManager::GetSpace(value) #else #define creaMessageSpace(value) "" #endif + + + //=========================================================== -#ifdef crea_COMPILE_MESSAGES + +#ifdef CREA_COMPILE_MESSAGES // Macro for messages #define creaMessage(key,value,MESSAGE) \ do { \ creaOnMessageLevel(key,value) \ { \ - std::cout << creaMessageCode \ - << creaMessageTab \ - << creaMessageSpace(value) \ - << MESSAGE; \ + std::ostringstream s; \ + s << creaMessageCode \ + << creaMessageTab \ + << creaMessageSpace(value) \ + << MESSAGE; \ + crea::MessageManager::SendMessage(key,s.str()); \ } \ } \ while (0) + + // Macro for continuing a message (when one wants to split the macro // call into multiple lines) -#define creaMessageCont(key,value,MESSAGE) \ - do \ - { \ - creaOnMessageLevel(key,value) \ - { \ - std::cout << MESSAGE; \ - } \ - } \ - while (0) - -#define creaMessageInc(key,value,MESSAGE) \ +#define creaMessageCont(key,value,MESSAGE) \ do \ { \ creaOnMessageLevel(key,value) \ { \ - std::cout << creaMessageCode \ - << creaMessageTab \ - << creaMessageSpace(value) \ - << MESSAGE; \ - crea::MessageManager::IncTab(); \ + std::ostringstream s; \ + s << MESSAGE; \ + crea::MessageManager::SendMessage(key,s.str()); \ } \ } \ while (0) -#define creaMessageDec(key,value,MESSAGE) \ + +#define creaMessageInc(key,value,MESSAGE) \ do \ { \ - creaOnMessageLevel(key,value) \ - { \ - crea::MessageManager::DecTab(); \ - std::cout << creaMessageCode \ - << creaMessageTab \ - << creaMessageSpace(value) \ - << MESSAGE; \ + std::ostringstream s; \ + s << creaMessageCode \ + << creaMessageTab \ + << creaMessageSpace(value) \ + << MESSAGE; \ + crea::MessageManager::SendMessage(key,s.str()); \ + crea::MessageManager::IncTab(); \ } \ } \ while (0) + +#define creaMessageDec(key,value,MESSAGE) \ + do \ + { \ + creaOnMessageLevel(key,value) \ + { \ + crea::MessageManager::DecTab(); \ + std::ostringstream s; \ + s << creaMessageCode \ + << creaMessageTab \ + << creaMessageSpace(value) \ + << MESSAGE; \ + crea::MessageManager::SendMessage(key,s.str()); \ + } \ + } \ + while (0) + + #define creaDecTab(key,value) \ do \ { \ @@ -166,6 +193,7 @@ } \ while (0) + #define creaIncTab(key,value) \ do \ { \ @@ -176,6 +204,7 @@ } \ while (0) + #define creaResetTab() \ do \ { \ @@ -192,110 +221,62 @@ #define creaIncTab(key,value) #define creaResetTab() #endif + //=========================================================== + + + + //=========================================================== -#ifdef crea_COMPILE_DEBUG_MESSAGES -// Macro for debug messages -#define creaDebugMessage(key,value,MESSAGE) \ - do \ - { \ - creaOnMessageLevel(key,value) \ - { \ - std::cout << creaMessageCode \ - << creaMessageTab \ - << creaMessageSpace(value) \ - << MESSAGE; \ - } \ - } \ - while (0) +// Macros for debug messages -// Macro for continuing a debug message (when one wants to split the -// macro call into multiple lines) -#define creaDebugMessageCont(key,value,MESSAGE) \ - do \ - { \ - creaOnMessageLevel(key,value) \ - { \ - std::cout << MESSAGE; \ - } \ - } \ - while (0) +#ifdef CREA_COMPILE_DEBUG_MESSAGES -#define creaDebugMessageInc(key,value,MESSAGE) \ - do \ - { \ - creaOnMessageLevel(key,value) \ - { \ - std::cout << creaMessageCode \ - << creaMessageTab \ - << creaMessageSpace(value) \ - << MESSAGE; \ - crea::MessageManager::IncTab(); \ - } \ - } \ - while (0) +#define creaDebugMessage(key,value,MESSAGE) creaMessage(key,value,MESSAGE) -#define creaDebugMessageDec(key,value,MESSAGE) \ - do \ - { \ - creaOnMessageLevel(key,value) \ - { \ - crea::MessageManager::DecTab(); \ - std::cout << creaMessageCode \ - << creaMessageTab \ - << creaMessageSpace(value) \ - << MESSAGE; \ - } \ - } \ - while (0) +#define creaDebugMessageCont(key,value,MESSAGE) creaMessageCont(key,value,MESSAGE) -#define creaDebugDecTab(key,value) \ - do \ - { \ - creaOnMessageLevel(key,value) \ - { \ - crea::MessageManager::DecTab(); \ - } \ - } \ - while (0) +#define creaDebugMessageInc(key,value,MESSAGE) creaMessageInc(key,value,MESSAGE) -#define creaDebugIncTab(key,value) \ - do \ - { \ - creaOnMessageLevel(key,value) \ - { \ - crea::MessageManager::IncTab(); \ - } \ - } \ - while (0) - -#define creaDebugResetTab() \ - do \ - { \ - crea::MessageManager::ResetTab(); \ - } \ - while (0) +#define creaDebugMessageDec(key,value,MESSAGE) creaMessageDec(key,value,MESSAGE) + +#define creaDebugDecTab(key,value) creaDecTab(key,value) + +#define creaDebugIncTab(key,value) creaIncTab(key,value) + +#define creaDebugResetTab() creaResetTab() #else + #define creaDebugMessage(key,value,MESSAGE) + #define creaDebugMessageCont(key,value,MESSAGE) + #define creaDebugMessageInc(key,value,MESSAGE) + #define creaDebugMessageDec(key,value,MESSAGE) + #define creaDebugDecTab(key,value) + #define creaDebugIncTab(key,value) + #endif + //=========================================================== + + //=========================================================== -#ifdef crea_COMPILE_WARNING_MESSAGES + +#ifdef CREA_COMPILE_WARNING_MESSAGES #define creaWarning(MESSAGE) \ do \ { \ - int lev = crea::MessageManager::GetMessageLevel("Warning"); \ + int lev = crea::MessageManager::GetMessageLevel("warning"); \ if (lev >0) \ { \ std::cerr << "!! WARNING !! " << MESSAGE << std::endl; \ @@ -311,11 +292,16 @@ #else #define creaWarning(MESSAGE) #endif + //=========================================================== + + + //=========================================================== -#ifdef crea_COMPILE_ERROR_MESSAGES + +#ifdef CREA_COMPILE_ERROR_MESSAGES //#include "creaWx.h" #define creaError(MESSAGE) \ do \ @@ -331,6 +317,8 @@ } \ while (0) + + #define creaGlobalError(MESSAGE) \ do \ { \ @@ -345,14 +333,16 @@ } \ while (0) -#define crea_INTERNAL_ERROR_MESSAGE \ - "\n\n***********************************************\n**** THIS IS AN INTERNAL ERROR TO crea ****\n**** Please send a full bug report to : ****\n**** crea-developers@creatis.insa-lyon.fr ****\n***********************************************\n\n" + + +#define CREA_INTERNAL_ERROR_MESSAGE \ + "\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" #define creaInternalError(MESSAGE) \ do \ { \ std::ostringstream s; \ - s << MESSAGE << crea_INTERNAL_ERROR_MESSAGE; \ + s << MESSAGE << CREA_INTERNAL_ERROR_MESSAGE; \ std::ostringstream f; \ f << __FILE__ << " (l."<<__LINE__<<")"; \ crea::Exception e( CREA_GET_CURRENT_OBJECT_NAME, \ @@ -362,61 +352,155 @@ } \ while (0) + + #else #define creaError(MESSAGE) #define creaGlobalError(MESSAGE) #define creaInternalError(MESSAGE) #endif + //=========================================================== + + //=========================================================== + #define creaendl std::endl + //=========================================================== + + + namespace crea + { + + class CREA_EXPORT MessageManager + { + public: + + //============================================= + + typedef boost::signal MessageSignalType; + + typedef MessageSignalType::slot_function_type MessageCallbackType; + + //============================================= + /// + MessageManager(); + /// + ~MessageManager(); + /// + static MessageManager* GetInstance(); + /// - static void RegisterMessageType(std::string key, - std::string help, + + static void RegisterMessageType(const std::string& key, + + const std::string& help, + unsigned char default_level = 9); + /// - static void SetMessageLevel(std::string key, unsigned char level); + + static void SetMessageLevel(const std::string& key, unsigned char level); + /// - static int GetMessageLevel(std::string key); + + static int GetMessageLevel(const std::string& key); + /// + + static void SendMessage(const std::string& key, const std::string& mess); + + /// + + static void AddMessageObserver(const std::string& key, MessageCallbackType callback ); + + /// + + static void SendMessagesToCout(bool v = true); + + /// + static std::string& GetTab() { static std::string s; return s; } + /// + static std::string GetSpace(int n) { + std::string s; s.insert(0," ",n); return s; } + /// + static void IncTab() { GetTab() += std::string(" "); } + /// + static void DecTab() { GetTab() = GetTab().substr(0,GetTab().length()-1); } + /// + static void ResetTab() { GetTab() = std::string(""); } + /// + static void PrintInfo(); + /// + + + private: - std::map mMessageLevel; - std::map mMessageHelp; + + struct MessageType + + { + + MessageType(int l, const std::string& h) : Level(l), Help(h) {} + + int Level; + + std::string Help; + + MessageSignalType Signal; + + }; + + typedef std::map MessageMapType; + + MessageMapType mMessageMap; + unsigned int mMaxMessageLength; + + bool mSendToCout; + }; + //=========================================================== + + } + + #include "creaException.h" + + #endif +