1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_extensions.hxx"
30 
31 #include "log_module.hxx"
32 
33 #include <stdio.h>
34 
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 === **/
40 
41 #include <comphelper/componentcontext.hxx>
42 
43 #include <cppuhelper/implbase2.hxx>
44 
45 #include <rtl/ustrbuf.hxx>
46 
47 #include <osl/thread.h>
48 
49 //........................................................................
50 namespace logging
51 {
52 //........................................................................
53 
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 === **/
64 
65 	//====================================================================
66 	//= PlainTextFormatter - declaration
67 	//====================================================================
68     typedef ::cppu::WeakImplHelper2 <   XLogFormatter
69                                     ,   XServiceInfo
70                                     >   PlainTextFormatter_Base;
71     class PlainTextFormatter : public PlainTextFormatter_Base
72 	{
73     private:
74         ::comphelper::ComponentContext  m_aContext;
75 
76     protected:
77         PlainTextFormatter( const Reference< XComponentContext >& _rxContext );
78         virtual ~PlainTextFormatter();
79 
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);
84 
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);
89 
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 );
95 	};
96 
97     //====================================================================
98 	//= PlainTextFormatter - implementation
99 	//====================================================================
100 	//--------------------------------------------------------------------
101     PlainTextFormatter::PlainTextFormatter( const Reference< XComponentContext >& _rxContext )
102         :m_aContext( _rxContext )
103     {
104     }
105 
106     //--------------------------------------------------------------------
107     PlainTextFormatter::~PlainTextFormatter()
108     {
109     }
110 
111     //--------------------------------------------------------------------
112     ::rtl::OUString SAL_CALL PlainTextFormatter::getHead(  ) throw (RuntimeException)
113     {
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();
126     }
127 
128     //--------------------------------------------------------------------
129     ::rtl::OUString SAL_CALL PlainTextFormatter::format( const LogRecord& _rRecord ) throw (RuntimeException)
130     {
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;
136 
137         ::rtl::OUStringBuffer aLogEntry;
138         aLogEntry.appendAscii( buffer );
139         aLogEntry.appendAscii( " " );
140 
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( " " );
145 
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( " " );
151 
152         if ( _rRecord.SourceClassName.getLength() && _rRecord.SourceMethodName.getLength() )
153         {
154             aLogEntry.append( _rRecord.SourceClassName );
155             aLogEntry.appendAscii( "::" );
156             aLogEntry.append( _rRecord.SourceMethodName );
157             aLogEntry.appendAscii( ": " );
158         }
159 
160         aLogEntry.append( _rRecord.Message );
161         aLogEntry.appendAscii( "\n" );
162 
163         return aLogEntry.makeStringAndClear();
164     }
165 
166     //--------------------------------------------------------------------
167     ::rtl::OUString SAL_CALL PlainTextFormatter::getTail(  ) throw (RuntimeException)
168     {
169         // no tail
170         return ::rtl::OUString();
171     }
172 
173     //--------------------------------------------------------------------
174     ::sal_Bool SAL_CALL PlainTextFormatter::supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException)
175     {
176         const Sequence< ::rtl::OUString > aServiceNames( getSupportedServiceNames() );
177         for (   const ::rtl::OUString* pServiceNames = aServiceNames.getConstArray();
178                 pServiceNames != aServiceNames.getConstArray() + aServiceNames.getLength();
179                 ++pServiceNames
180             )
181             if ( _rServiceName == *pServiceNames )
182                 return sal_True;
183         return sal_False;
184     }
185 
186     //--------------------------------------------------------------------
187     ::rtl::OUString SAL_CALL PlainTextFormatter::getImplementationName() throw(RuntimeException)
188     {
189         return getImplementationName_static();
190     }
191 
192     //--------------------------------------------------------------------
193     Sequence< ::rtl::OUString > SAL_CALL PlainTextFormatter::getSupportedServiceNames() throw(RuntimeException)
194     {
195         return getSupportedServiceNames_static();
196     }
197 
198     //--------------------------------------------------------------------
199     ::rtl::OUString SAL_CALL PlainTextFormatter::getImplementationName_static()
200     {
201         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.extensions.PlainTextFormatter" ) );
202     }
203 
204     //--------------------------------------------------------------------
205     Sequence< ::rtl::OUString > SAL_CALL PlainTextFormatter::getSupportedServiceNames_static()
206     {
207         Sequence< ::rtl::OUString > aServiceNames(1);
208         aServiceNames[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.logging.PlainTextFormatter" ) );
209         return aServiceNames;
210     }
211 
212     //--------------------------------------------------------------------
213     Reference< XInterface > PlainTextFormatter::Create( const Reference< XComponentContext >& _rxContext )
214     {
215         return *( new PlainTextFormatter( _rxContext ) );
216     }
217 
218     //--------------------------------------------------------------------
219     void createRegistryInfo_PlainTextFormatter()
220     {
221         static OAutoRegistration< PlainTextFormatter > aAutoRegistration;
222     }
223 
224 //........................................................................
225 } // namespace logging
226 //........................................................................
227