From 593c335a5e408e3644efac5e2e91a82df5d3b7b9 Mon Sep 17 00:00:00 2001 From: Nestal Wan Date: Mon, 16 Jul 2012 23:23:08 +0800 Subject: [PATCH] improved logging for HTTP responses --- libgrive/src/drive/Drive.cc | 4 ++-- libgrive/src/drive/Feed.cc | 5 +++-- libgrive/src/drive/Resource.cc | 9 +++++++-- libgrive/src/http/ResponseLog.cc | 24 +++++++++++++++++++----- libgrive/src/util/log/Log.hh | 6 ++++++ 5 files changed, 37 insertions(+), 11 deletions(-) diff --git a/libgrive/src/drive/Drive.cc b/libgrive/src/drive/Drive.cc index f0d454f..f83bdd6 100644 --- a/libgrive/src/drive/Drive.cc +++ b/libgrive/src/drive/Drive.cc @@ -140,7 +140,7 @@ void Drive::DetectChanges() Log( "Reading remote server file list", log::info ) ; Feed feed ; -// feed.EnableLog( "/tmp/file", ".xml" ) ; + feed.EnableLog( "/tmp/file", ".xml" ) ; feed.Start( &http, m_http_hdr, feed_base + "?showfolders=true&showroot=true" ) ; m_resume_link = feed.Root()["link"]. @@ -159,7 +159,7 @@ void Drive::DetectChanges() { Log( "Detecting changes from last sync", log::info ) ; Feed changes ; -// feed.EnableLog( "/tmp/changes", ".xml" ) ; + feed.EnableLog( "/tmp/changes", ".xml" ) ; feed.Start( &http, m_http_hdr, ChangesFeed(prev_stamp+1) ) ; std::for_each( diff --git a/libgrive/src/drive/Feed.cc b/libgrive/src/drive/Feed.cc index 72bfa9a..a0fe956 100644 --- a/libgrive/src/drive/Feed.cc +++ b/libgrive/src/drive/Feed.cc @@ -69,8 +69,9 @@ void Feed::Start( http::Agent *http, const http::Header& auth, const std::string if ( m_log.get() != 0 ) log.Reset( - (boost::format( "%1%-%2%." ) % m_log->prefix % m_log->sequence++).str(), - m_log->suffix, &xrsp ) ; + m_log->prefix, + (boost::format( "-#%1%%2%" ) % m_log->sequence++ % m_log->suffix ).str(), + &xrsp ) ; http->Get( url, &log, auth ) ; diff --git a/libgrive/src/drive/Resource.cc b/libgrive/src/drive/Resource.cc index e253cac..fb8d231 100644 --- a/libgrive/src/drive/Resource.cc +++ b/libgrive/src/drive/Resource.cc @@ -260,8 +260,8 @@ void Resource::FromLocal( const DateTime& last_sync ) // 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 ) ; +// Trace( "file %1% mtime %2%, last_sync %3%, state (%4%)", path, m_mtime, last_sync, m_state ) ; } m_name = Path2Str( path.filename() ) ; @@ -567,7 +567,12 @@ bool Resource::Create( http::Agent* http, const http::Header& auth, DateTime& sy } } -bool Resource::Upload( http::Agent* http, const std::string& link, const http::Header& auth, bool post, DateTime& sync_time ) +bool Resource::Upload( + http::Agent* http, + const std::string& link, + const http::Header& auth, + bool post, + DateTime& sync_time ) { assert( http != 0 ) ; diff --git a/libgrive/src/http/ResponseLog.cc b/libgrive/src/http/ResponseLog.cc index 8de81fe..3384bf7 100644 --- a/libgrive/src/http/ResponseLog.cc +++ b/libgrive/src/http/ResponseLog.cc @@ -19,6 +19,7 @@ #include "ResponseLog.hh" +#include "util/log/Log.hh" #include "util/DateTime.hh" #include @@ -30,10 +31,9 @@ ResponseLog::ResponseLog( const std::string& suffix, Receivable *next ) : m_enabled ( true ), - m_log ( Filename(prefix, suffix).c_str() ), m_next ( next ) { - assert( m_next != 0 ) ; + Reset( prefix, suffix, next ) ; } ResponseLog::ResponseLog( Receivable *next ) : @@ -62,7 +62,7 @@ void ResponseLog::Clear() std::string ResponseLog::Filename( const std::string& prefix, const std::string& suffix ) { - return prefix + DateTime::Now().Format( "%H%M%S" ) + suffix ; + return prefix + DateTime::Now().Format( "%F.%H%M%S" ) + suffix ; } void ResponseLog::Enable( bool enable ) @@ -77,9 +77,23 @@ void ResponseLog::Reset( const std::string& prefix, const std::string& suffix, R if ( m_log.is_open() ) m_log.close() ; - m_log.open( Filename(prefix, suffix).c_str() ) ; + const std::string fname = Filename(prefix, suffix) ; + + // reset previous stream state. don't care if file can be opened + // successfully previously + m_log.clear() ; + + // re-open the file + m_log.open( fname.c_str() ) ; + if ( m_log ) + { + Trace( "logging HTTP response: %1%", fname ) ; + m_enabled = true ; + } + else + Trace( "cannot open log file %1%", fname ) ; + m_next = next ; - m_enabled = true ; } }} // end of namespace diff --git a/libgrive/src/util/log/Log.hh b/libgrive/src/util/log/Log.hh index 8bdd6bb..0d43adb 100644 --- a/libgrive/src/util/log/Log.hh +++ b/libgrive/src/util/log/Log.hh @@ -135,4 +135,10 @@ 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 ) ; } +template +void Trace( const std::string& fmt, const P1& p1, const P2& p2, const P3& p3, const P4& p4 ) +{ + LogBase::Inst()->Log( log::Fmt(fmt) % p1 % p2 % p3 % p4, log::debug ) ; +} + } // end of namespace