LCOV - code coverage report
Current view: top level - extensions/source/logging - csvformatter.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 11 126 8.7 %
Date: 2015-06-13 12:38:46 Functions: 5 29 17.2 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.11