mirror of https://github.com/vitalif/grive2
added log interface
parent
8ced12d5a1
commit
63553aae04
|
@ -24,6 +24,7 @@
|
|||
|
||||
#include "bfd/Backtrace.hh"
|
||||
#include "util/Exception.hh"
|
||||
#include "util/Log.hh"
|
||||
|
||||
#include <boost/exception/all.hpp>
|
||||
|
||||
|
@ -138,8 +139,11 @@ int main( int argc, char **argv )
|
|||
}
|
||||
catch ( const std::runtime_error& error )
|
||||
{
|
||||
std::cerr << "Please run grive with the \"-a\" option if this is the "
|
||||
<< "first time you're accessing your Google Drive!\n";
|
||||
Logs(
|
||||
"Please run grive with the \"-a\" option if this is the "
|
||||
"first time you're accessing your Google Drive!",
|
||||
Log::critical ) ;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -150,10 +154,9 @@ int main( int argc, char **argv )
|
|||
}
|
||||
catch ( gr::Exception& e )
|
||||
{
|
||||
std::cerr
|
||||
<< "exception: " << e.what() << std::endl
|
||||
<< *boost::get_error_info<gr::expt::BacktraceInfo>( e )
|
||||
<< std::endl ;
|
||||
Logs( "exception: %1%\n%2%", e.what(), *boost::get_error_info<expt::BacktraceInfo>( e ),
|
||||
Log::critical ) ;
|
||||
return -1 ;
|
||||
}
|
||||
|
||||
return 0 ;
|
||||
|
|
|
@ -28,6 +28,7 @@
|
|||
#include "protocol/OAuth2.hh"
|
||||
#include "util/Crypt.hh"
|
||||
#include "util/DateTime.hh"
|
||||
#include "util/Log.hh"
|
||||
#include "util/OS.hh"
|
||||
#include "util/Path.hh"
|
||||
#include "xml/Node.hh"
|
||||
|
@ -76,7 +77,7 @@ Drive::Drive( OAuth2& auth ) :
|
|||
if ( pit != m_coll.end() && pit->IsInRootTree() )
|
||||
UpdateFile( file, *pit, &http ) ;
|
||||
else
|
||||
std::cout << "file " << file.Title() << " parent doesn't exist, ignored" << std::endl ;
|
||||
Logs( "file %1% parent doesn't exist, ignored", file.Title(), Log::info ) ;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -149,7 +150,7 @@ void Drive::ConstructDirTree( http::Agent *http )
|
|||
if ( e.ParentHrefs().size() == 1 )
|
||||
m_coll.push_back( Collection( e ) ) ;
|
||||
else
|
||||
std::cout << e.Title() << " has multiple parents, ignored" << std::endl ;
|
||||
Logs( "%1% has multiple parents, ignored", e.Title(), Log::info ) ;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -170,14 +171,12 @@ void Drive::ConstructDirTree( http::Agent *http )
|
|||
{
|
||||
// it shouldn't happen, just in case
|
||||
if ( &*i == &*pit )
|
||||
std::cout
|
||||
<< "the parent of folder " << i->Title()
|
||||
<< " is itself. ignored" << std::endl ;
|
||||
Logs( "the parent of folder %1% is itself, ignored.", i->Title(), Log::warning ) ;
|
||||
else
|
||||
pit->AddChild( &*i ) ;
|
||||
}
|
||||
else
|
||||
std::cout << "can't find folder " << i->Title() << " \"" << i->ParentHref() << "\"" << std::endl ;
|
||||
Logs( "can't find folder %1% (\"%2%\")", i->Title(), i->ParentHref() ) ;
|
||||
}
|
||||
|
||||
// lastly, iterating from the root, create the directories in the local file system
|
||||
|
|
|
@ -25,13 +25,13 @@
|
|||
#include "http/StringResponse.hh"
|
||||
#include "http/XmlResponse.hh"
|
||||
#include "protocol/OAuth2.hh"
|
||||
#include "util/Log.hh"
|
||||
#include "util/OS.hh"
|
||||
#include "util/Path.hh"
|
||||
#include "xml/Node.hh"
|
||||
#include "xml/NodeSet.hh"
|
||||
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
#include <sstream>
|
||||
|
||||
|
@ -171,7 +171,7 @@ bool Entry::Upload( http::Agent* http, std::streambuf *file, const http::Headers
|
|||
http::XmlResponse xml ;
|
||||
http->Put( uplink, data, &xml, uphdr ) ;
|
||||
|
||||
std::cout << xml.Response() << std::endl ;
|
||||
Log::Inst()( Fmt("receipted response = %1%\n") % xml.Response() ) ;
|
||||
|
||||
return true ;
|
||||
}
|
||||
|
@ -191,8 +191,6 @@ void Entry::Delete( http::Agent *http, const http::Headers& auth )
|
|||
http::Headers hdr( auth ) ;
|
||||
hdr.push_back( "If-Match: " + m_etag ) ;
|
||||
|
||||
std::cout << feed_base + "/" + m_resource_id + "?delete=true" << std::endl ;
|
||||
|
||||
http::StringResponse str ;
|
||||
http->Custom( "DELETE", feed_base + "/" + m_resource_id + "?delete=true", &str, hdr ) ;
|
||||
}
|
||||
|
|
|
@ -23,13 +23,14 @@
|
|||
#include "Error.hh"
|
||||
#include "Receivable.hh"
|
||||
|
||||
#include "util/Log.hh"
|
||||
|
||||
// dependent libraries
|
||||
#include <curl/curl.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <streambuf>
|
||||
|
||||
|
@ -113,12 +114,8 @@ std::size_t Agent::HeaderCallback( void *ptr, size_t size, size_t nmemb, Agent *
|
|||
return size*nmemb ;
|
||||
}
|
||||
|
||||
// std::ofstream g_log ;
|
||||
|
||||
std::size_t Agent::Receive( void* ptr, size_t size, size_t nmemb, Receivable *recv )
|
||||
{
|
||||
// g_log.rdbuf()->sputn( (char*)ptr, size*nmemb ) ;
|
||||
|
||||
assert( recv != 0 ) ;
|
||||
return recv->OnData( ptr, size * nmemb ) ;
|
||||
}
|
||||
|
@ -138,7 +135,7 @@ long Agent::ExecCurl(
|
|||
long http_code = 0;
|
||||
::curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
|
||||
|
||||
|
||||
// throw exception when error
|
||||
if ( curl_code != CURLE_OK || http_code >= 400 )
|
||||
{
|
||||
throw Error()
|
||||
|
@ -156,6 +153,8 @@ long Agent::Put(
|
|||
Receivable *dest,
|
||||
const http::Headers& hdr )
|
||||
{
|
||||
Log::Inst()( Fmt("HTTP PUT requesting \"%1%\"\n") % url, Log::info ) ;
|
||||
|
||||
CURL *curl = m_pimpl->curl ;
|
||||
|
||||
std::string put_data = data ;
|
||||
|
@ -177,6 +176,8 @@ long Agent::Get(
|
|||
Receivable *dest,
|
||||
const http::Headers& hdr )
|
||||
{
|
||||
Log::Inst()( Fmt("HTTP GET requesting \"%1%\"\n") % url, Log::info ) ;
|
||||
|
||||
CURL *curl = m_pimpl->curl ;
|
||||
|
||||
// set common options
|
||||
|
@ -194,6 +195,8 @@ long Agent::Post(
|
|||
Receivable *dest,
|
||||
const http::Headers& hdr )
|
||||
{
|
||||
Log::Inst()( Fmt("HTTP POST requesting \"%1%\" with \"%2%\"\n") % url % data, Log::info ) ;
|
||||
|
||||
CURL *curl = m_pimpl->curl ;
|
||||
|
||||
std::string post_data = data ;
|
||||
|
@ -214,6 +217,8 @@ long Agent::Custom(
|
|||
Receivable *dest,
|
||||
const http::Headers& hdr )
|
||||
{
|
||||
Log::Inst()( Fmt("HTTP %2% requesting \"%1%\"\n") % url % method, Log::info ) ;
|
||||
|
||||
CURL *curl = m_pimpl->curl ;
|
||||
|
||||
::curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, method.c_str() );
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
/*
|
||||
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 "util/Exception.hh"
|
||||
|
||||
namespace gr { namespace http {
|
||||
|
||||
struct Error : virtual Exception {} ;
|
||||
|
||||
// CURL error code
|
||||
typedef boost::error_info<struct CurlCodeTag, int> CurlCode ;
|
||||
|
||||
// HTTP response code
|
||||
typedef boost::error_info<struct HttpResponseTag, int> HttpResponse ;
|
||||
|
||||
} } // end of namespace
|
|
@ -0,0 +1,73 @@
|
|||
/*
|
||||
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 "Log.hh"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
|
||||
namespace gr {
|
||||
|
||||
class DefaultLog : public Log
|
||||
{
|
||||
public :
|
||||
// do nothing
|
||||
void operator()( const Fmt& msg, Serverity s )
|
||||
{
|
||||
operator()( msg.str().c_str(), s ) ;
|
||||
}
|
||||
|
||||
void operator()( const char *str, Serverity s )
|
||||
{
|
||||
switch ( s )
|
||||
{
|
||||
case debug:
|
||||
case info:
|
||||
std::cout << str << std::endl ;
|
||||
break ;
|
||||
|
||||
default:
|
||||
std::cerr << str << std::endl ;
|
||||
break ;
|
||||
}
|
||||
}
|
||||
} ;
|
||||
|
||||
Log& Log::Inst( Log *log )
|
||||
{
|
||||
static DefaultLog mlog ;
|
||||
static Log *inst = (log == 0 ? &mlog : log ) ;
|
||||
assert( inst != 0 ) ;
|
||||
return *inst ;
|
||||
}
|
||||
|
||||
Log::Log()
|
||||
{
|
||||
}
|
||||
|
||||
Log::~Log()
|
||||
{
|
||||
}
|
||||
|
||||
void Logs( const std::string& str, Log::Serverity s )
|
||||
{
|
||||
Log::Inst()( str.c_str(), s ) ;
|
||||
}
|
||||
|
||||
} // end of namespace
|
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
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 <boost/format.hpp>
|
||||
|
||||
namespace gr {
|
||||
|
||||
typedef boost::format Fmt ;
|
||||
|
||||
/*! \brief Base class and singleton of log facilities
|
||||
*/
|
||||
class Log
|
||||
{
|
||||
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 ;
|
||||
|
||||
static Log& Inst( Log *log = 0 ) ;
|
||||
|
||||
protected :
|
||||
Log() ;
|
||||
~Log() ;
|
||||
} ;
|
||||
|
||||
void Logs( const std::string& str, Log::Serverity s = Log::debug ) ;
|
||||
|
||||
template <typename P1>
|
||||
void Logs( const std::string& fmt, const P1& p1, Log::Serverity s = Log::debug )
|
||||
{
|
||||
Log::Inst()( 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 )
|
||||
{
|
||||
Log::Inst()( 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 )
|
||||
{
|
||||
Log::Inst()( Fmt(fmt) % p1 % p2 % p3, s ) ;
|
||||
}
|
||||
|
||||
} // end of namespace
|
|
@ -0,0 +1,28 @@
|
|||
/*
|
||||
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 "util/Exception.hh"
|
||||
|
||||
namespace gr { namespace xml {
|
||||
|
||||
struct Error : virtual Exception {} ;
|
||||
|
||||
} } // end of namespace
|
Loading…
Reference in New Issue