From 1f551ee3218436dec992de2213bf523ad08d3721 Mon Sep 17 00:00:00 2001 From: Matchman Green Date: Sun, 13 May 2012 17:27:40 +0800 Subject: [PATCH] added file log. fixed bug on getting upload link --- grive/src/main.cc | 14 ++++++- libgrive/src/drive/Drive.cc | 8 ++-- libgrive/src/drive/Entry.cc | 2 +- libgrive/src/util/DefaultLog.cc | 73 +++++++++++++++++++++++++++++++++ libgrive/src/util/DefaultLog.hh | 47 +++++++++++++++++++++ libgrive/src/util/Log.cc | 29 ++++++------- libgrive/src/util/Log.hh | 1 + 7 files changed, 152 insertions(+), 22 deletions(-) create mode 100644 libgrive/src/util/DefaultLog.cc create mode 100644 libgrive/src/util/DefaultLog.hh diff --git a/grive/src/main.cc b/grive/src/main.cc index 9694fd6..ea10aaa 100644 --- a/grive/src/main.cc +++ b/grive/src/main.cc @@ -25,6 +25,7 @@ #include "bfd/Backtrace.hh" #include "util/Exception.hh" #include "util/Log.hh" +#include "util/DefaultLog.hh" #include @@ -93,8 +94,11 @@ int main( int argc, char **argv ) Json config = ReadConfig() ; + DefaultLog nofile_log ; + LogBase::Inst( &nofile_log ) ; + int c ; - while ((c = getopt (argc, argv, "ac:v")) != -1) + while ((c = getopt(argc, argv, "al:v")) != -1) { switch ( c ) { @@ -123,6 +127,13 @@ int main( int argc, char **argv ) break ; } + case 'l' : + { + static DefaultLog log( optarg ) ; + LogBase::Inst( &log ) ; + break ; + } + case 'v' : { std::cout @@ -131,6 +142,7 @@ int main( int argc, char **argv ) } } } + std::string refresh_token ; try diff --git a/libgrive/src/drive/Drive.cc b/libgrive/src/drive/Drive.cc index c1792c3..79b5505 100644 --- a/libgrive/src/drive/Drive.cc +++ b/libgrive/src/drive/Drive.cc @@ -77,7 +77,7 @@ Drive::Drive( OAuth2& auth ) : if ( pit != m_coll.end() && pit->IsInRootTree() ) UpdateFile( file, *pit, &http ) ; else - Log( "file %1% parent doesn't exist, ignored", file.Title() ) ; + Log( "file \"%1%\" parent doesn't exist, ignored", file.Title() ) ; } } @@ -150,7 +150,7 @@ void Drive::ConstructDirTree( http::Agent *http ) if ( e.ParentHrefs().size() == 1 ) m_coll.push_back( Collection( e ) ) ; else - Log( "%1% has multiple parents, ignored", e.Title() ) ; + Log( "folder \"%1%\" has multiple parents, ignored", e.Title(), log::warning ) ; } } @@ -176,7 +176,7 @@ void Drive::ConstructDirTree( http::Agent *http ) pit->AddChild( &*i ) ; } else - Log( "can't find folder %1% (\"%2%\")", i->Title(), i->ParentHref(), log::warning ) ; + Log( "can't find folder \"%1%\" (\"%2%\")", i->Title(), i->ParentHref(), log::warning ) ; } // lastly, iterating from the root, create the directories in the local file system @@ -217,7 +217,7 @@ void Drive::UpdateFile( Entry& file, const Collection& parent, http::Agent *http } else { - Log( "%1% is a google document, ignored", file.Title() ) ; + Log( "file \"%1%\" is a google document, ignored", file.Title() ) ; } } diff --git a/libgrive/src/drive/Entry.cc b/libgrive/src/drive/Entry.cc index bf4d284..da87106 100644 --- a/libgrive/src/drive/Entry.cc +++ b/libgrive/src/drive/Entry.cc @@ -64,7 +64,7 @@ void Entry::Update( const xml::Node& n ) m_resource_id = n["gd:resourceId"] ; m_server_md5 = n["docs:md5Checksum"] ; m_kind = n["category"].Find( "@scheme", "http://schemas.google.com/g/2005#kind" )["@label"] ; - m_upload_link = n["link"].Find( "!rel", "http://schemas.google.com/g/2005#resumable-edit-media")["href"] ; + m_upload_link = n["link"].Find( "@rel", "http://schemas.google.com/g/2005#resumable-edit-media")["@href"] ; m_parent_hrefs.clear( ) ; xml::NodeSet parents = n["link"].Find( "@rel", "http://schemas.google.com/docs/2007#parent" ) ; diff --git a/libgrive/src/util/DefaultLog.cc b/libgrive/src/util/DefaultLog.cc new file mode 100644 index 0000000..12ebe1b --- /dev/null +++ b/libgrive/src/util/DefaultLog.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 "DefaultLog.hh" + +#include +#include + +namespace gr { + +DefaultLog::DefaultLog() : + m_log( std::cerr ) +{ + m_enabled[log::debug] = false ; + m_enabled[log::info] = true ; + m_enabled[log::warning] = true ; + m_enabled[log::error] = true ; + m_enabled[log::critical] = true ; +} + +DefaultLog::DefaultLog( const std::string& filename ) : + m_file( filename.c_str() ), + m_log( m_file ) +{ + m_enabled[log::debug] = true ; + m_enabled[log::info] = true ; + m_enabled[log::warning] = true ; + m_enabled[log::error] = true ; + m_enabled[log::critical] = true ; +} + +void DefaultLog::Log( const log::Fmt& msg, log::Serverity s ) +{ + assert( s >= log::debug && s <= log::critical ) ; + if ( m_enabled[s] ) + { + switch ( s ) + { + case log::debug: + case log::info: + m_log << msg << std::endl ; + break ; + + default: + m_log << msg << std::endl ; + break ; + } + } +} + +void DefaultLog::Enable( log::Serverity s, bool enable ) +{ + assert( s >= log::debug && s <= log::critical ) ; + m_enabled[s] = enable ; +} + +} // end of namespace diff --git a/libgrive/src/util/DefaultLog.hh b/libgrive/src/util/DefaultLog.hh new file mode 100644 index 0000000..e403d37 --- /dev/null +++ b/libgrive/src/util/DefaultLog.hh @@ -0,0 +1,47 @@ +/* + 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 "Log.hh" + +#include +#include +#include + +namespace gr { + +class DefaultLog : public LogBase +{ +public : + DefaultLog() ; + explicit DefaultLog( const std::string& filename ) ; + + void Log( const log::Fmt& msg, log::Serverity s ) ; + void Enable( log::Serverity s, bool enable ) ; + +private : + std::ofstream m_file ; + std::ostream& m_log ; + + std::bitset<5> m_enabled ; +} ; + + +} // end of namespace diff --git a/libgrive/src/util/Log.cc b/libgrive/src/util/Log.cc index c107f17..7cdce11 100644 --- a/libgrive/src/util/Log.cc +++ b/libgrive/src/util/Log.cc @@ -20,33 +20,30 @@ #include "Log.hh" #include -#include namespace gr { -class DefaultLog : public LogBase +class MockLog : public LogBase { public : - void Log( const log::Fmt& msg, log::Serverity s ) + void Log( const log::Fmt&, log::Serverity ) { - switch ( s ) - { - case log::debug: - case log::info: - std::cout << msg << std::endl ; - break ; - - default: - std::cerr << msg << std::endl ; - break ; - } } + + void Enable( log::Serverity s, bool enable ) + { + } + } ; LogBase* LogBase::Inst( LogBase *log ) { - static DefaultLog mlog ; - static LogBase *inst = (log == 0 ? &mlog : log ) ; + static MockLog mlog ; + static LogBase *inst = &mlog ; + + if ( log != 0 ) + inst = log ; + assert( inst != 0 ) ; return inst ; } diff --git a/libgrive/src/util/Log.hh b/libgrive/src/util/Log.hh index 8e5b207..30e9470 100644 --- a/libgrive/src/util/Log.hh +++ b/libgrive/src/util/Log.hh @@ -35,6 +35,7 @@ class LogBase { public : virtual void Log( const log::Fmt& msg, log::Serverity s = log::info ) = 0 ; + virtual void Enable( log::Serverity s, bool enable = true ) = 0 ; static LogBase* Inst( LogBase *log = 0 ) ;