Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #include "log_module.hxx"
22 :
23 : #include <stdio.h>
24 :
25 : #include <com/sun/star/logging/XLogFormatter.hpp>
26 : #include <com/sun/star/uno/XComponentContext.hpp>
27 : #include <com/sun/star/lang/XServiceInfo.hpp>
28 :
29 : #include <comphelper/componentcontext.hxx>
30 :
31 : #include <cppuhelper/implbase2.hxx>
32 :
33 : #include <rtl/ustrbuf.hxx>
34 :
35 : #include <osl/thread.h>
36 :
37 : //........................................................................
38 : namespace logging
39 : {
40 : //........................................................................
41 :
42 : /** === begin UNO using === **/
43 : using ::com::sun::star::logging::XLogFormatter;
44 : using ::com::sun::star::uno::XComponentContext;
45 : using ::com::sun::star::uno::Reference;
46 : using ::com::sun::star::uno::Sequence;
47 : using ::com::sun::star::lang::XServiceInfo;
48 : using ::com::sun::star::uno::RuntimeException;
49 : using ::com::sun::star::logging::LogRecord;
50 : using ::com::sun::star::uno::XInterface;
51 : /** === end UNO using === **/
52 :
53 : //====================================================================
54 : //= PlainTextFormatter - declaration
55 : //====================================================================
56 : typedef ::cppu::WeakImplHelper2 < XLogFormatter
57 : , XServiceInfo
58 : > PlainTextFormatter_Base;
59 : class PlainTextFormatter : public PlainTextFormatter_Base
60 : {
61 : private:
62 : ::comphelper::ComponentContext m_aContext;
63 :
64 : protected:
65 : PlainTextFormatter( const Reference< XComponentContext >& _rxContext );
66 : virtual ~PlainTextFormatter();
67 :
68 : // XLogFormatter
69 : virtual ::rtl::OUString SAL_CALL getHead( ) throw (RuntimeException);
70 : virtual ::rtl::OUString SAL_CALL format( const LogRecord& Record ) throw (RuntimeException);
71 : virtual ::rtl::OUString SAL_CALL getTail( ) throw (RuntimeException);
72 :
73 : // XServiceInfo
74 : virtual ::rtl::OUString SAL_CALL getImplementationName() throw(RuntimeException);
75 : virtual ::sal_Bool SAL_CALL supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException);
76 : virtual Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames() throw(RuntimeException);
77 :
78 : public:
79 : // XServiceInfo - static version
80 : static ::rtl::OUString SAL_CALL getImplementationName_static();
81 : static Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames_static();
82 : static Reference< XInterface > Create( const Reference< XComponentContext >& _rxContext );
83 : };
84 :
85 : //====================================================================
86 : //= PlainTextFormatter - implementation
87 : //====================================================================
88 : //--------------------------------------------------------------------
89 0 : PlainTextFormatter::PlainTextFormatter( const Reference< XComponentContext >& _rxContext )
90 0 : :m_aContext( _rxContext )
91 : {
92 0 : }
93 :
94 : //--------------------------------------------------------------------
95 0 : PlainTextFormatter::~PlainTextFormatter()
96 : {
97 0 : }
98 :
99 : //--------------------------------------------------------------------
100 0 : ::rtl::OUString SAL_CALL PlainTextFormatter::getHead( ) throw (RuntimeException)
101 : {
102 0 : ::rtl::OUStringBuffer aHeader;
103 0 : aHeader.appendAscii( " event no" ); // column 1: the event number
104 0 : aHeader.appendAscii( " " );
105 0 : aHeader.appendAscii( "thread " ); // column 2: the thread ID
106 0 : aHeader.appendAscii( " " );
107 0 : aHeader.appendAscii( "date " ); // column 3: date
108 0 : aHeader.appendAscii( " " );
109 0 : aHeader.appendAscii( "time " ); // column 4: time
110 0 : aHeader.appendAscii( " " );
111 0 : aHeader.appendAscii( "(class/method:) message" ); // column 5: class/method/message
112 0 : aHeader.appendAscii( "\n" );
113 0 : return aHeader.makeStringAndClear();
114 : }
115 :
116 : //--------------------------------------------------------------------
117 0 : ::rtl::OUString SAL_CALL PlainTextFormatter::format( const LogRecord& _rRecord ) throw (RuntimeException)
118 : {
119 : char buffer[ 30 ];
120 0 : const int buffer_size = sizeof( buffer );
121 0 : int used = snprintf( buffer, buffer_size, "%10i", (int)_rRecord.SequenceNumber );
122 0 : if ( used >= buffer_size || used < 0 )
123 0 : buffer[ buffer_size - 1 ] = 0;
124 :
125 0 : ::rtl::OUStringBuffer aLogEntry;
126 0 : aLogEntry.appendAscii( buffer );
127 0 : aLogEntry.appendAscii( " " );
128 :
129 0 : ::rtl::OString sThreadID( ::rtl::OUStringToOString( _rRecord.ThreadID, osl_getThreadTextEncoding() ) );
130 0 : snprintf( buffer, buffer_size, "%8s", sThreadID.getStr() );
131 0 : aLogEntry.appendAscii( buffer );
132 0 : aLogEntry.appendAscii( " " );
133 :
134 : snprintf( buffer, buffer_size, "%04i-%02i-%02i %02i:%02i:%02i.%02i",
135 : (int)_rRecord.LogTime.Year, (int)_rRecord.LogTime.Month, (int)_rRecord.LogTime.Day,
136 0 : (int)_rRecord.LogTime.Hours, (int)_rRecord.LogTime.Minutes, (int)_rRecord.LogTime.Seconds, (int)_rRecord.LogTime.HundredthSeconds );
137 0 : aLogEntry.appendAscii( buffer );
138 0 : aLogEntry.appendAscii( " " );
139 :
140 0 : if ( !(_rRecord.SourceClassName.isEmpty() || _rRecord.SourceMethodName.isEmpty()) )
141 : {
142 0 : aLogEntry.append( _rRecord.SourceClassName );
143 0 : aLogEntry.appendAscii( "::" );
144 0 : aLogEntry.append( _rRecord.SourceMethodName );
145 0 : aLogEntry.appendAscii( ": " );
146 : }
147 :
148 0 : aLogEntry.append( _rRecord.Message );
149 0 : aLogEntry.appendAscii( "\n" );
150 :
151 0 : return aLogEntry.makeStringAndClear();
152 : }
153 :
154 : //--------------------------------------------------------------------
155 0 : ::rtl::OUString SAL_CALL PlainTextFormatter::getTail( ) throw (RuntimeException)
156 : {
157 : // no tail
158 0 : return ::rtl::OUString();
159 : }
160 :
161 : //--------------------------------------------------------------------
162 0 : ::sal_Bool SAL_CALL PlainTextFormatter::supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException)
163 : {
164 0 : const Sequence< ::rtl::OUString > aServiceNames( getSupportedServiceNames() );
165 0 : for ( const ::rtl::OUString* pServiceNames = aServiceNames.getConstArray();
166 0 : pServiceNames != aServiceNames.getConstArray() + aServiceNames.getLength();
167 : ++pServiceNames
168 : )
169 0 : if ( _rServiceName == *pServiceNames )
170 0 : return sal_True;
171 0 : return sal_False;
172 : }
173 :
174 : //--------------------------------------------------------------------
175 0 : ::rtl::OUString SAL_CALL PlainTextFormatter::getImplementationName() throw(RuntimeException)
176 : {
177 0 : return getImplementationName_static();
178 : }
179 :
180 : //--------------------------------------------------------------------
181 0 : Sequence< ::rtl::OUString > SAL_CALL PlainTextFormatter::getSupportedServiceNames() throw(RuntimeException)
182 : {
183 0 : return getSupportedServiceNames_static();
184 : }
185 :
186 : //--------------------------------------------------------------------
187 0 : ::rtl::OUString SAL_CALL PlainTextFormatter::getImplementationName_static()
188 : {
189 0 : return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.extensions.PlainTextFormatter" ) );
190 : }
191 :
192 : //--------------------------------------------------------------------
193 0 : Sequence< ::rtl::OUString > SAL_CALL PlainTextFormatter::getSupportedServiceNames_static()
194 : {
195 0 : Sequence< ::rtl::OUString > aServiceNames(1);
196 0 : aServiceNames[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.logging.PlainTextFormatter" ) );
197 0 : return aServiceNames;
198 : }
199 :
200 : //--------------------------------------------------------------------
201 0 : Reference< XInterface > PlainTextFormatter::Create( const Reference< XComponentContext >& _rxContext )
202 : {
203 0 : return *( new PlainTextFormatter( _rxContext ) );
204 : }
205 :
206 : //--------------------------------------------------------------------
207 0 : void createRegistryInfo_PlainTextFormatter()
208 : {
209 0 : static OAutoRegistration< PlainTextFormatter > aAutoRegistration;
210 0 : }
211 :
212 : //........................................................................
213 : } // namespace logging
214 : //........................................................................
215 :
216 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|