1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include "log_module.hxx"
22 #include "methodguard.hxx"
23 #include "loghandler.hxx"
25 #include <com/sun/star/logging/XConsoleHandler.hpp>
26 #include <com/sun/star/lang/XServiceInfo.hpp>
27 #include <com/sun/star/logging/LogLevel.hpp>
28 #include <com/sun/star/lang/XInitialization.hpp>
29 #include <com/sun/star/ucb/AlreadyInitializedException.hpp>
30 #include <com/sun/star/lang/IllegalArgumentException.hpp>
31 #include <com/sun/star/beans/NamedValue.hpp>
33 #include <comphelper/componentcontext.hxx>
35 #include <cppuhelper/compbase3.hxx>
36 #include <cppuhelper/basemutex.hxx>
40 //........................................................................
43 //........................................................................
45 using ::com::sun::star::logging::XConsoleHandler
;
46 using ::com::sun::star::lang::XServiceInfo
;
47 using ::com::sun::star::uno::Reference
;
48 using ::com::sun::star::uno::XComponentContext
;
49 using ::com::sun::star::uno::RuntimeException
;
50 using ::com::sun::star::logging::XLogFormatter
;
51 using ::com::sun::star::uno::Sequence
;
52 using ::com::sun::star::logging::LogRecord
;
53 using ::com::sun::star::uno::UNO_QUERY_THROW
;
54 using ::com::sun::star::uno::Exception
;
55 using ::com::sun::star::uno::Any
;
56 using ::com::sun::star::uno::XInterface
;
57 using ::com::sun::star::lang::XInitialization
;
58 using ::com::sun::star::ucb::AlreadyInitializedException
;
59 using ::com::sun::star::lang::IllegalArgumentException
;
60 using ::com::sun::star::beans::NamedValue
;
62 namespace LogLevel
= ::com::sun::star::logging::LogLevel
;
64 //====================================================================
65 //= ConsoleHandler - declaration
66 //====================================================================
67 //--------------------------------------------------------------------
68 typedef ::cppu::WeakComponentImplHelper3
< XConsoleHandler
71 > ConsoleHandler_Base
;
72 class ConsoleHandler
:public ::cppu::BaseMutex
73 ,public ConsoleHandler_Base
76 ::comphelper::ComponentContext m_aContext
;
77 LogHandlerHelper m_aHandlerHelper
;
78 sal_Int32 m_nThreshold
;
81 ConsoleHandler( const Reference
< XComponentContext
>& _rxContext
);
82 virtual ~ConsoleHandler();
85 virtual ::sal_Int32 SAL_CALL
getThreshold() throw (RuntimeException
);
86 virtual void SAL_CALL
setThreshold( ::sal_Int32 _threshold
) throw (RuntimeException
);
89 virtual OUString SAL_CALL
getEncoding() throw (RuntimeException
);
90 virtual void SAL_CALL
setEncoding( const OUString
& _encoding
) throw (RuntimeException
);
91 virtual Reference
< XLogFormatter
> SAL_CALL
getFormatter() throw (RuntimeException
);
92 virtual void SAL_CALL
setFormatter( const Reference
< XLogFormatter
>& _formatter
) throw (RuntimeException
);
93 virtual ::sal_Int32 SAL_CALL
getLevel() throw (RuntimeException
);
94 virtual void SAL_CALL
setLevel( ::sal_Int32 _level
) throw (RuntimeException
);
95 virtual void SAL_CALL
flush( ) throw (RuntimeException
);
96 virtual ::sal_Bool SAL_CALL
publish( const LogRecord
& Record
) throw (RuntimeException
);
99 virtual void SAL_CALL
initialize( const ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Any
>& aArguments
) throw (::com::sun::star::uno::Exception
, ::com::sun::star::uno::RuntimeException
);
102 virtual OUString SAL_CALL
getImplementationName() throw(RuntimeException
);
103 virtual ::sal_Bool SAL_CALL
supportsService( const OUString
& _rServiceName
) throw(RuntimeException
);
104 virtual Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() throw(RuntimeException
);
107 virtual void SAL_CALL
disposing();
110 // XServiceInfo - static version
111 static OUString SAL_CALL
getImplementationName_static();
112 static Sequence
< OUString
> SAL_CALL
getSupportedServiceNames_static();
113 static Reference
< XInterface
> Create( const Reference
< XComponentContext
>& _rxContext
);
116 typedef ComponentMethodGuard
< ConsoleHandler
> MethodGuard
;
117 void enterMethod( MethodGuard::Access
);
118 void leaveMethod( MethodGuard::Access
);
121 //====================================================================
122 //= ConsoleHandler - implementation
123 //====================================================================
124 //--------------------------------------------------------------------
125 ConsoleHandler::ConsoleHandler( const Reference
< XComponentContext
>& _rxContext
)
126 :ConsoleHandler_Base( m_aMutex
)
127 ,m_aContext( _rxContext
)
128 ,m_aHandlerHelper( _rxContext
, m_aMutex
, rBHelper
)
129 ,m_nThreshold( LogLevel::SEVERE
)
133 //--------------------------------------------------------------------
134 ConsoleHandler::~ConsoleHandler()
136 if ( !rBHelper
.bDisposed
)
143 //--------------------------------------------------------------------
144 void SAL_CALL
ConsoleHandler::disposing()
146 m_aHandlerHelper
.setFormatter( NULL
);
149 //--------------------------------------------------------------------
150 void ConsoleHandler::enterMethod( MethodGuard::Access
)
152 m_aHandlerHelper
.enterMethod();
155 //--------------------------------------------------------------------
156 void ConsoleHandler::leaveMethod( MethodGuard::Access
)
161 //--------------------------------------------------------------------
162 ::sal_Int32 SAL_CALL
ConsoleHandler::getThreshold() throw (RuntimeException
)
164 MethodGuard
aGuard( *this );
168 //--------------------------------------------------------------------
169 void SAL_CALL
ConsoleHandler::setThreshold( ::sal_Int32 _threshold
) throw (RuntimeException
)
171 MethodGuard
aGuard( *this );
172 m_nThreshold
= _threshold
;
175 //--------------------------------------------------------------------
176 OUString SAL_CALL
ConsoleHandler::getEncoding() throw (RuntimeException
)
178 MethodGuard
aGuard( *this );
180 OSL_VERIFY( m_aHandlerHelper
.getEncoding( sEncoding
) );
184 //--------------------------------------------------------------------
185 void SAL_CALL
ConsoleHandler::setEncoding( const OUString
& _rEncoding
) throw (RuntimeException
)
187 MethodGuard
aGuard( *this );
188 OSL_VERIFY( m_aHandlerHelper
.setEncoding( _rEncoding
) );
191 //--------------------------------------------------------------------
192 Reference
< XLogFormatter
> SAL_CALL
ConsoleHandler::getFormatter() throw (RuntimeException
)
194 MethodGuard
aGuard( *this );
195 return m_aHandlerHelper
.getFormatter();
198 //--------------------------------------------------------------------
199 void SAL_CALL
ConsoleHandler::setFormatter( const Reference
< XLogFormatter
>& _rxFormatter
) throw (RuntimeException
)
201 MethodGuard
aGuard( *this );
202 m_aHandlerHelper
.setFormatter( _rxFormatter
);
205 //--------------------------------------------------------------------
206 ::sal_Int32 SAL_CALL
ConsoleHandler::getLevel() throw (RuntimeException
)
208 MethodGuard
aGuard( *this );
209 return m_aHandlerHelper
.getLevel();
212 //--------------------------------------------------------------------
213 void SAL_CALL
ConsoleHandler::setLevel( ::sal_Int32 _nLevel
) throw (RuntimeException
)
215 MethodGuard
aGuard( *this );
216 m_aHandlerHelper
.setLevel( _nLevel
);
219 //--------------------------------------------------------------------
220 void SAL_CALL
ConsoleHandler::flush( ) throw (RuntimeException
)
222 MethodGuard
aGuard( *this );
227 //--------------------------------------------------------------------
228 ::sal_Bool SAL_CALL
ConsoleHandler::publish( const LogRecord
& _rRecord
) throw (RuntimeException
)
230 MethodGuard
aGuard( *this );
233 if ( !m_aHandlerHelper
.formatForPublishing( _rRecord
, sEntry
) )
236 if ( _rRecord
.Level
>= m_nThreshold
)
237 fprintf( stderr
, "%s\n", sEntry
.getStr() );
239 fprintf( stdout
, "%s\n", sEntry
.getStr() );
244 //--------------------------------------------------------------------
245 void SAL_CALL
ConsoleHandler::initialize( const Sequence
< Any
>& _rArguments
) throw (Exception
, RuntimeException
)
247 ::osl::MutexGuard
aGuard( m_aMutex
);
249 if ( m_aHandlerHelper
.getIsInitialized() )
250 throw AlreadyInitializedException();
252 if ( _rArguments
.getLength() == 0 )
253 { // create() - nothing to init
254 m_aHandlerHelper
.setIsInitialized();
258 if ( _rArguments
.getLength() != 1 )
259 throw IllegalArgumentException( OUString(), *this, 1 );
261 Sequence
< NamedValue
> aSettings
;
262 if ( !( _rArguments
[0] >>= aSettings
) )
263 throw IllegalArgumentException( OUString(), *this, 1 );
265 // createWithSettings( [in] sequence< ::com::sun::star::beans::NamedValue > Settings )
266 ::comphelper::NamedValueCollection
aTypedSettings( aSettings
);
267 m_aHandlerHelper
.initFromSettings( aTypedSettings
);
269 aTypedSettings
.get_ensureType( "Threshold", m_nThreshold
);
271 m_aHandlerHelper
.setIsInitialized();
274 //--------------------------------------------------------------------
275 OUString SAL_CALL
ConsoleHandler::getImplementationName() throw(RuntimeException
)
277 return getImplementationName_static();
280 //--------------------------------------------------------------------
281 ::sal_Bool SAL_CALL
ConsoleHandler::supportsService( const OUString
& _rServiceName
) throw(RuntimeException
)
283 const Sequence
< OUString
> aServiceNames( getSupportedServiceNames() );
284 for ( const OUString
* pServiceNames
= aServiceNames
.getConstArray();
285 pServiceNames
!= aServiceNames
.getConstArray() + aServiceNames
.getLength();
288 if ( _rServiceName
== *pServiceNames
)
293 //--------------------------------------------------------------------
294 Sequence
< OUString
> SAL_CALL
ConsoleHandler::getSupportedServiceNames() throw(RuntimeException
)
296 return getSupportedServiceNames_static();
299 //--------------------------------------------------------------------
300 OUString SAL_CALL
ConsoleHandler::getImplementationName_static()
302 return OUString( "com.sun.star.comp.extensions.ConsoleHandler" );
305 //--------------------------------------------------------------------
306 Sequence
< OUString
> SAL_CALL
ConsoleHandler::getSupportedServiceNames_static()
308 Sequence
< OUString
> aServiceNames(1);
309 aServiceNames
[0] = OUString( "com.sun.star.logging.ConsoleHandler" );
310 return aServiceNames
;
313 //--------------------------------------------------------------------
314 Reference
< XInterface
> ConsoleHandler::Create( const Reference
< XComponentContext
>& _rxContext
)
316 return *( new ConsoleHandler( _rxContext
) );
319 //--------------------------------------------------------------------
320 void createRegistryInfo_ConsoleHandler()
322 static OAutoRegistration
< ConsoleHandler
> aAutoRegistration
;
325 //........................................................................
326 } // namespace logging
327 //........................................................................
329 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */