bbtkMessageManager.h

Go to the documentation of this file.
00001 /*=========================================================================                                                                               
00002   Program:   bbtk
00003   Module:    $RCSfile: bbtkMessageManager.h,v $
00004   Language:  C++
00005   Date:      $Date: 2008/10/17 08:18:13 $
00006   Version:   $Revision: 1.7 $
00007 =========================================================================*/
00008 
00009 /* ---------------------------------------------------------------------
00010 
00011 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
00012 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
00013 *
00014 *  This software is governed by the CeCILL-B license under French law and 
00015 *  abiding by the rules of distribution of free software. You can  use, 
00016 *  modify and/ or redistribute the software under the terms of the CeCILL-B 
00017 *  license as circulated by CEA, CNRS and INRIA at the following URL 
00018 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
00019 *  or in the file LICENSE.txt.
00020 *
00021 *  As a counterpart to the access to the source code and  rights to copy,
00022 *  modify and redistribute granted by the license, users are provided only
00023 *  with a limited warranty  and the software's author,  the holder of the
00024 *  economic rights,  and the successive licensors  have only  limited
00025 *  liability. 
00026 *
00027 *  The fact that you are presently reading this means that you have had
00028 *  knowledge of the CeCILL-B license and that you accept its terms.
00029 * ------------------------------------------------------------------------ */                                                                         
00030 
00031 
00032 
00074   //===========================================================
00081 #ifndef __bbtkMessageManager_h__
00082 #define __bbtkMessageManager_h__
00083 
00084 // The do { } while(0) statement in macros is made to "swallow the semicolon" 
00085 // see http://gcc.gnu.org/onlinedocs/cpp/Swallowing-the-Semicolon.html#Swallowing-the-Semicolon
00086 
00087 #include "bbtkSystem.h"
00088 #include "bbtkRTTI.h"
00089 
00090 #include <string>
00091 #include <map>
00092 #include <iostream>
00093 #include <sstream>
00094 
00095 // Comment out these symbols to prevent compilation 
00096 //#define BBTK_COMPILE_MESSAGES
00097 //#define BBTK_COMPILE_DEBUG_MESSAGES
00098 //#define BBTK_COMPILE_WARNING_MESSAGES
00099 //#define BBTK_COMPILE_ERROR_MESSAGES
00100 
00101 
00102 #define bbtkOnMessageLevel(key,value)                   \
00103   int __bbtkOnMessageLevelVariable =                    \
00104     bbtk::MessageManager::GetMessageLevel(key);         \
00105   if ( __bbtkOnMessageLevelVariable<0)                  \
00106     {                                                   \
00107       bbtkWarning("message type '"<<key<<"' unknown");  \
00108     }                                                   \
00109   else if (value<= __bbtkOnMessageLevelVariable) 
00110 
00111 #ifdef BBTK_PREPEND_MESSAGE_WITH_CODE
00112 #define bbtkMessageCode                         \
00113   key[0] << key[1] << key[2] << value << " "
00114 #else 
00115 #define bbtkMessageCode ""
00116 #endif 
00117 
00118 #ifdef BBTK_PREPEND_MESSAGE_WITH_TAB
00119 #define bbtkMessageTab                          \
00120   bbtk::MessageManager::GetTab()
00121 #else 
00122 #define bbtkMessageTab ""
00123 #endif
00124 
00125 #define BBTK_PREPEND_MESSAGE_WITH_SPACE
00126 #ifdef BBTK_PREPEND_MESSAGE_WITH_SPACE
00127 #define bbtkMessageSpace(value)                 \
00128   bbtk::MessageManager::GetSpace(value)
00129 #else 
00130 #define bbtkMessageSpace(value) ""
00131 #endif
00132  
00133 
00134 //===========================================================
00135 #ifdef BBTK_COMPILE_MESSAGES
00136 
00137 // Macro for messages
00138 #define bbtkMessage(key,value,MESSAGE)                  \
00139   do {                                                  \
00140     bbtkOnMessageLevel(key,value)                       \
00141       {                                                 \
00142         std::cout << bbtkMessageCode                    \
00143                   << bbtkMessageTab                     \
00144                   << bbtkMessageSpace(value)            \
00145                   << MESSAGE;                           \
00146       }                                                 \
00147   }                                                     \
00148   while (0)
00149 
00150 // Macro for continuing a message (when one wants to split the macro
00151 // call into multiple lines)
00152 #define bbtkMessageCont(key,value,MESSAGE)      \
00153   do                                            \
00154     {                                           \
00155       bbtkOnMessageLevel(key,value)             \
00156         {                                       \
00157           std::cout << MESSAGE;                 \
00158         }                                       \
00159     }                                           \
00160   while (0)
00161 
00162 #define bbtkMessageInc(key,value,MESSAGE)               \
00163   do                                                    \
00164     {                                                   \
00165       bbtkOnMessageLevel(key,value)                     \
00166         {                                               \
00167           std::cout << bbtkMessageCode                  \
00168                     << bbtkMessageTab                   \
00169                     << bbtkMessageSpace(value)          \
00170                     << MESSAGE;                         \
00171           bbtk::MessageManager::IncTab();               \
00172         }                                               \
00173     }                                                   \
00174   while (0)
00175 
00176 #define bbtkMessageDec(key,value,MESSAGE)               \
00177   do                                                    \
00178     {                                                   \
00179       bbtkOnMessageLevel(key,value)                     \
00180         {                                               \
00181           bbtk::MessageManager::DecTab();               \
00182           std::cout << bbtkMessageCode                  \
00183                     << bbtkMessageTab                   \
00184                     << bbtkMessageSpace(value)          \
00185                     << MESSAGE;                         \
00186         }                                               \
00187     }                                                   \
00188   while (0)
00189 
00190 #define bbtkDecTab(key,value)                   \
00191   do                                            \
00192     {                                           \
00193       bbtkOnMessageLevel(key,value)             \
00194         {                                       \
00195           bbtk::MessageManager::DecTab();       \
00196         }                                       \
00197     }                                           \
00198   while (0)
00199 
00200 #define bbtkIncTab(key,value)                   \
00201   do                                            \
00202     {                                           \
00203       bbtkOnMessageLevel(key,value)             \
00204         {                                       \
00205           bbtk::MessageManager::IncTab();       \
00206         }                                       \
00207     }                                           \
00208   while (0)
00209 
00210 #define bbtkResetTab()                          \
00211   do                                            \
00212     {                                           \
00213       bbtk::MessageManager::ResetTab();         \
00214     }                                           \
00215   while (0)
00216 
00217 #else
00218 #define bbtkMessage(key,value,MESSAGE)
00219 #define bbtkMessageInc(key,value,MESSAGE)
00220 #define bbtkMessageDec(key,value,MESSAGE)
00221 #define bbtkMessageCont(key,value,MESSAGE)
00222 #define bbtkDecTab(key,value)
00223 #define bbtkIncTab(key,value)
00224 #define bbtkResetTab()
00225 #endif
00226 //===========================================================
00227 
00228 
00229 
00230 //===========================================================
00231 #ifdef BBTK_COMPILE_DEBUG_MESSAGES
00232 
00233 // Macro for debug messages
00234 #define bbtkDebugMessage(key,value,MESSAGE)             \
00235   do                                                    \
00236     {                                                   \
00237       bbtkOnMessageLevel(key,value)                     \
00238         {                                               \
00239           std::cout << bbtkMessageCode                  \
00240                     << bbtkMessageTab                   \
00241                     << bbtkMessageSpace(value)          \
00242                     << MESSAGE;                         \
00243         }                                               \
00244     }                                                   \
00245   while (0)
00246 
00247 // Macro for continuing a debug message (when one wants to split the
00248 // macro call into multiple lines)
00249 #define bbtkDebugMessageCont(key,value,MESSAGE) \
00250   do                                            \
00251     {                                           \
00252       bbtkOnMessageLevel(key,value)             \
00253         {                                       \
00254           std::cout << MESSAGE;                 \
00255         }                                       \
00256     }                                           \
00257   while (0)
00258 
00259 #define bbtkDebugMessageInc(key,value,MESSAGE)          \
00260   do                                                    \
00261     {                                                   \
00262       bbtkOnMessageLevel(key,value)                     \
00263         {                                               \
00264           std::cout << bbtkMessageCode                  \
00265                     << bbtkMessageTab                   \
00266                     << bbtkMessageSpace(value)          \
00267                     << MESSAGE;                         \
00268           bbtk::MessageManager::IncTab();               \
00269         }                                               \
00270     }                                                   \
00271   while (0)
00272 
00273 #define bbtkDebugMessageDec(key,value,MESSAGE)          \
00274   do                                                    \
00275     {                                                   \
00276       bbtkOnMessageLevel(key,value)                     \
00277         {                                               \
00278           bbtk::MessageManager::DecTab();               \
00279           std::cout << bbtkMessageCode                  \
00280                     << bbtkMessageTab                   \
00281                     << bbtkMessageSpace(value)          \
00282                     << MESSAGE;                         \
00283         }                                               \
00284     }                                                   \
00285   while (0)
00286 
00287 #define bbtkDebugDecTab(key,value)              \
00288   do                                            \
00289     {                                           \
00290       bbtkOnMessageLevel(key,value)             \
00291         {                                       \
00292           bbtk::MessageManager::DecTab();       \
00293         }                                       \
00294     }                                           \
00295   while (0)
00296 
00297 #define bbtkDebugIncTab(key,value)              \
00298     do                                          \
00299       {                                         \
00300         bbtkOnMessageLevel(key,value)           \
00301           {                                     \
00302             bbtk::MessageManager::IncTab();     \
00303           }                                     \
00304       }                                         \
00305     while (0)
00306     
00307 #define bbtkDebugResetTab()                     \
00308     do                                          \
00309       {                                         \
00310         bbtk::MessageManager::ResetTab();       \
00311       }                                         \
00312     while (0)
00313 
00314 #else
00315 #define bbtkDebugMessage(key,value,MESSAGE) 
00316 #define bbtkDebugMessageCont(key,value,MESSAGE) 
00317 #define bbtkDebugMessageInc(key,value,MESSAGE)
00318 #define bbtkDebugMessageDec(key,value,MESSAGE) 
00319 #define bbtkDebugDecTab(key,value)
00320 #define bbtkDebugIncTab(key,value)
00321 #endif
00322 //===========================================================
00323 
00324 //===========================================================
00325 #ifdef BBTK_COMPILE_WARNING_MESSAGES
00326 #define bbtkWarning(MESSAGE)                                            \
00327   do                                                                    \
00328     {                                                                   \
00329       int lev = bbtk::MessageManager::GetMessageLevel("Warning");       \
00330       if (lev >0)                                                       \
00331         {                                                               \
00332           std::cerr << "!! WARNING !! " << MESSAGE << std::endl;        \
00333           if (lev >1)                                                   \
00334             {                                                           \
00335               std::cerr << "!! WARNING !! In file '"<<__FILE__          \
00336                         <<"' ; Line "<<__LINE__<<std::endl;             \
00337             }                                                           \
00338         }                                                               \
00339     }                                                                   \
00340   while (0) 
00341 
00342 #else
00343 #define bbtkWarning(MESSAGE) 
00344 #endif
00345 //===========================================================
00346 
00347 
00348 //===========================================================
00349 #ifdef BBTK_COMPILE_ERROR_MESSAGES
00350 //#include "bbtkWx.h"
00351 #define bbtkError(MESSAGE)                              \
00352   do                                                    \
00353     {                                                   \
00354       std::ostringstream s;                             \
00355       s << MESSAGE;                                     \
00356       std::ostringstream f;                             \
00357       f << __FILE__ << " (l."<<__LINE__<<")";           \
00358       bbtk::Exception e( BBTK_GET_CURRENT_OBJECT_NAME,  \
00359                         f.str(),                        \
00360                         s.str());                       \
00361       throw e;                                          \
00362     }                                                   \
00363   while (0) 
00364 
00365 #define bbtkGlobalError(MESSAGE)                                \
00366   do                                                    \
00367     {                                                   \
00368       std::ostringstream s;                             \
00369       s << MESSAGE;                                     \
00370       std::ostringstream f;                             \
00371       f << __FILE__ << " (l."<<__LINE__<<")";           \
00372       bbtk::Exception e( "global scope",                \
00373                         f.str(),                        \
00374                         s.str());                       \
00375       throw e;                                          \
00376     }                                                   \
00377   while (0) 
00378 
00379 #define BBTK_INTERNAL_ERROR_MESSAGE \
00380   "\n\n***********************************************\n**** THIS IS AN INTERNAL ERROR TO BBTK     ****\n**** Please send a full bug report to :    ****\n****  bbtk-developers@creatis.insa-lyon.fr ****\n***********************************************\n\n"
00381 
00382 #define bbtkInternalError(MESSAGE)                      \
00383   do                                                    \
00384     {                                                   \
00385       std::ostringstream s;                             \
00386       s << MESSAGE << BBTK_INTERNAL_ERROR_MESSAGE;      \
00387       std::ostringstream f;                             \
00388       f << __FILE__ << " (l."<<__LINE__<<")";           \
00389       bbtk::Exception e( BBTK_GET_CURRENT_OBJECT_NAME,  \
00390                          f.str(),                       \
00391                          s.str());                      \
00392       throw e;                                          \
00393     }                                                   \
00394   while (0) 
00395 
00396 #else
00397 #define bbtkError(MESSAGE)
00398 #define bbtkGlobalError(MESSAGE)
00399 #define bbtkInternalError(MESSAGE)
00400 #endif
00401 //===========================================================
00402 
00403 //===========================================================
00404 #define bbtkendl std::endl
00405 //===========================================================
00406 
00407 
00408 namespace bbtk 
00409 {
00410 
00411   class BBTK_EXPORT MessageManager
00412   {
00413   public:
00415     MessageManager();
00417     ~MessageManager();
00419     static MessageManager* GetInstance();
00421     static void RegisterMessageType(std::string key, 
00422                                     std::string help,
00423                                     unsigned char default_level = 9);
00425     static void SetMessageLevel(std::string key, unsigned char level);
00427     static int GetMessageLevel(std::string key);
00429     static std::string& GetTab() { static std::string s; return s; }
00431     static std::string GetSpace(int n) { 
00432       std::string s; s.insert(0,"                ",n); return s; }
00434     static void IncTab() { GetTab() += std::string(" "); }
00436     static void DecTab() { GetTab() = GetTab().substr(0,GetTab().length()-1); }
00438     static void ResetTab() { GetTab() = std::string(""); }
00440     static void PrintInfo();
00441 
00442   private:
00443     std::map<std::string,int> mMessageLevel;
00444     std::map<std::string,std::string> mMessageHelp;  
00445     unsigned int mMaxMessageLength;
00446   };
00447   //===========================================================
00448   
00449 }
00450 
00451 #include "bbtkException.h"
00452 
00453 #endif

Generated on Wed Nov 12 11:37:08 2008 for BBTK by  doxygen 1.5.6