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