LCOV - code coverage report
Current view: top level - libreoffice/desktop/source/deployment - dp_log.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 37 60 61.7 %
Date: 2012-12-17 Functions: 6 10 60.0 %
Legend: Lines: hit not hit

          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           4 : ProgressLogImpl::~ProgressLogImpl()
      67             : {
      68           4 : }
      69             : 
      70             : //______________________________________________________________________________
      71           4 : void ProgressLogImpl::disposing()
      72             : {
      73             :     try {
      74           4 :         if (m_xLogFile.is()) {
      75           4 :             m_xLogFile->closeOutput();
      76           4 :             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           4 : }
      85             : 
      86             : //______________________________________________________________________________
      87           4 : ProgressLogImpl::ProgressLogImpl(
      88             :     Sequence<Any> const & args,
      89             :     Reference<XComponentContext> const & xContext )
      90           4 :     : t_log_helper( getMutex() ),
      91           8 :       m_log_level( 0 )
      92             : {
      93           4 :     OUString log_file;
      94           4 :     boost::optional< Reference<task::XInteractionHandler> > interactionHandler;
      95           4 :     comphelper::unwrapArgs( args, log_file, interactionHandler );
      96             : 
      97           4 :     Reference<ucb::XSimpleFileAccess3> xSimpleFileAccess( ucb::SimpleFileAccess::create(xContext) );
      98             :     // optional ia handler:
      99           4 :     if (interactionHandler)
     100           0 :         xSimpleFileAccess->setInteractionHandler( *interactionHandler );
     101             : 
     102             :     m_xLogFile.set(
     103           4 :         xSimpleFileAccess->openFileWrite( log_file ), UNO_QUERY_THROW );
     104           4 :     Reference<io::XSeekable> xSeekable( m_xLogFile, UNO_QUERY_THROW );
     105           4 :     xSeekable->seek( xSeekable->getLength() );
     106             : 
     107             :     // write log stamp
     108           4 :     OStringBuffer buf;
     109             :     buf.append(
     110           4 :         RTL_CONSTASCII_STRINGPARAM("###### Progress log entry ") );
     111             :     TimeValue m_start_time, tLocal;
     112             :     oslDateTime date_time;
     113          12 :     if (osl_getSystemTime( &m_start_time ) &&
     114           4 :         osl_getLocalTimeFromSystemTime( &m_start_time, &tLocal ) &&
     115           4 :         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           4 :             date_time.Hours, date_time.Minutes, date_time.Seconds );
     123           4 :         buf.append( ar );
     124             :     }
     125           4 :     buf.append( RTL_CONSTASCII_STRINGPARAM("######\n") );
     126           4 :     log_write( buf.makeStringAndClear() );
     127           4 : }
     128             : 
     129             : //______________________________________________________________________________
     130           4 : void ProgressLogImpl::log_write( OString const & text )
     131             : {
     132             :     try {
     133           4 :         if (m_xLogFile.is()) {
     134           4 :             m_xLogFile->writeBytes(
     135             :                 Sequence< sal_Int8 >(
     136           4 :                     reinterpret_cast< sal_Int8 const * >(text.getStr()),
     137           8 :                     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           4 : }
     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           4 : sdecl::class_<ProgressLogImpl, sdecl::with_args<true> > servicePLI;
     191           4 : 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          12 : } // namespace dp_log
     198             : 
     199             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10