bump product version to 4.1.6.2
[LibreOffice.git] / extensions / source / logging / plaintextformatter.cxx
blob7ce48752cc85d6b17d57384f43bb770e6e290088
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include "log_module.hxx"
23 #include <stdio.h>
25 #include <com/sun/star/logging/XLogFormatter.hpp>
26 #include <com/sun/star/uno/XComponentContext.hpp>
27 #include <com/sun/star/lang/XServiceInfo.hpp>
29 #include <comphelper/componentcontext.hxx>
31 #include <cppuhelper/implbase2.hxx>
33 #include <rtl/ustrbuf.hxx>
35 #include <osl/thread.h>
37 //........................................................................
38 namespace logging
40 //........................................................................
42 using ::com::sun::star::logging::XLogFormatter;
43 using ::com::sun::star::uno::XComponentContext;
44 using ::com::sun::star::uno::Reference;
45 using ::com::sun::star::uno::Sequence;
46 using ::com::sun::star::lang::XServiceInfo;
47 using ::com::sun::star::uno::RuntimeException;
48 using ::com::sun::star::logging::LogRecord;
49 using ::com::sun::star::uno::XInterface;
51 //====================================================================
52 //= PlainTextFormatter - declaration
53 //====================================================================
54 typedef ::cppu::WeakImplHelper2 < XLogFormatter
55 , XServiceInfo
56 > PlainTextFormatter_Base;
57 class PlainTextFormatter : public PlainTextFormatter_Base
59 private:
60 ::comphelper::ComponentContext m_aContext;
62 protected:
63 PlainTextFormatter( const Reference< XComponentContext >& _rxContext );
64 virtual ~PlainTextFormatter();
66 // XLogFormatter
67 virtual OUString SAL_CALL getHead( ) throw (RuntimeException);
68 virtual OUString SAL_CALL format( const LogRecord& Record ) throw (RuntimeException);
69 virtual OUString SAL_CALL getTail( ) throw (RuntimeException);
71 // XServiceInfo
72 virtual OUString SAL_CALL getImplementationName() throw(RuntimeException);
73 virtual ::sal_Bool SAL_CALL supportsService( const OUString& _rServiceName ) throw(RuntimeException);
74 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() throw(RuntimeException);
76 public:
77 // XServiceInfo - static version
78 static OUString SAL_CALL getImplementationName_static();
79 static Sequence< OUString > SAL_CALL getSupportedServiceNames_static();
80 static Reference< XInterface > Create( const Reference< XComponentContext >& _rxContext );
83 //====================================================================
84 //= PlainTextFormatter - implementation
85 //====================================================================
86 //--------------------------------------------------------------------
87 PlainTextFormatter::PlainTextFormatter( const Reference< XComponentContext >& _rxContext )
88 :m_aContext( _rxContext )
92 //--------------------------------------------------------------------
93 PlainTextFormatter::~PlainTextFormatter()
97 //--------------------------------------------------------------------
98 OUString SAL_CALL PlainTextFormatter::getHead( ) throw (RuntimeException)
100 OUStringBuffer aHeader;
101 aHeader.appendAscii( " event no" ); // column 1: the event number
102 aHeader.appendAscii( " " );
103 aHeader.appendAscii( "thread " ); // column 2: the thread ID
104 aHeader.appendAscii( " " );
105 aHeader.appendAscii( "date " ); // column 3: date
106 aHeader.appendAscii( " " );
107 aHeader.appendAscii( "time " ); // column 4: time
108 aHeader.appendAscii( " " );
109 aHeader.appendAscii( "(class/method:) message" ); // column 5: class/method/message
110 aHeader.appendAscii( "\n" );
111 return aHeader.makeStringAndClear();
114 //--------------------------------------------------------------------
115 OUString SAL_CALL PlainTextFormatter::format( const LogRecord& _rRecord ) throw (RuntimeException)
117 char buffer[ 30 ];
118 const int buffer_size = sizeof( buffer );
119 int used = snprintf( buffer, buffer_size, "%10i", (int)_rRecord.SequenceNumber );
120 if ( used >= buffer_size || used < 0 )
121 buffer[ buffer_size - 1 ] = 0;
123 OUStringBuffer aLogEntry;
124 aLogEntry.appendAscii( buffer );
125 aLogEntry.appendAscii( " " );
127 OString sThreadID( OUStringToOString( _rRecord.ThreadID, osl_getThreadTextEncoding() ) );
128 snprintf( buffer, buffer_size, "%8s", sThreadID.getStr() );
129 aLogEntry.appendAscii( buffer );
130 aLogEntry.appendAscii( " " );
132 snprintf( buffer, buffer_size, "%04i-%02i-%02i %02i:%02i:%02i.%09i",
133 (int)_rRecord.LogTime.Year, (int)_rRecord.LogTime.Month, (int)_rRecord.LogTime.Day,
134 (int)_rRecord.LogTime.Hours, (int)_rRecord.LogTime.Minutes, (int)_rRecord.LogTime.Seconds, (int)_rRecord.LogTime.NanoSeconds );
135 aLogEntry.appendAscii( buffer );
136 aLogEntry.appendAscii( " " );
138 if ( !(_rRecord.SourceClassName.isEmpty() || _rRecord.SourceMethodName.isEmpty()) )
140 aLogEntry.append( _rRecord.SourceClassName );
141 aLogEntry.appendAscii( "::" );
142 aLogEntry.append( _rRecord.SourceMethodName );
143 aLogEntry.appendAscii( ": " );
146 aLogEntry.append( _rRecord.Message );
147 aLogEntry.appendAscii( "\n" );
149 return aLogEntry.makeStringAndClear();
152 //--------------------------------------------------------------------
153 OUString SAL_CALL PlainTextFormatter::getTail( ) throw (RuntimeException)
155 // no tail
156 return OUString();
159 //--------------------------------------------------------------------
160 ::sal_Bool SAL_CALL PlainTextFormatter::supportsService( const OUString& _rServiceName ) throw(RuntimeException)
162 const Sequence< OUString > aServiceNames( getSupportedServiceNames() );
163 for ( const OUString* pServiceNames = aServiceNames.getConstArray();
164 pServiceNames != aServiceNames.getConstArray() + aServiceNames.getLength();
165 ++pServiceNames
167 if ( _rServiceName == *pServiceNames )
168 return sal_True;
169 return sal_False;
172 //--------------------------------------------------------------------
173 OUString SAL_CALL PlainTextFormatter::getImplementationName() throw(RuntimeException)
175 return getImplementationName_static();
178 //--------------------------------------------------------------------
179 Sequence< OUString > SAL_CALL PlainTextFormatter::getSupportedServiceNames() throw(RuntimeException)
181 return getSupportedServiceNames_static();
184 //--------------------------------------------------------------------
185 OUString SAL_CALL PlainTextFormatter::getImplementationName_static()
187 return OUString( "com.sun.star.comp.extensions.PlainTextFormatter" );
190 //--------------------------------------------------------------------
191 Sequence< OUString > SAL_CALL PlainTextFormatter::getSupportedServiceNames_static()
193 Sequence< OUString > aServiceNames(1);
194 aServiceNames[0] = OUString( "com.sun.star.logging.PlainTextFormatter" );
195 return aServiceNames;
198 //--------------------------------------------------------------------
199 Reference< XInterface > PlainTextFormatter::Create( const Reference< XComponentContext >& _rxContext )
201 return *( new PlainTextFormatter( _rxContext ) );
204 //--------------------------------------------------------------------
205 void createRegistryInfo_PlainTextFormatter()
207 static OAutoRegistration< PlainTextFormatter > aAutoRegistration;
210 //........................................................................
211 } // namespace logging
212 //........................................................................
214 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */