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: 2009/05/28 08:12:06 $
00006   Version:   $Revision: 1.9 $
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 #define BBTK_PREPEND_MESSAGE_WITH_CODE
00112 #ifdef BBTK_PREPEND_MESSAGE_WITH_CODE
00113 #define bbtkMessageCode(key,value)                      \
00114   bbtk::MessageManager::FormatKey(key,value) 
00115 #else 
00116 #define bbtkMessageCode(key,value) ""
00117 #endif 
00118 
00119 #ifdef BBTK_PREPEND_MESSAGE_WITH_TAB
00120 #define bbtkMessageTab                          \
00121   bbtk::MessageManager::GetTab()
00122 #else 
00123 #define bbtkMessageTab ""
00124 #endif
00125 
00126 //#define BBTK_PREPEND_MESSAGE_WITH_SPACE
00127 #ifdef BBTK_PREPEND_MESSAGE_WITH_SPACE
00128 #define bbtkMessageSpace(value)                 \
00129   bbtk::MessageManager::GetSpace(value)
00130 #else 
00131 #define bbtkMessageSpace(value) ""
00132 #endif
00133  
00134 
00135 //===========================================================
00136 #ifdef BBTK_COMPILE_MESSAGES
00137 
00138 // Macro for messages
00139 #define bbtkMessage(key,value,MESSAGE)                  \
00140   do {                                                  \
00141     bbtkOnMessageLevel(key,value)                       \
00142       {                                                 \
00143         std::cout << bbtkMessageCode(key,value)         \
00144                   << bbtkMessageTab                     \
00145                   << bbtkMessageSpace(value)            \
00146                   << MESSAGE;                           \
00147       }                                                 \
00148   }                                                     \
00149   while (0)
00150 
00151 // Macro for continuing a message (when one wants to split the macro
00152 // call into multiple lines)
00153 #define bbtkMessageCont(key,value,MESSAGE)      \
00154   do                                            \
00155     {                                           \
00156       bbtkOnMessageLevel(key,value)             \
00157         {                                       \
00158           std::cout << MESSAGE;                 \
00159         }                                       \
00160     }                                           \
00161   while (0)
00162 
00163 #define bbtkMessageInc(key,value,MESSAGE)               \
00164   do                                                    \
00165     {                                                   \
00166       bbtkOnMessageLevel(key,value)                     \
00167         {                                               \
00168           std::cout << bbtkMessageCode(key,value)               \
00169                     << bbtkMessageTab                   \
00170                     << bbtkMessageSpace(value)          \
00171                     << MESSAGE;                         \
00172           bbtk::MessageManager::IncTab();               \
00173         }                                               \
00174     }                                                   \
00175   while (0)
00176 
00177 #define bbtkMessageDec(key,value,MESSAGE)               \
00178   do                                                    \
00179     {                                                   \
00180       bbtkOnMessageLevel(key,value)                     \
00181         {                                               \
00182           bbtk::MessageManager::DecTab();               \
00183           std::cout << bbtkMessageCode(key,value)               \
00184                     << bbtkMessageTab                   \
00185                     << bbtkMessageSpace(value)          \
00186                     << MESSAGE;                         \
00187         }                                               \
00188     }                                                   \
00189   while (0)
00190 
00191 #define bbtkDecTab(key,value)                   \
00192   do                                            \
00193     {                                           \
00194       bbtkOnMessageLevel(key,value)             \
00195         {                                       \
00196           bbtk::MessageManager::DecTab();       \
00197         }                                       \
00198     }                                           \
00199   while (0)
00200 
00201 #define bbtkIncTab(key,value)                   \
00202   do                                            \
00203     {                                           \
00204       bbtkOnMessageLevel(key,value)             \
00205         {                                       \
00206           bbtk::MessageManager::IncTab();       \
00207         }                                       \
00208     }                                           \
00209   while (0)
00210 
00211 #define bbtkResetTab()                          \
00212   do                                            \
00213     {                                           \
00214       bbtk::MessageManager::ResetTab();         \
00215     }                                           \
00216   while (0)
00217 
00218 #else
00219 #define bbtkMessage(key,value,MESSAGE)
00220 #define bbtkMessageInc(key,value,MESSAGE)
00221 #define bbtkMessageDec(key,value,MESSAGE)
00222 #define bbtkMessageCont(key,value,MESSAGE)
00223 #define bbtkDecTab(key,value)
00224 #define bbtkIncTab(key,value)
00225 #define bbtkResetTab()
00226 #endif
00227 //===========================================================
00228 
00229 
00230 
00231 //===========================================================
00232 #ifdef BBTK_COMPILE_DEBUG_MESSAGES
00233 
00234 // Macro for debug messages
00235 #define bbtkDebugMessage(key,value,MESSAGE)             \
00236   do                                                    \
00237     {                                                   \
00238       bbtkOnMessageLevel(key,value)                     \
00239         {                                               \
00240           std::cout << bbtkMessageCode(key,value)               \
00241                     << bbtkMessageTab                   \
00242                     << bbtkMessageSpace(value)          \
00243                     << MESSAGE;                         \
00244         }                                               \
00245     }                                                   \
00246   while (0)
00247 
00248 // Macro for continuing a debug message (when one wants to split the
00249 // macro call into multiple lines)
00250 #define bbtkDebugMessageCont(key,value,MESSAGE) \
00251   do                                            \
00252     {                                           \
00253       bbtkOnMessageLevel(key,value)             \
00254         {                                       \
00255           std::cout << MESSAGE;                 \
00256         }                                       \
00257     }                                           \
00258   while (0)
00259 
00260 #define bbtkDebugMessageInc(key,value,MESSAGE)          \
00261   do                                                    \
00262     {                                                   \
00263       bbtkOnMessageLevel(key,value)                     \
00264         {                                               \
00265           std::cout << bbtkMessageCode(key,value)               \
00266                     << bbtkMessageTab                   \
00267                     << bbtkMessageSpace(value)          \
00268                     << MESSAGE;                         \
00269           bbtk::MessageManager::IncTab();               \
00270         }                                               \
00271     }                                                   \
00272   while (0)
00273 
00274 #define bbtkDebugMessageDec(key,value,MESSAGE)          \
00275   do                                                    \
00276     {                                                   \
00277       bbtkOnMessageLevel(key,value)                     \
00278         {                                               \
00279           bbtk::MessageManager::DecTab();               \
00280           std::cout << bbtkMessageCode(key,value)       \
00281                     << bbtkMessageTab                   \
00282                     << bbtkMessageSpace(value)          \
00283                     << MESSAGE;                         \
00284         }                                               \
00285     }                                                   \
00286   while (0)
00287 
00288 #define bbtkDebugDecTab(key,value)              \
00289   do                                            \
00290     {                                           \
00291       bbtkOnMessageLevel(key,value)             \
00292         {                                       \
00293           bbtk::MessageManager::DecTab();       \
00294         }                                       \
00295     }                                           \
00296   while (0)
00297 
00298 #define bbtkDebugIncTab(key,value)              \
00299     do                                          \
00300       {                                         \
00301         bbtkOnMessageLevel(key,value)           \
00302           {                                     \
00303             bbtk::MessageManager::IncTab();     \
00304           }                                     \
00305       }                                         \
00306     while (0)
00307     
00308 #define bbtkDebugResetTab()                     \
00309     do                                          \
00310       {                                         \
00311         bbtk::MessageManager::ResetTab();       \
00312       }                                         \
00313     while (0)
00314 
00315 #else
00316 #define bbtkDebugMessage(key,value,MESSAGE) 
00317 #define bbtkDebugMessageCont(key,value,MESSAGE) 
00318 #define bbtkDebugMessageInc(key,value,MESSAGE)
00319 #define bbtkDebugMessageDec(key,value,MESSAGE) 
00320 #define bbtkDebugDecTab(key,value)
00321 #define bbtkDebugIncTab(key,value)
00322 #endif
00323 //===========================================================
00324 
00325 //===========================================================
00326 #ifdef BBTK_COMPILE_WARNING_MESSAGES
00327 #define bbtkWarning(MESSAGE)                                            \
00328   do                                                                    \
00329     {                                                                   \
00330       int lev = bbtk::MessageManager::GetMessageLevel("Warning");       \
00331       if (lev >0)                                                       \
00332         {                                                               \
00333           std::cerr << "!! WARNING !! " << MESSAGE << std::endl;        \
00334           if (lev >1)                                                   \
00335             {                                                           \
00336               std::cerr << "!! WARNING !! In file '"<<__FILE__          \
00337                         <<"' ; Line "<<__LINE__<<std::endl;             \
00338             }                                                           \
00339         }                                                               \
00340     }                                                                   \
00341   while (0) 
00342 
00343 #else
00344 #define bbtkWarning(MESSAGE) 
00345 #endif
00346 //===========================================================
00347 
00348 
00349 //===========================================================
00350 #ifdef BBTK_COMPILE_ERROR_MESSAGES
00351 //#include "bbtkWx.h"
00352 #define bbtkError(MESSAGE)                              \
00353   do                                                    \
00354     {                                                   \
00355       std::ostringstream s;                             \
00356       s << MESSAGE;                                     \
00357       std::ostringstream f;                             \
00358       f << __FILE__ << " (l."<<__LINE__<<")";           \
00359       bbtk::Exception e( BBTK_GET_CURRENT_OBJECT_NAME,  \
00360                         f.str(),                        \
00361                         s.str());                       \
00362       throw e;                                          \
00363     }                                                   \
00364   while (0) 
00365 
00366 #define bbtkGlobalError(MESSAGE)                                \
00367   do                                                    \
00368     {                                                   \
00369       std::ostringstream s;                             \
00370       s << MESSAGE;                                     \
00371       std::ostringstream f;                             \
00372       f << __FILE__ << " (l."<<__LINE__<<")";           \
00373       bbtk::Exception e( "global scope",                \
00374                         f.str(),                        \
00375                         s.str());                       \
00376       throw e;                                          \
00377     }                                                   \
00378   while (0) 
00379 
00380 #define BBTK_INTERNAL_ERROR_MESSAGE \
00381   "\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"
00382 
00383 #define bbtkInternalError(MESSAGE)                      \
00384   do                                                    \
00385     {                                                   \
00386       std::ostringstream s;                             \
00387       s << MESSAGE << BBTK_INTERNAL_ERROR_MESSAGE;      \
00388       std::ostringstream f;                             \
00389       f << __FILE__ << " (l."<<__LINE__<<")";           \
00390       bbtk::Exception e( BBTK_GET_CURRENT_OBJECT_NAME,  \
00391                          f.str(),                       \
00392                          s.str());                      \
00393       throw e;                                          \
00394     }                                                   \
00395   while (0) 
00396 
00397 #else
00398 #define bbtkError(MESSAGE)
00399 #define bbtkGlobalError(MESSAGE)
00400 #define bbtkInternalError(MESSAGE)
00401 #endif
00402 //===========================================================
00403 
00404 //===========================================================
00405 #define bbtkendl std::endl
00406 //===========================================================
00407 
00408 
00409 namespace bbtk 
00410 {
00411 
00412   class BBTK_EXPORT MessageManager
00413   {
00414   public:
00416     MessageManager();
00418     ~MessageManager();
00420     static MessageManager* GetInstance();
00422     static bool RegisterMessageType(std::string key, 
00423                                     std::string help,
00424                                     unsigned char default_level = 9);
00426     static void SetMessageLevel(std::string key, unsigned char level);
00428     static int GetMessageLevel(std::string key);
00430     static std::string& GetTab() { static std::string s; return s; }
00432     static std::string GetSpace(int n) { 
00433       std::string s; s.insert(0,"                ",n); return s; }
00435     static void IncTab() { GetTab() += std::string(" "); }
00437     static void DecTab() { GetTab() = GetTab().substr(0,GetTab().length()-1); }
00439     static void ResetTab() { GetTab() = std::string(""); }
00441     static void PrintInfo();
00442 
00443     static std::string FormatKey(const std::string& key, int value);
00444 
00445   private:
00446     std::map<std::string,int> mMessageLevel;
00447     std::map<std::string,std::string> mMessageHelp;  
00448     unsigned int mMaxMessageLength;
00449   };
00450   //===========================================================
00451   
00452 }
00453 
00454 #include "bbtkException.h"
00455 
00456 #endif

Generated on Thu May 31 14:12:03 2012 for BBTK by  doxygen 1.5.7.1