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