From d3b841ccfda21c076bc14ef0be9ac40c84280caa Mon Sep 17 00:00:00 2001 From: Matchman Green Date: Thu, 10 May 2012 01:20:38 +0800 Subject: [PATCH] added debug logs. the XML nodes need to be improved --- libgrive/src/drive/Drive.cc | 9 ++- libgrive/src/http/Agent.cc | 102 ++++++++----------------------- libgrive/src/http/Agent.hh | 4 +- libgrive/src/http/ResponseLog.cc | 51 ++++++++++++++++ libgrive/src/http/ResponseLog.hh | 45 ++++++++++++++ libgrive/src/util/DateTime.cc | 14 +++++ libgrive/src/util/DateTime.hh | 5 +- 7 files changed, 150 insertions(+), 80 deletions(-) create mode 100644 libgrive/src/http/ResponseLog.cc create mode 100644 libgrive/src/http/ResponseLog.hh diff --git a/libgrive/src/drive/Drive.cc b/libgrive/src/drive/Drive.cc index 58fd943..f780464 100644 --- a/libgrive/src/drive/Drive.cc +++ b/libgrive/src/drive/Drive.cc @@ -23,6 +23,7 @@ #include "Entry.hh" #include "http/Agent.hh" +#include "http/ResponseLog.hh" #include "http/XmlResponse.hh" #include "protocol/Json.hh" #include "protocol/JsonResponse.hh" @@ -114,10 +115,12 @@ Drive::FolderListIterator Drive::FindFolder( const std::string& href ) void Drive::ConstructDirTree( http::Agent *http ) { http::XmlResponse xml ; - http->Get( root_url + "/-/folder?showroot=true&max-results=10", &xml, m_http_hdr ) ; + http::ResponseLog log( "dir-", &xml ) ; + + http->Get( root_url + "/-/folder?showroot=true&max-results=10", &log, m_http_hdr ) ; - std::ofstream abc( "abc.xml" ) ; -abc << xml.Response()["feed"] ; +std::ofstream abc( "abc.xml" ) ; +abc << xml.Response()["feed"]["entry"] ; http::JsonResponse jrsp ; http->Get( root_url + "/-/folder?alt=json", &jrsp, m_http_hdr ) ; diff --git a/libgrive/src/http/Agent.cc b/libgrive/src/http/Agent.cc index e7d4316..d47da46 100644 --- a/libgrive/src/http/Agent.cc +++ b/libgrive/src/http/Agent.cc @@ -97,10 +97,6 @@ void Agent::SetLogFile( const std::string& prefix ) m_pimpl->log_prefix = prefix ; } -std::string Agent::LogFilename() const -{ -} - std::size_t Agent::HeaderCallback( void *ptr, size_t size, size_t nmemb, Agent *pthis ) { char *str = reinterpret_cast(ptr) ; @@ -127,24 +123,12 @@ std::size_t Agent::Receive( void* ptr, size_t size, size_t nmemb, Receivable *re return recv->OnData( ptr, size * nmemb ) ; } -long Agent::Put( - const std::string& url, - const std::string& data, +long Agent::ExecCurl( Receivable *dest, const http::Headers& hdr ) { CURL *curl = m_pimpl->curl ; - - std::string put_data = data ; - - // set common options - ::curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L ) ; - ::curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); - ::curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &Agent::Receive ) ; - ::curl_easy_setopt(curl, CURLOPT_WRITEDATA, dest ) ; - ::curl_easy_setopt(curl, CURLOPT_READFUNCTION, &ReadCallback ) ; - ::curl_easy_setopt(curl, CURLOPT_READDATA , &put_data ) ; - ::curl_easy_setopt(curl, CURLOPT_INFILESIZE, put_data.size() ) ; + assert( curl != 0 ) ; SetHeader( hdr ) ; @@ -167,6 +151,28 @@ long Agent::Put( return http_code ; } +long Agent::Put( + const std::string& url, + const std::string& data, + Receivable *dest, + const http::Headers& hdr ) +{ + CURL *curl = m_pimpl->curl ; + + std::string put_data = data ; + + // set common options + ::curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L ) ; + ::curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); + ::curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &Agent::Receive ) ; + ::curl_easy_setopt(curl, CURLOPT_WRITEDATA, dest ) ; + ::curl_easy_setopt(curl, CURLOPT_READFUNCTION, &ReadCallback ) ; + ::curl_easy_setopt(curl, CURLOPT_READDATA , &put_data ) ; + ::curl_easy_setopt(curl, CURLOPT_INFILESIZE, put_data.size() ) ; + + return ExecCurl( dest, hdr ) ; +} + long Agent::Get( const std::string& url, Receivable *dest, @@ -180,29 +186,7 @@ long Agent::Get( ::curl_easy_setopt(curl, CURLOPT_WRITEDATA, dest ) ; ::curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); - SetHeader( hdr ) ; -/* - static int s_count = 0 ; - std::ostringstream logss ; - logss << "get" << s_count++ << ".txt" ; - g_log.open( logss.str().c_str() ) ; - */ - dest->Clear() ; - CURLcode curl_code = ::curl_easy_perform(curl); -// g_log.close() ; - - long http_code = 0; - ::curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code); - - if ( curl_code != CURLE_OK ) - throw Exception( curl_code, http_code, m_pimpl->error ) ; - else if (http_code >= 400 ) - { - std::cout << "http error " << http_code << std::endl ; - throw Exception( curl_code, http_code, m_pimpl->error ) ; - } - - return http_code ; + return ExecCurl( dest, hdr ) ; } long Agent::Post( @@ -222,23 +206,7 @@ long Agent::Post( ::curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &Agent::Receive ) ; ::curl_easy_setopt(curl, CURLOPT_WRITEDATA, dest ) ; - SetHeader( hdr ) ; - - dest->Clear() ; - CURLcode curl_code = ::curl_easy_perform(curl); - - long http_code = 0; - ::curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code); - - if ( curl_code != CURLE_OK ) - throw Exception( curl_code, http_code, m_pimpl->error ) ; - else if (http_code >= 400 ) - { - std::cout << "http error " << http_code << std::endl ; - throw Exception( curl_code, http_code, m_pimpl->error ) ; - } - - return http_code ; + return ExecCurl( dest, hdr ) ; } long Agent::Custom( @@ -254,23 +222,7 @@ long Agent::Custom( ::curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &Agent::Receive ) ; ::curl_easy_setopt(curl, CURLOPT_WRITEDATA, dest ) ; - SetHeader( hdr ) ; - - dest->Clear() ; - CURLcode curl_code = ::curl_easy_perform(curl); - - long http_code = 0; - ::curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code); - - if ( curl_code != CURLE_OK ) - throw Exception( curl_code, http_code, m_pimpl->error ) ; - else if (http_code >= 400 ) - { - std::cout << "http error " << http_code << std::endl ; - throw Exception( curl_code, http_code, m_pimpl->error ) ; - } - - return http_code ; + return ExecCurl( dest, hdr ) ; } void Agent::SetHeader( const http::Headers& hdr ) diff --git a/libgrive/src/http/Agent.hh b/libgrive/src/http/Agent.hh index bf8e163..3567f4f 100644 --- a/libgrive/src/http/Agent.hh +++ b/libgrive/src/http/Agent.hh @@ -76,7 +76,9 @@ private : static std::size_t Receive( void* ptr, size_t size, size_t nmemb, Receivable *recv ) ; void SetHeader( const http::Headers& hdr ) ; - std::string LogFilename() const ; + long ExecCurl( + Receivable *dest, + const http::Headers& hdr) ; private : struct Impl ; diff --git a/libgrive/src/http/ResponseLog.cc b/libgrive/src/http/ResponseLog.cc new file mode 100644 index 0000000..23442c1 --- /dev/null +++ b/libgrive/src/http/ResponseLog.cc @@ -0,0 +1,51 @@ +/* + 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 "ResponseLog.hh" + +#include "util/DateTime.hh" + +#include + +namespace gr { namespace http { + +ResponseLog::ResponseLog( const std::string& prefix, Receivable *next ) : + m_log( Filename(prefix).c_str() ), + m_next( next ) +{ +} + +std::size_t ResponseLog::OnData( void *data, std::size_t count ) +{ + m_log.rdbuf()->sputn( reinterpret_cast(data), count ) ; + return m_next->OnData( data, count ) ; +} + +void ResponseLog::Clear() +{ + assert( m_next != 0 ) ; + m_next->Clear() ; +} + +std::string ResponseLog::Filename( const std::string& prefix ) +{ + return prefix + DateTime::Now().Format( "%H%M%S" ) ; +} + +}} // end of namespace diff --git a/libgrive/src/http/ResponseLog.hh b/libgrive/src/http/ResponseLog.hh new file mode 100644 index 0000000..952e123 --- /dev/null +++ b/libgrive/src/http/ResponseLog.hh @@ -0,0 +1,45 @@ +/* + 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 "Receivable.hh" + +#include +#include + +namespace gr { namespace http { + +class ResponseLog : public Receivable +{ +public : + ResponseLog( const std::string& prefix, Receivable *next ) ; + + std::size_t OnData( void *data, std::size_t count ) ; + void Clear() ; + +private : + static std::string Filename( const std::string& prefix ) ; + +private : + std::ofstream m_log ; + Receivable *m_next ; +} ; + +} } // end of namespace diff --git a/libgrive/src/util/DateTime.cc b/libgrive/src/util/DateTime.cc index 354ee8e..424a8c0 100644 --- a/libgrive/src/util/DateTime.cc +++ b/libgrive/src/util/DateTime.cc @@ -59,6 +59,20 @@ DateTime::DateTime( std::time_t sec, unsigned long nsec ) : { } +DateTime DateTime::Now() +{ + return DateTime( std::time(0) ) ; +} + +std::string DateTime::Format( const std::string& format ) const +{ + struct tm tp = Tm() ; + + char tmp[1024] ; + std::size_t count = ::strftime( tmp, sizeof(tmp), format.c_str(), &tp ) ; + return count > 0 ? std::string( tmp, count ) : "" ; +} + struct tm DateTime::Tm() const { struct tm tp ; diff --git a/libgrive/src/util/DateTime.hh b/libgrive/src/util/DateTime.hh index 585668e..aebc86e 100644 --- a/libgrive/src/util/DateTime.hh +++ b/libgrive/src/util/DateTime.hh @@ -34,9 +34,12 @@ public : explicit DateTime( const std::string& iso ) ; explicit DateTime( std::time_t sec, unsigned long nsec = 0 ) ; + static DateTime Now() ; + std::time_t Sec( ) const ; unsigned long NanoSec( ) const ; - + std::string Format( const std::string& format ) const ; + tm Tm() const ; timeval Tv() const ;