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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_extensions.hxx"
26 
27 #include "log_module.hxx"
28 
29 #include <stdio.h>
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/lang/XServiceInfo.hpp>
35 /** === end UNO includes === **/
36 
37 #include <comphelper/componentcontext.hxx>
38 
39 #include <cppuhelper/implbase2.hxx>
40 
41 #include <rtl/ustrbuf.hxx>
42 
43 #include <osl/thread.h>
44 
45 //........................................................................
46 namespace logging
47 {
48 //........................................................................
49 
50 	/** === begin UNO using === **/
51     using ::com::sun::star::logging::XLogFormatter;
52     using ::com::sun::star::uno::XComponentContext;
53     using ::com::sun::star::uno::Reference;
54     using ::com::sun::star::uno::Sequence;
55     using ::com::sun::star::lang::XServiceInfo;
56     using ::com::sun::star::uno::RuntimeException;
57     using ::com::sun::star::logging::LogRecord;
58     using ::com::sun::star::uno::XInterface;
59 	/** === end UNO using === **/
60 
61 	//====================================================================
62 	//= PlainTextFormatter - declaration
63 	//====================================================================
64     typedef ::cppu::WeakImplHelper2 <   XLogFormatter
65                                     ,   XServiceInfo
66                                     >   PlainTextFormatter_Base;
67     class PlainTextFormatter : public PlainTextFormatter_Base
68 	{
69     private:
70         ::comphelper::ComponentContext  m_aContext;
71 
72     protected:
73         PlainTextFormatter( const Reference< XComponentContext >& _rxContext );
74         virtual ~PlainTextFormatter();
75 
76         // XLogFormatter
77         virtual ::rtl::OUString SAL_CALL getHead(  ) throw (RuntimeException);
78         virtual ::rtl::OUString SAL_CALL format( const LogRecord& Record ) throw (RuntimeException);
79         virtual ::rtl::OUString SAL_CALL getTail(  ) throw (RuntimeException);
80 
81         // XServiceInfo
82 		virtual ::rtl::OUString SAL_CALL getImplementationName() throw(RuntimeException);
83         virtual ::sal_Bool SAL_CALL supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException);
84         virtual Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames() throw(RuntimeException);
85 
86     public:
87         // XServiceInfo - static version
88 		static ::rtl::OUString SAL_CALL getImplementationName_static();
89         static Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames_static();
90         static Reference< XInterface > Create( const Reference< XComponentContext >& _rxContext );
91 	};
92 
93     //====================================================================
94 	//= PlainTextFormatter - implementation
95 	//====================================================================
96 	//--------------------------------------------------------------------
PlainTextFormatter(const Reference<XComponentContext> & _rxContext)97     PlainTextFormatter::PlainTextFormatter( const Reference< XComponentContext >& _rxContext )
98         :m_aContext( _rxContext )
99     {
100     }
101 
102     //--------------------------------------------------------------------
~PlainTextFormatter()103     PlainTextFormatter::~PlainTextFormatter()
104     {
105     }
106 
107     //--------------------------------------------------------------------
getHead()108     ::rtl::OUString SAL_CALL PlainTextFormatter::getHead(  ) throw (RuntimeException)
109     {
110         ::rtl::OUStringBuffer aHeader;
111         aHeader.appendAscii( "  event no" );                 // column 1: the event number
112         aHeader.appendAscii( " " );
113         aHeader.appendAscii( "thread  " );                   // column 2: the thread ID
114         aHeader.appendAscii( " " );
115         aHeader.appendAscii( "date      " );                 // column 3: date
116         aHeader.appendAscii( " " );
117         aHeader.appendAscii( "time       " );         // column 4: time
118         aHeader.appendAscii( " " );
119         aHeader.appendAscii( "(class/method:) message" );    // column 5: class/method/message
120         aHeader.appendAscii( "\n" );
121         return aHeader.makeStringAndClear();
122     }
123 
124     //--------------------------------------------------------------------
format(const LogRecord & _rRecord)125     ::rtl::OUString SAL_CALL PlainTextFormatter::format( const LogRecord& _rRecord ) throw (RuntimeException)
126     {
127         char buffer[ 30 ];
128         const int buffer_size = sizeof( buffer );
129         int used = snprintf( buffer, buffer_size, "%10i", (int)_rRecord.SequenceNumber );
130         if ( used >= buffer_size || used < 0 )
131             buffer[ buffer_size - 1 ] = 0;
132 
133         ::rtl::OUStringBuffer aLogEntry;
134         aLogEntry.appendAscii( buffer );
135         aLogEntry.appendAscii( " " );
136 
137         ::rtl::OString sThreadID( ::rtl::OUStringToOString( _rRecord.ThreadID, osl_getThreadTextEncoding() ) );
138         snprintf( buffer, buffer_size, "%8s", sThreadID.getStr() );
139         aLogEntry.appendAscii( buffer );
140         aLogEntry.appendAscii( " " );
141 
142         snprintf( buffer, buffer_size, "%04i-%02i-%02i %02i:%02i:%02i.%02i",
143             (int)_rRecord.LogTime.Year, (int)_rRecord.LogTime.Month, (int)_rRecord.LogTime.Day,
144             (int)_rRecord.LogTime.Hours, (int)_rRecord.LogTime.Minutes, (int)_rRecord.LogTime.Seconds, (int)_rRecord.LogTime.HundredthSeconds );
145         aLogEntry.appendAscii( buffer );
146         aLogEntry.appendAscii( " " );
147 
148         if ( _rRecord.SourceClassName.getLength() && _rRecord.SourceMethodName.getLength() )
149         {
150             aLogEntry.append( _rRecord.SourceClassName );
151             aLogEntry.appendAscii( "::" );
152             aLogEntry.append( _rRecord.SourceMethodName );
153             aLogEntry.appendAscii( ": " );
154         }
155 
156         aLogEntry.append( _rRecord.Message );
157         aLogEntry.appendAscii( "\n" );
158 
159         return aLogEntry.makeStringAndClear();
160     }
161 
162     //--------------------------------------------------------------------
getTail()163     ::rtl::OUString SAL_CALL PlainTextFormatter::getTail(  ) throw (RuntimeException)
164     {
165         // no tail
166         return ::rtl::OUString();
167     }
168 
169     //--------------------------------------------------------------------
supportsService(const::rtl::OUString & _rServiceName)170     ::sal_Bool SAL_CALL PlainTextFormatter::supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException)
171     {
172         const Sequence< ::rtl::OUString > aServiceNames( getSupportedServiceNames() );
173         for (   const ::rtl::OUString* pServiceNames = aServiceNames.getConstArray();
174                 pServiceNames != aServiceNames.getConstArray() + aServiceNames.getLength();
175                 ++pServiceNames
176             )
177             if ( _rServiceName == *pServiceNames )
178                 return sal_True;
179         return sal_False;
180     }
181 
182     //--------------------------------------------------------------------
getImplementationName()183     ::rtl::OUString SAL_CALL PlainTextFormatter::getImplementationName() throw(RuntimeException)
184     {
185         return getImplementationName_static();
186     }
187 
188     //--------------------------------------------------------------------
getSupportedServiceNames()189     Sequence< ::rtl::OUString > SAL_CALL PlainTextFormatter::getSupportedServiceNames() throw(RuntimeException)
190     {
191         return getSupportedServiceNames_static();
192     }
193 
194     //--------------------------------------------------------------------
getImplementationName_static()195     ::rtl::OUString SAL_CALL PlainTextFormatter::getImplementationName_static()
196     {
197         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.extensions.PlainTextFormatter" ) );
198     }
199 
200     //--------------------------------------------------------------------
getSupportedServiceNames_static()201     Sequence< ::rtl::OUString > SAL_CALL PlainTextFormatter::getSupportedServiceNames_static()
202     {
203         Sequence< ::rtl::OUString > aServiceNames(1);
204         aServiceNames[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.logging.PlainTextFormatter" ) );
205         return aServiceNames;
206     }
207 
208     //--------------------------------------------------------------------
Create(const Reference<XComponentContext> & _rxContext)209     Reference< XInterface > PlainTextFormatter::Create( const Reference< XComponentContext >& _rxContext )
210     {
211         return *( new PlainTextFormatter( _rxContext ) );
212     }
213 
214     //--------------------------------------------------------------------
createRegistryInfo_PlainTextFormatter()215     void createRegistryInfo_PlainTextFormatter()
216     {
217         static OAutoRegistration< PlainTextFormatter > aAutoRegistration;
218     }
219 
220 //........................................................................
221 } // namespace logging
222 //........................................................................
223