added file log. fixed bug on getting upload link

pull/40/head
Matchman Green 2012-05-13 17:27:40 +08:00
parent 4408f51300
commit 1f551ee321
7 changed files with 152 additions and 22 deletions

View File

@ -25,6 +25,7 @@
#include "bfd/Backtrace.hh"
#include "util/Exception.hh"
#include "util/Log.hh"
#include "util/DefaultLog.hh"
#include <boost/exception/all.hpp>
@ -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

View File

@ -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() ) ;
}
}

View File

@ -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" ) ;

View File

@ -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 <cassert>
#include <iostream>
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

View File

@ -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 <bitset>
#include <fstream>
#include <string>
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

View File

@ -20,33 +20,30 @@
#include "Log.hh"
#include <cassert>
#include <iostream>
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 ;
}

View File

@ -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 ) ;