bump product version to 4.1.6.2
[LibreOffice.git] / desktop / source / deployment / dp_log.cxx
blob461f55f81b09382111b915c4c419bfc65351c32e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "dp_misc.h"
22 #include "rtl/strbuf.hxx"
23 #include "osl/time.h"
24 #include "osl/thread.h"
25 #include "cppuhelper/compbase1.hxx"
26 #include "comphelper/anytostring.hxx"
27 #include "comphelper/servicedecl.hxx"
28 #include "comphelper/unwrapargs.hxx"
29 #include "com/sun/star/deployment/DeploymentException.hpp"
30 #include "com/sun/star/ucb/XProgressHandler.hpp"
31 #include "com/sun/star/ucb/SimpleFileAccess.hpp"
32 #include "com/sun/star/io/XSeekable.hpp"
33 #include <stdio.h>
36 using namespace ::rtl;
37 using namespace ::com::sun::star;
38 using namespace ::com::sun::star::uno;
40 namespace dp_log {
42 typedef ::cppu::WeakComponentImplHelper1<ucb::XProgressHandler> t_log_helper;
44 //==============================================================================
45 class ProgressLogImpl : public ::dp_misc::MutexHolder, public t_log_helper
47 Reference<io::XOutputStream> m_xLogFile;
48 sal_Int32 m_log_level;
49 void log_write( OString const & text );
51 protected:
52 virtual void SAL_CALL disposing();
53 virtual ~ProgressLogImpl();
55 public:
56 ProgressLogImpl( Sequence<Any> const & args,
57 Reference<XComponentContext> const & xContext );
59 // XProgressHandler
60 virtual void SAL_CALL push( Any const & Status ) throw (RuntimeException);
61 virtual void SAL_CALL update( Any const & Status ) throw (RuntimeException);
62 virtual void SAL_CALL pop() throw (RuntimeException);
65 //______________________________________________________________________________
66 ProgressLogImpl::~ProgressLogImpl()
70 //______________________________________________________________________________
71 void ProgressLogImpl::disposing()
73 try {
74 if (m_xLogFile.is()) {
75 m_xLogFile->closeOutput();
76 m_xLogFile.clear();
79 catch (const Exception & exc) {
80 (void) exc;
81 OSL_FAIL( OUStringToOString(
82 exc.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
86 //______________________________________________________________________________
87 ProgressLogImpl::ProgressLogImpl(
88 Sequence<Any> const & args,
89 Reference<XComponentContext> const & xContext )
90 : t_log_helper( getMutex() ),
91 m_log_level( 0 )
93 OUString log_file;
94 boost::optional< Reference<task::XInteractionHandler> > interactionHandler;
95 comphelper::unwrapArgs( args, log_file, interactionHandler );
97 Reference<ucb::XSimpleFileAccess3> xSimpleFileAccess( ucb::SimpleFileAccess::create(xContext) );
98 // optional ia handler:
99 if (interactionHandler)
100 xSimpleFileAccess->setInteractionHandler( *interactionHandler );
102 m_xLogFile.set(
103 xSimpleFileAccess->openFileWrite( log_file ), UNO_QUERY_THROW );
104 Reference<io::XSeekable> xSeekable( m_xLogFile, UNO_QUERY_THROW );
105 xSeekable->seek( xSeekable->getLength() );
107 // write log stamp
108 OStringBuffer buf;
109 buf.append( "###### Progress log entry " );
110 TimeValue m_start_time, tLocal;
111 oslDateTime date_time;
112 if (osl_getSystemTime( &m_start_time ) &&
113 osl_getLocalTimeFromSystemTime( &m_start_time, &tLocal ) &&
114 osl_getDateTimeFromTimeValue( &tLocal, &date_time ))
116 char ar[ 128 ];
117 snprintf(
118 ar, sizeof (ar),
119 "%04d-%02d-%02d %02d:%02d:%02d ",
120 date_time.Year, date_time.Month, date_time.Day,
121 date_time.Hours, date_time.Minutes, date_time.Seconds );
122 buf.append( ar );
124 buf.append( "######\n" );
125 log_write( buf.makeStringAndClear() );
128 //______________________________________________________________________________
129 void ProgressLogImpl::log_write( OString const & text )
131 try {
132 if (m_xLogFile.is()) {
133 m_xLogFile->writeBytes(
134 Sequence< sal_Int8 >(
135 reinterpret_cast< sal_Int8 const * >(text.getStr()),
136 text.getLength() ) );
139 catch (const io::IOException & exc) {
140 (void) exc;
141 OSL_FAIL( OUStringToOString(
142 exc.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
146 // XProgressHandler
147 //______________________________________________________________________________
148 void ProgressLogImpl::push( Any const & Status )
149 throw (RuntimeException)
151 update( Status );
152 OSL_ASSERT( m_log_level >= 0 );
153 ++m_log_level;
156 //______________________________________________________________________________
157 void ProgressLogImpl::update( Any const & Status )
158 throw (RuntimeException)
160 if (! Status.hasValue())
161 return;
163 OUStringBuffer buf;
164 OSL_ASSERT( m_log_level >= 0 );
165 for ( sal_Int32 n = 0; n < m_log_level; ++n )
166 buf.append( static_cast<sal_Unicode>(' ') );
168 OUString msg;
169 if (Status >>= msg) {
170 buf.append( msg );
172 else {
173 buf.appendAscii( "ERROR: " );
174 buf.append( ::comphelper::anyToString(Status) );
176 buf.appendAscii( "\n" );
177 log_write( OUStringToOString(
178 buf.makeStringAndClear(), osl_getThreadTextEncoding() ) );
181 //______________________________________________________________________________
182 void ProgressLogImpl::pop() throw (RuntimeException)
184 OSL_ASSERT( m_log_level > 0 );
185 --m_log_level;
188 namespace sdecl = comphelper::service_decl;
189 sdecl::class_<ProgressLogImpl, sdecl::with_args<true> > servicePLI;
190 extern sdecl::ServiceDecl const serviceDecl(
191 servicePLI,
192 // a private one:
193 "com.sun.star.comp.deployment.ProgressLog",
194 "com.sun.star.comp.deployment.ProgressLog" );
196 } // namespace dp_log
198 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */