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 #ifndef LOGHANDLER_HXX
29 #define LOGHANDLER_HXX
30 
31 /** === begin UNO includes === **/
32 #include <com/sun/star/logging/XLogFormatter.hpp>
33 #include <com/sun/star/uno/XComponentContext.hpp>
34 #include <com/sun/star/logging/LogRecord.hpp>
35 /** === end UNO includes === **/
36 
37 #include <comphelper/namedvaluecollection.hxx>
38 #include <cppuhelper/interfacecontainer.hxx>
39 #include <rtl/string.hxx>
40 
41 //........................................................................
42 namespace logging
43 {
44 //........................................................................
45 
46 	//====================================================================
47 	//=
48 	//====================================================================
49 	class LogHandlerHelper
50 	{
51     private:
52         // <attributes>
53         rtl_TextEncoding    m_eEncoding;
54         sal_Int32           m_nLevel;
55         ::com::sun::star::uno::Reference< ::com::sun::star::logging::XLogFormatter >
56                             m_xFormatter;
57         // <//attributes>
58 
59         ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >
60                                     m_xContext;
61         ::osl::Mutex&               m_rMutex;
62         ::cppu::OBroadcastHelper&   m_rBHelper;
63         bool                        m_bInitialized;
64 
65     public:
66         LogHandlerHelper(
67             const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& _rxContext,
68             ::osl::Mutex& _rMutex,
69             ::cppu::OBroadcastHelper& _rBHelper
70         );
71 
72     public:
73         bool    getIsInitialized() const { return m_bInitialized; }
74         void    setIsInitialized() { m_bInitialized = true; }
75 
76         bool    getEncoding(        ::rtl::OUString& _out_rEncoding ) const;
77         bool    setEncoding( const  ::rtl::OUString& _rEncoding     );
78 
79         inline  rtl_TextEncoding
80                 getTextEncoding() const { return m_eEncoding; }
81 
82         inline  ::com::sun::star::uno::Reference< ::com::sun::star::logging::XLogFormatter >
83                 getFormatter() const { return m_xFormatter; }
84         inline  void
85                 setFormatter( const ::com::sun::star::uno::Reference< ::com::sun::star::logging::XLogFormatter >& _rxFormatter )
86                 {
87                     m_xFormatter = _rxFormatter;
88                 }
89 
90         inline  sal_Int32
91                 getLevel() const { return m_nLevel; }
92         inline  void
93                 setLevel( const sal_Int32 _nLevel )
94                 {
95                     m_nLevel = _nLevel;
96                 }
97 
98         /** prepares implementation of an public accessible method of a log handler
99 
100             <code>enterMethod</code> does the following things:
101             <ul><li>It acquires the mutex given in the constructor.</li>
102                 <li>It checks whether the component is already initialized, and throws an exception if not os.</li>
103                 <li>It checks whether the component is already disposed, and throws an exception if not os.</li>
104                 <li>It creates a default formatter (PlainTextFormatter), if no formatter exists at this time.</li>
105             </ul>
106         */
107         void enterMethod();
108 
109         /** formats a record for publishing it
110 
111             The method first checks whether the records log level is greater or equal our own
112             log level. If not, <FALSE/> is returned.
113 
114             Second, our formatter is used to create a unicode string from the log record. If an error occurs
115             during this, e.g. if the formatter is <NULL/> or throws an exception during formatting,
116             <FALSE/> is returned.
117 
118             Finally, the unicode string is encoded into a byte string, using our encoding setting. Then,
119             <TRUE/> is returned.
120         */
121         bool    formatForPublishing( const ::com::sun::star::logging::LogRecord& _rRecord, ::rtl::OString& _out_rEntry ) const;
122 
123         /** retrieves our formatter's heading, encoded with our encoding
124 
125             @return <TRUE/> in case of success, <FALSE/> if any error occured
126         */
127         bool    getEncodedHead( ::rtl::OString& _out_rHead ) const;
128 
129         /** retrieves our formatter's tail, encoded with our encoding
130 
131             @return <TRUE/> in case of success, <FALSE/> if any error occured
132         */
133         bool    getEncodedTail( ::rtl::OString& _out_rTail ) const;
134 
135         /** initializes the instance from a collection of named settings
136 
137             The recognized named settings are <code>Encoding</code>, <code>Formatter</code>, and <code>Level</code>,
138             which initialize the respective attributes.
139 
140             Settings which are recognized are remove from the given collection. This allows
141             the caller to determine whether or not the collection contained any unsupported
142             items, and react appropriately.
143 
144             @throws IllegalArgumentException
145                 if one of the values in the collection is of wrong type.
146         */
147         void    initFromSettings( const ::comphelper::NamedValueCollection& _rSettings );
148 	};
149 
150 //........................................................................
151 } // namespace logging
152 //........................................................................
153 
154 #endif // LOGHANDLER_HXX
155