fully configurable log

pull/40/head
Nestal Wan 2012-06-03 18:31:02 +08:00
parent 53f9c39941
commit 379dddc532
20 changed files with 253 additions and 66 deletions

View File

@ -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 <boost/exception/all.hpp>
@ -45,13 +46,13 @@ int main( int argc, char **argv )
Config config ;
DefaultLog nofile_log ;
LogBase::Inst( &nofile_log ) ;
std::auto_ptr<log::CompositeLog> comp_log(new log::CompositeLog) ;
LogBase* console_log = comp_log->Add( std::auto_ptr<LogBase>( 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<LogBase> 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<LogBase>(comp_log.release()) ) ;
std::string refresh_token ;
try

View File

@ -58,6 +58,7 @@ file (GLOB LIBGRIVE_SRC
src/http/*.cc
src/protocol/*.cc
src/util/*.cc
src/util/log/*.cc
src/xml/*.cc
)

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -22,7 +22,7 @@
#include "protocol/Json.hh"
#include "util/Destroy.hh"
#include "util/Log.hh"
#include "util/log/Log.hh"
#include <algorithm>
#include <cassert>

View File

@ -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 <boost/bind.hpp>

View File

@ -23,7 +23,7 @@
#include "Error.hh"
#include "Receivable.hh"
#include "util/Log.hh"
#include "util/log/Log.hh"
#include <boost/throw_exception.hpp>

View File

@ -19,7 +19,6 @@
#include "XmlResponse.hh"
#include "util/Log.hh"
#include "xml/Node.hh"
#include "xml/TreeBuilder.hh"

View File

@ -23,7 +23,7 @@
#include "Json.hh"
#include "http/Agent.hh"
#include "util/Log.hh"
#include "util/log/Log.hh"
// for debugging
#include <iostream>

View File

@ -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

View File

@ -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 <bitset>
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<serverity_count> m_enabled ;
} ;
} } // end of namespace

View File

@ -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 <algorithm>
#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<LogBase> log )
{
m_logs.push_back( log.get() ) ;
return log.release() ;
}
void CompositeLog::Log( const log::Fmt& msg, log::Serverity s )
{
for ( std::vector<LogBase*>::iterator i = m_logs.begin(); i != m_logs.end(); ++i )
{
if ( IsEnabled(s) && (*i)->IsEnabled(s) )
(*i)->Log( msg, s ) ;
}
}
} } // end of namespace

View File

@ -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 <memory>
#include <vector>
namespace gr { namespace log {
class CompositeLog : public CommonLog
{
public :
CompositeLog() ;
~CompositeLog() ;
LogBase* Add( std::auto_ptr<LogBase> log ) ;
void Log( const log::Fmt& msg, log::Serverity s ) ;
private :
std::vector<LogBase*> m_logs ;
} ;
}} // end of namespace

View File

@ -22,35 +22,24 @@
#include <cassert>
#include <iostream>
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

View File

@ -19,28 +19,24 @@
#pragma once
#include "Log.hh"
#include "CommonLog.hh"
#include <bitset>
#include <fstream>
#include <string>
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

View File

@ -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<LogBase> log )
{
static MockLog mlog ;
static LogBase *inst = &mlog ;
static std::auto_ptr<LogBase> 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()

View File

@ -20,6 +20,7 @@
#pragma once
#include <boost/format.hpp>
#include <memory>
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<LogBase> log = std::auto_ptr<LogBase>() ) ;
~LogBase() ;
protected :
LogBase() ;
~LogBase() ;
} ;
class DisableLog

View File

@ -19,7 +19,7 @@
#include <cppunit/ui/text/TestRunner.h>
#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<gr::LogBase>(new gr::log::DefaultLog) ) ;
CppUnit::TextUi::TestRunner runner;
runner.addTest( EntryTest::suite( ) ) ;
runner.addTest( StateTest::suite( ) ) ;

View File

@ -23,7 +23,7 @@
#include "drive/State.hh"
#include "protocol/Json.hh"
#include "util/Log.hh"
#include "util/log/Log.hh"
#include <iostream>