diff --git a/grive/src/main.cc b/grive/src/main.cc index e270da6..f211a13 100644 --- a/grive/src/main.cc +++ b/grive/src/main.cc @@ -24,6 +24,7 @@ #include "bfd/Backtrace.hh" #include "util/Exception.hh" +#include "util/Log.hh" #include @@ -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( e ) - << std::endl ; + Logs( "exception: %1%\n%2%", e.what(), *boost::get_error_info( e ), + Log::critical ) ; + return -1 ; } return 0 ; diff --git a/libgrive/src/drive/Drive.cc b/libgrive/src/drive/Drive.cc index ff47399..55bd22f 100644 --- a/libgrive/src/drive/Drive.cc +++ b/libgrive/src/drive/Drive.cc @@ -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 diff --git a/libgrive/src/drive/Entry.cc b/libgrive/src/drive/Entry.cc index 4bf9bb9..15946f8 100644 --- a/libgrive/src/drive/Entry.cc +++ b/libgrive/src/drive/Entry.cc @@ -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 -#include #include #include @@ -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 ) ; } diff --git a/libgrive/src/http/Agent.cc b/libgrive/src/http/Agent.cc index b562ada..a284722 100644 --- a/libgrive/src/http/Agent.cc +++ b/libgrive/src/http/Agent.cc @@ -23,13 +23,14 @@ #include "Error.hh" #include "Receivable.hh" +#include "util/Log.hh" + // dependent libraries #include #include #include #include -#include #include #include @@ -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() ); diff --git a/libgrive/src/http/Error.hh b/libgrive/src/http/Error.hh new file mode 100644 index 0000000..f6ed2d7 --- /dev/null +++ b/libgrive/src/http/Error.hh @@ -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 CurlCode ; + +// HTTP response code +typedef boost::error_info HttpResponse ; + +} } // end of namespace diff --git a/libgrive/src/util/Log.cc b/libgrive/src/util/Log.cc new file mode 100644 index 0000000..69b12bb --- /dev/null +++ b/libgrive/src/util/Log.cc @@ -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 +#include + +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 diff --git a/libgrive/src/util/Log.hh b/libgrive/src/util/Log.hh new file mode 100644 index 0000000..35610b2 --- /dev/null +++ b/libgrive/src/util/Log.hh @@ -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 + +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 +void Logs( const std::string& fmt, const P1& p1, Log::Serverity s = Log::debug ) +{ + Log::Inst()( Fmt(fmt) % p1, s ) ; +} + +template +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 +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 diff --git a/libgrive/src/xml/Error.hh b/libgrive/src/xml/Error.hh new file mode 100644 index 0000000..3aa3d5e --- /dev/null +++ b/libgrive/src/xml/Error.hh @@ -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