include/EDoc/Logger.h

Go to the documentation of this file.
00001 /*******************************************************************************
00002 
00003    Copyright (C) 2007 by Brendon Costa
00004 
00005    This library is free software; you can redistribute it and/or modify 
00006    it under the terms of the "LGPL Like" License defined in the file COPYING 
00007    that should have been distributed along with this source.
00008 
00009    This library is distributed in the hope that it will be useful, 
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of 
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
00012 
00013    You should have received a copy of the "LGPL Like" License 
00014    along with this library; see the file COPYING. if not, it can be 
00015    obtained from the EDoc++ website: 
00016    http://edoc.sourceforge.net/license.html
00017 
00018 *******************************************************************************/
00019 #ifndef EDOC_LOGGER_H
00020 #define EDOC_LOGGER_H
00021 
00022 #include "EDoc/LogLevels.h"
00023 
00024 #include <sstream>
00025 #include <iostream>
00026 #include <fstream>
00027 
00028 // The default log level to use.
00029 #ifndef EDOC_DEFAULT_LOG_LEVEL
00030    //#define EDOC_DEFAULT_LOG_LEVEL   EDOC_LOG_LEVEL_ERROR
00031    //#define EDOC_DEFAULT_LOG_LEVEL   EDOC_LOG_LEVEL_WARNING
00032    #define EDOC_DEFAULT_LOG_LEVEL   EDOC_LOG_LEVEL_INFO
00033    //#define EDOC_DEFAULT_LOG_LEVEL   EDOC_LOG_LEVEL_DEBUG
00034    //#define EDOC_DEFAULT_LOG_LEVEL   EDOC_LOG_LEVEL_FINE
00035    //#define EDOC_DEFAULT_LOG_LEVEL   EDOC_LOG_LEVEL_FINER
00036    //#define EDOC_DEFAULT_LOG_LEVEL   EDOC_LOG_LEVEL_FINEST
00037 #endif
00038 
00039 namespace EDoc
00040 {
00041    //===========================================================================
00042    /** \brief Simple logging singleton class.
00043     */
00044    class Logger
00045    {
00046    public:
00047 
00048       //------------------------------------------------------------------------
00049       /** \brief Singleton accessor.
00050        */
00051       static inline Logger* Instance()
00052       {
00053          if (instance == NULL)
00054          {
00055             instance = new Logger();
00056          }
00057 
00058          return instance;
00059       }
00060 
00061       //------------------------------------------------------------------------
00062       /** \brief Deletes singleton.
00063        */
00064       static void Destroy()
00065       {
00066          if (instance)
00067          {
00068             delete instance;
00069             instance = NULL;
00070          }
00071       }
00072       
00073       //------------------------------------------------------------------------
00074       /** \brief Destructor.
00075        */
00076       ~Logger();
00077       
00078       //------------------------------------------------------------------------
00079       /** \brief If true log to stdout.
00080        */
00081       bool use_stdout;
00082 
00083       //------------------------------------------------------------------------
00084       /** \brief If true log to stderr.
00085        */
00086       bool use_stderr;
00087       
00088       //------------------------------------------------------------------------
00089       /** \brief Ensure it does not log to any files.
00090        */
00091       void ClearUseFile();
00092 
00093       //------------------------------------------------------------------------
00094       /** \brief If true log to a file.
00095        */
00096       bool GetUseFile() const;
00097 
00098       //------------------------------------------------------------------------
00099       /** \brief If logging to a file returns true and also returns the name of
00100        * the file being logged to.
00101        */
00102       bool GetLogFileName(std::string& filename_out) const;
00103 
00104       //------------------------------------------------------------------------
00105       /** \brief Set the name of the file to log to.
00106        */
00107       bool SetLogFilename(std::string filename_in);
00108 
00109       //------------------------------------------------------------------------
00110       /** \brief Set the name of the file to log to including the full path.
00111        */
00112       bool SetLogFilenameFullyQualified(std::string filename_in);
00113 
00114       //------------------------------------------------------------------------
00115       /** \brief Logs a message to all appropiate output sources.
00116        */
00117       inline void LogMessage(const std::string& message)
00118       {
00119          if (use_file)
00120          {
00121             // If the log file has not yet been created, then create it to log 
00122             // this message and all future messages.
00123             if (!file_initialised)
00124             {
00125                InitialiseFile();
00126             }
00127 
00128             file << message << ::std::endl;
00129          }
00130 
00131          if (use_stdout)
00132          {
00133             std::cout << message << ::std::endl;
00134          }
00135 
00136          if (use_stderr)
00137          {
00138             std::cerr << message << ::std::endl;
00139          }
00140       }
00141 
00142       //------------------------------------------------------------------------
00143 
00144    private:
00145 
00146       //------------------------------------------------------------------------
00147       /** \brief Constructor.
00148        */
00149       Logger();
00150 
00151       //------------------------------------------------------------------------
00152       /** \brief Called to setup the file to be logged to.
00153        */
00154       void InitialiseFile();
00155 
00156       //------------------------------------------------------------------------
00157       /** \brief If true log to file.
00158        */
00159       bool use_file;
00160 
00161       //------------------------------------------------------------------------
00162       /** \brief Name of file being loged to.
00163        */
00164       std::string filename;
00165 
00166       //------------------------------------------------------------------------
00167       /** \brief Output stream for file to log to.
00168        */
00169       std::ofstream file;
00170 
00171       //------------------------------------------------------------------------
00172       /** \brief True if the file to log to has been initialized false if needs
00173        * to be opened before logging.
00174        */
00175       bool file_initialised;
00176 
00177       //------------------------------------------------------------------------
00178       /** \brief Singleton instance for logger.
00179        */
00180       static Logger* instance;
00181       
00182       //------------------------------------------------------------------------
00183 
00184    };
00185    //===========================================================================
00186 }
00187 
00188 #endif // EDOC_LOGGER_H
00189 
00190 
00191 
00192 //==============================================================================
00193 //==============================================================================
00194 #undef EDOC_LOG_MESSAGE
00195 #undef EDOC_IMPL_LOG_LEVEL
00196 #undef EDOC_Fatal
00197 #undef EDOC_Error
00198 #undef EDOC_Warning
00199 #undef EDOC_Info
00200 #undef EDOC_Debug
00201 #undef EDOC_Fine
00202 #undef EDOC_Finer
00203 #undef EDOC_Finest
00204 //==============================================================================
00205 //==============================================================================
00206 
00207 
00208 
00209 //==============================================================================
00210 /** \brief If the user did not request otherwise then log the level.
00211  */
00212 #ifndef EDOC_LOG_WRITE_LEVEL
00213    #define EDOC_LOG_WRITE_LEVEL(Level, LevelString) << LevelString << ":"
00214 #endif
00215 
00216 
00217 //==============================================================================
00218 /** \brief If the user did not request otherwise then log the filename.
00219  */
00220 #ifndef EDOC_LOG_WRITE_FILENAME
00221    #define EDOC_LOG_WRITE_FILENAME << __FILE__ << ":"
00222 #endif
00223 
00224 
00225 //==============================================================================
00226 /** \brief If the user did not request otherwise then log the line number.
00227  */
00228 #ifndef EDOC_LOG_WRITE_LINE
00229    #define EDOC_LOG_WRITE_LINE << __LINE__ << ":"
00230 #endif
00231 
00232 //==============================================================================
00233 /** \brief If the user did not request otherwise then log the function name.
00234  */
00235 #define EDOC_LOG_WRITE_FUNCTION
00236 
00237 #ifndef EDOC_LOG_WRITE_FUNCTION
00238    #define EDOC_LOG_WRITE_FUNCTION << __FUNCTION__ << "():"
00239 #endif
00240 
00241 
00242 //==============================================================================
00243 /** \brief If the user did not request otherwise then log the message.
00244  */
00245 #ifndef EDOC_LOG_WRITE_MESSAGE
00246    #define EDOC_LOG_WRITE_MESSAGE(Message) << " " << Message
00247 #endif
00248 
00249 
00250 //==============================================================================
00251 /** \brief 
00252  */
00253 #define EDOC_LOG_MESSAGE(Level, LevelString, Message) \
00254 { \
00255    ::std::ostringstream stream_jh8732rhb; \
00256    stream_jh8732rhb \
00257       EDOC_LOG_WRITE_LEVEL(Level, LevelString) \
00258       EDOC_LOG_WRITE_FILENAME \
00259       EDOC_LOG_WRITE_LINE \
00260       EDOC_LOG_WRITE_FUNCTION \
00261       EDOC_LOG_WRITE_MESSAGE(Message) \
00262       << std::flush; \
00263    EDoc::Logger::Instance()->LogMessage(stream_jh8732rhb.str()); \
00264 }
00265 
00266 //==============================================================================
00267 /** \brief Set the default log level.
00268  */
00269 #ifndef EDOC_LOG_LEVEL
00270    #define EDOC_IMPL_LOG_LEVEL   EDOC_DEFAULT_LOG_LEVEL
00271 #else
00272    // Use the log level defined.
00273    #define EDOC_IMPL_LOG_LEVEL   EDOC_LOG_LEVEL
00274 #endif
00275 
00276 
00277 //==============================================================================
00278 /** Create macros for all log levels compiling out code for those outside the
00279  * log level being used.
00280  */
00281 #if EDOC_IMPL_LOG_LEVEL <= EDOC_LOG_LEVEL_FATAL
00282    #define EDOC_Fatal(Message)   EDOC_LOG_MESSAGE(EDOC_LOG_LEVEL_FATAL, "FATAL  ", Message)
00283 #else
00284    #define EDOC_Fatal(Message) 
00285 #endif
00286 
00287 #if EDOC_IMPL_LOG_LEVEL <= EDOC_LOG_LEVEL_ERROR
00288    #define EDOC_Error(Message)   EDOC_LOG_MESSAGE(EDOC_LOG_LEVEL_ERROR, "ERROR  ", Message)
00289 #else
00290    #define EDOC_Error(Message) 
00291 #endif
00292 
00293 #if EDOC_IMPL_LOG_LEVEL <= EDOC_LOG_LEVEL_WARNING
00294    #define EDOC_Warning(Message) EDOC_LOG_MESSAGE(EDOC_LOG_LEVEL_WARNING, "WARNING", Message)
00295 #else
00296    #define EDOC_Warning(Message) 
00297 #endif
00298 
00299 #if EDOC_IMPL_LOG_LEVEL <= EDOC_LOG_LEVEL_INFO
00300    #define EDOC_Info(Message)    EDOC_LOG_MESSAGE(EDOC_LOG_LEVEL_INFO, "INFO   ", Message)
00301 #else
00302    #define EDOC_Info(Message) 
00303 #endif
00304 
00305 #if EDOC_IMPL_LOG_LEVEL <= EDOC_LOG_LEVEL_DEBUG
00306    #define EDOC_Debug(Message)   EDOC_LOG_MESSAGE(EDOC_LOG_LEVEL_DEBUG, "DEBUG  ", Message)
00307 #else
00308    #define EDOC_Debug(Message) 
00309 #endif
00310 
00311 #if EDOC_IMPL_LOG_LEVEL <= EDOC_LOG_LEVEL_FINE
00312    #define EDOC_Fine(Message)    EDOC_LOG_MESSAGE(EDOC_LOG_LEVEL_FINE, "FINE   ", Message)
00313 #else
00314    #define EDOC_Fine(Message) 
00315 #endif
00316 
00317 #if EDOC_IMPL_LOG_LEVEL <= EDOC_LOG_LEVEL_FINER
00318    #define EDOC_Finer(Message)   EDOC_LOG_MESSAGE(EDOC_LOG_LEVEL_FINER, "FINER  ", Message)
00319 #else
00320    #define EDOC_Finer(Message) 
00321 #endif
00322 
00323 #if EDOC_IMPL_LOG_LEVEL <= EDOC_LOG_LEVEL_FINEST
00324    #define EDOC_Finest(Message)  EDOC_LOG_MESSAGE(EDOC_LOG_LEVEL_FINEST, "FINEST ", Message)
00325 #else
00326    #define EDOC_Finest(Message) 
00327 #endif

Generated on Tue Jan 20 18:26:07 2009 for EDoc-0.2.1 by  doxygen 1.5.1