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