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 "log_services.hxx"
23 #include "methodguard.hxx"
24 #include "loghandler.hxx"
26 #include <com/sun/star/logging/XConsoleHandler.hpp>
27 #include <com/sun/star/lang/XServiceInfo.hpp>
28 #include <com/sun/star/logging/LogLevel.hpp>
29 #include <com/sun/star/lang/XInitialization.hpp>
30 #include <com/sun/star/ucb/AlreadyInitializedException.hpp>
31 #include <com/sun/star/lang/IllegalArgumentException.hpp>
32 #include <com/sun/star/beans/NamedValue.hpp>
34 #include <cppuhelper/compbase3.hxx>
35 #include <cppuhelper/basemutex.hxx>
36 #include <cppuhelper/supportsservice.hxx>
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 typedef ::cppu::WeakComponentImplHelper3
< XConsoleHandler
67 > ConsoleHandler_Base
;
68 class ConsoleHandler
:public ::cppu::BaseMutex
69 ,public ConsoleHandler_Base
72 LogHandlerHelper m_aHandlerHelper
;
73 sal_Int32 m_nThreshold
;
76 ConsoleHandler( const Reference
< XComponentContext
>& _rxContext
);
77 virtual ~ConsoleHandler();
80 virtual ::sal_Int32 SAL_CALL
getThreshold() throw (RuntimeException
, std::exception
) SAL_OVERRIDE
;
81 virtual void SAL_CALL
setThreshold( ::sal_Int32 _threshold
) throw (RuntimeException
, std::exception
) SAL_OVERRIDE
;
84 virtual OUString SAL_CALL
getEncoding() throw (RuntimeException
, std::exception
) SAL_OVERRIDE
;
85 virtual void SAL_CALL
setEncoding( const OUString
& _encoding
) throw (RuntimeException
, std::exception
) SAL_OVERRIDE
;
86 virtual Reference
< XLogFormatter
> SAL_CALL
getFormatter() throw (RuntimeException
, std::exception
) SAL_OVERRIDE
;
87 virtual void SAL_CALL
setFormatter( const Reference
< XLogFormatter
>& _formatter
) throw (RuntimeException
, std::exception
) SAL_OVERRIDE
;
88 virtual ::sal_Int32 SAL_CALL
getLevel() throw (RuntimeException
, std::exception
) SAL_OVERRIDE
;
89 virtual void SAL_CALL
setLevel( ::sal_Int32 _level
) throw (RuntimeException
, std::exception
) SAL_OVERRIDE
;
90 virtual void SAL_CALL
flush( ) throw (RuntimeException
, std::exception
) SAL_OVERRIDE
;
91 virtual sal_Bool SAL_CALL
publish( const LogRecord
& Record
) throw (RuntimeException
, std::exception
) SAL_OVERRIDE
;
94 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
, std::exception
) SAL_OVERRIDE
;
97 virtual OUString SAL_CALL
getImplementationName() throw(RuntimeException
, std::exception
) SAL_OVERRIDE
;
98 virtual sal_Bool SAL_CALL
supportsService( const OUString
& _rServiceName
) throw(RuntimeException
, std::exception
) SAL_OVERRIDE
;
99 virtual Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() throw(RuntimeException
, std::exception
) SAL_OVERRIDE
;
102 virtual void SAL_CALL
disposing() SAL_OVERRIDE
;
105 // XServiceInfo - static version
106 static OUString SAL_CALL
getImplementationName_static();
107 static Sequence
< OUString
> SAL_CALL
getSupportedServiceNames_static();
108 static Reference
< XInterface
> Create( const Reference
< XComponentContext
>& _rxContext
);
111 typedef ComponentMethodGuard
< ConsoleHandler
> MethodGuard
;
112 void enterMethod( MethodGuard::Access
);
113 void leaveMethod( MethodGuard::Access
);
116 ConsoleHandler::ConsoleHandler( const Reference
< XComponentContext
>& _rxContext
)
117 :ConsoleHandler_Base( m_aMutex
)
118 ,m_aHandlerHelper( _rxContext
, m_aMutex
, rBHelper
)
119 ,m_nThreshold( LogLevel::SEVERE
)
124 ConsoleHandler::~ConsoleHandler()
126 if ( !rBHelper
.bDisposed
)
134 void SAL_CALL
ConsoleHandler::disposing()
136 m_aHandlerHelper
.setFormatter( NULL
);
140 void ConsoleHandler::enterMethod( MethodGuard::Access
)
142 m_aHandlerHelper
.enterMethod();
146 void ConsoleHandler::leaveMethod( MethodGuard::Access
)
152 ::sal_Int32 SAL_CALL
ConsoleHandler::getThreshold() throw (RuntimeException
, std::exception
)
154 MethodGuard
aGuard( *this );
159 void SAL_CALL
ConsoleHandler::setThreshold( ::sal_Int32 _threshold
) throw (RuntimeException
, std::exception
)
161 MethodGuard
aGuard( *this );
162 m_nThreshold
= _threshold
;
166 OUString SAL_CALL
ConsoleHandler::getEncoding() throw (RuntimeException
, std::exception
)
168 MethodGuard
aGuard( *this );
170 OSL_VERIFY( m_aHandlerHelper
.getEncoding( sEncoding
) );
175 void SAL_CALL
ConsoleHandler::setEncoding( const OUString
& _rEncoding
) throw (RuntimeException
, std::exception
)
177 MethodGuard
aGuard( *this );
178 OSL_VERIFY( m_aHandlerHelper
.setEncoding( _rEncoding
) );
182 Reference
< XLogFormatter
> SAL_CALL
ConsoleHandler::getFormatter() throw (RuntimeException
, std::exception
)
184 MethodGuard
aGuard( *this );
185 return m_aHandlerHelper
.getFormatter();
189 void SAL_CALL
ConsoleHandler::setFormatter( const Reference
< XLogFormatter
>& _rxFormatter
) throw (RuntimeException
, std::exception
)
191 MethodGuard
aGuard( *this );
192 m_aHandlerHelper
.setFormatter( _rxFormatter
);
196 ::sal_Int32 SAL_CALL
ConsoleHandler::getLevel() throw (RuntimeException
, std::exception
)
198 MethodGuard
aGuard( *this );
199 return m_aHandlerHelper
.getLevel();
203 void SAL_CALL
ConsoleHandler::setLevel( ::sal_Int32 _nLevel
) throw (RuntimeException
, std::exception
)
205 MethodGuard
aGuard( *this );
206 m_aHandlerHelper
.setLevel( _nLevel
);
210 void SAL_CALL
ConsoleHandler::flush( ) throw (RuntimeException
, std::exception
)
212 MethodGuard
aGuard( *this );
218 sal_Bool SAL_CALL
ConsoleHandler::publish( const LogRecord
& _rRecord
) throw (RuntimeException
, std::exception
)
220 MethodGuard
aGuard( *this );
223 if ( !m_aHandlerHelper
.formatForPublishing( _rRecord
, sEntry
) )
226 if ( _rRecord
.Level
>= m_nThreshold
)
227 fprintf( stderr
, "%s\n", sEntry
.getStr() );
229 fprintf( stdout
, "%s\n", sEntry
.getStr() );
235 void SAL_CALL
ConsoleHandler::initialize( const Sequence
< Any
>& _rArguments
) throw (Exception
, RuntimeException
, std::exception
)
237 ::osl::MutexGuard
aGuard( m_aMutex
);
239 if ( m_aHandlerHelper
.getIsInitialized() )
240 throw AlreadyInitializedException();
242 if ( _rArguments
.getLength() == 0 )
243 { // create() - nothing to init
244 m_aHandlerHelper
.setIsInitialized();
248 if ( _rArguments
.getLength() != 1 )
249 throw IllegalArgumentException( OUString(), *this, 1 );
251 Sequence
< NamedValue
> aSettings
;
252 if ( !( _rArguments
[0] >>= aSettings
) )
253 throw IllegalArgumentException( OUString(), *this, 1 );
255 // createWithSettings( [in] sequence< ::com::sun::star::beans::NamedValue > Settings )
256 ::comphelper::NamedValueCollection
aTypedSettings( aSettings
);
257 m_aHandlerHelper
.initFromSettings( aTypedSettings
);
259 aTypedSettings
.get_ensureType( "Threshold", m_nThreshold
);
261 m_aHandlerHelper
.setIsInitialized();
265 OUString SAL_CALL
ConsoleHandler::getImplementationName() throw(RuntimeException
, std::exception
)
267 return getImplementationName_static();
271 sal_Bool SAL_CALL
ConsoleHandler::supportsService( const OUString
& _rServiceName
) throw(RuntimeException
, std::exception
)
273 return cppu::supportsService(this, _rServiceName
);
277 Sequence
< OUString
> SAL_CALL
ConsoleHandler::getSupportedServiceNames() throw(RuntimeException
, std::exception
)
279 return getSupportedServiceNames_static();
283 OUString SAL_CALL
ConsoleHandler::getImplementationName_static()
285 return OUString( "com.sun.star.comp.extensions.ConsoleHandler" );
289 Sequence
< OUString
> SAL_CALL
ConsoleHandler::getSupportedServiceNames_static()
291 Sequence
< OUString
> aServiceNames(1);
292 aServiceNames
[0] = "com.sun.star.logging.ConsoleHandler";
293 return aServiceNames
;
297 Reference
< XInterface
> ConsoleHandler::Create( const Reference
< XComponentContext
>& _rxContext
)
299 return *( new ConsoleHandler( _rxContext
) );
303 void createRegistryInfo_ConsoleHandler()
305 static OAutoRegistration
< ConsoleHandler
> aAutoRegistration
;
309 } // namespace logging
312 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */