1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_desktop.hxx" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir #include "dp_misc.h" 32*cdf0e10cSrcweir #include "rtl/strbuf.hxx" 33*cdf0e10cSrcweir #include "osl/time.h" 34*cdf0e10cSrcweir #include "osl/thread.h" 35*cdf0e10cSrcweir #include "cppuhelper/compbase1.hxx" 36*cdf0e10cSrcweir #include "comphelper/anytostring.hxx" 37*cdf0e10cSrcweir #include "comphelper/servicedecl.hxx" 38*cdf0e10cSrcweir #include "comphelper/unwrapargs.hxx" 39*cdf0e10cSrcweir #include "com/sun/star/deployment/DeploymentException.hpp" 40*cdf0e10cSrcweir #include "com/sun/star/ucb/XProgressHandler.hpp" 41*cdf0e10cSrcweir #include "com/sun/star/ucb/XSimpleFileAccess.hpp" 42*cdf0e10cSrcweir #include "com/sun/star/io/XSeekable.hpp" 43*cdf0e10cSrcweir #include <stdio.h> 44*cdf0e10cSrcweir 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir using namespace ::rtl; 47*cdf0e10cSrcweir using namespace ::com::sun::star; 48*cdf0e10cSrcweir using namespace ::com::sun::star::uno; 49*cdf0e10cSrcweir 50*cdf0e10cSrcweir namespace dp_log { 51*cdf0e10cSrcweir 52*cdf0e10cSrcweir typedef ::cppu::WeakComponentImplHelper1<ucb::XProgressHandler> t_log_helper; 53*cdf0e10cSrcweir 54*cdf0e10cSrcweir //============================================================================== 55*cdf0e10cSrcweir class ProgressLogImpl : public ::dp_misc::MutexHolder, public t_log_helper 56*cdf0e10cSrcweir { 57*cdf0e10cSrcweir Reference<io::XOutputStream> m_xLogFile; 58*cdf0e10cSrcweir sal_Int32 m_log_level; 59*cdf0e10cSrcweir void log_write( OString const & text ); 60*cdf0e10cSrcweir 61*cdf0e10cSrcweir protected: 62*cdf0e10cSrcweir virtual void SAL_CALL disposing(); 63*cdf0e10cSrcweir virtual ~ProgressLogImpl(); 64*cdf0e10cSrcweir 65*cdf0e10cSrcweir public: 66*cdf0e10cSrcweir ProgressLogImpl( Sequence<Any> const & args, 67*cdf0e10cSrcweir Reference<XComponentContext> const & xContext ); 68*cdf0e10cSrcweir 69*cdf0e10cSrcweir // XProgressHandler 70*cdf0e10cSrcweir virtual void SAL_CALL push( Any const & Status ) throw (RuntimeException); 71*cdf0e10cSrcweir virtual void SAL_CALL update( Any const & Status ) throw (RuntimeException); 72*cdf0e10cSrcweir virtual void SAL_CALL pop() throw (RuntimeException); 73*cdf0e10cSrcweir }; 74*cdf0e10cSrcweir 75*cdf0e10cSrcweir //______________________________________________________________________________ 76*cdf0e10cSrcweir ProgressLogImpl::~ProgressLogImpl() 77*cdf0e10cSrcweir { 78*cdf0e10cSrcweir } 79*cdf0e10cSrcweir 80*cdf0e10cSrcweir //______________________________________________________________________________ 81*cdf0e10cSrcweir void ProgressLogImpl::disposing() 82*cdf0e10cSrcweir { 83*cdf0e10cSrcweir try { 84*cdf0e10cSrcweir if (m_xLogFile.is()) { 85*cdf0e10cSrcweir m_xLogFile->closeOutput(); 86*cdf0e10cSrcweir m_xLogFile.clear(); 87*cdf0e10cSrcweir } 88*cdf0e10cSrcweir } 89*cdf0e10cSrcweir catch (Exception & exc) { 90*cdf0e10cSrcweir (void) exc; 91*cdf0e10cSrcweir OSL_ENSURE( 0, OUStringToOString( 92*cdf0e10cSrcweir exc.Message, RTL_TEXTENCODING_UTF8 ).getStr() ); 93*cdf0e10cSrcweir } 94*cdf0e10cSrcweir } 95*cdf0e10cSrcweir 96*cdf0e10cSrcweir //______________________________________________________________________________ 97*cdf0e10cSrcweir ProgressLogImpl::ProgressLogImpl( 98*cdf0e10cSrcweir Sequence<Any> const & args, 99*cdf0e10cSrcweir Reference<XComponentContext> const & xContext ) 100*cdf0e10cSrcweir : t_log_helper( getMutex() ), 101*cdf0e10cSrcweir m_log_level( 0 ) 102*cdf0e10cSrcweir { 103*cdf0e10cSrcweir OUString log_file; 104*cdf0e10cSrcweir boost::optional< Reference<task::XInteractionHandler> > interactionHandler; 105*cdf0e10cSrcweir comphelper::unwrapArgs( args, log_file, interactionHandler ); 106*cdf0e10cSrcweir 107*cdf0e10cSrcweir Reference<ucb::XSimpleFileAccess> xSimpleFileAccess( 108*cdf0e10cSrcweir xContext->getServiceManager()->createInstanceWithContext( 109*cdf0e10cSrcweir OUSTR("com.sun.star.ucb.SimpleFileAccess"), 110*cdf0e10cSrcweir xContext ), UNO_QUERY_THROW ); 111*cdf0e10cSrcweir // optional ia handler: 112*cdf0e10cSrcweir if (interactionHandler) 113*cdf0e10cSrcweir xSimpleFileAccess->setInteractionHandler( *interactionHandler ); 114*cdf0e10cSrcweir 115*cdf0e10cSrcweir m_xLogFile.set( 116*cdf0e10cSrcweir xSimpleFileAccess->openFileWrite( log_file ), UNO_QUERY_THROW ); 117*cdf0e10cSrcweir Reference<io::XSeekable> xSeekable( m_xLogFile, UNO_QUERY_THROW ); 118*cdf0e10cSrcweir xSeekable->seek( xSeekable->getLength() ); 119*cdf0e10cSrcweir 120*cdf0e10cSrcweir // write log stamp 121*cdf0e10cSrcweir OStringBuffer buf; 122*cdf0e10cSrcweir buf.append( 123*cdf0e10cSrcweir RTL_CONSTASCII_STRINGPARAM("###### Progress log entry ") ); 124*cdf0e10cSrcweir TimeValue m_start_time, tLocal; 125*cdf0e10cSrcweir oslDateTime date_time; 126*cdf0e10cSrcweir if (osl_getSystemTime( &m_start_time ) && 127*cdf0e10cSrcweir osl_getLocalTimeFromSystemTime( &m_start_time, &tLocal ) && 128*cdf0e10cSrcweir osl_getDateTimeFromTimeValue( &tLocal, &date_time )) 129*cdf0e10cSrcweir { 130*cdf0e10cSrcweir char ar[ 128 ]; 131*cdf0e10cSrcweir snprintf( 132*cdf0e10cSrcweir ar, sizeof (ar), 133*cdf0e10cSrcweir "%04d-%02d-%02d %02d:%02d:%02d ", 134*cdf0e10cSrcweir date_time.Year, date_time.Month, date_time.Day, 135*cdf0e10cSrcweir date_time.Hours, date_time.Minutes, date_time.Seconds ); 136*cdf0e10cSrcweir buf.append( ar ); 137*cdf0e10cSrcweir } 138*cdf0e10cSrcweir buf.append( RTL_CONSTASCII_STRINGPARAM("######\n") ); 139*cdf0e10cSrcweir log_write( buf.makeStringAndClear() ); 140*cdf0e10cSrcweir } 141*cdf0e10cSrcweir 142*cdf0e10cSrcweir //______________________________________________________________________________ 143*cdf0e10cSrcweir void ProgressLogImpl::log_write( OString const & text ) 144*cdf0e10cSrcweir { 145*cdf0e10cSrcweir try { 146*cdf0e10cSrcweir if (m_xLogFile.is()) { 147*cdf0e10cSrcweir m_xLogFile->writeBytes( 148*cdf0e10cSrcweir Sequence< sal_Int8 >( 149*cdf0e10cSrcweir reinterpret_cast< sal_Int8 const * >(text.getStr()), 150*cdf0e10cSrcweir text.getLength() ) ); 151*cdf0e10cSrcweir } 152*cdf0e10cSrcweir } 153*cdf0e10cSrcweir catch (io::IOException & exc) { 154*cdf0e10cSrcweir (void) exc; 155*cdf0e10cSrcweir OSL_ENSURE( 0, OUStringToOString( 156*cdf0e10cSrcweir exc.Message, RTL_TEXTENCODING_UTF8 ).getStr() ); 157*cdf0e10cSrcweir } 158*cdf0e10cSrcweir } 159*cdf0e10cSrcweir 160*cdf0e10cSrcweir // XProgressHandler 161*cdf0e10cSrcweir //______________________________________________________________________________ 162*cdf0e10cSrcweir void ProgressLogImpl::push( Any const & Status ) 163*cdf0e10cSrcweir throw (RuntimeException) 164*cdf0e10cSrcweir { 165*cdf0e10cSrcweir update( Status ); 166*cdf0e10cSrcweir OSL_ASSERT( m_log_level >= 0 ); 167*cdf0e10cSrcweir ++m_log_level; 168*cdf0e10cSrcweir } 169*cdf0e10cSrcweir 170*cdf0e10cSrcweir //______________________________________________________________________________ 171*cdf0e10cSrcweir void ProgressLogImpl::update( Any const & Status ) 172*cdf0e10cSrcweir throw (RuntimeException) 173*cdf0e10cSrcweir { 174*cdf0e10cSrcweir if (! Status.hasValue()) 175*cdf0e10cSrcweir return; 176*cdf0e10cSrcweir 177*cdf0e10cSrcweir OUStringBuffer buf; 178*cdf0e10cSrcweir OSL_ASSERT( m_log_level >= 0 ); 179*cdf0e10cSrcweir for ( sal_Int32 n = 0; n < m_log_level; ++n ) 180*cdf0e10cSrcweir buf.append( static_cast<sal_Unicode>(' ') ); 181*cdf0e10cSrcweir 182*cdf0e10cSrcweir OUString msg; 183*cdf0e10cSrcweir if (Status >>= msg) { 184*cdf0e10cSrcweir buf.append( msg ); 185*cdf0e10cSrcweir } 186*cdf0e10cSrcweir else { 187*cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("ERROR: ") ); 188*cdf0e10cSrcweir buf.append( ::comphelper::anyToString(Status) ); 189*cdf0e10cSrcweir } 190*cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\n") ); 191*cdf0e10cSrcweir log_write( OUStringToOString( 192*cdf0e10cSrcweir buf.makeStringAndClear(), osl_getThreadTextEncoding() ) ); 193*cdf0e10cSrcweir } 194*cdf0e10cSrcweir 195*cdf0e10cSrcweir //______________________________________________________________________________ 196*cdf0e10cSrcweir void ProgressLogImpl::pop() throw (RuntimeException) 197*cdf0e10cSrcweir { 198*cdf0e10cSrcweir OSL_ASSERT( m_log_level > 0 ); 199*cdf0e10cSrcweir --m_log_level; 200*cdf0e10cSrcweir } 201*cdf0e10cSrcweir 202*cdf0e10cSrcweir namespace sdecl = comphelper::service_decl; 203*cdf0e10cSrcweir sdecl::class_<ProgressLogImpl, sdecl::with_args<true> > servicePLI; 204*cdf0e10cSrcweir extern sdecl::ServiceDecl const serviceDecl( 205*cdf0e10cSrcweir servicePLI, 206*cdf0e10cSrcweir // a private one: 207*cdf0e10cSrcweir "com.sun.star.comp.deployment.ProgressLog", 208*cdf0e10cSrcweir "com.sun.star.comp.deployment.ProgressLog" ); 209*cdf0e10cSrcweir 210*cdf0e10cSrcweir } // namespace dp_log 211*cdf0e10cSrcweir 212