mirror of https://github.com/vitalif/grive2
improved and simplified log API
parent
63553aae04
commit
4408f51300
|
@ -139,10 +139,10 @@ int main( int argc, char **argv )
|
|||
}
|
||||
catch ( const std::runtime_error& error )
|
||||
{
|
||||
Logs(
|
||||
Log(
|
||||
"Please run grive with the \"-a\" option if this is the "
|
||||
"first time you're accessing your Google Drive!",
|
||||
Log::critical ) ;
|
||||
log::critical ) ;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
@ -154,8 +154,7 @@ int main( int argc, char **argv )
|
|||
}
|
||||
catch ( gr::Exception& e )
|
||||
{
|
||||
Logs( "exception: %1%\n%2%", e.what(), *boost::get_error_info<expt::BacktraceInfo>( e ),
|
||||
Log::critical ) ;
|
||||
Log( "exception: %1%", boost::diagnostic_information(e), log::critical ) ;
|
||||
return -1 ;
|
||||
}
|
||||
|
||||
|
|
|
@ -77,7 +77,7 @@ Drive::Drive( OAuth2& auth ) :
|
|||
if ( pit != m_coll.end() && pit->IsInRootTree() )
|
||||
UpdateFile( file, *pit, &http ) ;
|
||||
else
|
||||
Logs( "file %1% parent doesn't exist, ignored", file.Title(), Log::info ) ;
|
||||
Log( "file %1% parent doesn't exist, ignored", file.Title() ) ;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -150,7 +150,7 @@ void Drive::ConstructDirTree( http::Agent *http )
|
|||
if ( e.ParentHrefs().size() == 1 )
|
||||
m_coll.push_back( Collection( e ) ) ;
|
||||
else
|
||||
Logs( "%1% has multiple parents, ignored", e.Title(), Log::info ) ;
|
||||
Log( "%1% has multiple parents, ignored", e.Title() ) ;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -171,12 +171,12 @@ void Drive::ConstructDirTree( http::Agent *http )
|
|||
{
|
||||
// it shouldn't happen, just in case
|
||||
if ( &*i == &*pit )
|
||||
Logs( "the parent of folder %1% is itself, ignored.", i->Title(), Log::warning ) ;
|
||||
Log( "the parent of folder %1% is itself, ignored.", i->Title(), log::warning ) ;
|
||||
else
|
||||
pit->AddChild( &*i ) ;
|
||||
}
|
||||
else
|
||||
Logs( "can't find folder %1% (\"%2%\")", i->Title(), i->ParentHref() ) ;
|
||||
Log( "can't find folder %1% (\"%2%\")", i->Title(), i->ParentHref(), log::warning ) ;
|
||||
}
|
||||
|
||||
// lastly, iterating from the root, create the directories in the local file system
|
||||
|
@ -205,24 +205,19 @@ void Drive::UpdateFile( Entry& file, const Collection& parent, http::Agent *http
|
|||
|
||||
// remote file is newer, download file
|
||||
if ( !ifile || remote > local )
|
||||
{
|
||||
std::cout << "downloading " << path << std::endl ;
|
||||
file.Download( http, path, m_http_hdr ) ;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
std::cout << "local " << path << " is newer" << std::endl ;
|
||||
// re-reading the file
|
||||
ifile.seekg(0) ;
|
||||
|
||||
if ( !file.Upload( http, ifile.rdbuf(), m_http_hdr ) )
|
||||
std::cout << path << " is read only" << std::endl ;
|
||||
file.Upload( http, ifile.rdbuf(), m_http_hdr ) ;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << file.Title() << " is a google document, ignored" << std::endl ;
|
||||
Log( "%1% is a google document, ignored", file.Title() ) ;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -124,6 +124,7 @@ std::string Entry::ParentHref() const
|
|||
|
||||
void Entry::Download( http::Agent* http, const Path& file, const http::Headers& auth ) const
|
||||
{
|
||||
Log( "Downloading %1%", file ) ;
|
||||
http::Download dl( file.Str(), http::Download::NoChecksum() ) ;
|
||||
long r = http->Get( m_content_src, &dl, auth ) ;
|
||||
if ( r <= 400 )
|
||||
|
@ -134,7 +135,12 @@ bool Entry::Upload( http::Agent* http, std::streambuf *file, const http::Headers
|
|||
{
|
||||
// upload link missing means that file is read only
|
||||
if ( m_upload_link.empty() )
|
||||
{
|
||||
Log( "Cannot upload %1%: file read-only.", m_title, log::warning ) ;
|
||||
return false ;
|
||||
}
|
||||
|
||||
Log( "Uploading %1%", m_title ) ;
|
||||
|
||||
std::string meta =
|
||||
"<?xml version='1.0' encoding='UTF-8'?>\n"
|
||||
|
@ -171,7 +177,7 @@ bool Entry::Upload( http::Agent* http, std::streambuf *file, const http::Headers
|
|||
http::XmlResponse xml ;
|
||||
http->Put( uplink, data, &xml, uphdr ) ;
|
||||
|
||||
Log::Inst()( Fmt("receipted response = %1%\n") % xml.Response() ) ;
|
||||
Trace( "Receipted response = %1%", xml.Response() ) ;
|
||||
|
||||
return true ;
|
||||
}
|
||||
|
|
|
@ -135,7 +135,8 @@ long Agent::ExecCurl(
|
|||
long http_code = 0;
|
||||
::curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
|
||||
|
||||
// throw exception when error
|
||||
Log( "HTTP response %1%", http_code ) ;
|
||||
|
||||
if ( curl_code != CURLE_OK || http_code >= 400 )
|
||||
{
|
||||
throw Error()
|
||||
|
@ -153,7 +154,7 @@ long Agent::Put(
|
|||
Receivable *dest,
|
||||
const http::Headers& hdr )
|
||||
{
|
||||
Log::Inst()( Fmt("HTTP PUT requesting \"%1%\"\n") % url, Log::info ) ;
|
||||
Log("HTTP PUT \"%1%\"", url ) ;
|
||||
|
||||
CURL *curl = m_pimpl->curl ;
|
||||
|
||||
|
@ -176,7 +177,7 @@ long Agent::Get(
|
|||
Receivable *dest,
|
||||
const http::Headers& hdr )
|
||||
{
|
||||
Log::Inst()( Fmt("HTTP GET requesting \"%1%\"\n") % url, Log::info ) ;
|
||||
Log("HTTP GET \"%1%\"", url ) ;
|
||||
|
||||
CURL *curl = m_pimpl->curl ;
|
||||
|
||||
|
@ -195,7 +196,7 @@ long Agent::Post(
|
|||
Receivable *dest,
|
||||
const http::Headers& hdr )
|
||||
{
|
||||
Log::Inst()( Fmt("HTTP POST requesting \"%1%\" with \"%2%\"\n") % url % data, Log::info ) ;
|
||||
Log("HTTP POST \"%1%\" with \"%2%\"", url, data ) ;
|
||||
|
||||
CURL *curl = m_pimpl->curl ;
|
||||
|
||||
|
@ -217,7 +218,7 @@ long Agent::Custom(
|
|||
Receivable *dest,
|
||||
const http::Headers& hdr )
|
||||
{
|
||||
Log::Inst()( Fmt("HTTP %2% requesting \"%1%\"\n") % url % method, Log::info ) ;
|
||||
Log("HTTP %2% \"%1%\"", url, method ) ;
|
||||
|
||||
CURL *curl = m_pimpl->curl ;
|
||||
|
||||
|
|
|
@ -22,7 +22,6 @@
|
|||
#include <boost/exception/exception.hpp>
|
||||
#include <boost/exception/info.hpp>
|
||||
|
||||
#include <typeinfo>
|
||||
#include <exception>
|
||||
#include <string>
|
||||
|
||||
|
|
|
@ -24,50 +24,49 @@
|
|||
|
||||
namespace gr {
|
||||
|
||||
class DefaultLog : public Log
|
||||
class DefaultLog : public LogBase
|
||||
{
|
||||
public :
|
||||
// do nothing
|
||||
void operator()( const Fmt& msg, Serverity s )
|
||||
{
|
||||
operator()( msg.str().c_str(), s ) ;
|
||||
}
|
||||
|
||||
void operator()( const char *str, Serverity s )
|
||||
void Log( const log::Fmt& msg, log::Serverity s )
|
||||
{
|
||||
switch ( s )
|
||||
{
|
||||
case debug:
|
||||
case info:
|
||||
std::cout << str << std::endl ;
|
||||
case log::debug:
|
||||
case log::info:
|
||||
std::cout << msg << std::endl ;
|
||||
break ;
|
||||
|
||||
default:
|
||||
std::cerr << str << std::endl ;
|
||||
std::cerr << msg << std::endl ;
|
||||
break ;
|
||||
}
|
||||
}
|
||||
} ;
|
||||
|
||||
Log& Log::Inst( Log *log )
|
||||
LogBase* LogBase::Inst( LogBase *log )
|
||||
{
|
||||
static DefaultLog mlog ;
|
||||
static Log *inst = (log == 0 ? &mlog : log ) ;
|
||||
static LogBase *inst = (log == 0 ? &mlog : log ) ;
|
||||
assert( inst != 0 ) ;
|
||||
return *inst ;
|
||||
return inst ;
|
||||
}
|
||||
|
||||
Log::Log()
|
||||
LogBase::LogBase()
|
||||
{
|
||||
}
|
||||
|
||||
Log::~Log()
|
||||
LogBase::~LogBase()
|
||||
{
|
||||
}
|
||||
|
||||
void Logs( const std::string& str, Log::Serverity s )
|
||||
void Log( const std::string& str, log::Serverity s )
|
||||
{
|
||||
Log::Inst()( str.c_str(), s ) ;
|
||||
LogBase::Inst()->Log( log::Fmt(str), s ) ;
|
||||
}
|
||||
|
||||
void Trace( const std::string& str )
|
||||
{
|
||||
LogBase::Inst()->Log( log::Fmt(str), log::debug ) ;
|
||||
}
|
||||
|
||||
} // end of namespace
|
||||
|
|
|
@ -23,44 +23,64 @@
|
|||
|
||||
namespace gr {
|
||||
|
||||
typedef boost::format Fmt ;
|
||||
namespace log
|
||||
{
|
||||
enum Serverity { debug, info, warning, error, critical } ;
|
||||
typedef boost::format Fmt ;
|
||||
}
|
||||
|
||||
/*! \brief Base class and singleton of log facilities
|
||||
*/
|
||||
class Log
|
||||
class LogBase
|
||||
{
|
||||
public :
|
||||
enum Serverity { debug, info, warning, error, critical } ;
|
||||
|
||||
public :
|
||||
virtual void operator()( const Fmt& msg, Serverity s = debug ) = 0 ;
|
||||
virtual void operator()( const char *str, Serverity s = debug ) = 0 ;
|
||||
virtual void Log( const log::Fmt& msg, log::Serverity s = log::info ) = 0 ;
|
||||
|
||||
static Log& Inst( Log *log = 0 ) ;
|
||||
static LogBase* Inst( LogBase *log = 0 ) ;
|
||||
|
||||
protected :
|
||||
Log() ;
|
||||
~Log() ;
|
||||
LogBase() ;
|
||||
~LogBase() ;
|
||||
} ;
|
||||
|
||||
void Logs( const std::string& str, Log::Serverity s = Log::debug ) ;
|
||||
void Log( const std::string& str, log::Serverity s = log::info ) ;
|
||||
|
||||
template <typename P1>
|
||||
void Logs( const std::string& fmt, const P1& p1, Log::Serverity s = Log::debug )
|
||||
void Log( const std::string& fmt, const P1& p1, log::Serverity s = log::info )
|
||||
{
|
||||
Log::Inst()( Fmt(fmt) % p1, s ) ;
|
||||
LogBase::Inst()->Log( log::Fmt(fmt) % p1, s ) ;
|
||||
}
|
||||
|
||||
template <typename P1, typename P2>
|
||||
void Logs( const std::string& fmt, const P1& p1, const P2& p2, Log::Serverity s = Log::debug )
|
||||
void Log( const std::string& fmt, const P1& p1, const P2& p2, log::Serverity s = log::info )
|
||||
{
|
||||
Log::Inst()( Fmt(fmt) % p1 % p2, s ) ;
|
||||
LogBase::Inst()->Log( log::Fmt(fmt) % p1 % p2, s ) ;
|
||||
}
|
||||
|
||||
template <typename P1, typename P2, typename P3>
|
||||
void Logs( const std::string& fmt, const P1& p1, const P2& p2, const P3& p3, Log::Serverity s = Log::debug )
|
||||
void Log( const std::string& fmt, const P1& p1, const P2& p2, const P3& p3, log::Serverity s = log::info )
|
||||
{
|
||||
Log::Inst()( Fmt(fmt) % p1 % p2 % p3, s ) ;
|
||||
LogBase::Inst()->Log( log::Fmt(fmt) % p1 % p2 % p3, s ) ;
|
||||
}
|
||||
|
||||
void Trace( const std::string& str ) ;
|
||||
|
||||
template <typename P1>
|
||||
void Trace( const std::string& fmt, const P1& p1 )
|
||||
{
|
||||
LogBase::Inst()->Log( log::Fmt(fmt) % p1, log::debug ) ;
|
||||
}
|
||||
|
||||
template <typename P1, typename P2>
|
||||
void Trace( const std::string& fmt, const P1& p1, const P2& p2 )
|
||||
{
|
||||
LogBase::Inst()->Log( log::Fmt(fmt) % p1 % p2, log::debug ) ;
|
||||
}
|
||||
|
||||
template <typename P1, typename P2, typename P3>
|
||||
void Trace( const std::string& fmt, const P1& p1, const P2& p2, const P3& p3 )
|
||||
{
|
||||
LogBase::Inst()->Log( log::Fmt(fmt) % p1 % p2 % p3, log::debug ) ;
|
||||
}
|
||||
|
||||
} // end of namespace
|
||||
|
|
Loading…
Reference in New Issue