Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | Related Pages

logstream.h

Go to the documentation of this file.
00001 /***************************************************************************
00002          logstream.h  -  Filtered logging to multiple output targets
00003                              -------------------
00004     begin                : Tue Aug 26 2003
00005     copyright            : (C) 2003 by Cody Russell
00006     email                : cody `at' jhu.edu 
00007 ***************************************************************************
00008 
00009  The contents of this file are subject to the Mozilla Public License Version
00010  1.1 (the "License"); you may not use this file except in compliance with
00011  the License. You may obtain a copy of the License at 
00012  http://www.mozilla.org/MPL/
00013 
00014  Software distributed under the License is distributed on an "AS IS" basis,
00015  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00016  for the specific language governing rights and limitations under the
00017  License.
00018 
00019  The Original Code is the NeoEngine, logstream.h
00020 
00021  The Initial Developer of the Original Code is Cody Russell.
00022  Portions created by Cody Russell are Copyright (C) 2003
00023  Cody Russell. All Rights Reserved.
00024 
00025  Contributors: Mattias Jansson (mattias@realityrift.com)
00026                Bert van der Weerd
00027  
00028 ***************************************************************************/
00029 
00030 #ifndef __NELOGSTREAM_H
00031 #define __NELOGSTREAM_H
00032 
00033 #include "base.h"
00034 
00035 #include <ios>
00036 #include <vector>
00037 #include <iostream>
00038 
00039 
00088 // Stupid Windows headers define ERROR to 0
00089 #ifdef WIN32
00090 #  undef ERROR
00091 #endif
00092 
00093 
00094 namespace NeoEngine
00095 {
00096 
00097 
00098 // External classes
00099 class File;
00100 
00101 
00107 enum LOGLEVEL
00108 {
00110     PANIC   = 0x01,
00111 
00113     ERROR   = 0x04,
00114 
00116     WARNING = 0x08,
00117 
00119     INFO    = 0x10,
00120 
00122     DEBUG   = 0x80
00123 };
00124 
00125 
00137 class NEOENGINE_API LogSink
00138 {
00139     friend class LogSource;
00140 
00141     protected:
00142 
00144         unsigned int                                m_uiThreshold;
00145 
00146 
00147     public:
00148 
00151                                                     LogSink();
00152 
00155         virtual                                    ~LogSink();
00156 
00161         void                                        SetLogThreshold( unsigned int uiLevel );
00162 
00167         virtual void                                Write( const std::string &rstrMsg ) = 0;
00168 };
00169 
00170 
00177 class NEOENGINE_API LogStdoutSink : public LogSink
00178 {
00179     protected:
00180 
00181 
00182     public:
00183 
00186                                                     LogStdoutSink();
00187 
00190         virtual                                    ~LogStdoutSink();
00191 
00196         virtual void                                Write( const std::string &rstrMsg );
00197 };
00198 
00199 
00207 class NEOENGINE_API LogFileSink : public LogSink
00208 {
00209     protected:
00210 
00212         File                                       *m_pkFile;
00213 
00214     public:
00215 
00219                                                     LogFileSink( const std::string &rstrFileName );
00220 
00224                                                     LogFileSink( File *pkFile );
00225 
00229         virtual                                    ~LogFileSink();
00230 
00235         virtual void                                Write( const std::string &rstrMsg );
00236 };
00237 
00238 
00243 class NEOENGINE_API LogMSVCDebugSink : public LogSink
00244 {
00245     public:
00246 
00249                                                     LogMSVCDebugSink();
00250 
00253         virtual                                    ~LogMSVCDebugSink();
00254 
00259         virtual void                                Write( const std::string &rstrMsg );
00260 };
00261 
00262 
00268 class NEOENGINE_API LogSourceStreamBuf : public std::basic_streambuf< char >
00269 {
00270     private:
00271 
00273         LogSource                                  *m_pkLogSource;
00274 
00276         std::string                                 m_strMsg;
00277 
00278     protected:
00279 
00281         virtual int_type                            overflow( int_type c );
00282 
00283     public:
00284 
00287                                                     LogSourceStreamBuf();
00288 
00291         virtual                                    ~LogSourceStreamBuf();
00292 
00297         void                                        SetLogSource( LogSource *pkSource );
00298 };
00299 
00300 
00305 class NEOENGINE_API LogManipulator
00306 {
00307     public:
00308 
00309         virtual std::ostream                       &operator ()( std::ostream &rkOut ) const = 0;
00310 };
00311 
00312 
00321 class NEOENGINE_API LogLevel : public LogManipulator
00322 {
00323     private:
00324 
00326         unsigned int                                m_uiLevel;
00327 
00328     public:
00329 
00333                                                     LogLevel( unsigned int uiLevel ) : m_uiLevel( uiLevel ) {}
00334 
00335         virtual std::ostream                       &operator ()( std::ostream &rkOut ) const;
00336 };
00337 
00338 
00346 class NEOENGINE_API LogLevelPush : public LogManipulator
00347 {
00348     private:
00349 
00351         unsigned int                                m_uiLevel;
00352 
00353     public:
00354     
00358                                                     LogLevelPush( unsigned int uiLevel ) : LogManipulator(), m_uiLevel( uiLevel ) {}
00359 
00360         virtual std::ostream                       &operator ()( std::ostream &rkOut ) const;
00361 };
00362 
00363 
00369 class NEOENGINE_API LogLevelPop : public LogManipulator
00370 {
00371     public:
00372     
00373                                                     LogLevelPop() : LogManipulator() {}
00374     
00375         virtual std::ostream                       &operator ()( std::ostream &rkOut ) const;
00376 };
00377 
00378 
00385 NEOENGINE_API std::ostream &operator << ( std::ostream &rkOut, const LogManipulator &rkManip );
00386 
00387 
00394 class NEOENGINE_API LogErrorCallback
00395 {
00396     public:
00397 
00403         virtual void                                ErrorCallback( unsigned int uiLevel, const std::string &rstrMsg ) = 0;
00404 };
00405 
00406 
00407 #ifdef WIN32
00408 #  ifdef _MSC_VER
00409 #    pragma warning( disable : 4231 )
00410 #    pragma warning( disable : 4660 )
00411 #  endif
00412 #  ifndef __HAVE_VECTOR_LOGSINK
00413      UDTVectorEXPIMP( LogSink* );
00414 #    define __HAVE_VECTOR_LOGSINK
00415 #  endif
00416 #  ifndef __HAVE_VECTOR_UNSIGNED_INT
00417      UDTVectorEXPIMP( unsigned int );
00418 #    define __HAVE_VECTOR_UNSIGNED_INT
00419 #  endif
00420 #endif
00421 
00422 
00432 class NEOENGINE_API LogSource : public std::basic_ostream< char >
00433 {
00434     friend class LogLevel;
00435 
00436     protected:
00437 
00439         LogSourceStreamBuf                          m_kStreamBuf;
00440 
00442         std::vector< LogSink* >                     m_vpkSinks;
00443 
00445         unsigned int                                m_uiLogLevel;
00446 
00448         unsigned int                                m_uiThreshold;
00449 
00451         LogErrorCallback                           *m_pkErrorCallback;
00452 
00454         std::vector< unsigned int >                 m_vuiLogLevelStack;
00455 
00456 
00457     public:
00458 
00461                                                     LogSource();
00462 
00467         void                                        AttachSink( LogSink *pkSink );
00468 
00473         void                                        DetachSink( LogSink *pkSink );
00474 
00479         void                                        Write( const std::string &rstrMsg );
00480 
00485         void                                        SetLogLevel( unsigned int uiLevel );
00486 
00490         unsigned int                                GetLogLevel() { return m_uiLogLevel; }
00491 
00496         void                                        PushLogLevel( unsigned int uiLevel );
00497 
00501         void                                        PopLogLevel();
00502 
00507         void                                        SetLogThreshold( unsigned int uiLevel );
00508 
00513         void                                        SetErrorCallback( LogErrorCallback *pkCallback );
00514 };
00515 
00516 
00518 extern NEOENGINE_API LogSource neolog;
00519 
00520 
00521 };
00522 
00523 
00524 #endif

Generated on Wed Jan 21 14:21:06 2004 for NeoEngine by doxygen 1.3.5