1 /*
2 	grive: an GPL program to sync a local directory with Google Drive
3 	Copyright (C) 2012  Wan Wai Ho
4 
5 	This program is free software; you can redistribute it and/or
6 	modify it under the terms of the GNU General Public License
7 	as published by the Free Software Foundation version 2
8 	of the License.
9 
10 	This program is distributed in the hope that it will be useful,
11 	but WITHOUT ANY WARRANTY; without even the implied warranty of
12 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 	GNU General Public License for more details.
14 
15 	You should have received a copy of the GNU General Public License
16 	along with this program; if not, write to the Free Software
17 	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 */
19 
20 #include "ResponseLog.hh"
21 
22 #include "util/log/Log.hh"
23 #include "util/DateTime.hh"
24 
25 #include <cassert>
26 
27 namespace gr { namespace http {
28 
ResponseLog(const std::string & prefix,const std::string & suffix)29 ResponseLog::ResponseLog(
30 	const std::string&	prefix,
31 	const std::string&	suffix )
32 {
33 	Reset( prefix, suffix ) ;
34 }
35 
Write(const char * data,std::size_t count)36 std::size_t ResponseLog::Write( const char *data, std::size_t count )
37 {
38 	if ( m_enabled )
39 	{
40 		assert( m_log.rdbuf() != 0 ) ;
41 		m_log.rdbuf()->sputn( data, count ) ;
42 		m_log.flush();
43 	}
44 	return count;
45 }
46 
Read(char * data,std::size_t count)47 std::size_t ResponseLog::Read( char *data, std::size_t count )
48 {
49 	return 0 ;
50 }
51 
Filename(const std::string & prefix,const std::string & suffix)52 std::string ResponseLog::Filename( const std::string& prefix, const std::string& suffix )
53 {
54 	return prefix + DateTime::Now().Format( "%F.%H%M%S" ) + suffix ;
55 }
56 
Reset(const std::string & prefix,const std::string & suffix)57 void ResponseLog::Reset( const std::string& prefix, const std::string& suffix )
58 {
59 	if ( m_log.is_open() )
60 		m_log.close() ;
61 
62 	const std::string fname = Filename( prefix, suffix ) ;
63 
64 	// reset previous stream state. don't care if file can be opened
65 	// successfully previously
66 	m_log.clear() ;
67 
68 	// re-open the file
69 	m_log.open( fname.c_str() ) ;
70 	if ( m_log )
71 	{
72 		Trace( "logging HTTP response: %1%", fname ) ;
73 		m_enabled = true ;
74 	}
75 	else
76 	{
77 		Trace( "cannot open log file %1%", fname ) ;
78 		m_enabled = false ;
79 	}
80 }
81 
82 }} // end of namespace
83