1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_extensions.hxx"
30 
31 #ifndef EXTENSIONS_LOGHANDLER_HXX
32 #include "loghandler.hxx"
33 #endif
34 
35 /** === begin UNO includes === **/
36 #include <com/sun/star/logging/LogLevel.hpp>
37 #include <com/sun/star/lang/IllegalArgumentException.hpp>
38 #include <com/sun/star/lang/DisposedException.hpp>
39 #include <com/sun/star/logging/PlainTextFormatter.hpp>
40 /** === end UNO includes === **/
41 
42 #include <tools/diagnose_ex.h>
43 #include <comphelper/componentcontext.hxx>
44 #include <rtl/tencinfo.h>
45 
46 //........................................................................
47 namespace logging
48 {
49 //........................................................................
50 
51 	/** === begin UNO using === **/
52     using ::com::sun::star::uno::Reference;
53     using ::com::sun::star::uno::XComponentContext;
54     using ::com::sun::star::uno::Any;
55     using ::com::sun::star::logging::LogRecord;
56     using ::com::sun::star::uno::UNO_QUERY_THROW;
57     using ::com::sun::star::logging::XLogFormatter;
58     using ::com::sun::star::uno::Exception;
59     using ::com::sun::star::lang::IllegalArgumentException;
60     using ::com::sun::star::lang::DisposedException;
61     using ::com::sun::star::logging::PlainTextFormatter;
62 	/** === end UNO using === **/
63     namespace LogLevel = ::com::sun::star::logging::LogLevel;
64 
65 	//====================================================================
66 	//= LogHandlerHelper
67 	//====================================================================
68 	//--------------------------------------------------------------------
69     LogHandlerHelper::LogHandlerHelper( const Reference< XComponentContext >& _rxContext, ::osl::Mutex& _rMutex, ::cppu::OBroadcastHelper& _rBHelper )
70         :m_eEncoding( RTL_TEXTENCODING_UTF8 )
71         ,m_nLevel( LogLevel::SEVERE )
72         ,m_xFormatter( NULL )
73         ,m_xContext( _rxContext )
74         ,m_rMutex( _rMutex )
75         ,m_rBHelper( _rBHelper )
76         ,m_bInitialized( false )
77     {
78     }
79 
80 	//--------------------------------------------------------------------
81     void LogHandlerHelper::initFromSettings( const ::comphelper::NamedValueCollection& _rSettings )
82     {
83         ::rtl::OUString sEncoding;
84         if ( _rSettings.get_ensureType( "Encoding", sEncoding ) )
85         {
86             if ( !setEncoding( sEncoding ) )
87                 throw IllegalArgumentException();
88         }
89 
90         _rSettings.get_ensureType( "Formatter", m_xFormatter );
91         _rSettings.get_ensureType( "Level", m_nLevel );
92     }
93 
94 	//--------------------------------------------------------------------
95     void LogHandlerHelper::enterMethod()
96     {
97         m_rMutex.acquire();
98 
99         if ( !getIsInitialized() )
100             throw DisposedException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "component not initialized" ) ), NULL );
101 
102         if ( m_rBHelper.bDisposed )
103             throw DisposedException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "component already disposed" ) ), NULL );
104 
105         // fallback settings, in case they weren't passed at construction time
106         if ( !getFormatter().is() )
107         {
108             try
109             {
110                 Reference< XLogFormatter > xFormatter( PlainTextFormatter::create( m_xContext ), UNO_QUERY_THROW );
111                 setFormatter( xFormatter );
112             }
113             catch( const Exception& )
114             {
115             	DBG_UNHANDLED_EXCEPTION();
116             }
117         }
118     }
119 
120 	//--------------------------------------------------------------------
121     bool LogHandlerHelper::getEncoding( ::rtl::OUString& _out_rEncoding ) const
122     {
123         const char* pMimeCharset = rtl_getMimeCharsetFromTextEncoding( m_eEncoding );
124         if ( pMimeCharset )
125         {
126             _out_rEncoding = ::rtl::OUString::createFromAscii( pMimeCharset );
127             return true;
128         }
129         _out_rEncoding = ::rtl::OUString();
130         return false;
131     }
132 
133     //--------------------------------------------------------------------
134     bool LogHandlerHelper::setEncoding( const ::rtl::OUString& _rEncoding )
135     {
136         ::rtl::OString sAsciiEncoding( ::rtl::OUStringToOString( _rEncoding, RTL_TEXTENCODING_ASCII_US ) );
137         rtl_TextEncoding eEncoding = rtl_getTextEncodingFromMimeCharset( sAsciiEncoding.getStr() );
138         if ( eEncoding != RTL_TEXTENCODING_DONTKNOW )
139         {
140             m_eEncoding = eEncoding;
141             return true;
142         }
143         return false;
144     }
145 
146     //--------------------------------------------------------------------
147     bool LogHandlerHelper::formatForPublishing( const LogRecord& _rRecord, ::rtl::OString& _out_rEntry ) const
148     {
149         if ( _rRecord.Level < getLevel() )
150             // not to be published due to low level
151             return false;
152 
153         try
154         {
155             Reference< XLogFormatter > xFormatter( getFormatter(), UNO_QUERY_THROW );
156             ::rtl::OUString sEntry( xFormatter->format( _rRecord ) );
157             _out_rEntry = ::rtl::OUStringToOString( sEntry, getTextEncoding() );
158             return true;
159         }
160         catch( const Exception& )
161         {
162             DBG_UNHANDLED_EXCEPTION();
163         }
164         return false;
165     }
166 
167     //--------------------------------------------------------------------
168     bool LogHandlerHelper::getEncodedHead( ::rtl::OString& _out_rHead ) const
169     {
170         try
171         {
172             Reference< XLogFormatter > xFormatter( getFormatter(), UNO_QUERY_THROW );
173             ::rtl::OUString sHead( xFormatter->getHead() );
174             _out_rHead = ::rtl::OUStringToOString( sHead, getTextEncoding() );
175             return true;
176         }
177         catch( const Exception& )
178         {
179             DBG_UNHANDLED_EXCEPTION();
180         }
181         return false;
182     }
183 
184     //--------------------------------------------------------------------
185     bool LogHandlerHelper::getEncodedTail( ::rtl::OString& _out_rTail ) const
186     {
187         try
188         {
189             Reference< XLogFormatter > xFormatter( getFormatter(), UNO_QUERY_THROW );
190             ::rtl::OUString sTail( xFormatter->getTail() );
191             _out_rTail = ::rtl::OUStringToOString( sTail, getTextEncoding() );
192             return true;
193         }
194         catch( const Exception& )
195         {
196             DBG_UNHANDLED_EXCEPTION();
197         }
198         return false;
199     }
200 
201 //........................................................................
202 } // namespace logging
203 //........................................................................
204 
205