LCOV - code coverage report
Current view: top level - extensions/source/logging - loghandler.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 17 59 28.8 %
Date: 2015-06-13 12:38:46 Functions: 3 8 37.5 %
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 "loghandler.hxx"
      22             : 
      23             : #include <com/sun/star/logging/LogLevel.hpp>
      24             : #include <com/sun/star/lang/IllegalArgumentException.hpp>
      25             : #include <com/sun/star/lang/DisposedException.hpp>
      26             : #include <com/sun/star/logging/PlainTextFormatter.hpp>
      27             : 
      28             : #include <tools/diagnose_ex.h>
      29             : #include <rtl/tencinfo.h>
      30             : 
      31             : 
      32             : namespace logging
      33             : {
      34             : 
      35             : 
      36             :     using ::com::sun::star::uno::Reference;
      37             :     using ::com::sun::star::uno::XComponentContext;
      38             :     using ::com::sun::star::uno::Any;
      39             :     using ::com::sun::star::logging::LogRecord;
      40             :     using ::com::sun::star::uno::UNO_QUERY_THROW;
      41             :     using ::com::sun::star::logging::XLogFormatter;
      42             :     using ::com::sun::star::uno::Exception;
      43             :     using ::com::sun::star::lang::IllegalArgumentException;
      44             :     using ::com::sun::star::lang::DisposedException;
      45             :     using ::com::sun::star::logging::PlainTextFormatter;
      46             : 
      47             :     namespace LogLevel = ::com::sun::star::logging::LogLevel;
      48             : 
      49          17 :     LogHandlerHelper::LogHandlerHelper( const Reference< XComponentContext >& _rxContext, ::osl::Mutex& _rMutex, ::cppu::OBroadcastHelper& _rBHelper )
      50             :         :m_eEncoding( RTL_TEXTENCODING_UTF8 )
      51             :         ,m_nLevel( LogLevel::SEVERE )
      52             :         ,m_xFormatter( NULL )
      53             :         ,m_xContext( _rxContext )
      54             :         ,m_rMutex( _rMutex )
      55             :         ,m_rBHelper( _rBHelper )
      56          17 :         ,m_bInitialized( false )
      57             :     {
      58          17 :     }
      59             : 
      60             : 
      61          16 :     void LogHandlerHelper::initFromSettings( const ::comphelper::NamedValueCollection& _rSettings )
      62             :     {
      63          16 :         OUString sEncoding;
      64          16 :         if ( _rSettings.get_ensureType( "Encoding", sEncoding ) )
      65             :         {
      66           0 :             if ( !setEncoding( sEncoding ) )
      67           0 :                 throw IllegalArgumentException();
      68             :         }
      69             : 
      70          16 :         _rSettings.get_ensureType( "Formatter", m_xFormatter );
      71          16 :         _rSettings.get_ensureType( "Level", m_nLevel );
      72          16 :     }
      73             : 
      74             : 
      75          32 :     void LogHandlerHelper::enterMethod()
      76             :     {
      77          32 :         m_rMutex.acquire();
      78             : 
      79          32 :         if ( !getIsInitialized() )
      80           0 :             throw DisposedException("component not initialized" );
      81             : 
      82          32 :         if ( m_rBHelper.bDisposed )
      83           0 :             throw DisposedException("component already disposed" );
      84             : 
      85             :         // fallback settings, in case they weren't passed at construction time
      86          32 :         if ( !getFormatter().is() )
      87             :         {
      88             :             try
      89             :             {
      90          16 :                 Reference< XLogFormatter > xFormatter( PlainTextFormatter::create( m_xContext ), UNO_QUERY_THROW );
      91          16 :                 setFormatter( xFormatter );
      92             :             }
      93           0 :             catch( const Exception& )
      94             :             {
      95             :                 DBG_UNHANDLED_EXCEPTION();
      96             :             }
      97             :         }
      98          32 :     }
      99             : 
     100             : 
     101           0 :     bool LogHandlerHelper::getEncoding( OUString& _out_rEncoding ) const
     102             :     {
     103           0 :         const char* pMimeCharset = rtl_getMimeCharsetFromTextEncoding( m_eEncoding );
     104           0 :         if ( pMimeCharset )
     105             :         {
     106           0 :             _out_rEncoding = OUString::createFromAscii( pMimeCharset );
     107           0 :             return true;
     108             :         }
     109           0 :         _out_rEncoding.clear();
     110           0 :         return false;
     111             :     }
     112             : 
     113             : 
     114           0 :     bool LogHandlerHelper::setEncoding( const OUString& _rEncoding )
     115             :     {
     116           0 :         OString sAsciiEncoding( OUStringToOString( _rEncoding, RTL_TEXTENCODING_ASCII_US ) );
     117           0 :         rtl_TextEncoding eEncoding = rtl_getTextEncodingFromMimeCharset( sAsciiEncoding.getStr() );
     118           0 :         if ( eEncoding != RTL_TEXTENCODING_DONTKNOW )
     119             :         {
     120           0 :             m_eEncoding = eEncoding;
     121           0 :             return true;
     122             :         }
     123           0 :         return false;
     124             :     }
     125             : 
     126             : 
     127           0 :     bool LogHandlerHelper::formatForPublishing( const LogRecord& _rRecord, OString& _out_rEntry ) const
     128             :     {
     129           0 :         if ( _rRecord.Level < getLevel() )
     130             :             // not to be published due to low level
     131           0 :             return false;
     132             : 
     133             :         try
     134             :         {
     135           0 :             Reference< XLogFormatter > xFormatter( getFormatter(), UNO_QUERY_THROW );
     136           0 :             OUString sEntry( xFormatter->format( _rRecord ) );
     137           0 :             _out_rEntry = OUStringToOString( sEntry, getTextEncoding() );
     138           0 :             return true;
     139             :         }
     140           0 :         catch( const Exception& )
     141             :         {
     142             :             DBG_UNHANDLED_EXCEPTION();
     143             :         }
     144           0 :         return false;
     145             :     }
     146             : 
     147             : 
     148           0 :     bool LogHandlerHelper::getEncodedHead( OString& _out_rHead ) const
     149             :     {
     150             :         try
     151             :         {
     152           0 :             Reference< XLogFormatter > xFormatter( getFormatter(), UNO_QUERY_THROW );
     153           0 :             OUString sHead( xFormatter->getHead() );
     154           0 :             _out_rHead = OUStringToOString( sHead, getTextEncoding() );
     155           0 :             return true;
     156             :         }
     157           0 :         catch( const Exception& )
     158             :         {
     159             :             DBG_UNHANDLED_EXCEPTION();
     160             :         }
     161           0 :         return false;
     162             :     }
     163             : 
     164             : 
     165           0 :     bool LogHandlerHelper::getEncodedTail( OString& _out_rTail ) const
     166             :     {
     167             :         try
     168             :         {
     169           0 :             Reference< XLogFormatter > xFormatter( getFormatter(), UNO_QUERY_THROW );
     170           0 :             OUString sTail( xFormatter->getTail() );
     171           0 :             _out_rTail = OUStringToOString( sTail, getTextEncoding() );
     172           0 :             return true;
     173             :         }
     174           0 :         catch( const Exception& )
     175             :         {
     176             :             DBG_UNHANDLED_EXCEPTION();
     177             :         }
     178           0 :         return false;
     179             :     }
     180             : 
     181             : 
     182             : } // namespace logging
     183             : 
     184             : 
     185             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11