LCOV - code coverage report
Current view: top level - desktop/source/deployment - dp_log.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 49 60 81.7 %
Date: 2012-08-25 Functions: 7 10 70.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 50 110 45.5 %

           Branch data     Line data    Source code
       1                 :            : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2                 :            : /*************************************************************************
       3                 :            :  *
       4                 :            :  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       5                 :            :  *
       6                 :            :  * Copyright 2000, 2010 Oracle and/or its affiliates.
       7                 :            :  *
       8                 :            :  * OpenOffice.org - a multi-platform office productivity suite
       9                 :            :  *
      10                 :            :  * This file is part of OpenOffice.org.
      11                 :            :  *
      12                 :            :  * OpenOffice.org is free software: you can redistribute it and/or modify
      13                 :            :  * it under the terms of the GNU Lesser General Public License version 3
      14                 :            :  * only, as published by the Free Software Foundation.
      15                 :            :  *
      16                 :            :  * OpenOffice.org is distributed in the hope that it will be useful,
      17                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      18                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      19                 :            :  * GNU Lesser General Public License version 3 for more details
      20                 :            :  * (a copy is included in the LICENSE file that accompanied this code).
      21                 :            :  *
      22                 :            :  * You should have received a copy of the GNU Lesser General Public License
      23                 :            :  * version 3 along with OpenOffice.org.  If not, see
      24                 :            :  * <http://www.openoffice.org/license.html>
      25                 :            :  * for a copy of the LGPLv3 License.
      26                 :            :  *
      27                 :            :  ************************************************************************/
      28                 :            : 
      29                 :            : 
      30                 :            : #include "dp_misc.h"
      31                 :            : #include "rtl/strbuf.hxx"
      32                 :            : #include "osl/time.h"
      33                 :            : #include "osl/thread.h"
      34                 :            : #include "cppuhelper/compbase1.hxx"
      35                 :            : #include "comphelper/anytostring.hxx"
      36                 :            : #include "comphelper/servicedecl.hxx"
      37                 :            : #include "comphelper/unwrapargs.hxx"
      38                 :            : #include "com/sun/star/deployment/DeploymentException.hpp"
      39                 :            : #include "com/sun/star/ucb/XProgressHandler.hpp"
      40                 :            : #include "com/sun/star/ucb/SimpleFileAccess.hpp"
      41                 :            : #include "com/sun/star/ucb/XSimpleFileAccess2.hpp"
      42                 :            : #include "com/sun/star/io/XSeekable.hpp"
      43                 :            : #include <stdio.h>
      44                 :            : 
      45                 :            : 
      46                 :            : using namespace ::rtl;
      47                 :            : using namespace ::com::sun::star;
      48                 :            : using namespace ::com::sun::star::uno;
      49                 :            : 
      50                 :            : namespace dp_log {
      51                 :            : 
      52                 :            : typedef ::cppu::WeakComponentImplHelper1<ucb::XProgressHandler> t_log_helper;
      53                 :            : 
      54                 :            : //==============================================================================
      55                 :            : class ProgressLogImpl : public ::dp_misc::MutexHolder, public t_log_helper
      56                 :            : {
      57                 :            :     Reference<io::XOutputStream> m_xLogFile;
      58                 :            :     sal_Int32 m_log_level;
      59                 :            :     void log_write( OString const & text );
      60                 :            : 
      61                 :            : protected:
      62                 :            :     virtual void SAL_CALL disposing();
      63                 :            :     virtual ~ProgressLogImpl();
      64                 :            : 
      65                 :            : public:
      66                 :            :     ProgressLogImpl( Sequence<Any> const & args,
      67                 :            :                      Reference<XComponentContext> const & xContext );
      68                 :            : 
      69                 :            :     // XProgressHandler
      70                 :            :     virtual void SAL_CALL push( Any const & Status ) throw (RuntimeException);
      71                 :            :     virtual void SAL_CALL update( Any const & Status ) throw (RuntimeException);
      72                 :            :     virtual void SAL_CALL pop() throw (RuntimeException);
      73                 :            : };
      74                 :            : 
      75                 :            : //______________________________________________________________________________
      76         [ +  - ]:        246 : ProgressLogImpl::~ProgressLogImpl()
      77                 :            : {
      78         [ -  + ]:        246 : }
      79                 :            : 
      80                 :            : //______________________________________________________________________________
      81                 :        246 : void ProgressLogImpl::disposing()
      82                 :            : {
      83                 :            :     try {
      84         [ +  - ]:        246 :         if (m_xLogFile.is()) {
      85 [ +  - ][ +  - ]:        246 :             m_xLogFile->closeOutput();
      86                 :        246 :             m_xLogFile.clear();
      87                 :            :         }
      88                 :            :     }
      89                 :          0 :     catch (const Exception & exc) {
      90                 :            :         (void) exc;
      91                 :            :         OSL_FAIL( OUStringToOString(
      92                 :            :                         exc.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
      93                 :            :     }
      94         [ #  # ]:        246 : }
      95                 :            : 
      96                 :            : //______________________________________________________________________________
      97                 :        246 : ProgressLogImpl::ProgressLogImpl(
      98                 :            :     Sequence<Any> const & args,
      99                 :            :     Reference<XComponentContext> const & xContext )
     100                 :        246 :     : t_log_helper( getMutex() ),
     101                 :        492 :       m_log_level( 0 )
     102                 :            : {
     103                 :        246 :     OUString log_file;
     104         [ +  - ]:        246 :     boost::optional< Reference<task::XInteractionHandler> > interactionHandler;
     105         [ +  - ]:        246 :     comphelper::unwrapArgs( args, log_file, interactionHandler );
     106                 :            : 
     107         [ +  - ]:        246 :     Reference<ucb::XSimpleFileAccess2> xSimpleFileAccess( ucb::SimpleFileAccess::create(xContext) );
     108                 :            :     // optional ia handler:
     109 [ +  - ][ -  + ]:        246 :     if (interactionHandler)
     110 [ #  # ][ #  # ]:          0 :         xSimpleFileAccess->setInteractionHandler( *interactionHandler );
                 [ #  # ]
     111                 :            : 
     112                 :            :     m_xLogFile.set(
     113 [ +  - ][ +  - ]:        246 :         xSimpleFileAccess->openFileWrite( log_file ), UNO_QUERY_THROW );
                 [ +  - ]
     114         [ +  - ]:        246 :     Reference<io::XSeekable> xSeekable( m_xLogFile, UNO_QUERY_THROW );
     115 [ +  - ][ +  - ]:        246 :     xSeekable->seek( xSeekable->getLength() );
         [ +  - ][ +  - ]
     116                 :            : 
     117                 :            :     // write log stamp
     118                 :        246 :     OStringBuffer buf;
     119                 :            :     buf.append(
     120         [ +  - ]:        246 :         RTL_CONSTASCII_STRINGPARAM("###### Progress log entry ") );
     121                 :            :     TimeValue m_start_time, tLocal;
     122                 :            :     oslDateTime date_time;
     123 [ +  - ][ +  - ]:        738 :     if (osl_getSystemTime( &m_start_time ) &&
         [ +  - ][ +  - ]
                 [ +  - ]
     124         [ +  - ]:        246 :         osl_getLocalTimeFromSystemTime( &m_start_time, &tLocal ) &&
     125         [ +  - ]:        246 :         osl_getDateTimeFromTimeValue( &tLocal, &date_time ))
     126                 :            :     {
     127                 :            :         char ar[ 128 ];
     128                 :            :         snprintf(
     129                 :            :             ar, sizeof (ar),
     130                 :            :             "%04d-%02d-%02d %02d:%02d:%02d ",
     131                 :            :             date_time.Year, date_time.Month, date_time.Day,
     132                 :        246 :             date_time.Hours, date_time.Minutes, date_time.Seconds );
     133         [ +  - ]:        246 :         buf.append( ar );
     134                 :            :     }
     135         [ +  - ]:        246 :     buf.append( RTL_CONSTASCII_STRINGPARAM("######\n") );
     136 [ +  - ][ +  - ]:        246 :     log_write( buf.makeStringAndClear() );
     137                 :        246 : }
     138                 :            : 
     139                 :            : //______________________________________________________________________________
     140                 :        262 : void ProgressLogImpl::log_write( OString const & text )
     141                 :            : {
     142                 :            :     try {
     143         [ +  - ]:        262 :         if (m_xLogFile.is()) {
     144         [ +  - ]:        262 :             m_xLogFile->writeBytes(
     145                 :            :                 Sequence< sal_Int8 >(
     146                 :        262 :                     reinterpret_cast< sal_Int8 const * >(text.getStr()),
     147 [ +  - ][ +  - ]:        524 :                     text.getLength() ) );
         [ +  - ][ #  # ]
     148                 :            :         }
     149                 :            :     }
     150                 :          0 :     catch (const io::IOException & exc) {
     151                 :            :         (void) exc;
     152                 :            :         OSL_FAIL( OUStringToOString(
     153                 :            :                         exc.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
     154                 :            :     }
     155                 :        262 : }
     156                 :            : 
     157                 :            : // XProgressHandler
     158                 :            : //______________________________________________________________________________
     159                 :          0 : void ProgressLogImpl::push( Any const & Status )
     160                 :            :     throw (RuntimeException)
     161                 :            : {
     162                 :          0 :     update( Status );
     163                 :            :     OSL_ASSERT( m_log_level >= 0 );
     164                 :          0 :     ++m_log_level;
     165                 :          0 : }
     166                 :            : 
     167                 :            : //______________________________________________________________________________
     168                 :         16 : void ProgressLogImpl::update( Any const & Status )
     169                 :            :     throw (RuntimeException)
     170                 :            : {
     171         [ +  - ]:         16 :     if (! Status.hasValue())
     172                 :         16 :         return;
     173                 :            : 
     174                 :         16 :     OUStringBuffer buf;
     175                 :            :     OSL_ASSERT( m_log_level >= 0 );
     176         [ -  + ]:         16 :     for ( sal_Int32 n = 0; n < m_log_level; ++n )
     177         [ #  # ]:          0 :         buf.append( static_cast<sal_Unicode>(' ') );
     178                 :            : 
     179                 :         16 :     OUString msg;
     180         [ +  + ]:         16 :     if (Status >>= msg) {
     181         [ +  - ]:          2 :         buf.append( msg );
     182                 :            :     }
     183                 :            :     else {
     184         [ +  - ]:         14 :         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("ERROR: ") );
     185 [ +  - ][ +  - ]:         14 :         buf.append( ::comphelper::anyToString(Status) );
     186                 :            :     }
     187         [ +  - ]:         16 :     buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\n") );
     188                 :            :     log_write( OUStringToOString(
     189 [ +  - ][ +  - ]:         16 :                    buf.makeStringAndClear(), osl_getThreadTextEncoding() ) );
         [ +  - ][ +  - ]
     190                 :            : }
     191                 :            : 
     192                 :            : //______________________________________________________________________________
     193                 :          0 : void ProgressLogImpl::pop() throw (RuntimeException)
     194                 :            : {
     195                 :            :     OSL_ASSERT( m_log_level > 0 );
     196                 :          0 :     --m_log_level;
     197                 :          0 : }
     198                 :            : 
     199                 :            : namespace sdecl = comphelper::service_decl;
     200                 :        124 : sdecl::class_<ProgressLogImpl, sdecl::with_args<true> > servicePLI;
     201                 :        124 : extern sdecl::ServiceDecl const serviceDecl(
     202                 :            :     servicePLI,
     203                 :            :     // a private one:
     204                 :            :     "com.sun.star.comp.deployment.ProgressLog",
     205                 :            :     "com.sun.star.comp.deployment.ProgressLog" );
     206                 :            : 
     207 [ +  - ][ +  - ]:        372 : } // namespace dp_log
     208                 :            : 
     209                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10