From aca47165598d479b6bb189e8c7ca05c279f32282 Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Fri, 24 Aug 2012 20:29:40 +0200 Subject: sorted out visibility issues around Logger/Singleton/scoped_ptr added more logging tests --- docs/LINKS | 3 +++ src/Logger.hpp | 10 +++++++--- src/ScopedPtr.hpp | 2 +- src/Singleton.hpp | 11 +++++++++-- tests/logger/test1.cpp | 10 +++++++++- 5 files changed, 29 insertions(+), 7 deletions(-) diff --git a/docs/LINKS b/docs/LINKS index c27648f..7600d3a 100755 --- a/docs/LINKS +++ b/docs/LINKS @@ -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 #include +#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; 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 + +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; } -- cgit v1.2.3-54-g00ecf