1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
20 #include <sal/config.h>
22 #include <com/sun/star/logging/XLogFormatter.hpp>
23 #include <com/sun/star/uno/XComponentContext.hpp>
24 #include <com/sun/star/lang/XServiceInfo.hpp>
26 #include <cppuhelper/implbase.hxx>
27 #include <cppuhelper/supportsservice.hxx>
29 #include <rtl/ustrbuf.hxx>
30 #include <osl/thread.h>
36 using ::com::sun::star::uno::XComponentContext
;
37 using ::com::sun::star::uno::Sequence
;
38 using ::com::sun::star::logging::LogRecord
;
39 using ::com::sun::star::uno::XInterface
;
43 class PlainTextFormatter
: public cppu::WeakImplHelper
<css::logging::XLogFormatter
, css::lang::XServiceInfo
>
50 virtual OUString SAL_CALL
getHead( ) override
;
51 virtual OUString SAL_CALL
format( const LogRecord
& Record
) override
;
52 virtual OUString SAL_CALL
getTail( ) override
;
55 virtual OUString SAL_CALL
getImplementationName() override
;
56 virtual sal_Bool SAL_CALL
supportsService( const OUString
& _rServiceName
) override
;
57 virtual Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() override
;
62 PlainTextFormatter::PlainTextFormatter()
66 OUString SAL_CALL
PlainTextFormatter::getHead( )
69 " event no" // column 1: the event number
71 "thread " // column 2: the thread ID
73 "date " // column 3: date
75 "time " // column 4: time
77 "(class/method:) message" // column 5: class/method/message
82 OUString SAL_CALL
PlainTextFormatter::format( const LogRecord
& _rRecord
)
84 char buffer
[ sizeof("-32768-65535-65535 65535:65535:65535.4294967295") ];
85 // reserve enough space for hypothetical max length
86 const int buffer_size
= sizeof( buffer
);
87 int used
= snprintf( buffer
, buffer_size
, "%10i", static_cast<int>(_rRecord
.SequenceNumber
) );
88 if ( used
>= buffer_size
|| used
< 0 )
89 buffer
[ buffer_size
- 1 ] = 0;
91 OUStringBuffer aLogEntry
;
92 aLogEntry
.appendAscii( buffer
);
93 aLogEntry
.append( " " );
95 OString
sThreadID( OUStringToOString( _rRecord
.ThreadID
, osl_getThreadTextEncoding() ) );
96 snprintf( buffer
, buffer_size
, "%8s", sThreadID
.getStr() );
97 aLogEntry
.appendAscii( buffer
);
98 aLogEntry
.append( " " );
100 snprintf( buffer
, buffer_size
, "%04" SAL_PRIdINT32
"-%02" SAL_PRIuUINT32
"-%02" SAL_PRIuUINT32
" %02" SAL_PRIuUINT32
":%02" SAL_PRIuUINT32
":%02" SAL_PRIuUINT32
".%09" SAL_PRIuUINT32
,
101 static_cast<sal_Int32
>(_rRecord
.LogTime
.Year
), static_cast<sal_uInt32
>(_rRecord
.LogTime
.Month
), static_cast<sal_uInt32
>(_rRecord
.LogTime
.Day
),
102 static_cast<sal_uInt32
>(_rRecord
.LogTime
.Hours
), static_cast<sal_uInt32
>(_rRecord
.LogTime
.Minutes
), static_cast<sal_uInt32
>(_rRecord
.LogTime
.Seconds
), _rRecord
.LogTime
.NanoSeconds
);
103 aLogEntry
.appendAscii( buffer
);
104 aLogEntry
.append( " " );
106 if ( !(_rRecord
.SourceClassName
.isEmpty() || _rRecord
.SourceMethodName
.isEmpty()) )
108 aLogEntry
.append( _rRecord
.SourceClassName
+ "::"
109 + _rRecord
.SourceMethodName
+ ": " );
112 aLogEntry
.append( _rRecord
.Message
+ "\n" );
114 return aLogEntry
.makeStringAndClear();
118 OUString SAL_CALL
PlainTextFormatter::getTail( )
124 sal_Bool SAL_CALL
PlainTextFormatter::supportsService( const OUString
& _rServiceName
)
126 return cppu::supportsService(this, _rServiceName
);
130 OUString SAL_CALL
PlainTextFormatter::getImplementationName()
132 return "com.sun.star.comp.extensions.PlainTextFormatter";
135 Sequence
< OUString
> SAL_CALL
PlainTextFormatter::getSupportedServiceNames()
137 return { "com.sun.star.logging.PlainTextFormatter" };
140 } // namespace logging
142 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
143 com_sun_star_comp_extensions_PlainTextFormatter(
144 css::uno::XComponentContext
*,
145 css::uno::Sequence
<css::uno::Any
> const &)
147 return cppu::acquire(new logging::PlainTextFormatter());
151 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */