1*2722ceddSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*2722ceddSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*2722ceddSAndrew Rist * or more contributor license agreements. See the NOTICE file
5*2722ceddSAndrew Rist * distributed with this work for additional information
6*2722ceddSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*2722ceddSAndrew Rist * to you under the Apache License, Version 2.0 (the
8*2722ceddSAndrew Rist * "License"); you may not use this file except in compliance
9*2722ceddSAndrew Rist * with the License. You may obtain a copy of the License at
10cdf0e10cSrcweir *
11*2722ceddSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir *
13*2722ceddSAndrew Rist * Unless required by applicable law or agreed to in writing,
14*2722ceddSAndrew Rist * software distributed under the License is distributed on an
15*2722ceddSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2722ceddSAndrew Rist * KIND, either express or implied. See the License for the
17*2722ceddSAndrew Rist * specific language governing permissions and limitations
18*2722ceddSAndrew Rist * under the License.
19cdf0e10cSrcweir *
20*2722ceddSAndrew Rist *************************************************************/
21*2722ceddSAndrew Rist
22*2722ceddSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_desktop.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include "dp_misc.h"
28cdf0e10cSrcweir #include "rtl/strbuf.hxx"
29cdf0e10cSrcweir #include "osl/time.h"
30cdf0e10cSrcweir #include "osl/thread.h"
31cdf0e10cSrcweir #include "cppuhelper/compbase1.hxx"
32cdf0e10cSrcweir #include "comphelper/anytostring.hxx"
33cdf0e10cSrcweir #include "comphelper/servicedecl.hxx"
34cdf0e10cSrcweir #include "comphelper/unwrapargs.hxx"
35cdf0e10cSrcweir #include "com/sun/star/deployment/DeploymentException.hpp"
36cdf0e10cSrcweir #include "com/sun/star/ucb/XProgressHandler.hpp"
37cdf0e10cSrcweir #include "com/sun/star/ucb/XSimpleFileAccess.hpp"
38cdf0e10cSrcweir #include "com/sun/star/io/XSeekable.hpp"
39cdf0e10cSrcweir #include <stdio.h>
40cdf0e10cSrcweir
41cdf0e10cSrcweir
42cdf0e10cSrcweir using namespace ::rtl;
43cdf0e10cSrcweir using namespace ::com::sun::star;
44cdf0e10cSrcweir using namespace ::com::sun::star::uno;
45cdf0e10cSrcweir
46cdf0e10cSrcweir namespace dp_log {
47cdf0e10cSrcweir
48cdf0e10cSrcweir typedef ::cppu::WeakComponentImplHelper1<ucb::XProgressHandler> t_log_helper;
49cdf0e10cSrcweir
50cdf0e10cSrcweir //==============================================================================
51cdf0e10cSrcweir class ProgressLogImpl : public ::dp_misc::MutexHolder, public t_log_helper
52cdf0e10cSrcweir {
53cdf0e10cSrcweir Reference<io::XOutputStream> m_xLogFile;
54cdf0e10cSrcweir sal_Int32 m_log_level;
55cdf0e10cSrcweir void log_write( OString const & text );
56cdf0e10cSrcweir
57cdf0e10cSrcweir protected:
58cdf0e10cSrcweir virtual void SAL_CALL disposing();
59cdf0e10cSrcweir virtual ~ProgressLogImpl();
60cdf0e10cSrcweir
61cdf0e10cSrcweir public:
62cdf0e10cSrcweir ProgressLogImpl( Sequence<Any> const & args,
63cdf0e10cSrcweir Reference<XComponentContext> const & xContext );
64cdf0e10cSrcweir
65cdf0e10cSrcweir // XProgressHandler
66cdf0e10cSrcweir virtual void SAL_CALL push( Any const & Status ) throw (RuntimeException);
67cdf0e10cSrcweir virtual void SAL_CALL update( Any const & Status ) throw (RuntimeException);
68cdf0e10cSrcweir virtual void SAL_CALL pop() throw (RuntimeException);
69cdf0e10cSrcweir };
70cdf0e10cSrcweir
71cdf0e10cSrcweir //______________________________________________________________________________
~ProgressLogImpl()72cdf0e10cSrcweir ProgressLogImpl::~ProgressLogImpl()
73cdf0e10cSrcweir {
74cdf0e10cSrcweir }
75cdf0e10cSrcweir
76cdf0e10cSrcweir //______________________________________________________________________________
disposing()77cdf0e10cSrcweir void ProgressLogImpl::disposing()
78cdf0e10cSrcweir {
79cdf0e10cSrcweir try {
80cdf0e10cSrcweir if (m_xLogFile.is()) {
81cdf0e10cSrcweir m_xLogFile->closeOutput();
82cdf0e10cSrcweir m_xLogFile.clear();
83cdf0e10cSrcweir }
84cdf0e10cSrcweir }
85cdf0e10cSrcweir catch (Exception & exc) {
86cdf0e10cSrcweir (void) exc;
87cdf0e10cSrcweir OSL_ENSURE( 0, OUStringToOString(
88cdf0e10cSrcweir exc.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
89cdf0e10cSrcweir }
90cdf0e10cSrcweir }
91cdf0e10cSrcweir
92cdf0e10cSrcweir //______________________________________________________________________________
ProgressLogImpl(Sequence<Any> const & args,Reference<XComponentContext> const & xContext)93cdf0e10cSrcweir ProgressLogImpl::ProgressLogImpl(
94cdf0e10cSrcweir Sequence<Any> const & args,
95cdf0e10cSrcweir Reference<XComponentContext> const & xContext )
96cdf0e10cSrcweir : t_log_helper( getMutex() ),
97cdf0e10cSrcweir m_log_level( 0 )
98cdf0e10cSrcweir {
99cdf0e10cSrcweir OUString log_file;
100cdf0e10cSrcweir boost::optional< Reference<task::XInteractionHandler> > interactionHandler;
101cdf0e10cSrcweir comphelper::unwrapArgs( args, log_file, interactionHandler );
102cdf0e10cSrcweir
103cdf0e10cSrcweir Reference<ucb::XSimpleFileAccess> xSimpleFileAccess(
104cdf0e10cSrcweir xContext->getServiceManager()->createInstanceWithContext(
105cdf0e10cSrcweir OUSTR("com.sun.star.ucb.SimpleFileAccess"),
106cdf0e10cSrcweir xContext ), UNO_QUERY_THROW );
107cdf0e10cSrcweir // optional ia handler:
108cdf0e10cSrcweir if (interactionHandler)
109cdf0e10cSrcweir xSimpleFileAccess->setInteractionHandler( *interactionHandler );
110cdf0e10cSrcweir
111cdf0e10cSrcweir m_xLogFile.set(
112cdf0e10cSrcweir xSimpleFileAccess->openFileWrite( log_file ), UNO_QUERY_THROW );
113cdf0e10cSrcweir Reference<io::XSeekable> xSeekable( m_xLogFile, UNO_QUERY_THROW );
114cdf0e10cSrcweir xSeekable->seek( xSeekable->getLength() );
115cdf0e10cSrcweir
116cdf0e10cSrcweir // write log stamp
117cdf0e10cSrcweir OStringBuffer buf;
118cdf0e10cSrcweir buf.append(
119cdf0e10cSrcweir RTL_CONSTASCII_STRINGPARAM("###### Progress log entry ") );
120cdf0e10cSrcweir TimeValue m_start_time, tLocal;
121cdf0e10cSrcweir oslDateTime date_time;
122cdf0e10cSrcweir if (osl_getSystemTime( &m_start_time ) &&
123cdf0e10cSrcweir osl_getLocalTimeFromSystemTime( &m_start_time, &tLocal ) &&
124cdf0e10cSrcweir osl_getDateTimeFromTimeValue( &tLocal, &date_time ))
125cdf0e10cSrcweir {
126cdf0e10cSrcweir char ar[ 128 ];
127cdf0e10cSrcweir snprintf(
128cdf0e10cSrcweir ar, sizeof (ar),
129cdf0e10cSrcweir "%04d-%02d-%02d %02d:%02d:%02d ",
130cdf0e10cSrcweir date_time.Year, date_time.Month, date_time.Day,
131cdf0e10cSrcweir date_time.Hours, date_time.Minutes, date_time.Seconds );
132cdf0e10cSrcweir buf.append( ar );
133cdf0e10cSrcweir }
134cdf0e10cSrcweir buf.append( RTL_CONSTASCII_STRINGPARAM("######\n") );
135cdf0e10cSrcweir log_write( buf.makeStringAndClear() );
136cdf0e10cSrcweir }
137cdf0e10cSrcweir
138cdf0e10cSrcweir //______________________________________________________________________________
log_write(OString const & text)139cdf0e10cSrcweir void ProgressLogImpl::log_write( OString const & text )
140cdf0e10cSrcweir {
141cdf0e10cSrcweir try {
142cdf0e10cSrcweir if (m_xLogFile.is()) {
143cdf0e10cSrcweir m_xLogFile->writeBytes(
144cdf0e10cSrcweir Sequence< sal_Int8 >(
145cdf0e10cSrcweir reinterpret_cast< sal_Int8 const * >(text.getStr()),
146cdf0e10cSrcweir text.getLength() ) );
147cdf0e10cSrcweir }
148cdf0e10cSrcweir }
149cdf0e10cSrcweir catch (io::IOException & exc) {
150cdf0e10cSrcweir (void) exc;
151cdf0e10cSrcweir OSL_ENSURE( 0, OUStringToOString(
152cdf0e10cSrcweir exc.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
153cdf0e10cSrcweir }
154cdf0e10cSrcweir }
155cdf0e10cSrcweir
156cdf0e10cSrcweir // XProgressHandler
157cdf0e10cSrcweir //______________________________________________________________________________
push(Any const & Status)158cdf0e10cSrcweir void ProgressLogImpl::push( Any const & Status )
159cdf0e10cSrcweir throw (RuntimeException)
160cdf0e10cSrcweir {
161cdf0e10cSrcweir update( Status );
162cdf0e10cSrcweir OSL_ASSERT( m_log_level >= 0 );
163cdf0e10cSrcweir ++m_log_level;
164cdf0e10cSrcweir }
165cdf0e10cSrcweir
166cdf0e10cSrcweir //______________________________________________________________________________
update(Any const & Status)167cdf0e10cSrcweir void ProgressLogImpl::update( Any const & Status )
168cdf0e10cSrcweir throw (RuntimeException)
169cdf0e10cSrcweir {
170cdf0e10cSrcweir if (! Status.hasValue())
171cdf0e10cSrcweir return;
172cdf0e10cSrcweir
173cdf0e10cSrcweir OUStringBuffer buf;
174cdf0e10cSrcweir OSL_ASSERT( m_log_level >= 0 );
175cdf0e10cSrcweir for ( sal_Int32 n = 0; n < m_log_level; ++n )
176cdf0e10cSrcweir buf.append( static_cast<sal_Unicode>(' ') );
177cdf0e10cSrcweir
178cdf0e10cSrcweir OUString msg;
179cdf0e10cSrcweir if (Status >>= msg) {
180cdf0e10cSrcweir buf.append( msg );
181cdf0e10cSrcweir }
182cdf0e10cSrcweir else {
183cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("ERROR: ") );
184cdf0e10cSrcweir buf.append( ::comphelper::anyToString(Status) );
185cdf0e10cSrcweir }
186cdf0e10cSrcweir buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\n") );
187cdf0e10cSrcweir log_write( OUStringToOString(
188cdf0e10cSrcweir buf.makeStringAndClear(), osl_getThreadTextEncoding() ) );
189cdf0e10cSrcweir }
190cdf0e10cSrcweir
191cdf0e10cSrcweir //______________________________________________________________________________
pop()192cdf0e10cSrcweir void ProgressLogImpl::pop() throw (RuntimeException)
193cdf0e10cSrcweir {
194cdf0e10cSrcweir OSL_ASSERT( m_log_level > 0 );
195cdf0e10cSrcweir --m_log_level;
196cdf0e10cSrcweir }
197cdf0e10cSrcweir
198cdf0e10cSrcweir namespace sdecl = comphelper::service_decl;
199cdf0e10cSrcweir sdecl::class_<ProgressLogImpl, sdecl::with_args<true> > servicePLI;
200cdf0e10cSrcweir extern sdecl::ServiceDecl const serviceDecl(
201cdf0e10cSrcweir servicePLI,
202cdf0e10cSrcweir // a private one:
203cdf0e10cSrcweir "com.sun.star.comp.deployment.ProgressLog",
204cdf0e10cSrcweir "com.sun.star.comp.deployment.ProgressLog" );
205cdf0e10cSrcweir
206cdf0e10cSrcweir } // namespace dp_log
207cdf0e10cSrcweir
208