LibreOffice Module extensions (master) 1
loghandler.cxx
Go to the documentation of this file.
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
29#include <rtl/tencinfo.h>
30
31
32namespace logging
33{
34
35
36 using ::com::sun::star::uno::Reference;
37 using ::com::sun::star::uno::XComponentContext;
38 using ::com::sun::star::logging::LogRecord;
39 using ::com::sun::star::logging::XLogFormatter;
40 using ::com::sun::star::uno::Exception;
41 using ::com::sun::star::lang::IllegalArgumentException;
42 using ::com::sun::star::lang::DisposedException;
43 using ::com::sun::star::logging::PlainTextFormatter;
44
45 namespace LogLevel = ::com::sun::star::logging::LogLevel;
46
47 LogHandlerHelper::LogHandlerHelper( const Reference< XComponentContext >& _rxContext, ::osl::Mutex& _rMutex, ::cppu::OBroadcastHelper& _rBHelper )
48 :m_eEncoding( RTL_TEXTENCODING_UTF8 )
49 ,m_nLevel( LogLevel::SEVERE )
50 ,m_xContext( _rxContext )
51 ,m_rMutex( _rMutex )
52 ,m_rBHelper( _rBHelper )
53 ,m_bInitialized( false )
54 {
55 }
56
57
58 void LogHandlerHelper::initFromSettings( const ::comphelper::NamedValueCollection& _rSettings )
59 {
60 OUString sEncoding;
61 if ( _rSettings.get_ensureType( "Encoding", sEncoding ) )
62 {
63 if ( !setEncoding( sEncoding ) )
64 throw IllegalArgumentException();
65 }
66
67 _rSettings.get_ensureType( "Formatter", m_xFormatter );
68 _rSettings.get_ensureType( "Level", m_nLevel );
69 }
70
71
73 {
74 m_rMutex.acquire();
75
76 if ( !m_bInitialized )
77 throw DisposedException("component not initialized" );
78
80 throw DisposedException("component already disposed" );
81
82 // fallback settings, in case they weren't passed at construction time
83 if ( !getFormatter().is() )
84 {
85 try
86 {
87 Reference< XLogFormatter > xFormatter( PlainTextFormatter::create( m_xContext ), css::uno::UNO_SET_THROW );
88 setFormatter( xFormatter );
89 }
90 catch( const Exception& )
91 {
92 DBG_UNHANDLED_EXCEPTION("extensions.logging");
93 }
94 }
95 }
96
97
98 bool LogHandlerHelper::getEncoding( OUString& _out_rEncoding ) const
99 {
100 const char* pMimeCharset = rtl_getMimeCharsetFromTextEncoding( m_eEncoding );
101 if ( pMimeCharset )
102 {
103 _out_rEncoding = OUString::createFromAscii( pMimeCharset );
104 return true;
105 }
106 _out_rEncoding.clear();
107 return false;
108 }
109
110
111 bool LogHandlerHelper::setEncoding( std::u16string_view _rEncoding )
112 {
113 OString sAsciiEncoding( OUStringToOString( _rEncoding, RTL_TEXTENCODING_ASCII_US ) );
114 rtl_TextEncoding eEncoding = rtl_getTextEncodingFromMimeCharset( sAsciiEncoding.getStr() );
115 if ( eEncoding != RTL_TEXTENCODING_DONTKNOW )
116 {
117 m_eEncoding = eEncoding;
118 return true;
119 }
120 return false;
121 }
122
123
124 bool LogHandlerHelper::formatForPublishing( const LogRecord& _rRecord, OString& _out_rEntry ) const
125 {
126 if ( _rRecord.Level < getLevel() )
127 // not to be published due to low level
128 return false;
129
130 try
131 {
132 Reference< XLogFormatter > xFormatter( getFormatter(), css::uno::UNO_SET_THROW );
133 OUString sEntry( xFormatter->format( _rRecord ) );
134 _out_rEntry = OUStringToOString( sEntry, getTextEncoding() );
135 return true;
136 }
137 catch( const Exception& )
138 {
139 DBG_UNHANDLED_EXCEPTION("extensions.logging");
140 }
141 return false;
142 }
143
144
145 bool LogHandlerHelper::getEncodedHead( OString& _out_rHead ) const
146 {
147 try
148 {
149 Reference< XLogFormatter > xFormatter( getFormatter(), css::uno::UNO_SET_THROW );
150 OUString sHead( xFormatter->getHead() );
151 _out_rHead = OUStringToOString( sHead, getTextEncoding() );
152 return true;
153 }
154 catch( const Exception& )
155 {
156 DBG_UNHANDLED_EXCEPTION("extensions.logging");
157 }
158 return false;
159 }
160
161
162 bool LogHandlerHelper::getEncodedTail( OString& _out_rTail ) const
163 {
164 try
165 {
166 Reference< XLogFormatter > xFormatter( getFormatter(), css::uno::UNO_SET_THROW );
167 OUString sTail( xFormatter->getTail() );
168 _out_rTail = OUStringToOString( sTail, getTextEncoding() );
169 return true;
170 }
171 catch( const Exception& )
172 {
173 DBG_UNHANDLED_EXCEPTION("extensions.logging");
174 }
175 return false;
176 }
177
178
179} // namespace logging
180
181
182/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int32 m_nLevel
LogHandlerHelper(const css::uno::Reference< css::uno::XComponentContext > &_rxContext, ::osl::Mutex &_rMutex, ::cppu::OBroadcastHelper &_rBHelper)
Definition: loghandler.cxx:47
bool getEncoding(OUString &_out_rEncoding) const
Definition: loghandler.cxx:98
void enterMethod()
prepares implementation of a public accessible method of a log handler
Definition: loghandler.cxx:72
const css::uno::Reference< css::logging::XLogFormatter > & getFormatter() const
Definition: loghandler.hxx:71
::cppu::OBroadcastHelper & m_rBHelper
Definition: loghandler.hxx:51
css::uno::Reference< css::uno::XComponentContext > m_xContext
Definition: loghandler.hxx:49
bool setEncoding(std::u16string_view _rEncoding)
Definition: loghandler.cxx:111
void initFromSettings(const ::comphelper::NamedValueCollection &_rSettings)
initializes the instance from a collection of named settings
Definition: loghandler.cxx:58
sal_Int32 getLevel() const
Definition: loghandler.hxx:79
void setFormatter(const css::uno::Reference< css::logging::XLogFormatter > &_rxFormatter)
Definition: loghandler.hxx:73
bool formatForPublishing(const css::logging::LogRecord &_rRecord, OString &_out_rEntry) const
formats a record for publishing it
Definition: loghandler.cxx:124
rtl_TextEncoding getTextEncoding() const
Definition: loghandler.hxx:68
css::uno::Reference< css::logging::XLogFormatter > m_xFormatter
Definition: loghandler.hxx:45
bool getEncodedTail(OString &_out_rTail) const
retrieves our formatter's tail, encoded with our encoding
Definition: loghandler.cxx:162
bool getEncodedHead(OString &_out_rHead) const
retrieves our formatter's heading, encoded with our encoding
Definition: loghandler.cxx:145
::osl::Mutex & m_rMutex
Definition: loghandler.hxx:50
rtl_TextEncoding m_eEncoding
Definition: loghandler.hxx:42
::osl::Mutex & m_rMutex
#define DBG_UNHANDLED_EXCEPTION(...)
Reference< XComponentContext > m_xContext
Definition: filehandler.cxx:78
@ Exception
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)