xref: /AOO41X/main/extensions/source/logging/plaintextformatter.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_extensions.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "log_module.hxx"
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include <stdio.h>
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir /** === begin UNO includes === **/
36*cdf0e10cSrcweir #include <com/sun/star/logging/XLogFormatter.hpp>
37*cdf0e10cSrcweir #include <com/sun/star/uno/XComponentContext.hpp>
38*cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
39*cdf0e10cSrcweir /** === end UNO includes === **/
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir #include <comphelper/componentcontext.hxx>
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir #include <cppuhelper/implbase2.hxx>
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir #include <osl/thread.h>
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir //........................................................................
50*cdf0e10cSrcweir namespace logging
51*cdf0e10cSrcweir {
52*cdf0e10cSrcweir //........................................................................
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir 	/** === begin UNO using === **/
55*cdf0e10cSrcweir     using ::com::sun::star::logging::XLogFormatter;
56*cdf0e10cSrcweir     using ::com::sun::star::uno::XComponentContext;
57*cdf0e10cSrcweir     using ::com::sun::star::uno::Reference;
58*cdf0e10cSrcweir     using ::com::sun::star::uno::Sequence;
59*cdf0e10cSrcweir     using ::com::sun::star::lang::XServiceInfo;
60*cdf0e10cSrcweir     using ::com::sun::star::uno::RuntimeException;
61*cdf0e10cSrcweir     using ::com::sun::star::logging::LogRecord;
62*cdf0e10cSrcweir     using ::com::sun::star::uno::XInterface;
63*cdf0e10cSrcweir 	/** === end UNO using === **/
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir 	//====================================================================
66*cdf0e10cSrcweir 	//= PlainTextFormatter - declaration
67*cdf0e10cSrcweir 	//====================================================================
68*cdf0e10cSrcweir     typedef ::cppu::WeakImplHelper2 <   XLogFormatter
69*cdf0e10cSrcweir                                     ,   XServiceInfo
70*cdf0e10cSrcweir                                     >   PlainTextFormatter_Base;
71*cdf0e10cSrcweir     class PlainTextFormatter : public PlainTextFormatter_Base
72*cdf0e10cSrcweir 	{
73*cdf0e10cSrcweir     private:
74*cdf0e10cSrcweir         ::comphelper::ComponentContext  m_aContext;
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir     protected:
77*cdf0e10cSrcweir         PlainTextFormatter( const Reference< XComponentContext >& _rxContext );
78*cdf0e10cSrcweir         virtual ~PlainTextFormatter();
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir         // XLogFormatter
81*cdf0e10cSrcweir         virtual ::rtl::OUString SAL_CALL getHead(  ) throw (RuntimeException);
82*cdf0e10cSrcweir         virtual ::rtl::OUString SAL_CALL format( const LogRecord& Record ) throw (RuntimeException);
83*cdf0e10cSrcweir         virtual ::rtl::OUString SAL_CALL getTail(  ) throw (RuntimeException);
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir         // XServiceInfo
86*cdf0e10cSrcweir 		virtual ::rtl::OUString SAL_CALL getImplementationName() throw(RuntimeException);
87*cdf0e10cSrcweir         virtual ::sal_Bool SAL_CALL supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException);
88*cdf0e10cSrcweir         virtual Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames() throw(RuntimeException);
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir     public:
91*cdf0e10cSrcweir         // XServiceInfo - static version
92*cdf0e10cSrcweir 		static ::rtl::OUString SAL_CALL getImplementationName_static();
93*cdf0e10cSrcweir         static Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames_static();
94*cdf0e10cSrcweir         static Reference< XInterface > Create( const Reference< XComponentContext >& _rxContext );
95*cdf0e10cSrcweir 	};
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir     //====================================================================
98*cdf0e10cSrcweir 	//= PlainTextFormatter - implementation
99*cdf0e10cSrcweir 	//====================================================================
100*cdf0e10cSrcweir 	//--------------------------------------------------------------------
101*cdf0e10cSrcweir     PlainTextFormatter::PlainTextFormatter( const Reference< XComponentContext >& _rxContext )
102*cdf0e10cSrcweir         :m_aContext( _rxContext )
103*cdf0e10cSrcweir     {
104*cdf0e10cSrcweir     }
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir     //--------------------------------------------------------------------
107*cdf0e10cSrcweir     PlainTextFormatter::~PlainTextFormatter()
108*cdf0e10cSrcweir     {
109*cdf0e10cSrcweir     }
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir     //--------------------------------------------------------------------
112*cdf0e10cSrcweir     ::rtl::OUString SAL_CALL PlainTextFormatter::getHead(  ) throw (RuntimeException)
113*cdf0e10cSrcweir     {
114*cdf0e10cSrcweir         ::rtl::OUStringBuffer aHeader;
115*cdf0e10cSrcweir         aHeader.appendAscii( "  event no" );                 // column 1: the event number
116*cdf0e10cSrcweir         aHeader.appendAscii( " " );
117*cdf0e10cSrcweir         aHeader.appendAscii( "thread  " );                   // column 2: the thread ID
118*cdf0e10cSrcweir         aHeader.appendAscii( " " );
119*cdf0e10cSrcweir         aHeader.appendAscii( "date      " );                 // column 3: date
120*cdf0e10cSrcweir         aHeader.appendAscii( " " );
121*cdf0e10cSrcweir         aHeader.appendAscii( "time       " );         // column 4: time
122*cdf0e10cSrcweir         aHeader.appendAscii( " " );
123*cdf0e10cSrcweir         aHeader.appendAscii( "(class/method:) message" );    // column 5: class/method/message
124*cdf0e10cSrcweir         aHeader.appendAscii( "\n" );
125*cdf0e10cSrcweir         return aHeader.makeStringAndClear();
126*cdf0e10cSrcweir     }
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir     //--------------------------------------------------------------------
129*cdf0e10cSrcweir     ::rtl::OUString SAL_CALL PlainTextFormatter::format( const LogRecord& _rRecord ) throw (RuntimeException)
130*cdf0e10cSrcweir     {
131*cdf0e10cSrcweir         char buffer[ 30 ];
132*cdf0e10cSrcweir         const int buffer_size = sizeof( buffer );
133*cdf0e10cSrcweir         int used = snprintf( buffer, buffer_size, "%10i", (int)_rRecord.SequenceNumber );
134*cdf0e10cSrcweir         if ( used >= buffer_size || used < 0 )
135*cdf0e10cSrcweir             buffer[ buffer_size - 1 ] = 0;
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir         ::rtl::OUStringBuffer aLogEntry;
138*cdf0e10cSrcweir         aLogEntry.appendAscii( buffer );
139*cdf0e10cSrcweir         aLogEntry.appendAscii( " " );
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir         ::rtl::OString sThreadID( ::rtl::OUStringToOString( _rRecord.ThreadID, osl_getThreadTextEncoding() ) );
142*cdf0e10cSrcweir         snprintf( buffer, buffer_size, "%8s", sThreadID.getStr() );
143*cdf0e10cSrcweir         aLogEntry.appendAscii( buffer );
144*cdf0e10cSrcweir         aLogEntry.appendAscii( " " );
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir         snprintf( buffer, buffer_size, "%04i-%02i-%02i %02i:%02i:%02i.%02i",
147*cdf0e10cSrcweir             (int)_rRecord.LogTime.Year, (int)_rRecord.LogTime.Month, (int)_rRecord.LogTime.Day,
148*cdf0e10cSrcweir             (int)_rRecord.LogTime.Hours, (int)_rRecord.LogTime.Minutes, (int)_rRecord.LogTime.Seconds, (int)_rRecord.LogTime.HundredthSeconds );
149*cdf0e10cSrcweir         aLogEntry.appendAscii( buffer );
150*cdf0e10cSrcweir         aLogEntry.appendAscii( " " );
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir         if ( _rRecord.SourceClassName.getLength() && _rRecord.SourceMethodName.getLength() )
153*cdf0e10cSrcweir         {
154*cdf0e10cSrcweir             aLogEntry.append( _rRecord.SourceClassName );
155*cdf0e10cSrcweir             aLogEntry.appendAscii( "::" );
156*cdf0e10cSrcweir             aLogEntry.append( _rRecord.SourceMethodName );
157*cdf0e10cSrcweir             aLogEntry.appendAscii( ": " );
158*cdf0e10cSrcweir         }
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir         aLogEntry.append( _rRecord.Message );
161*cdf0e10cSrcweir         aLogEntry.appendAscii( "\n" );
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir         return aLogEntry.makeStringAndClear();
164*cdf0e10cSrcweir     }
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir     //--------------------------------------------------------------------
167*cdf0e10cSrcweir     ::rtl::OUString SAL_CALL PlainTextFormatter::getTail(  ) throw (RuntimeException)
168*cdf0e10cSrcweir     {
169*cdf0e10cSrcweir         // no tail
170*cdf0e10cSrcweir         return ::rtl::OUString();
171*cdf0e10cSrcweir     }
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir     //--------------------------------------------------------------------
174*cdf0e10cSrcweir     ::sal_Bool SAL_CALL PlainTextFormatter::supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException)
175*cdf0e10cSrcweir     {
176*cdf0e10cSrcweir         const Sequence< ::rtl::OUString > aServiceNames( getSupportedServiceNames() );
177*cdf0e10cSrcweir         for (   const ::rtl::OUString* pServiceNames = aServiceNames.getConstArray();
178*cdf0e10cSrcweir                 pServiceNames != aServiceNames.getConstArray() + aServiceNames.getLength();
179*cdf0e10cSrcweir                 ++pServiceNames
180*cdf0e10cSrcweir             )
181*cdf0e10cSrcweir             if ( _rServiceName == *pServiceNames )
182*cdf0e10cSrcweir                 return sal_True;
183*cdf0e10cSrcweir         return sal_False;
184*cdf0e10cSrcweir     }
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir     //--------------------------------------------------------------------
187*cdf0e10cSrcweir     ::rtl::OUString SAL_CALL PlainTextFormatter::getImplementationName() throw(RuntimeException)
188*cdf0e10cSrcweir     {
189*cdf0e10cSrcweir         return getImplementationName_static();
190*cdf0e10cSrcweir     }
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir     //--------------------------------------------------------------------
193*cdf0e10cSrcweir     Sequence< ::rtl::OUString > SAL_CALL PlainTextFormatter::getSupportedServiceNames() throw(RuntimeException)
194*cdf0e10cSrcweir     {
195*cdf0e10cSrcweir         return getSupportedServiceNames_static();
196*cdf0e10cSrcweir     }
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir     //--------------------------------------------------------------------
199*cdf0e10cSrcweir     ::rtl::OUString SAL_CALL PlainTextFormatter::getImplementationName_static()
200*cdf0e10cSrcweir     {
201*cdf0e10cSrcweir         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.extensions.PlainTextFormatter" ) );
202*cdf0e10cSrcweir     }
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir     //--------------------------------------------------------------------
205*cdf0e10cSrcweir     Sequence< ::rtl::OUString > SAL_CALL PlainTextFormatter::getSupportedServiceNames_static()
206*cdf0e10cSrcweir     {
207*cdf0e10cSrcweir         Sequence< ::rtl::OUString > aServiceNames(1);
208*cdf0e10cSrcweir         aServiceNames[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.logging.PlainTextFormatter" ) );
209*cdf0e10cSrcweir         return aServiceNames;
210*cdf0e10cSrcweir     }
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir     //--------------------------------------------------------------------
213*cdf0e10cSrcweir     Reference< XInterface > PlainTextFormatter::Create( const Reference< XComponentContext >& _rxContext )
214*cdf0e10cSrcweir     {
215*cdf0e10cSrcweir         return *( new PlainTextFormatter( _rxContext ) );
216*cdf0e10cSrcweir     }
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir     //--------------------------------------------------------------------
219*cdf0e10cSrcweir     void createRegistryInfo_PlainTextFormatter()
220*cdf0e10cSrcweir     {
221*cdf0e10cSrcweir         static OAutoRegistration< PlainTextFormatter > aAutoRegistration;
222*cdf0e10cSrcweir     }
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir //........................................................................
225*cdf0e10cSrcweir } // namespace logging
226*cdf0e10cSrcweir //........................................................................
227