LibreOffice Module extensions (master) 1
plaintextformatter.cxx
Go to the documentation of this file.
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#include <sal/config.h>
21
22#include <com/sun/star/logging/XLogFormatter.hpp>
23#include <com/sun/star/uno/XComponentContext.hpp>
24#include <com/sun/star/lang/XServiceInfo.hpp>
25
28
29#include <rtl/ustrbuf.hxx>
30#include <osl/thread.h>
31
32#include <stdio.h>
33
34namespace logging
35{
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;
40
41 namespace {
42
43 class PlainTextFormatter : public cppu::WeakImplHelper<css::logging::XLogFormatter, css::lang::XServiceInfo>
44 {
45 public:
46 PlainTextFormatter();
47
48 private:
49 // XLogFormatter
50 virtual OUString SAL_CALL getHead( ) override;
51 virtual OUString SAL_CALL format( const LogRecord& Record ) override;
52 virtual OUString SAL_CALL getTail( ) override;
53
54 // XServiceInfo
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;
58 };
59
60 }
61
62 PlainTextFormatter::PlainTextFormatter()
63 {
64 }
65
66 OUString SAL_CALL PlainTextFormatter::getHead( )
67 {
68 return
69 " event no" // column 1: the event number
70 " "
71 "thread " // column 2: the thread ID
72 " "
73 "date " // column 3: date
74 " "
75 "time " // column 4: time
76 " "
77 "(class/method:) message" // column 5: class/method/message
78 "\n";
79 }
80
81
82 OUString SAL_CALL PlainTextFormatter::format( const LogRecord& _rRecord )
83 {
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;
90
91 OUStringBuffer aLogEntry;
92 aLogEntry.appendAscii( buffer );
93 aLogEntry.append( " " );
94
95 OString sThreadID( OUStringToOString( _rRecord.ThreadID, osl_getThreadTextEncoding() ) );
96 snprintf( buffer, buffer_size, "%8s", sThreadID.getStr() );
97 aLogEntry.appendAscii( buffer );
98 aLogEntry.append( " " );
99
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( " " );
105
106 if ( !(_rRecord.SourceClassName.isEmpty() || _rRecord.SourceMethodName.isEmpty()) )
107 {
108 aLogEntry.append( _rRecord.SourceClassName + "::"
109 + _rRecord.SourceMethodName + ": " );
110 }
111
112 aLogEntry.append( _rRecord.Message + "\n" );
113
114 return aLogEntry.makeStringAndClear();
115 }
116
117
118 OUString SAL_CALL PlainTextFormatter::getTail( )
119 {
120 // no tail
121 return OUString();
122 }
123
124 sal_Bool SAL_CALL PlainTextFormatter::supportsService( const OUString& _rServiceName )
125 {
126 return cppu::supportsService(this, _rServiceName);
127 }
128
129
130 OUString SAL_CALL PlainTextFormatter::getImplementationName()
131 {
132 return "com.sun.star.comp.extensions.PlainTextFormatter";
133 }
134
135 Sequence< OUString > SAL_CALL PlainTextFormatter::getSupportedServiceNames()
136 {
137 return { "com.sun.star.logging.PlainTextFormatter" };
138 }
139
140} // namespace logging
141
142extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
144 css::uno::XComponentContext *,
145 css::uno::Sequence<css::uno::Any> const &)
146{
147 return cppu::acquire(new logging::PlainTextFormatter());
148}
149
150
151/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_extensions_PlainTextFormatter(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
unsigned char sal_Bool