LCOV - code coverage report
Current view: top level - desktop/source/deployment - dp_log.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 49 60 81.7 %
Date: 2014-04-11 Functions: 7 10 70.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() 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          88 : ProgressLogImpl::~ProgressLogImpl()
      67             : {
      68          88 : }
      69             : 
      70             : 
      71          88 : void ProgressLogImpl::disposing()
      72             : {
      73             :     try {
      74          88 :         if (m_xLogFile.is()) {
      75          88 :             m_xLogFile->closeOutput();
      76          88 :             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          88 : }
      85             : 
      86             : 
      87          89 : ProgressLogImpl::ProgressLogImpl(
      88             :     Sequence<Any> const & args,
      89             :     Reference<XComponentContext> const & xContext )
      90          89 :     : t_log_helper( getMutex() ),
      91         179 :       m_log_level( 0 )
      92             : {
      93          89 :     OUString log_file;
      94         178 :     boost::optional< Reference<task::XInteractionHandler> > interactionHandler;
      95          89 :     comphelper::unwrapArgs( args, log_file, interactionHandler );
      96             : 
      97         178 :     Reference<ucb::XSimpleFileAccess3> xSimpleFileAccess( ucb::SimpleFileAccess::create(xContext) );
      98             :     // optional ia handler:
      99          89 :     if (interactionHandler)
     100           0 :         xSimpleFileAccess->setInteractionHandler( *interactionHandler );
     101             : 
     102             :     m_xLogFile.set(
     103          89 :         xSimpleFileAccess->openFileWrite( log_file ), UNO_QUERY_THROW );
     104         176 :     Reference<io::XSeekable> xSeekable( m_xLogFile, UNO_QUERY_THROW );
     105          88 :     xSeekable->seek( xSeekable->getLength() );
     106             : 
     107             :     // write log stamp
     108         176 :     OStringBuffer buf;
     109          88 :     buf.append( "###### Progress log entry " );
     110             :     TimeValue m_start_time, tLocal;
     111             :     oslDateTime date_time;
     112         264 :     if (osl_getSystemTime( &m_start_time ) &&
     113         176 :         osl_getLocalTimeFromSystemTime( &m_start_time, &tLocal ) &&
     114          88 :         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          88 :             date_time.Hours, date_time.Minutes, date_time.Seconds );
     122          88 :         buf.append( ar );
     123             :     }
     124          88 :     buf.append( "######\n" );
     125         177 :     log_write( buf.makeStringAndClear() );
     126          88 : }
     127             : 
     128             : 
     129          96 : void ProgressLogImpl::log_write( OString const & text )
     130             : {
     131             :     try {
     132          96 :         if (m_xLogFile.is()) {
     133          96 :             m_xLogFile->writeBytes(
     134             :                 Sequence< sal_Int8 >(
     135          96 :                     reinterpret_cast< sal_Int8 const * >(text.getStr()),
     136         192 :                     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          96 : }
     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           8 : void ProgressLogImpl::update( Any const & Status )
     158             :     throw (RuntimeException, std::exception)
     159             : {
     160           8 :     if (! Status.hasValue())
     161           8 :         return;
     162             : 
     163           8 :     OUStringBuffer buf;
     164             :     OSL_ASSERT( m_log_level >= 0 );
     165           8 :     for ( sal_Int32 n = 0; n < m_log_level; ++n )
     166           0 :         buf.append( ' ' );
     167             : 
     168          16 :     OUString msg;
     169           8 :     if (Status >>= msg) {
     170           1 :         buf.append( msg );
     171             :     }
     172             :     else {
     173           7 :         buf.appendAscii( "ERROR: " );
     174           7 :         buf.append( ::comphelper::anyToString(Status) );
     175             :     }
     176           8 :     buf.appendAscii( "\n" );
     177             :     log_write( OUStringToOString(
     178          16 :                    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          67 : sdecl::class_<ProgressLogImpl, sdecl::with_args<true> > servicePLI;
     190          67 : 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         201 : } // namespace dp_log
     197             : 
     198             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10