Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
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>
34 :
35 :
36 : using namespace ::rtl;
37 : using namespace ::com::sun::star;
38 : using namespace ::com::sun::star::uno;
39 :
40 : namespace dp_log {
41 :
42 : typedef ::cppu::WeakComponentImplHelper1<ucb::XProgressHandler> t_log_helper;
43 :
44 :
45 : class ProgressLogImpl : public ::dp_misc::MutexHolder, public t_log_helper
46 : {
47 : Reference<io::XOutputStream> m_xLogFile;
48 : sal_Int32 m_log_level;
49 : void log_write( OString const & text );
50 :
51 : protected:
52 : virtual void SAL_CALL disposing() SAL_OVERRIDE;
53 : virtual ~ProgressLogImpl();
54 :
55 : public:
56 : ProgressLogImpl( Sequence<Any> const & args,
57 : Reference<XComponentContext> const & xContext );
58 :
59 : // XProgressHandler
60 : virtual void SAL_CALL push( Any const & Status ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
61 : virtual void SAL_CALL update( Any const & Status ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
62 : virtual void SAL_CALL pop() throw (RuntimeException, std::exception) SAL_OVERRIDE;
63 : };
64 :
65 :
66 0 : ProgressLogImpl::~ProgressLogImpl()
67 : {
68 0 : }
69 :
70 :
71 0 : void ProgressLogImpl::disposing()
72 : {
73 : try {
74 0 : if (m_xLogFile.is()) {
75 0 : m_xLogFile->closeOutput();
76 0 : m_xLogFile.clear();
77 : }
78 : }
79 0 : catch (const Exception & exc) {
80 : (void) exc;
81 : OSL_FAIL( OUStringToOString(
82 : exc.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
83 : }
84 0 : }
85 :
86 :
87 0 : ProgressLogImpl::ProgressLogImpl(
88 : Sequence<Any> const & args,
89 : Reference<XComponentContext> const & xContext )
90 0 : : t_log_helper( getMutex() ),
91 0 : m_log_level( 0 )
92 : {
93 0 : OUString log_file;
94 0 : boost::optional< Reference<task::XInteractionHandler> > interactionHandler;
95 0 : comphelper::unwrapArgs( args, log_file, interactionHandler );
96 :
97 0 : Reference<ucb::XSimpleFileAccess3> xSimpleFileAccess( ucb::SimpleFileAccess::create(xContext) );
98 : // optional ia handler:
99 0 : if (interactionHandler)
100 0 : xSimpleFileAccess->setInteractionHandler( *interactionHandler );
101 :
102 : m_xLogFile.set(
103 0 : xSimpleFileAccess->openFileWrite( log_file ), UNO_QUERY_THROW );
104 0 : Reference<io::XSeekable> xSeekable( m_xLogFile, UNO_QUERY_THROW );
105 0 : xSeekable->seek( xSeekable->getLength() );
106 :
107 : // write log stamp
108 0 : OStringBuffer buf;
109 0 : buf.append( "###### Progress log entry " );
110 : TimeValue m_start_time, tLocal;
111 : oslDateTime date_time;
112 0 : if (osl_getSystemTime( &m_start_time ) &&
113 0 : osl_getLocalTimeFromSystemTime( &m_start_time, &tLocal ) &&
114 0 : osl_getDateTimeFromTimeValue( &tLocal, &date_time ))
115 : {
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 0 : date_time.Hours, date_time.Minutes, date_time.Seconds );
122 0 : buf.append( ar );
123 : }
124 0 : buf.append( "######\n" );
125 0 : log_write( buf.makeStringAndClear() );
126 0 : }
127 :
128 :
129 0 : void ProgressLogImpl::log_write( OString const & text )
130 : {
131 : try {
132 0 : if (m_xLogFile.is()) {
133 0 : m_xLogFile->writeBytes(
134 : Sequence< sal_Int8 >(
135 0 : reinterpret_cast< sal_Int8 const * >(text.getStr()),
136 0 : text.getLength() ) );
137 : }
138 : }
139 0 : catch (const io::IOException & exc) {
140 : (void) exc;
141 : OSL_FAIL( OUStringToOString(
142 : exc.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
143 : }
144 0 : }
145 :
146 : // XProgressHandler
147 :
148 0 : void ProgressLogImpl::push( Any const & Status )
149 : throw (RuntimeException, std::exception)
150 : {
151 0 : update( Status );
152 : OSL_ASSERT( m_log_level >= 0 );
153 0 : ++m_log_level;
154 0 : }
155 :
156 :
157 0 : void ProgressLogImpl::update( Any const & Status )
158 : throw (RuntimeException, std::exception)
159 : {
160 0 : if (! Status.hasValue())
161 0 : return;
162 :
163 0 : OUStringBuffer buf;
164 : OSL_ASSERT( m_log_level >= 0 );
165 0 : for ( sal_Int32 n = 0; n < m_log_level; ++n )
166 0 : buf.append( ' ' );
167 :
168 0 : OUString msg;
169 0 : if (Status >>= msg) {
170 0 : buf.append( msg );
171 : }
172 : else {
173 0 : buf.appendAscii( "ERROR: " );
174 0 : buf.append( ::comphelper::anyToString(Status) );
175 : }
176 0 : buf.appendAscii( "\n" );
177 : log_write( OUStringToOString(
178 0 : buf.makeStringAndClear(), osl_getThreadTextEncoding() ) );
179 : }
180 :
181 :
182 0 : void ProgressLogImpl::pop() throw (RuntimeException, std::exception)
183 : {
184 : OSL_ASSERT( m_log_level > 0 );
185 0 : --m_log_level;
186 0 : }
187 :
188 : namespace sdecl = comphelper::service_decl;
189 0 : sdecl::class_<ProgressLogImpl, sdecl::with_args<true> > servicePLI;
190 0 : 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" );
195 :
196 0 : } // namespace dp_log
197 :
198 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|