LibreOffice Module extensions (master) 1
csvformatter.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/XCsvLogFormatter.hpp>
23#include <com/sun/star/uno/XComponentContext.hpp>
24#include <com/sun/star/lang/XServiceInfo.hpp>
25#include <com/sun/star/lang/IllegalArgumentException.hpp>
26
29
30#include <rtl/ustrbuf.hxx>
31#include <sal/macros.h>
32#include <sal/types.h>
33
34#include <stdio.h>
35#include <string_view>
36
37namespace logging
38{
39 using ::com::sun::star::uno::Sequence;
40 using ::com::sun::star::logging::LogRecord;
41
42 namespace {
43
44 // formats for csv files as defined by RFC4180
45 class CsvFormatter : public cppu::WeakImplHelper<css::logging::XCsvLogFormatter, css::lang::XServiceInfo>
46 {
47 public:
48 virtual OUString SAL_CALL formatMultiColumn(const Sequence< OUString>& column_data) override;
49
50 CsvFormatter();
51
52 private:
53 // XCsvLogFormatter
54 virtual sal_Bool SAL_CALL getLogEventNo() override;
55 virtual sal_Bool SAL_CALL getLogThread() override;
56 virtual sal_Bool SAL_CALL getLogTimestamp() override;
57 virtual sal_Bool SAL_CALL getLogSource() override;
58 virtual Sequence< OUString > SAL_CALL getColumnnames() override;
59
60 virtual void SAL_CALL setLogEventNo( sal_Bool log_event_no ) override;
61 virtual void SAL_CALL setLogThread( sal_Bool log_thread ) override;
62 virtual void SAL_CALL setLogTimestamp( sal_Bool log_timestamp ) override;
63 virtual void SAL_CALL setLogSource( sal_Bool log_source ) override;
64 virtual void SAL_CALL setColumnnames( const Sequence< OUString>& column_names) override;
65
66 // XLogFormatter
67 virtual OUString SAL_CALL getHead( ) override;
68 virtual OUString SAL_CALL format( const LogRecord& Record ) override;
69 virtual OUString SAL_CALL getTail( ) override;
70
71 // XServiceInfo
72 virtual OUString SAL_CALL getImplementationName() override;
73 virtual sal_Bool SAL_CALL supportsService( const OUString& service_name ) override;
74 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
75
76 private:
82 css::uno::Sequence< OUString > m_Columnnames;
83 };
84
85 }
86} // namespace logging
87
88// private helpers
89namespace
90{
91 const sal_Unicode quote_char = '"';
92 const sal_Unicode comma_char = ',';
93 constexpr OUStringLiteral dos_newline = u"\r\n";
94
95 bool needsQuoting(std::u16string_view str)
96 {
97 return str.find_first_of(u"\",\n\r") != std::u16string_view::npos;
98 };
99
100 void appendEncodedString(OUStringBuffer& buf, std::u16string_view str)
101 {
102 if(needsQuoting(str))
103 {
104 // each double-quote will get replaced by two double-quotes
105 buf.append(quote_char);
106 const sal_Int32 buf_offset = buf.getLength();
107 const size_t str_length = str.size();
108 buf.append(str);
109 // special treatment for the last character
110 if(quote_char==str[str_length-1])
111 buf.append(quote_char);
112 // iterating backwards because the index at which we insert won't be shifted
113 // when moving that way.
114 for(size_t i = str_length;; )
115 {
116 --i;
117 i=str.substr(i).rfind(quote_char);
118 if(i==std::u16string_view::npos)
119 break;
120 buf.insert(buf_offset + i, quote_char);
121 }
122 buf.append(quote_char);
123 }
124 else
125 buf.append(str);
126 };
127}
128
129namespace logging
130{
131 CsvFormatter::CsvFormatter()
132 :m_LogEventNo(true),
133 m_LogThread(true),
134 m_LogTimestamp(true),
135 m_LogSource(false),
136 m_MultiColumn(false),
137 m_Columnnames({ "message" })
138 { }
139
140 sal_Bool CsvFormatter::getLogEventNo()
141 {
142 return m_LogEventNo;
143 }
144
145 sal_Bool CsvFormatter::getLogThread()
146 {
147 return m_LogThread;
148 }
149
150 sal_Bool CsvFormatter::getLogTimestamp()
151 {
152 return m_LogTimestamp;
153 }
154
155 sal_Bool CsvFormatter::getLogSource()
156 {
157 return m_LogSource;
158 }
159
160 Sequence< OUString > CsvFormatter::getColumnnames()
161 {
162 return m_Columnnames;
163 }
164
165 void CsvFormatter::setLogEventNo(sal_Bool log_event_no)
166 {
167 m_LogEventNo = log_event_no;
168 }
169
170 void CsvFormatter::setLogThread(sal_Bool log_thread)
171 {
172 m_LogThread = log_thread;
173 }
174
175 void CsvFormatter::setLogTimestamp(sal_Bool log_timestamp)
176 {
177 m_LogTimestamp = log_timestamp;
178 }
179
180 void CsvFormatter::setLogSource(sal_Bool log_source)
181 {
182 m_LogSource = log_source;
183 }
184
185 void CsvFormatter::setColumnnames(const Sequence< OUString >& columnnames)
186 {
187 m_Columnnames = columnnames;
188 m_MultiColumn = (m_Columnnames.getLength()>1);
189 }
190
191 OUString SAL_CALL CsvFormatter::getHead( )
192 {
193 OUStringBuffer buf;
194 if(m_LogEventNo)
195 buf.append("event no,");
196 if(m_LogThread)
197 buf.append("thread,");
199 buf.append("timestamp,");
200 if(m_LogSource)
201 buf.append("class,method,");
202 sal_Int32 columns = m_Columnnames.getLength();
203 for(sal_Int32 i=0; i<columns; i++)
204 {
205 buf.append(m_Columnnames[i] + OUStringChar(comma_char));
206 }
207 buf.setLength(buf.getLength()-1);
208 buf.append(dos_newline);
209 return buf.makeStringAndClear();
210 }
211
212 OUString SAL_CALL CsvFormatter::format( const LogRecord& record )
213 {
214 OUStringBuffer aLogEntry;
215
216 if(m_LogEventNo)
217 {
218 aLogEntry.append(record.SequenceNumber + comma_char);
219 }
220
221 if(m_LogThread)
222 {
223 aLogEntry.append(record.ThreadID + OUStringChar(comma_char));
224 }
225
227 {
228 if ( record.LogTime.Year < -9999 || 9999 < record.LogTime.Year
229 || record.LogTime.Month < 1 || 12 < record.LogTime.Month
230 || record.LogTime.Day < 1 || 31 < record.LogTime.Day
231 || 24 < record.LogTime.Hours
232 || 60 < record.LogTime.Minutes
233 || 60 < record.LogTime.Seconds
234 || 999999999 < record.LogTime.NanoSeconds)
235 {
236 throw css::lang::IllegalArgumentException("invalid date", static_cast<cppu::OWeakObject*>(this), 1);
237 }
238
239 // ISO 8601
240 char buffer[ SAL_N_ELEMENTS("-32768-65535-65535T65535:65535:65535.4294967295") ];
241 const size_t buffer_size = sizeof( buffer );
242 snprintf( buffer, buffer_size, "%04i-%02u-%02uT%02u:%02u:%02u.%09" SAL_PRIuUINT32,
243 static_cast<int>(record.LogTime.Year),
244 static_cast<unsigned int>(record.LogTime.Month),
245 static_cast<unsigned int>(record.LogTime.Day),
246 static_cast<unsigned int>(record.LogTime.Hours),
247 static_cast<unsigned int>(record.LogTime.Minutes),
248 static_cast<unsigned int>(record.LogTime.Seconds),
249 record.LogTime.NanoSeconds );
250 aLogEntry.appendAscii( buffer );
251 aLogEntry.append(comma_char);
252 }
253
254 if(m_LogSource)
255 {
256 appendEncodedString(aLogEntry, record.SourceClassName);
257 aLogEntry.append(comma_char);
258
259 appendEncodedString(aLogEntry, record.SourceMethodName);
260 aLogEntry.append(comma_char);
261 }
262
263 // if the CsvFormatter has multiple columns set via setColumnnames(), the
264 // message of the record is expected to be encoded with formatMultiColumn
265 // if the CsvFormatter has only one column set, the message is expected not
266 // to be encoded
267 if(m_MultiColumn)
268 aLogEntry.append(record.Message);
269 else
270 appendEncodedString(aLogEntry, record.Message);
271
272 aLogEntry.append( dos_newline );
273 return aLogEntry.makeStringAndClear();
274 }
275
276 OUString SAL_CALL CsvFormatter::getTail( )
277 {
278 return OUString();
279 }
280
281 OUString SAL_CALL CsvFormatter::formatMultiColumn(const Sequence< OUString>& column_data)
282 {
283 sal_Int32 columns = column_data.getLength();
284 OUStringBuffer buf;
285 for(int i=0; i<columns; i++)
286 {
287 appendEncodedString(buf, column_data[i]);
288 buf.append(comma_char);
289 }
290 buf.setLength(buf.getLength()-1);
291 return buf.makeStringAndClear();
292 }
293
294 sal_Bool SAL_CALL CsvFormatter::supportsService( const OUString& service_name )
295 {
296 return cppu::supportsService(this, service_name);
297 }
298
299 OUString SAL_CALL CsvFormatter::getImplementationName()
300 {
301 return "com.sun.star.comp.extensions.CsvFormatter";
302 }
303
304 Sequence< OUString > SAL_CALL CsvFormatter::getSupportedServiceNames()
305 {
306 return { "com.sun.star.logging.CsvFormatter" };
307 }
308
309} // namespace logging
310
311extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
313 css::uno::XComponentContext *,
314 css::uno::Sequence<css::uno::Any> const &)
315{
316 return cppu::acquire(new logging::CsvFormatter());
317}
318
319/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool m_LogEventNo
bool m_MultiColumn
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_extensions_CsvFormatter(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
bool m_LogThread
css::uno::Sequence< OUString > m_Columnnames
bool m_LogTimestamp
bool m_LogSource
float u
#define SAL_N_ELEMENTS(arr)
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
int i
unsigned char sal_Bool
sal_uInt16 sal_Unicode