Line data Source code
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 :
21 : #include "log_module.hxx"
22 : #include "log_services.hxx"
23 :
24 : #include <stdio.h>
25 : #include <string>
26 :
27 : #include <com/sun/star/logging/XCsvLogFormatter.hpp>
28 : #include <com/sun/star/logging/XLogFormatter.hpp>
29 : #include <com/sun/star/uno/XComponentContext.hpp>
30 : #include <com/sun/star/lang/XServiceInfo.hpp>
31 :
32 : #include <cppuhelper/implbase2.hxx>
33 : #include <cppuhelper/supportsservice.hxx>
34 :
35 : #include <rtl/ustrbuf.hxx>
36 :
37 : #include <osl/thread.h>
38 :
39 : namespace logging
40 : {
41 :
42 : using ::com::sun::star::logging::XCsvLogFormatter;
43 : using ::com::sun::star::logging::XLogFormatter;
44 : using ::com::sun::star::uno::XComponentContext;
45 : using ::com::sun::star::uno::Reference;
46 : using ::com::sun::star::uno::Sequence;
47 : using ::com::sun::star::lang::XServiceInfo;
48 : using ::com::sun::star::uno::RuntimeException;
49 : using ::com::sun::star::logging::LogRecord;
50 : using ::com::sun::star::uno::XInterface;
51 :
52 : //= CsvFormatter - declaration
53 : //= formats for csv files as defined by RFC4180
54 : typedef ::cppu::WeakImplHelper2 < XCsvLogFormatter
55 : , XServiceInfo
56 : > CsvFormatter_Base;
57 : class CsvFormatter : public CsvFormatter_Base
58 : {
59 : public:
60 : virtual OUString SAL_CALL formatMultiColumn(const Sequence< OUString>& column_data) throw (RuntimeException, std::exception) SAL_OVERRIDE;
61 :
62 : // XServiceInfo - static version
63 : static OUString SAL_CALL getImplementationName_static();
64 : static Sequence< OUString > SAL_CALL getSupportedServiceNames_static();
65 : static Reference< XInterface > Create( const Reference< XComponentContext >& context );
66 :
67 : protected:
68 : CsvFormatter();
69 : virtual ~CsvFormatter();
70 :
71 : // XCsvLogFormatter
72 : virtual sal_Bool SAL_CALL getLogEventNo() throw (RuntimeException, std::exception) SAL_OVERRIDE;
73 : virtual sal_Bool SAL_CALL getLogThread() throw (RuntimeException, std::exception) SAL_OVERRIDE;
74 : virtual sal_Bool SAL_CALL getLogTimestamp() throw (RuntimeException, std::exception) SAL_OVERRIDE;
75 : virtual sal_Bool SAL_CALL getLogSource() throw (RuntimeException, std::exception) SAL_OVERRIDE;
76 : virtual Sequence< OUString > SAL_CALL getColumnnames() throw (RuntimeException, std::exception) SAL_OVERRIDE;
77 :
78 : virtual void SAL_CALL setLogEventNo( sal_Bool log_event_no ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
79 : virtual void SAL_CALL setLogThread( sal_Bool log_thread ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
80 : virtual void SAL_CALL setLogTimestamp( sal_Bool log_timestamp ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
81 : virtual void SAL_CALL setLogSource( sal_Bool log_source ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
82 : virtual void SAL_CALL setColumnnames( const Sequence< OUString>& column_names) throw (RuntimeException, std::exception) SAL_OVERRIDE;
83 :
84 : // XLogFormatter
85 : virtual OUString SAL_CALL getHead( ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
86 : virtual OUString SAL_CALL format( const LogRecord& Record ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
87 : virtual OUString SAL_CALL getTail( ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
88 :
89 : // XServiceInfo
90 : virtual OUString SAL_CALL getImplementationName() throw(RuntimeException, std::exception) SAL_OVERRIDE;
91 : virtual sal_Bool SAL_CALL supportsService( const OUString& service_name ) throw(RuntimeException, std::exception) SAL_OVERRIDE;
92 : virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() throw(RuntimeException, std::exception) SAL_OVERRIDE;
93 :
94 : private:
95 : bool m_LogEventNo;
96 : bool m_LogThread;
97 : bool m_LogTimestamp;
98 : bool m_LogSource;
99 : bool m_MultiColumn;
100 : ::com::sun::star::uno::Sequence< OUString > m_Columnnames;
101 : };
102 : } // namespace logging
103 :
104 : //= private helpers
105 : namespace
106 : {
107 : const sal_Unicode quote_char = '"';
108 : const sal_Unicode comma_char = ',';
109 14 : const OUString dos_newline = "\r\n";
110 :
111 0 : inline bool needsQuoting(const OUString& str)
112 : {
113 0 : static const OUString quote_trigger_chars = "\",\n\r";
114 0 : sal_Int32 len = str.getLength();
115 0 : for(sal_Int32 i=0; i<len; i++)
116 0 : if(quote_trigger_chars.indexOf(str[i])!=-1)
117 0 : return true;
118 0 : return false;
119 : };
120 :
121 0 : inline void appendEncodedString(OUStringBuffer& buf, const OUString& str)
122 : {
123 0 : if(needsQuoting(str))
124 : {
125 : // each double-quote will get replaced by two double-quotes
126 0 : buf.append(quote_char);
127 0 : const sal_Int32 buf_offset = buf.getLength();
128 0 : const sal_Int32 str_length = str.getLength();
129 0 : buf.append(str);
130 : // special treatment for the last character
131 0 : if(quote_char==str[str_length-1])
132 0 : buf.append(quote_char);
133 : // iterating backwards because the index at which we insert wont be shifted
134 : // when moving that way.
135 0 : for(sal_Int32 i = str_length; i>=0; )
136 : {
137 0 : i=str.lastIndexOf(quote_char, --i);
138 0 : if(i!=-1)
139 0 : buf.insert(buf_offset + i, quote_char);
140 : }
141 0 : buf.append(quote_char);
142 : }
143 : else
144 0 : buf.append(str);
145 0 : };
146 :
147 0 : ::com::sun::star::uno::Sequence< OUString> initialColumns()
148 : {
149 0 : com::sun::star::uno::Sequence< OUString> result = ::com::sun::star::uno::Sequence< OUString>(1);
150 0 : result[0] = "message";
151 0 : return result;
152 : };
153 : }
154 :
155 : //= CsvFormatter - implementation
156 : namespace logging
157 : {
158 0 : CsvFormatter::CsvFormatter()
159 : :m_LogEventNo(true),
160 : m_LogThread(true),
161 : m_LogTimestamp(true),
162 : m_LogSource(false),
163 : m_MultiColumn(false),
164 0 : m_Columnnames(initialColumns())
165 0 : { }
166 :
167 0 : CsvFormatter::~CsvFormatter()
168 0 : { }
169 :
170 0 : sal_Bool CsvFormatter::getLogEventNo() throw (RuntimeException, std::exception)
171 : {
172 0 : return m_LogEventNo;
173 : }
174 :
175 0 : sal_Bool CsvFormatter::getLogThread() throw (RuntimeException, std::exception)
176 : {
177 0 : return m_LogThread;
178 : }
179 :
180 0 : sal_Bool CsvFormatter::getLogTimestamp() throw (RuntimeException, std::exception)
181 : {
182 0 : return m_LogTimestamp;
183 : }
184 :
185 0 : sal_Bool CsvFormatter::getLogSource() throw (RuntimeException, std::exception)
186 : {
187 0 : return m_LogSource;
188 : }
189 :
190 0 : Sequence< OUString > CsvFormatter::getColumnnames() throw (RuntimeException, std::exception)
191 : {
192 0 : return m_Columnnames;
193 : }
194 :
195 0 : void CsvFormatter::setLogEventNo(sal_Bool log_event_no) throw (RuntimeException, std::exception)
196 : {
197 0 : m_LogEventNo = log_event_no;
198 0 : }
199 :
200 0 : void CsvFormatter::setLogThread(sal_Bool log_thread) throw (RuntimeException, std::exception)
201 : {
202 0 : m_LogThread = log_thread;
203 0 : }
204 :
205 0 : void CsvFormatter::setLogTimestamp(sal_Bool log_timestamp) throw (RuntimeException, std::exception)
206 : {
207 0 : m_LogTimestamp = log_timestamp;
208 0 : }
209 :
210 0 : void CsvFormatter::setLogSource(sal_Bool log_source) throw (RuntimeException, std::exception)
211 : {
212 0 : m_LogSource = log_source;
213 0 : }
214 :
215 0 : void CsvFormatter::setColumnnames(const Sequence< OUString >& columnnames) throw (RuntimeException, std::exception)
216 : {
217 0 : m_Columnnames = Sequence< OUString>(columnnames);
218 0 : m_MultiColumn = (m_Columnnames.getLength()>1);
219 0 : }
220 :
221 0 : OUString SAL_CALL CsvFormatter::getHead( ) throw (RuntimeException, std::exception)
222 : {
223 0 : OUStringBuffer buf;
224 0 : if(m_LogEventNo)
225 0 : buf.appendAscii("event no,");
226 0 : if(m_LogThread)
227 0 : buf.appendAscii("thread,");
228 0 : if(m_LogTimestamp)
229 0 : buf.appendAscii("timestamp,");
230 0 : if(m_LogSource)
231 0 : buf.appendAscii("class,method,");
232 0 : sal_Int32 columns = m_Columnnames.getLength();
233 0 : for(sal_Int32 i=0; i<columns; i++)
234 : {
235 0 : buf.append(m_Columnnames[i]);
236 0 : buf.append(comma_char);
237 : }
238 0 : buf.setLength(buf.getLength()-1);
239 0 : buf.append(dos_newline);
240 0 : return buf.makeStringAndClear();
241 : }
242 :
243 0 : OUString SAL_CALL CsvFormatter::format( const LogRecord& record ) throw (RuntimeException, std::exception)
244 : {
245 0 : OUStringBuffer aLogEntry;
246 :
247 0 : if(m_LogEventNo)
248 : {
249 0 : aLogEntry.append( record.SequenceNumber );
250 0 : aLogEntry.append(comma_char);
251 : }
252 :
253 0 : if(m_LogThread)
254 : {
255 0 : aLogEntry.append( record.ThreadID );
256 0 : aLogEntry.append(comma_char);
257 : }
258 :
259 0 : if(m_LogTimestamp)
260 : {
261 : // ISO 8601
262 : char buffer[ 30 ];
263 0 : const size_t buffer_size = sizeof( buffer );
264 : snprintf( buffer, buffer_size, "%04i-%02i-%02iT%02i:%02i:%02i.%09i",
265 : (int)record.LogTime.Year,
266 : (int)record.LogTime.Month,
267 : (int)record.LogTime.Day,
268 : (int)record.LogTime.Hours,
269 : (int)record.LogTime.Minutes,
270 : (int)record.LogTime.Seconds,
271 0 : (int)record.LogTime.NanoSeconds );
272 0 : aLogEntry.appendAscii( buffer );
273 0 : aLogEntry.append(comma_char);
274 : }
275 :
276 0 : if(m_LogSource)
277 : {
278 0 : appendEncodedString(aLogEntry, record.SourceClassName);
279 0 : aLogEntry.append(comma_char);
280 :
281 0 : appendEncodedString(aLogEntry, record.SourceMethodName);
282 0 : aLogEntry.append(comma_char);
283 : }
284 :
285 : // if the CsvFormatter has multiple columns set via setColumnnames(), the
286 : // message of the record is expected to be encoded with formatMultiColumn
287 : // if the CsvFormatter has only one column set, the message is expected not
288 : // to be encoded
289 0 : if(m_MultiColumn)
290 0 : aLogEntry.append(record.Message);
291 : else
292 0 : appendEncodedString(aLogEntry, record.Message);
293 :
294 0 : aLogEntry.append( dos_newline );
295 0 : return aLogEntry.makeStringAndClear();
296 : }
297 :
298 0 : OUString SAL_CALL CsvFormatter::getTail( ) throw (RuntimeException, std::exception)
299 : {
300 0 : return OUString();
301 : }
302 :
303 0 : OUString SAL_CALL CsvFormatter::formatMultiColumn(const Sequence< OUString>& column_data) throw (RuntimeException, std::exception)
304 : {
305 0 : sal_Int32 columns = column_data.getLength();
306 0 : OUStringBuffer buf;
307 0 : for(int i=0; i<columns; i++)
308 : {
309 0 : appendEncodedString(buf, column_data[i]);
310 0 : buf.append(comma_char);
311 : }
312 0 : buf.setLength(buf.getLength()-1);
313 0 : return buf.makeStringAndClear();
314 : }
315 :
316 0 : sal_Bool SAL_CALL CsvFormatter::supportsService( const OUString& service_name ) throw(RuntimeException, std::exception)
317 : {
318 0 : return cppu::supportsService(this, service_name);
319 : }
320 :
321 0 : OUString SAL_CALL CsvFormatter::getImplementationName() throw(RuntimeException, std::exception)
322 : {
323 0 : return getImplementationName_static();
324 : }
325 :
326 0 : Sequence< OUString > SAL_CALL CsvFormatter::getSupportedServiceNames() throw(RuntimeException, std::exception)
327 : {
328 0 : return getSupportedServiceNames_static();
329 : }
330 :
331 14 : OUString SAL_CALL CsvFormatter::getImplementationName_static()
332 : {
333 14 : return OUString( "com.sun.star.comp.extensions.CsvFormatter" );
334 : }
335 :
336 14 : Sequence< OUString > SAL_CALL CsvFormatter::getSupportedServiceNames_static()
337 : {
338 14 : Sequence< OUString > aServiceNames(1);
339 14 : aServiceNames[0] = "com.sun.star.logging.CsvFormatter";
340 14 : return aServiceNames;
341 : }
342 :
343 0 : Reference< XInterface > CsvFormatter::Create( const Reference< XComponentContext >& )
344 : {
345 0 : return *( new CsvFormatter );
346 : }
347 :
348 44 : void createRegistryInfo_CsvFormatter()
349 : {
350 44 : static OAutoRegistration< CsvFormatter > aAutoRegistration;
351 44 : }
352 42 : } // namespace logging
353 :
354 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|