1*2a97ec55SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*2a97ec55SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*2a97ec55SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*2a97ec55SAndrew Rist  * distributed with this work for additional information
6*2a97ec55SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*2a97ec55SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*2a97ec55SAndrew Rist  * "License"); you may not use this file except in compliance
9*2a97ec55SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*2a97ec55SAndrew Rist  *
11*2a97ec55SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*2a97ec55SAndrew Rist  *
13*2a97ec55SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*2a97ec55SAndrew Rist  * software distributed under the License is distributed on an
15*2a97ec55SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2a97ec55SAndrew Rist  * KIND, either express or implied.  See the License for the
17*2a97ec55SAndrew Rist  * specific language governing permissions and limitations
18*2a97ec55SAndrew Rist  * under the License.
19*2a97ec55SAndrew Rist  *
20*2a97ec55SAndrew Rist  *************************************************************/
21*2a97ec55SAndrew Rist 
22*2a97ec55SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_extensions.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "log_module.hxx"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <stdio.h>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir /** === begin UNO includes === **/
32cdf0e10cSrcweir #include <com/sun/star/logging/XLogFormatter.hpp>
33cdf0e10cSrcweir #include <com/sun/star/uno/XComponentContext.hpp>
34cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
35cdf0e10cSrcweir /** === end UNO includes === **/
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #include <comphelper/componentcontext.hxx>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #include <cppuhelper/implbase2.hxx>
40cdf0e10cSrcweir 
41cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
42cdf0e10cSrcweir 
43cdf0e10cSrcweir #include <osl/thread.h>
44cdf0e10cSrcweir 
45cdf0e10cSrcweir //........................................................................
46cdf0e10cSrcweir namespace logging
47cdf0e10cSrcweir {
48cdf0e10cSrcweir //........................................................................
49cdf0e10cSrcweir 
50cdf0e10cSrcweir 	/** === begin UNO using === **/
51cdf0e10cSrcweir     using ::com::sun::star::logging::XLogFormatter;
52cdf0e10cSrcweir     using ::com::sun::star::uno::XComponentContext;
53cdf0e10cSrcweir     using ::com::sun::star::uno::Reference;
54cdf0e10cSrcweir     using ::com::sun::star::uno::Sequence;
55cdf0e10cSrcweir     using ::com::sun::star::lang::XServiceInfo;
56cdf0e10cSrcweir     using ::com::sun::star::uno::RuntimeException;
57cdf0e10cSrcweir     using ::com::sun::star::logging::LogRecord;
58cdf0e10cSrcweir     using ::com::sun::star::uno::XInterface;
59cdf0e10cSrcweir 	/** === end UNO using === **/
60cdf0e10cSrcweir 
61cdf0e10cSrcweir 	//====================================================================
62cdf0e10cSrcweir 	//= PlainTextFormatter - declaration
63cdf0e10cSrcweir 	//====================================================================
64cdf0e10cSrcweir     typedef ::cppu::WeakImplHelper2 <   XLogFormatter
65cdf0e10cSrcweir                                     ,   XServiceInfo
66cdf0e10cSrcweir                                     >   PlainTextFormatter_Base;
67cdf0e10cSrcweir     class PlainTextFormatter : public PlainTextFormatter_Base
68cdf0e10cSrcweir 	{
69cdf0e10cSrcweir     private:
70cdf0e10cSrcweir         ::comphelper::ComponentContext  m_aContext;
71cdf0e10cSrcweir 
72cdf0e10cSrcweir     protected:
73cdf0e10cSrcweir         PlainTextFormatter( const Reference< XComponentContext >& _rxContext );
74cdf0e10cSrcweir         virtual ~PlainTextFormatter();
75cdf0e10cSrcweir 
76cdf0e10cSrcweir         // XLogFormatter
77cdf0e10cSrcweir         virtual ::rtl::OUString SAL_CALL getHead(  ) throw (RuntimeException);
78cdf0e10cSrcweir         virtual ::rtl::OUString SAL_CALL format( const LogRecord& Record ) throw (RuntimeException);
79cdf0e10cSrcweir         virtual ::rtl::OUString SAL_CALL getTail(  ) throw (RuntimeException);
80cdf0e10cSrcweir 
81cdf0e10cSrcweir         // XServiceInfo
82cdf0e10cSrcweir 		virtual ::rtl::OUString SAL_CALL getImplementationName() throw(RuntimeException);
83cdf0e10cSrcweir         virtual ::sal_Bool SAL_CALL supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException);
84cdf0e10cSrcweir         virtual Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames() throw(RuntimeException);
85cdf0e10cSrcweir 
86cdf0e10cSrcweir     public:
87cdf0e10cSrcweir         // XServiceInfo - static version
88cdf0e10cSrcweir 		static ::rtl::OUString SAL_CALL getImplementationName_static();
89cdf0e10cSrcweir         static Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames_static();
90cdf0e10cSrcweir         static Reference< XInterface > Create( const Reference< XComponentContext >& _rxContext );
91cdf0e10cSrcweir 	};
92cdf0e10cSrcweir 
93cdf0e10cSrcweir     //====================================================================
94cdf0e10cSrcweir 	//= PlainTextFormatter - implementation
95cdf0e10cSrcweir 	//====================================================================
96cdf0e10cSrcweir 	//--------------------------------------------------------------------
PlainTextFormatter(const Reference<XComponentContext> & _rxContext)97cdf0e10cSrcweir     PlainTextFormatter::PlainTextFormatter( const Reference< XComponentContext >& _rxContext )
98cdf0e10cSrcweir         :m_aContext( _rxContext )
99cdf0e10cSrcweir     {
100cdf0e10cSrcweir     }
101cdf0e10cSrcweir 
102cdf0e10cSrcweir     //--------------------------------------------------------------------
~PlainTextFormatter()103cdf0e10cSrcweir     PlainTextFormatter::~PlainTextFormatter()
104cdf0e10cSrcweir     {
105cdf0e10cSrcweir     }
106cdf0e10cSrcweir 
107cdf0e10cSrcweir     //--------------------------------------------------------------------
getHead()108cdf0e10cSrcweir     ::rtl::OUString SAL_CALL PlainTextFormatter::getHead(  ) throw (RuntimeException)
109cdf0e10cSrcweir     {
110cdf0e10cSrcweir         ::rtl::OUStringBuffer aHeader;
111cdf0e10cSrcweir         aHeader.appendAscii( "  event no" );                 // column 1: the event number
112cdf0e10cSrcweir         aHeader.appendAscii( " " );
113cdf0e10cSrcweir         aHeader.appendAscii( "thread  " );                   // column 2: the thread ID
114cdf0e10cSrcweir         aHeader.appendAscii( " " );
115cdf0e10cSrcweir         aHeader.appendAscii( "date      " );                 // column 3: date
116cdf0e10cSrcweir         aHeader.appendAscii( " " );
117cdf0e10cSrcweir         aHeader.appendAscii( "time       " );         // column 4: time
118cdf0e10cSrcweir         aHeader.appendAscii( " " );
119cdf0e10cSrcweir         aHeader.appendAscii( "(class/method:) message" );    // column 5: class/method/message
120cdf0e10cSrcweir         aHeader.appendAscii( "\n" );
121cdf0e10cSrcweir         return aHeader.makeStringAndClear();
122cdf0e10cSrcweir     }
123cdf0e10cSrcweir 
124cdf0e10cSrcweir     //--------------------------------------------------------------------
format(const LogRecord & _rRecord)125cdf0e10cSrcweir     ::rtl::OUString SAL_CALL PlainTextFormatter::format( const LogRecord& _rRecord ) throw (RuntimeException)
126cdf0e10cSrcweir     {
127cdf0e10cSrcweir         char buffer[ 30 ];
128cdf0e10cSrcweir         const int buffer_size = sizeof( buffer );
129cdf0e10cSrcweir         int used = snprintf( buffer, buffer_size, "%10i", (int)_rRecord.SequenceNumber );
130cdf0e10cSrcweir         if ( used >= buffer_size || used < 0 )
131cdf0e10cSrcweir             buffer[ buffer_size - 1 ] = 0;
132cdf0e10cSrcweir 
133cdf0e10cSrcweir         ::rtl::OUStringBuffer aLogEntry;
134cdf0e10cSrcweir         aLogEntry.appendAscii( buffer );
135cdf0e10cSrcweir         aLogEntry.appendAscii( " " );
136cdf0e10cSrcweir 
137cdf0e10cSrcweir         ::rtl::OString sThreadID( ::rtl::OUStringToOString( _rRecord.ThreadID, osl_getThreadTextEncoding() ) );
138cdf0e10cSrcweir         snprintf( buffer, buffer_size, "%8s", sThreadID.getStr() );
139cdf0e10cSrcweir         aLogEntry.appendAscii( buffer );
140cdf0e10cSrcweir         aLogEntry.appendAscii( " " );
141cdf0e10cSrcweir 
142cdf0e10cSrcweir         snprintf( buffer, buffer_size, "%04i-%02i-%02i %02i:%02i:%02i.%02i",
143cdf0e10cSrcweir             (int)_rRecord.LogTime.Year, (int)_rRecord.LogTime.Month, (int)_rRecord.LogTime.Day,
144cdf0e10cSrcweir             (int)_rRecord.LogTime.Hours, (int)_rRecord.LogTime.Minutes, (int)_rRecord.LogTime.Seconds, (int)_rRecord.LogTime.HundredthSeconds );
145cdf0e10cSrcweir         aLogEntry.appendAscii( buffer );
146cdf0e10cSrcweir         aLogEntry.appendAscii( " " );
147cdf0e10cSrcweir 
148cdf0e10cSrcweir         if ( _rRecord.SourceClassName.getLength() && _rRecord.SourceMethodName.getLength() )
149cdf0e10cSrcweir         {
150cdf0e10cSrcweir             aLogEntry.append( _rRecord.SourceClassName );
151cdf0e10cSrcweir             aLogEntry.appendAscii( "::" );
152cdf0e10cSrcweir             aLogEntry.append( _rRecord.SourceMethodName );
153cdf0e10cSrcweir             aLogEntry.appendAscii( ": " );
154cdf0e10cSrcweir         }
155cdf0e10cSrcweir 
156cdf0e10cSrcweir         aLogEntry.append( _rRecord.Message );
157cdf0e10cSrcweir         aLogEntry.appendAscii( "\n" );
158cdf0e10cSrcweir 
159cdf0e10cSrcweir         return aLogEntry.makeStringAndClear();
160cdf0e10cSrcweir     }
161cdf0e10cSrcweir 
162cdf0e10cSrcweir     //--------------------------------------------------------------------
getTail()163cdf0e10cSrcweir     ::rtl::OUString SAL_CALL PlainTextFormatter::getTail(  ) throw (RuntimeException)
164cdf0e10cSrcweir     {
165cdf0e10cSrcweir         // no tail
166cdf0e10cSrcweir         return ::rtl::OUString();
167cdf0e10cSrcweir     }
168cdf0e10cSrcweir 
169cdf0e10cSrcweir     //--------------------------------------------------------------------
supportsService(const::rtl::OUString & _rServiceName)170cdf0e10cSrcweir     ::sal_Bool SAL_CALL PlainTextFormatter::supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException)
171cdf0e10cSrcweir     {
172cdf0e10cSrcweir         const Sequence< ::rtl::OUString > aServiceNames( getSupportedServiceNames() );
173cdf0e10cSrcweir         for (   const ::rtl::OUString* pServiceNames = aServiceNames.getConstArray();
174cdf0e10cSrcweir                 pServiceNames != aServiceNames.getConstArray() + aServiceNames.getLength();
175cdf0e10cSrcweir                 ++pServiceNames
176cdf0e10cSrcweir             )
177cdf0e10cSrcweir             if ( _rServiceName == *pServiceNames )
178cdf0e10cSrcweir                 return sal_True;
179cdf0e10cSrcweir         return sal_False;
180cdf0e10cSrcweir     }
181cdf0e10cSrcweir 
182cdf0e10cSrcweir     //--------------------------------------------------------------------
getImplementationName()183cdf0e10cSrcweir     ::rtl::OUString SAL_CALL PlainTextFormatter::getImplementationName() throw(RuntimeException)
184cdf0e10cSrcweir     {
185cdf0e10cSrcweir         return getImplementationName_static();
186cdf0e10cSrcweir     }
187cdf0e10cSrcweir 
188cdf0e10cSrcweir     //--------------------------------------------------------------------
getSupportedServiceNames()189cdf0e10cSrcweir     Sequence< ::rtl::OUString > SAL_CALL PlainTextFormatter::getSupportedServiceNames() throw(RuntimeException)
190cdf0e10cSrcweir     {
191cdf0e10cSrcweir         return getSupportedServiceNames_static();
192cdf0e10cSrcweir     }
193cdf0e10cSrcweir 
194cdf0e10cSrcweir     //--------------------------------------------------------------------
getImplementationName_static()195cdf0e10cSrcweir     ::rtl::OUString SAL_CALL PlainTextFormatter::getImplementationName_static()
196cdf0e10cSrcweir     {
197cdf0e10cSrcweir         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.extensions.PlainTextFormatter" ) );
198cdf0e10cSrcweir     }
199cdf0e10cSrcweir 
200cdf0e10cSrcweir     //--------------------------------------------------------------------
getSupportedServiceNames_static()201cdf0e10cSrcweir     Sequence< ::rtl::OUString > SAL_CALL PlainTextFormatter::getSupportedServiceNames_static()
202cdf0e10cSrcweir     {
203cdf0e10cSrcweir         Sequence< ::rtl::OUString > aServiceNames(1);
204cdf0e10cSrcweir         aServiceNames[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.logging.PlainTextFormatter" ) );
205cdf0e10cSrcweir         return aServiceNames;
206cdf0e10cSrcweir     }
207cdf0e10cSrcweir 
208cdf0e10cSrcweir     //--------------------------------------------------------------------
Create(const Reference<XComponentContext> & _rxContext)209cdf0e10cSrcweir     Reference< XInterface > PlainTextFormatter::Create( const Reference< XComponentContext >& _rxContext )
210cdf0e10cSrcweir     {
211cdf0e10cSrcweir         return *( new PlainTextFormatter( _rxContext ) );
212cdf0e10cSrcweir     }
213cdf0e10cSrcweir 
214cdf0e10cSrcweir     //--------------------------------------------------------------------
createRegistryInfo_PlainTextFormatter()215cdf0e10cSrcweir     void createRegistryInfo_PlainTextFormatter()
216cdf0e10cSrcweir     {
217cdf0e10cSrcweir         static OAutoRegistration< PlainTextFormatter > aAutoRegistration;
218cdf0e10cSrcweir     }
219cdf0e10cSrcweir 
220cdf0e10cSrcweir //........................................................................
221cdf0e10cSrcweir } // namespace logging
222cdf0e10cSrcweir //........................................................................
223