1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef LOGHANDLER_HXX
25 #define LOGHANDLER_HXX
26 
27 /** === begin UNO includes === **/
28 #include <com/sun/star/logging/XLogFormatter.hpp>
29 #include <com/sun/star/uno/XComponentContext.hpp>
30 #include <com/sun/star/logging/LogRecord.hpp>
31 /** === end UNO includes === **/
32 
33 #include <comphelper/namedvaluecollection.hxx>
34 #include <cppuhelper/interfacecontainer.hxx>
35 #include <rtl/string.hxx>
36 
37 //........................................................................
38 namespace logging
39 {
40 //........................................................................
41 
42 	//====================================================================
43 	//=
44 	//====================================================================
45 	class LogHandlerHelper
46 	{
47     private:
48         // <attributes>
49         rtl_TextEncoding    m_eEncoding;
50         sal_Int32           m_nLevel;
51         ::com::sun::star::uno::Reference< ::com::sun::star::logging::XLogFormatter >
52                             m_xFormatter;
53         // <//attributes>
54 
55         ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >
56                                     m_xContext;
57         ::osl::Mutex&               m_rMutex;
58         ::cppu::OBroadcastHelper&   m_rBHelper;
59         bool                        m_bInitialized;
60 
61     public:
62         LogHandlerHelper(
63             const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& _rxContext,
64             ::osl::Mutex& _rMutex,
65             ::cppu::OBroadcastHelper& _rBHelper
66         );
67 
68     public:
getIsInitialized() const69         bool    getIsInitialized() const { return m_bInitialized; }
setIsInitialized()70         void    setIsInitialized() { m_bInitialized = true; }
71 
72         bool    getEncoding(        ::rtl::OUString& _out_rEncoding ) const;
73         bool    setEncoding( const  ::rtl::OUString& _rEncoding     );
74 
75         inline  rtl_TextEncoding
getTextEncoding() const76                 getTextEncoding() const { return m_eEncoding; }
77 
78         inline  ::com::sun::star::uno::Reference< ::com::sun::star::logging::XLogFormatter >
getFormatter() const79                 getFormatter() const { return m_xFormatter; }
80         inline  void
setFormatter(const::com::sun::star::uno::Reference<::com::sun::star::logging::XLogFormatter> & _rxFormatter)81                 setFormatter( const ::com::sun::star::uno::Reference< ::com::sun::star::logging::XLogFormatter >& _rxFormatter )
82                 {
83                     m_xFormatter = _rxFormatter;
84                 }
85 
86         inline  sal_Int32
getLevel() const87                 getLevel() const { return m_nLevel; }
88         inline  void
setLevel(const sal_Int32 _nLevel)89                 setLevel( const sal_Int32 _nLevel )
90                 {
91                     m_nLevel = _nLevel;
92                 }
93 
94         /** prepares implementation of an public accessible method of a log handler
95 
96             <code>enterMethod</code> does the following things:
97             <ul><li>It acquires the mutex given in the constructor.</li>
98                 <li>It checks whether the component is already initialized, and throws an exception if not os.</li>
99                 <li>It checks whether the component is already disposed, and throws an exception if not os.</li>
100                 <li>It creates a default formatter (PlainTextFormatter), if no formatter exists at this time.</li>
101             </ul>
102         */
103         void enterMethod();
104 
105         /** formats a record for publishing it
106 
107             The method first checks whether the records log level is greater or equal our own
108             log level. If not, <FALSE/> is returned.
109 
110             Second, our formatter is used to create a unicode string from the log record. If an error occurs
111             during this, e.g. if the formatter is <NULL/> or throws an exception during formatting,
112             <FALSE/> is returned.
113 
114             Finally, the unicode string is encoded into a byte string, using our encoding setting. Then,
115             <TRUE/> is returned.
116         */
117         bool    formatForPublishing( const ::com::sun::star::logging::LogRecord& _rRecord, ::rtl::OString& _out_rEntry ) const;
118 
119         /** retrieves our formatter's heading, encoded with our encoding
120 
121             @return <TRUE/> in case of success, <FALSE/> if any error occurred
122         */
123         bool    getEncodedHead( ::rtl::OString& _out_rHead ) const;
124 
125         /** retrieves our formatter's tail, encoded with our encoding
126 
127             @return <TRUE/> in case of success, <FALSE/> if any error occurred
128         */
129         bool    getEncodedTail( ::rtl::OString& _out_rTail ) const;
130 
131         /** initializes the instance from a collection of named settings
132 
133             The recognized named settings are <code>Encoding</code>, <code>Formatter</code>, and <code>Level</code>,
134             which initialize the respective attributes.
135 
136             Settings which are recognized are remove from the given collection. This allows
137             the caller to determine whether or not the collection contained any unsupported
138             items, and react appropriately.
139 
140             @throws IllegalArgumentException
141                 if one of the values in the collection is of wrong type.
142         */
143         void    initFromSettings( const ::comphelper::NamedValueCollection& _rSettings );
144 	};
145 
146 //........................................................................
147 } // namespace logging
148 //........................................................................
149 
150 #endif // LOGHANDLER_HXX
151