diff --git a/grive/src/main.cc b/grive/src/main.cc index e522a1e..0ce91b0 100644 --- a/grive/src/main.cc +++ b/grive/src/main.cc @@ -26,8 +26,9 @@ #include "bfd/Backtrace.hh" #include "util/Exception.hh" -#include "util/Log.hh" -#include "util/DefaultLog.hh" +#include "util/log/Log.hh" +#include "util/log/CompositeLog.hh" +#include "util/log/DefaultLog.hh" #include @@ -45,13 +46,13 @@ int main( int argc, char **argv ) Config config ; - DefaultLog nofile_log ; - LogBase::Inst( &nofile_log ) ; + std::auto_ptr comp_log(new log::CompositeLog) ; + LogBase* console_log = comp_log->Add( std::auto_ptr( new log::DefaultLog ) ) ; Json options ; int c ; - while ((c = getopt(argc, argv, "al:vVf")) != -1) + while ((c = getopt(argc, argv, "al:vVfd")) != -1) { switch ( c ) { @@ -81,8 +82,14 @@ int main( int argc, char **argv ) case 'l' : { - static DefaultLog log( optarg ) ; - LogBase::Inst( &log ) ; + std::auto_ptr file_log(new log::DefaultLog(optarg)) ; + file_log->Enable( log::debug ) ; + file_log->Enable( log::verbose ) ; + file_log->Enable( log::info ) ; + file_log->Enable( log::warning ) ; + file_log->Enable( log::error ) ; + file_log->Enable( log::critical ) ; + comp_log->Add( file_log ) ; break ; } @@ -95,7 +102,14 @@ int main( int argc, char **argv ) case 'V' : { - LogBase::Inst()->Enable( log::verbose ) ; + console_log->Enable( log::verbose ) ; + break ; + } + + case 'd' : + { + console_log->Enable( log::verbose ) ; + console_log->Enable( log::debug ) ; break ; } @@ -106,7 +120,7 @@ int main( int argc, char **argv ) } } } - + LogBase::Inst( std::auto_ptr(comp_log.release()) ) ; std::string refresh_token ; try diff --git a/libgrive/CMakeLists.txt b/libgrive/CMakeLists.txt index ba17ce9..b57cdcc 100644 --- a/libgrive/CMakeLists.txt +++ b/libgrive/CMakeLists.txt @@ -58,6 +58,7 @@ file (GLOB LIBGRIVE_SRC src/http/*.cc src/protocol/*.cc src/util/*.cc + src/util/log/*.cc src/xml/*.cc ) diff --git a/libgrive/src/drive/Drive.cc b/libgrive/src/drive/Drive.cc index 456cc1b..cb74091 100644 --- a/libgrive/src/drive/Drive.cc +++ b/libgrive/src/drive/Drive.cc @@ -28,7 +28,7 @@ #include "protocol/Json.hh" #include "protocol/OAuth2.hh" #include "util/Destroy.hh" -#include "util/Log.hh" +#include "util/log/Log.hh" #include "xml/Node.hh" #include "xml/NodeSet.hh" diff --git a/libgrive/src/drive/Entry.cc b/libgrive/src/drive/Entry.cc index ba77408..f67a3b4 100644 --- a/libgrive/src/drive/Entry.cc +++ b/libgrive/src/drive/Entry.cc @@ -21,7 +21,7 @@ #include "CommonUri.hh" #include "util/Crypt.hh" -#include "util/Log.hh" +#include "util/log/Log.hh" #include "util/OS.hh" #include "xml/Node.hh" #include "xml/NodeSet.hh" diff --git a/libgrive/src/drive/Resource.cc b/libgrive/src/drive/Resource.cc index ffd692c..61d03b4 100644 --- a/libgrive/src/drive/Resource.cc +++ b/libgrive/src/drive/Resource.cc @@ -28,7 +28,7 @@ #include "protocol/Json.hh" #include "util/CArray.hh" #include "util/Crypt.hh" -#include "util/Log.hh" +#include "util/log/Log.hh" #include "util/OS.hh" #include "util/StdioFile.hh" #include "xml/Node.hh" diff --git a/libgrive/src/drive/ResourceTree.cc b/libgrive/src/drive/ResourceTree.cc index 0523ce3..8dd9d59 100644 --- a/libgrive/src/drive/ResourceTree.cc +++ b/libgrive/src/drive/ResourceTree.cc @@ -22,7 +22,7 @@ #include "protocol/Json.hh" #include "util/Destroy.hh" -#include "util/Log.hh" +#include "util/log/Log.hh" #include #include diff --git a/libgrive/src/drive/State.cc b/libgrive/src/drive/State.cc index bdf5bac..db628c4 100644 --- a/libgrive/src/drive/State.cc +++ b/libgrive/src/drive/State.cc @@ -24,7 +24,7 @@ #include "http/Agent.hh" #include "util/Crypt.hh" -#include "util/Log.hh" +#include "util/log/Log.hh" #include "protocol/Json.hh" #include diff --git a/libgrive/src/http/Agent.cc b/libgrive/src/http/Agent.cc index 0866826..c689ae7 100644 --- a/libgrive/src/http/Agent.cc +++ b/libgrive/src/http/Agent.cc @@ -23,7 +23,7 @@ #include "Error.hh" #include "Receivable.hh" -#include "util/Log.hh" +#include "util/log/Log.hh" #include diff --git a/libgrive/src/http/XmlResponse.cc b/libgrive/src/http/XmlResponse.cc index 586a916..49395f1 100644 --- a/libgrive/src/http/XmlResponse.cc +++ b/libgrive/src/http/XmlResponse.cc @@ -19,7 +19,6 @@ #include "XmlResponse.hh" -#include "util/Log.hh" #include "xml/Node.hh" #include "xml/TreeBuilder.hh" diff --git a/libgrive/src/protocol/OAuth2.cc b/libgrive/src/protocol/OAuth2.cc index 17afd7f..6b7cade 100644 --- a/libgrive/src/protocol/OAuth2.cc +++ b/libgrive/src/protocol/OAuth2.cc @@ -23,7 +23,7 @@ #include "Json.hh" #include "http/Agent.hh" -#include "util/Log.hh" +#include "util/log/Log.hh" // for debugging #include diff --git a/libgrive/src/util/log/CommonLog.cc b/libgrive/src/util/log/CommonLog.cc new file mode 100644 index 0000000..d9cab05 --- /dev/null +++ b/libgrive/src/util/log/CommonLog.cc @@ -0,0 +1,50 @@ +/* + grive: an GPL program to sync a local directory with Google Drive + Copyright (C) 2012 Wan Wai Ho + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation version 2 + of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include "CommonLog.hh" + +namespace gr { namespace log { + +CommonLog::CommonLog() +{ + m_enabled[log::debug] = false ; + m_enabled[log::verbose] = false ; + m_enabled[log::info] = true ; + m_enabled[log::warning] = true ; + m_enabled[log::error] = true ; + m_enabled[log::critical] = true ; +} + +bool CommonLog::Enable( log::Serverity s, bool enable ) +{ + assert( s >= debug && s < serverity_count ) ; + + bool prev = m_enabled[s] ; + m_enabled[s] = enable ; + + return prev ; +} + +bool CommonLog::IsEnabled( log::Serverity s ) const +{ + assert( s >= debug && s < serverity_count ) ; + return m_enabled[s] ; +} + +}} // end of namespace diff --git a/libgrive/src/util/log/CommonLog.hh b/libgrive/src/util/log/CommonLog.hh new file mode 100644 index 0000000..a8e910e --- /dev/null +++ b/libgrive/src/util/log/CommonLog.hh @@ -0,0 +1,40 @@ +/* + grive: an GPL program to sync a local directory with Google Drive + Copyright (C) 2012 Wan Wai Ho + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation version 2 + of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#pragma once + +#include "Log.hh" + +#include + +namespace gr { namespace log { + +class CommonLog : public LogBase +{ +public : + CommonLog() ; + + bool Enable( log::Serverity s, bool enable = true ) ; + bool IsEnabled( log::Serverity s ) const ; + +private : + std::bitset m_enabled ; +} ; + +} } // end of namespace diff --git a/libgrive/src/util/log/CompositeLog.cc b/libgrive/src/util/log/CompositeLog.cc new file mode 100644 index 0000000..6c9140a --- /dev/null +++ b/libgrive/src/util/log/CompositeLog.cc @@ -0,0 +1,57 @@ +/* + grive: an GPL program to sync a local directory with Google Drive + Copyright (C) 2012 Wan Wai Ho + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation version 2 + of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include "CompositeLog.hh" + +#include +#include "util/Destroy.hh" + +namespace gr { namespace log { + +CompositeLog::CompositeLog() +{ + Enable(log::debug, true) ; + Enable(log::verbose, true) ; + Enable(log::info, true) ; + Enable(log::warning, true) ; + Enable(log::error, true) ; + Enable(log::critical, true) ; +} + +CompositeLog::~CompositeLog() +{ + std::for_each( m_logs.begin(), m_logs.end(), Destroy() ) ; +} + +LogBase* CompositeLog::Add( std::auto_ptr log ) +{ + m_logs.push_back( log.get() ) ; + return log.release() ; +} + +void CompositeLog::Log( const log::Fmt& msg, log::Serverity s ) +{ + for ( std::vector::iterator i = m_logs.begin(); i != m_logs.end(); ++i ) + { + if ( IsEnabled(s) && (*i)->IsEnabled(s) ) + (*i)->Log( msg, s ) ; + } +} + +} } // end of namespace diff --git a/libgrive/src/util/log/CompositeLog.hh b/libgrive/src/util/log/CompositeLog.hh new file mode 100644 index 0000000..6efe3be --- /dev/null +++ b/libgrive/src/util/log/CompositeLog.hh @@ -0,0 +1,43 @@ +/* + grive: an GPL program to sync a local directory with Google Drive + Copyright (C) 2012 Wan Wai Ho + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation version 2 + of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#pragma once + +#include "CommonLog.hh" + +#include +#include + +namespace gr { namespace log { + +class CompositeLog : public CommonLog +{ +public : + CompositeLog() ; + ~CompositeLog() ; + + LogBase* Add( std::auto_ptr log ) ; + + void Log( const log::Fmt& msg, log::Serverity s ) ; + +private : + std::vector m_logs ; +} ; + +}} // end of namespace diff --git a/libgrive/src/util/DefaultLog.cc b/libgrive/src/util/log/DefaultLog.cc similarity index 63% rename from libgrive/src/util/DefaultLog.cc rename to libgrive/src/util/log/DefaultLog.cc index f279a31..6f4c64d 100644 --- a/libgrive/src/util/DefaultLog.cc +++ b/libgrive/src/util/log/DefaultLog.cc @@ -22,35 +22,24 @@ #include #include -namespace gr { +namespace gr { namespace log { DefaultLog::DefaultLog() : m_log( std::cerr ) { - m_enabled[log::debug] = false ; - m_enabled[log::verbose] = false ; - m_enabled[log::info] = true ; - m_enabled[log::warning] = true ; - m_enabled[log::error] = true ; - m_enabled[log::critical] = true ; +// Enable(log::debug, true) ; +// Enable(log::verbose, true) ; } DefaultLog::DefaultLog( const std::string& filename ) : m_file( filename.c_str() ), m_log( m_file ) { - m_enabled[log::debug] = true ; - m_enabled[log::verbose] = true ; - m_enabled[log::info] = true ; - m_enabled[log::warning] = true ; - m_enabled[log::error] = true ; - m_enabled[log::critical] = true ; } void DefaultLog::Log( const log::Fmt& msg, log::Serverity s ) { - assert( s >= log::debug && s <= log::critical ) ; - if ( m_enabled[s] ) + if ( IsEnabled(s) ) { switch ( s ) { @@ -66,12 +55,4 @@ void DefaultLog::Log( const log::Fmt& msg, log::Serverity s ) } } -bool DefaultLog::Enable( log::Serverity s, bool enable ) -{ - assert( s >= log::debug && s <= log::critical ) ; - bool prev = m_enabled[s] ; - m_enabled[s] = enable ; - return prev ; -} - -} // end of namespace +} } // end of namespace diff --git a/libgrive/src/util/DefaultLog.hh b/libgrive/src/util/log/DefaultLog.hh similarity index 84% rename from libgrive/src/util/DefaultLog.hh rename to libgrive/src/util/log/DefaultLog.hh index 64625e6..60e92e1 100644 --- a/libgrive/src/util/DefaultLog.hh +++ b/libgrive/src/util/log/DefaultLog.hh @@ -19,28 +19,24 @@ #pragma once -#include "Log.hh" +#include "CommonLog.hh" -#include #include #include -namespace gr { +namespace gr { namespace log { -class DefaultLog : public LogBase +class DefaultLog : public CommonLog { public : DefaultLog() ; explicit DefaultLog( const std::string& filename ) ; void Log( const log::Fmt& msg, log::Serverity s ) ; - bool Enable( log::Serverity s, bool enable ) ; private : std::ofstream m_file ; std::ostream& m_log ; - - std::bitset<5> m_enabled ; } ; -} // end of namespace +} } // end of namespace diff --git a/libgrive/src/util/Log.cc b/libgrive/src/util/log/Log.cc similarity index 83% rename from libgrive/src/util/Log.cc rename to libgrive/src/util/log/Log.cc index cf89290..b93e1b7 100644 --- a/libgrive/src/util/Log.cc +++ b/libgrive/src/util/log/Log.cc @@ -30,23 +30,25 @@ public : { } - bool Enable( log::Serverity s, bool enable ) + bool Enable( log::Serverity, bool enable ) { return enable ; } - + bool IsEnabled( log::Serverity ) const + { + return true ; + } } ; -LogBase* LogBase::Inst( LogBase *log ) +LogBase* LogBase::Inst( std::auto_ptr log ) { - static MockLog mlog ; - static LogBase *inst = &mlog ; + static std::auto_ptr inst( new MockLog ) ; - if ( log != 0 ) + if ( log.get() != 0 ) inst = log ; - assert( inst != 0 ) ; - return inst ; + assert( inst.get() != 0 ) ; + return inst.get() ; } LogBase::LogBase() diff --git a/libgrive/src/util/Log.hh b/libgrive/src/util/log/Log.hh similarity index 92% rename from libgrive/src/util/Log.hh rename to libgrive/src/util/log/Log.hh index 043d7b0..8bdd6bb 100644 --- a/libgrive/src/util/Log.hh +++ b/libgrive/src/util/log/Log.hh @@ -20,6 +20,7 @@ #pragma once #include +#include namespace gr { @@ -45,7 +46,11 @@ namespace log error, /// grive cannot proceed - critical + critical, + + + /// must be put at the end, equal to number of serverities + serverity_count } ; typedef boost::format Fmt ; @@ -58,12 +63,13 @@ class LogBase public : virtual void Log( const log::Fmt& msg, log::Serverity s = log::info ) = 0 ; virtual bool Enable( log::Serverity s, bool enable = true ) = 0 ; + virtual bool IsEnabled( log::Serverity s ) const = 0 ; - static LogBase* Inst( LogBase *log = 0 ) ; + static LogBase* Inst( std::auto_ptr log = std::auto_ptr() ) ; + ~LogBase() ; protected : LogBase() ; - ~LogBase() ; } ; class DisableLog diff --git a/libgrive/test/UnitTest.cc b/libgrive/test/UnitTest.cc index e793f5f..f599edf 100644 --- a/libgrive/test/UnitTest.cc +++ b/libgrive/test/UnitTest.cc @@ -19,7 +19,7 @@ #include -#include "util/DefaultLog.hh" +#include "util/log/DefaultLog.hh" #include "drive/EntryTest.hh" #include "drive/ResourceTest.hh" @@ -34,10 +34,8 @@ int main( int argc, char **argv ) { using namespace grut ; - gr::DefaultLog nofile_log ; - nofile_log.Enable( gr::log::debug, true ) ; - gr::LogBase::Inst( &nofile_log ) ; - + gr::LogBase::Inst( std::auto_ptr(new gr::log::DefaultLog) ) ; + CppUnit::TextUi::TestRunner runner; runner.addTest( EntryTest::suite( ) ) ; runner.addTest( StateTest::suite( ) ) ; diff --git a/libgrive/test/drive/StateTest.cc b/libgrive/test/drive/StateTest.cc index 4982855..71ed8be 100644 --- a/libgrive/test/drive/StateTest.cc +++ b/libgrive/test/drive/StateTest.cc @@ -23,7 +23,7 @@ #include "drive/State.hh" #include "protocol/Json.hh" -#include "util/Log.hh" +#include "util/log/Log.hh" #include