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();
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);
61 : virtual void SAL_CALL update( Any const & Status ) throw (RuntimeException);
62 : virtual void SAL_CALL pop() throw (RuntimeException);
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 : buf.append(
110 0 : RTL_CONSTASCII_STRINGPARAM("###### Progress log entry ") );
111 : TimeValue m_start_time, tLocal;
112 : oslDateTime date_time;
113 0 : if (osl_getSystemTime( &m_start_time ) &&
114 0 : osl_getLocalTimeFromSystemTime( &m_start_time, &tLocal ) &&
115 0 : osl_getDateTimeFromTimeValue( &tLocal, &date_time ))
116 : {
117 : char ar[ 128 ];
118 : snprintf(
119 : ar, sizeof (ar),
120 : "%04d-%02d-%02d %02d:%02d:%02d ",
121 : date_time.Year, date_time.Month, date_time.Day,
122 0 : date_time.Hours, date_time.Minutes, date_time.Seconds );
123 0 : buf.append( ar );
124 : }
125 0 : buf.append( RTL_CONSTASCII_STRINGPARAM("######\n") );
126 0 : log_write( buf.makeStringAndClear() );
127 0 : }
128 :
129 : //______________________________________________________________________________
130 0 : void ProgressLogImpl::log_write( OString const & text )
131 : {
132 : try {
133 0 : if (m_xLogFile.is()) {
134 0 : m_xLogFile->writeBytes(
135 : Sequence< sal_Int8 >(
136 0 : reinterpret_cast< sal_Int8 const * >(text.getStr()),
137 0 : text.getLength() ) );
138 : }
139 : }
140 0 : catch (const io::IOException & exc) {
141 : (void) exc;
142 : OSL_FAIL( OUStringToOString(
143 : exc.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
144 : }
145 0 : }
146 :
147 : // XProgressHandler
148 : //______________________________________________________________________________
149 0 : void ProgressLogImpl::push( Any const & Status )
150 : throw (RuntimeException)
151 : {
152 0 : update( Status );
153 : OSL_ASSERT( m_log_level >= 0 );
154 0 : ++m_log_level;
155 0 : }
156 :
157 : //______________________________________________________________________________
158 0 : void ProgressLogImpl::update( Any const & Status )
159 : throw (RuntimeException)
160 : {
161 0 : if (! Status.hasValue())
162 0 : return;
163 :
164 0 : OUStringBuffer buf;
165 : OSL_ASSERT( m_log_level >= 0 );
166 0 : for ( sal_Int32 n = 0; n < m_log_level; ++n )
167 0 : buf.append( static_cast<sal_Unicode>(' ') );
168 :
169 0 : OUString msg;
170 0 : if (Status >>= msg) {
171 0 : buf.append( msg );
172 : }
173 : else {
174 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("ERROR: ") );
175 0 : buf.append( ::comphelper::anyToString(Status) );
176 : }
177 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\n") );
178 : log_write( OUStringToOString(
179 0 : buf.makeStringAndClear(), osl_getThreadTextEncoding() ) );
180 : }
181 :
182 : //______________________________________________________________________________
183 0 : void ProgressLogImpl::pop() throw (RuntimeException)
184 : {
185 : OSL_ASSERT( m_log_level > 0 );
186 0 : --m_log_level;
187 0 : }
188 :
189 : namespace sdecl = comphelper::service_decl;
190 1 : sdecl::class_<ProgressLogImpl, sdecl::with_args<true> > servicePLI;
191 1 : extern sdecl::ServiceDecl const serviceDecl(
192 : servicePLI,
193 : // a private one:
194 : "com.sun.star.comp.deployment.ProgressLog",
195 : "com.sun.star.comp.deployment.ProgressLog" );
196 :
197 3 : } // namespace dp_log
198 :
199 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|