Permalink
Browse files

refactored the logs to debug sync problems

  • Loading branch information...
1 parent 278037b commit 80f3fbf37055baa038339d6f633178fc6fdba1d2 @nestal nestal committed Jul 11, 2012
@@ -139,28 +139,32 @@ void Drive::DetectChanges()
SyncFolders( &http ) ;
Log( "Reading remote server file list", log::info ) ;
- http::XmlResponse xrsp ;
- http.Get( feed_base + "?showfolders=true&showroot=true", &xrsp, m_http_hdr ) ;
- xml::Node resp = xrsp.Response() ;
-
- m_resume_link = resp["link"].
+ Feed feed ;
+// feed.EnableLog( "/tmp/file", ".xml" ) ;
+ feed.Start( &http, m_http_hdr, feed_base + "?showfolders=true&showroot=true" ) ;
+
+ m_resume_link = feed.Root()["link"].
Find( "@rel", "http://schemas.google.com/g/2005#resumable-create-media" )["@href"] ;
-
- Feed feed( resp ) ;
+
do
{
- std::for_each( feed.begin(), feed.end(), boost::bind( &Drive::FromRemote, this, _1 ) ) ;
+ std::for_each(
+ feed.begin(), feed.end(),
+ boost::bind( &Drive::FromRemote, this, _1 ) ) ;
+
} while ( feed.GetNext( &http, m_http_hdr ) ) ;
// pull the changes feed
if ( prev_stamp != -1 )
{
- http::ResponseLog log( "/tmp/changes-", ".xml", &xrsp ) ;
Log( "Detecting changes from last sync", log::info ) ;
- http.Get( ChangesFeed(prev_stamp+1), &log, m_http_hdr ) ;
+ Feed changes ;
+// feed.EnableLog( "/tmp/changes", ".xml" ) ;
+ feed.Start( &http, m_http_hdr, ChangesFeed(prev_stamp+1) ) ;
- Feed changes( xrsp.Response() ) ;
- std::for_each( changes.begin(), changes.end(), boost::bind( &Drive::FromChange, this, _1 ) ) ;
+ std::for_each(
+ changes.begin(), changes.end(),
+ boost::bind( &Drive::FromChange, this, _1 ) ) ;
}
}
View
@@ -20,13 +20,20 @@
#include "Feed.hh"
#include "http/Agent.hh"
+#include "http/ResponseLog.hh"
#include "http/XmlResponse.hh"
#include "xml/NodeSet.hh"
+#include <boost/format.hpp>
+
#include <cassert>
namespace gr {
+Feed::Feed( )
+{
+}
+
Feed::Feed( const xml::Node& root ) :
m_root ( root ),
m_entries ( m_root["entry"] )
@@ -55,19 +62,30 @@ std::string Feed::Next() const
return nss.empty() ? "" : std::string(nss["@href"]) ;
}
+void Feed::Start( http::Agent *http, const http::Header& auth, const std::string& url )
+{
+ http::XmlResponse xrsp ;
+ http::ResponseLog log( &xrsp ) ;
+
+ if ( m_log.get() != 0 )
+ log.Reset(
+ (boost::format( "%1%-%2%." ) % m_log->prefix % m_log->sequence++).str(),
+ m_log->suffix, &xrsp ) ;
+
+ http->Get( url, &log, auth ) ;
+
+ m_root = xrsp.Response() ;
+ m_entries = m_root["entry"] ;
+}
+
bool Feed::GetNext( http::Agent *http, const http::Header& auth )
{
assert( http != 0 ) ;
xml::NodeSet nss = m_root["link"].Find( "@rel", "next" ) ;
if ( !nss.empty() )
{
- http::XmlResponse xrsp ;
- http->Get( nss["@href"], &xrsp, auth ) ;
-
- m_root = xrsp.Response() ;
- m_entries = m_root["entry"] ;
-
+ Start( http, auth, nss["@href"] ) ;
return true ;
}
else
@@ -90,4 +108,17 @@ Feed::iterator::reference Feed::iterator::dereference() const
return Entry( *base_reference() ) ;
}
+void Feed::EnableLog( const std::string& prefix, const std::string& suffix )
+{
+ m_log.reset( new LogInfo ) ;
+ m_log->prefix = prefix ;
+ m_log->suffix = suffix ;
+ m_log->sequence = 0 ;
+}
+
+const xml::Node& Feed::Root() const
+{
+ return m_root ;
+}
+
} // end of namespace
View
@@ -43,15 +43,29 @@ public :
public :
explicit Feed( const xml::Node& root ) ;
+ Feed( ) ;
+ void Start( http::Agent *http, const http::Header& auth, const std::string& url ) ;
+
void Assign( const xml::Node& root ) ;
+ const xml::Node& Root() const ;
iterator begin() const ;
iterator end() const ;
std::string Next() const ;
bool GetNext( http::Agent *http, const http::Header& auth ) ;
-
+
+ void EnableLog( const std::string& prefix, const std::string& suffix ) ;
+
private :
+ struct LogInfo
+ {
+ std::string prefix ;
+ std::string suffix ;
+ std::size_t sequence ;
+ } ;
+ std::auto_ptr<LogInfo> m_log ;
+
xml::Node m_root ;
xml::NodeSet m_entries ;
} ;
@@ -259,7 +259,10 @@ void Resource::FromLocal( const DateTime& last_sync )
// remote_deleted first, it will be updated to sync/remote_changed
// in FromRemote()
else
+ {
+// Trace( "file %1% mtime %2%, last_sync %3%", path, m_mtime, last_sync ) ;
m_state = ( m_mtime > last_sync ? local_new : remote_deleted ) ;
+ }
m_name = Path2Str( path.filename() ) ;
m_kind = fs::is_directory(path) ? "folder" : "file" ;
@@ -73,7 +73,6 @@ void State::FromLocal( const fs::path& p, gr::Resource* folder )
for ( fs::directory_iterator i( p ) ; i != fs::directory_iterator() ; ++i )
{
std::string fname = Path2Str(i->path().filename()) ;
-// Trace( "found file %1%", i->path() ) ;
if ( IsIgnore(fname) )
Log( "file %1% is ignored by grive", fname, log::verbose ) ;
@@ -29,14 +29,28 @@ ResponseLog::ResponseLog(
const std::string& prefix,
const std::string& suffix,
Receivable *next ) :
- m_log( Filename(prefix, suffix).c_str() ),
- m_next( next )
+ m_enabled ( true ),
+ m_log ( Filename(prefix, suffix).c_str() ),
+ m_next ( next )
{
+ assert( m_next != 0 ) ;
+}
+
+ResponseLog::ResponseLog( Receivable *next ) :
+ m_enabled ( false ),
+ m_next ( next )
+{
+ assert( m_next != 0 ) ;
}
std::size_t ResponseLog::OnData( void *data, std::size_t count )
{
- m_log.rdbuf()->sputn( reinterpret_cast<char*>(data), count ) ;
+ if ( m_enabled )
+ {
+ assert( m_log.rdbuf() != 0 ) ;
+ m_log.rdbuf()->sputn( reinterpret_cast<char*>(data), count ) ;
+ }
+
return m_next->OnData( data, count ) ;
}
@@ -51,4 +65,21 @@ std::string ResponseLog::Filename( const std::string& prefix, const std::string&
return prefix + DateTime::Now().Format( "%H%M%S" ) + suffix ;
}
+void ResponseLog::Enable( bool enable )
+{
+ m_enabled = enable ;
+}
+
+void ResponseLog::Reset( const std::string& prefix, const std::string& suffix, Receivable *next )
+{
+ assert( next != 0 ) ;
+
+ if ( m_log.is_open() )
+ m_log.close() ;
+
+ m_log.open( Filename(prefix, suffix).c_str() ) ;
+ m_next = next ;
+ m_enabled = true ;
+}
+
}} // end of namespace
@@ -33,14 +33,19 @@ public :
const std::string& prefix,
const std::string& suffix,
Receivable *next ) ;
-
+ ResponseLog( Receivable *next ) ;
+
std::size_t OnData( void *data, std::size_t count ) ;
void Clear() ;
+ void Enable( bool enable = true ) ;
+ void Reset( const std::string& prefix, const std::string& suffix, Receivable *next ) ;
+
private :
static std::string Filename( const std::string& prefix, const std::string& suffix ) ;
private :
+ bool m_enabled ;
std::ofstream m_log ;
Receivable *m_next ;
} ;

0 comments on commit 80f3fbf

Please sign in to comment.