/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * This file incorporates work covered by the following license notice: * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed * with this work for additional information regarding copyright * ownership. The ASF licenses this file to you under the Apache * License, Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.apache.org/licenses/LICENSE-2.0 . */ #include #include #include #include #include #include #include #include #include #include #include namespace logging { using ::com::sun::star::uno::Sequence; using ::com::sun::star::uno::RuntimeException; using ::com::sun::star::logging::LogRecord; // formats for csv files as defined by RFC4180 class CsvFormatter : public cppu::WeakImplHelper { public: virtual OUString SAL_CALL formatMultiColumn(const Sequence< OUString>& column_data) throw (RuntimeException, std::exception) override; CsvFormatter(); virtual ~CsvFormatter(); private: // XCsvLogFormatter virtual sal_Bool SAL_CALL getLogEventNo() throw (RuntimeException, std::exception) override; virtual sal_Bool SAL_CALL getLogThread() throw (RuntimeException, std::exception) override; virtual sal_Bool SAL_CALL getLogTimestamp() throw (RuntimeException, std::exception) override; virtual sal_Bool SAL_CALL getLogSource() throw (RuntimeException, std::exception) override; virtual Sequence< OUString > SAL_CALL getColumnnames() throw (RuntimeException, std::exception) override; virtual void SAL_CALL setLogEventNo( sal_Bool log_event_no ) throw (RuntimeException, std::exception) override; virtual void SAL_CALL setLogThread( sal_Bool log_thread ) throw (RuntimeException, std::exception) override; virtual void SAL_CALL setLogTimestamp( sal_Bool log_timestamp ) throw (RuntimeException, std::exception) override; virtual void SAL_CALL setLogSource( sal_Bool log_source ) throw (RuntimeException, std::exception) override; virtual void SAL_CALL setColumnnames( const Sequence< OUString>& column_names) throw (RuntimeException, std::exception) override; // XLogFormatter virtual OUString SAL_CALL getHead( ) throw (RuntimeException, std::exception) override; virtual OUString SAL_CALL format( const LogRecord& Record ) throw (RuntimeException, std::exception) override; virtual OUString SAL_CALL getTail( ) throw (RuntimeException, std::exception) override; // XServiceInfo virtual OUString SAL_CALL getImplementationName() throw(RuntimeException, std::exception) override; virtual sal_Bool SAL_CALL supportsService( const OUString& service_name ) throw(RuntimeException, std::exception) override; virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() throw(RuntimeException, std::exception) override; private: bool m_LogEventNo; bool m_LogThread; bool m_LogTimestamp; bool m_LogSource; bool m_MultiColumn; css::uno::Sequence< OUString > m_Columnnames; }; } // namespace logging // private helpers namespace { const sal_Unicode quote_char = '"'; const sal_Unicode comma_char = ','; const OUString dos_newline = "\r\n"; inline bool needsQuoting(const OUString& str) { static const OUString quote_trigger_chars = "\",\n\r"; sal_Int32 len = str.getLength(); for(sal_Int32 i=0; i=0; ) { i=str.lastIndexOf(quote_char, --i); if(i!=-1) buf.insert(buf_offset + i, quote_char); } buf.append(quote_char); } else buf.append(str); }; } namespace logging { CsvFormatter::CsvFormatter() :m_LogEventNo(true), m_LogThread(true), m_LogTimestamp(true), m_LogSource(false), m_MultiColumn(false), m_Columnnames({ "message" }) { } CsvFormatter::~CsvFormatter() { } sal_Bool CsvFormatter::getLogEventNo() throw (RuntimeException, std::exception) { return m_LogEventNo; } sal_Bool CsvFormatter::getLogThread() throw (RuntimeException, std::exception) { return m_LogThread; } sal_Bool CsvFormatter::getLogTimestamp() throw (RuntimeException, std::exception) { return m_LogTimestamp; } sal_Bool CsvFormatter::getLogSource() throw (RuntimeException, std::exception) { return m_LogSource; } Sequence< OUString > CsvFormatter::getColumnnames() throw (RuntimeException, std::exception) { return m_Columnnames; } void CsvFormatter::setLogEventNo(sal_Bool log_event_no) throw (RuntimeException, std::exception) { m_LogEventNo = log_event_no; } void CsvFormatter::setLogThread(sal_Bool log_thread) throw (RuntimeException, std::exception) { m_LogThread = log_thread; } void CsvFormatter::setLogTimestamp(sal_Bool log_timestamp) throw (RuntimeException, std::exception) { m_LogTimestamp = log_timestamp; } void CsvFormatter::setLogSource(sal_Bool log_source) throw (RuntimeException, std::exception) { m_LogSource = log_source; } void CsvFormatter::setColumnnames(const Sequence< OUString >& columnnames) throw (RuntimeException, std::exception) { m_Columnnames = Sequence< OUString>(columnnames); m_MultiColumn = (m_Columnnames.getLength()>1); } OUString SAL_CALL CsvFormatter::getHead( ) throw (RuntimeException, std::exception) { OUStringBuffer buf; if(m_LogEventNo) buf.append("event no,"); if(m_LogThread) buf.append("thread,"); if(m_LogTimestamp) buf.append("timestamp,"); if(m_LogSource) buf.append("class,method,"); sal_Int32 columns = m_Columnnames.getLength(); for(sal_Int32 i=0; i& column_data) throw (RuntimeException, std::exception) { sal_Int32 columns = column_data.getLength(); OUStringBuffer buf; for(int i=0; i SAL_CALL CsvFormatter::getSupportedServiceNames() throw(RuntimeException, std::exception) { return { "com.sun.star.logging.CsvFormatter" }; } } // namespace logging extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL com_sun_star_comp_extensions_CsvFormatter( css::uno::XComponentContext *, css::uno::Sequence const &) { return cppu::acquire(new logging::CsvFormatter()); } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */