1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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"
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 //........................................................................
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
73 > PlainTextFormatter_Base
;
74 class PlainTextFormatter
: public PlainTextFormatter_Base
77 ::comphelper::ComponentContext m_aContext
;
80 PlainTextFormatter( const Reference
< XComponentContext
>& _rxContext
);
81 virtual ~PlainTextFormatter();
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
);
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
);
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
)
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
)
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();
184 if ( _rServiceName
== *pServiceNames
)
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 //........................................................................