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