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/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>
27 #include <cppuhelper/implbase.hxx>
28 #include <cppuhelper/supportsservice.hxx>
30 #include <rtl/ustrbuf.hxx>
31 #include <sal/macros.h>
32 #include <sal/types.h>
35 #include <string_view>
39 using ::com::sun::star::uno::Sequence
;
40 using ::com::sun::star::logging::LogRecord
;
44 // formats for csv files as defined by RFC4180
45 class CsvFormatter
: public cppu::WeakImplHelper
<css::logging::XCsvLogFormatter
, css::lang::XServiceInfo
>
48 virtual OUString SAL_CALL
formatMultiColumn(const Sequence
< OUString
>& column_data
) override
;
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
;
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
;
67 virtual OUString SAL_CALL
getHead( ) override
;
68 virtual OUString SAL_CALL
format( const LogRecord
& Record
) override
;
69 virtual OUString SAL_CALL
getTail( ) override
;
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
;
82 css::uno::Sequence
< OUString
> m_Columnnames
;
86 } // namespace logging
91 const sal_Unicode quote_char
= '"';
92 const sal_Unicode comma_char
= ',';
93 constexpr OUString dos_newline
= u
"\r\n"_ustr
;
95 bool needsQuoting(std::u16string_view str
)
97 return str
.find_first_of(u
"\",\n\r") != std::u16string_view::npos
;
100 void appendEncodedString(OUStringBuffer
& buf
, std::u16string_view str
)
102 if(needsQuoting(str
))
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();
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
;; )
117 i
=str
.substr(i
).rfind(quote_char
);
118 if(i
==std::u16string_view::npos
)
120 buf
.insert(buf_offset
+ i
, quote_char
);
122 buf
.append(quote_char
);
131 CsvFormatter::CsvFormatter()
134 m_LogTimestamp(true),
136 m_MultiColumn(false),
137 m_Columnnames({ "message" })
140 sal_Bool
CsvFormatter::getLogEventNo()
145 sal_Bool
CsvFormatter::getLogThread()
150 sal_Bool
CsvFormatter::getLogTimestamp()
152 return m_LogTimestamp
;
155 sal_Bool
CsvFormatter::getLogSource()
160 Sequence
< OUString
> CsvFormatter::getColumnnames()
162 return m_Columnnames
;
165 void CsvFormatter::setLogEventNo(sal_Bool log_event_no
)
167 m_LogEventNo
= log_event_no
;
170 void CsvFormatter::setLogThread(sal_Bool log_thread
)
172 m_LogThread
= log_thread
;
175 void CsvFormatter::setLogTimestamp(sal_Bool log_timestamp
)
177 m_LogTimestamp
= log_timestamp
;
180 void CsvFormatter::setLogSource(sal_Bool log_source
)
182 m_LogSource
= log_source
;
185 void CsvFormatter::setColumnnames(const Sequence
< OUString
>& columnnames
)
187 m_Columnnames
= columnnames
;
188 m_MultiColumn
= (m_Columnnames
.getLength()>1);
191 OUString SAL_CALL
CsvFormatter::getHead( )
195 buf
.append("event no,");
197 buf
.append("thread,");
199 buf
.append("timestamp,");
201 buf
.append("class,method,");
202 sal_Int32 columns
= m_Columnnames
.getLength();
203 for(sal_Int32 i
=0; i
<columns
; i
++)
205 buf
.append(m_Columnnames
[i
] + OUStringChar(comma_char
));
207 buf
.setLength(buf
.getLength()-1);
208 buf
.append(dos_newline
);
209 return buf
.makeStringAndClear();
212 OUString SAL_CALL
CsvFormatter::format( const LogRecord
& record
)
214 OUStringBuffer aLogEntry
;
218 aLogEntry
.append(record
.SequenceNumber
+ comma_char
);
223 aLogEntry
.append(record
.ThreadID
+ OUStringChar(comma_char
));
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
)
236 throw css::lang::IllegalArgumentException("invalid date", static_cast<cppu::OWeakObject
*>(this), 1);
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
);
256 appendEncodedString(aLogEntry
, record
.SourceClassName
);
257 aLogEntry
.append(comma_char
);
259 appendEncodedString(aLogEntry
, record
.SourceMethodName
);
260 aLogEntry
.append(comma_char
);
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
268 aLogEntry
.append(record
.Message
);
270 appendEncodedString(aLogEntry
, record
.Message
);
272 aLogEntry
.append( dos_newline
);
273 return aLogEntry
.makeStringAndClear();
276 OUString SAL_CALL
CsvFormatter::getTail( )
281 OUString SAL_CALL
CsvFormatter::formatMultiColumn(const Sequence
< OUString
>& column_data
)
283 sal_Int32 columns
= column_data
.getLength();
285 for(int i
=0; i
<columns
; i
++)
287 appendEncodedString(buf
, column_data
[i
]);
288 buf
.append(comma_char
);
290 buf
.setLength(buf
.getLength()-1);
291 return buf
.makeStringAndClear();
294 sal_Bool SAL_CALL
CsvFormatter::supportsService( const OUString
& service_name
)
296 return cppu::supportsService(this, service_name
);
299 OUString SAL_CALL
CsvFormatter::getImplementationName()
301 return "com.sun.star.comp.extensions.CsvFormatter";
304 Sequence
< OUString
> SAL_CALL
CsvFormatter::getSupportedServiceNames()
306 return { "com.sun.star.logging.CsvFormatter" };
309 } // namespace logging
311 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
312 com_sun_star_comp_extensions_CsvFormatter(
313 css::uno::XComponentContext
*,
314 css::uno::Sequence
<css::uno::Any
> const &)
316 return cppu::acquire(new logging::CsvFormatter());
319 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */