diff options
-rwxr-xr-x | docs/LINKS | 3 | ||||
-rwxr-xr-x | src/Logger.hpp | 10 | ||||
-rw-r--r-- | src/ScopedPtr.hpp | 2 | ||||
-rwxr-xr-x | src/Singleton.hpp | 11 | ||||
-rw-r--r-- | tests/logger/test1.cpp | 10 |
5 files changed, 29 insertions, 7 deletions
@@ -84,5 +84,8 @@ DLL and shared globals (for Singleton): - http://www.ogre3d.org/forums/viewtopic.php?p=75622&sid=ce193664e1d3d7c4af509e6f4e2718c6 - http://www.lurklurk.org/linkers/linkers.html#wincircular +Singleton design: +- http://www.oop-trainer.de/Themen/Singleton.html + Linking with gcc and visibility: - http://gcc.gnu.org/wiki/Visibility diff --git a/src/Logger.hpp b/src/Logger.hpp index 341283a..5b2578f 100755 --- a/src/Logger.hpp +++ b/src/Logger.hpp @@ -47,24 +47,28 @@ class LogSink; class Logger : public Singleton< Logger > { public: + DECLARE_SINGLETON( Logger ) + void addSink( LogSink *sink ); void removeSink( LogSink *sink ); void log( const LogLevel level, const std::string &msg ); - ~Logger( ); - static std::string toString( const LogLevel level ); static LogLevel fromString( const std::string &s ); void openConsoleLog( const LogLevel level ); void openFileLog( const LogLevel level, const std::string &filename ); + protected: + Logger( ) { } + virtual ~Logger( ); + private: typedef std::list< LogSink * > SinkList; SinkList m_sinks; }; -SINGLETON_EXTERN template class SINGLETON_EXPORT Singleton< Logger >; +DEFINE_SINGLETON( Logger ) #define LOG( level ) LogStream( Logger::instance( ), level ).get( ) diff --git a/src/ScopedPtr.hpp b/src/ScopedPtr.hpp index 8357e85..19a41d2 100644 --- a/src/ScopedPtr.hpp +++ b/src/ScopedPtr.hpp @@ -4,7 +4,7 @@ #include "NonCopyable.hpp" template< typename T > -class scopedPtr : noncopyable +class scopedPtr : private noncopyable { public: explicit scopedPtr( T *p = 0 ) : m_p( p ) { } diff --git a/src/Singleton.hpp b/src/Singleton.hpp index 2bdf257..4b7e2b4 100755 --- a/src/Singleton.hpp +++ b/src/Singleton.hpp @@ -7,6 +7,13 @@ #include <cstdlib> #include <stdexcept> +#define DECLARE_SINGLETON( T ) \ + friend class scopedPtr< T >; \ + friend class Singleton< T >; + +#define DEFINE_SINGLETON( T ) \ + SINGLETON_EXTERN template class SINGLETON_EXPORT Singleton< T >; + template< class T > class Singleton : private noncopyable { @@ -29,7 +36,7 @@ class Singleton : private noncopyable { } - ~Singleton( ) + virtual ~Singleton( ) { destroyed = true; } @@ -44,7 +51,7 @@ class Singleton : private noncopyable { throw std::runtime_error( "singleton has already been destroyed!" ); } - + static scopedPtr<T> t; static bool destroyed; }; diff --git a/tests/logger/test1.cpp b/tests/logger/test1.cpp index 5940d60..eea3ff9 100644 --- a/tests/logger/test1.cpp +++ b/tests/logger/test1.cpp @@ -2,11 +2,15 @@ #include "ConsoleLogSink.hpp" #include "FileLogSink.hpp" +#include <string> + +using namespace std; + int main( void ) { LogSink *sink = new ConsoleLogSink( logNOTICE ); Logger::instance( ).addSink( sink ); - Logger::instance( ).addSink( new FileLogSink( logNOTICE, "test1.log" ) ); + Logger::instance( ).openFileLog( logNOTICE, "test1.log" ); LOG( logFATAL ) << "fatal error"; LOG( logCRITICAL ) << "critical error"; @@ -48,5 +52,9 @@ int main( void ) LOG( logDEBUG3 ) << "debug level 3"; LOG( logDEBUG4 ) << "debug level 4"; + sink->setReportingLevel( logINFO ); + LogLevel level = Logger::fromString( "bla" ); + LOG( logINFO ) << "the level is " << Logger::toString( level ); + return 0; } |