LCOV - code coverage report
Current view: top level - extensions/source/logging - csvformatter.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 126 0.0 %
Date: 2014-04-14 Functions: 0 29 0.0 %
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             : 
      23             : #include <stdio.h>
      24             : #include <string>
      25             : 
      26             : #include <com/sun/star/logging/XCsvLogFormatter.hpp>
      27             : #include <com/sun/star/logging/XLogFormatter.hpp>
      28             : #include <com/sun/star/uno/XComponentContext.hpp>
      29             : #include <com/sun/star/lang/XServiceInfo.hpp>
      30             : 
      31             : #include <cppuhelper/implbase2.hxx>
      32             : #include <cppuhelper/supportsservice.hxx>
      33             : 
      34             : #include <rtl/ustrbuf.hxx>
      35             : 
      36             : #include <osl/thread.h>
      37             : 
      38             : namespace logging
      39             : {
      40             : 
      41             :     using ::com::sun::star::logging::XCsvLogFormatter;
      42             :     using ::com::sun::star::logging::XLogFormatter;
      43             :     using ::com::sun::star::uno::XComponentContext;
      44             :     using ::com::sun::star::uno::Reference;
      45             :     using ::com::sun::star::uno::Sequence;
      46             :     using ::com::sun::star::lang::XServiceInfo;
      47             :     using ::com::sun::star::uno::RuntimeException;
      48             :     using ::com::sun::star::logging::LogRecord;
      49             :     using ::com::sun::star::uno::XInterface;
      50             : 
      51             :     //= CsvFormatter - declaration
      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             :         sal_Bool m_LogEventNo;
      95             :         sal_Bool m_LogThread;
      96             :         sal_Bool m_LogTimestamp;
      97             :         sal_Bool m_LogSource;
      98             :         sal_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           0 :     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             : //= CsvFormatter - implementation
     155             : namespace logging
     156             : {
     157           0 :     CsvFormatter::CsvFormatter()
     158             :         :m_LogEventNo(true),
     159             :         m_LogThread(true),
     160             :         m_LogTimestamp(true),
     161             :         m_LogSource(false),
     162             :         m_MultiColumn(false),
     163           0 :         m_Columnnames(initialColumns())
     164           0 :     { }
     165             : 
     166           0 :     CsvFormatter::~CsvFormatter()
     167           0 :     { }
     168             : 
     169           0 :     sal_Bool CsvFormatter::getLogEventNo() throw (RuntimeException, std::exception)
     170             :     {
     171           0 :         return m_LogEventNo;
     172             :     }
     173             : 
     174           0 :     sal_Bool CsvFormatter::getLogThread() throw (RuntimeException, std::exception)
     175             :     {
     176           0 :         return m_LogThread;
     177             :     }
     178             : 
     179           0 :     sal_Bool CsvFormatter::getLogTimestamp() throw (RuntimeException, std::exception)
     180             :     {
     181           0 :         return m_LogTimestamp;
     182             :     }
     183             : 
     184           0 :     sal_Bool CsvFormatter::getLogSource() throw (RuntimeException, std::exception)
     185             :     {
     186           0 :         return m_LogSource;
     187             :     }
     188             : 
     189           0 :     Sequence< OUString > CsvFormatter::getColumnnames() throw (RuntimeException, std::exception)
     190             :     {
     191           0 :         return m_Columnnames;
     192             :     }
     193             : 
     194           0 :     void CsvFormatter::setLogEventNo(sal_Bool log_event_no) throw (RuntimeException, std::exception)
     195             :     {
     196           0 :         m_LogEventNo = log_event_no;
     197           0 :     }
     198             : 
     199           0 :     void CsvFormatter::setLogThread(sal_Bool log_thread) throw (RuntimeException, std::exception)
     200             :     {
     201           0 :         m_LogThread = log_thread;
     202           0 :     }
     203             : 
     204           0 :     void CsvFormatter::setLogTimestamp(sal_Bool log_timestamp) throw (RuntimeException, std::exception)
     205             :     {
     206           0 :         m_LogTimestamp = log_timestamp;
     207           0 :     }
     208             : 
     209           0 :     void CsvFormatter::setLogSource(sal_Bool log_source) throw (RuntimeException, std::exception)
     210             :     {
     211           0 :         m_LogSource = log_source;
     212           0 :     }
     213             : 
     214           0 :     void CsvFormatter::setColumnnames(const Sequence< OUString >& columnnames) throw (RuntimeException, std::exception)
     215             :     {
     216           0 :         m_Columnnames = Sequence< OUString>(columnnames);
     217           0 :         m_MultiColumn = (m_Columnnames.getLength()>1);
     218           0 :     }
     219             : 
     220           0 :     OUString SAL_CALL CsvFormatter::getHead(  ) throw (RuntimeException, std::exception)
     221             :     {
     222           0 :         OUStringBuffer buf;
     223           0 :         if(m_LogEventNo)
     224           0 :             buf.appendAscii("event no,");
     225           0 :         if(m_LogThread)
     226           0 :             buf.appendAscii("thread,");
     227           0 :         if(m_LogTimestamp)
     228           0 :             buf.appendAscii("timestamp,");
     229           0 :         if(m_LogSource)
     230           0 :             buf.appendAscii("class,method,");
     231           0 :         sal_Int32 columns = m_Columnnames.getLength();
     232           0 :         for(sal_Int32 i=0; i<columns; i++)
     233             :         {
     234           0 :             buf.append(m_Columnnames[i]);
     235           0 :             buf.append(comma_char);
     236             :         }
     237           0 :         buf.setLength(buf.getLength()-1);
     238           0 :         buf.append(dos_newline);
     239           0 :         return buf.makeStringAndClear();
     240             :     }
     241             : 
     242           0 :     OUString SAL_CALL CsvFormatter::format( const LogRecord& record ) throw (RuntimeException, std::exception)
     243             :     {
     244           0 :         OUStringBuffer aLogEntry;
     245             : 
     246           0 :         if(m_LogEventNo)
     247             :         {
     248           0 :             aLogEntry.append( record.SequenceNumber );
     249           0 :             aLogEntry.append(comma_char);
     250             :         }
     251             : 
     252           0 :         if(m_LogThread)
     253             :         {
     254           0 :             aLogEntry.append( record.ThreadID );
     255           0 :             aLogEntry.append(comma_char);
     256             :         }
     257             : 
     258           0 :         if(m_LogTimestamp)
     259             :         {
     260             :             // ISO 8601
     261             :             char buffer[ 30 ];
     262           0 :             const size_t buffer_size = sizeof( buffer );
     263             :             snprintf( buffer, buffer_size, "%04i-%02i-%02iT%02i:%02i:%02i.%09i",
     264             :                 (int)record.LogTime.Year,
     265             :                 (int)record.LogTime.Month,
     266             :                 (int)record.LogTime.Day,
     267             :                 (int)record.LogTime.Hours,
     268             :                 (int)record.LogTime.Minutes,
     269             :                 (int)record.LogTime.Seconds,
     270           0 :                 (int)record.LogTime.NanoSeconds );
     271           0 :             aLogEntry.appendAscii( buffer );
     272           0 :             aLogEntry.append(comma_char);
     273             :         }
     274             : 
     275           0 :         if(m_LogSource)
     276             :         {
     277           0 :             appendEncodedString(aLogEntry, record.SourceClassName);
     278           0 :             aLogEntry.append(comma_char);
     279             : 
     280           0 :             appendEncodedString(aLogEntry, record.SourceMethodName);
     281           0 :             aLogEntry.append(comma_char);
     282             :         }
     283             : 
     284             :         // if the CsvFormatter has multiple columns set via setColumnnames(), the
     285             :         // message of the record is expected to be encoded with formatMultiColumn
     286             :         // if the CsvFormatter has only one column set, the message is expected not
     287             :         // to be encoded
     288           0 :         if(m_MultiColumn)
     289           0 :             aLogEntry.append(record.Message);
     290             :         else
     291           0 :             appendEncodedString(aLogEntry, record.Message);
     292             : 
     293           0 :         aLogEntry.append( dos_newline );
     294           0 :         return aLogEntry.makeStringAndClear();
     295             :     }
     296             : 
     297           0 :     OUString SAL_CALL CsvFormatter::getTail(  ) throw (RuntimeException, std::exception)
     298             :     {
     299           0 :         return OUString();
     300             :     }
     301             : 
     302           0 :     OUString SAL_CALL CsvFormatter::formatMultiColumn(const Sequence< OUString>& column_data) throw (RuntimeException, std::exception)
     303             :     {
     304           0 :         sal_Int32 columns = column_data.getLength();
     305           0 :         OUStringBuffer buf;
     306           0 :         for(int i=0; i<columns; i++)
     307             :         {
     308           0 :             appendEncodedString(buf, column_data[i]);
     309           0 :             buf.append(comma_char);
     310             :         }
     311           0 :         buf.setLength(buf.getLength()-1);
     312           0 :         return buf.makeStringAndClear();
     313             :     }
     314             : 
     315           0 :     sal_Bool SAL_CALL CsvFormatter::supportsService( const OUString& service_name ) throw(RuntimeException, std::exception)
     316             :     {
     317           0 :         return cppu::supportsService(this, service_name);
     318             :     }
     319             : 
     320           0 :     OUString SAL_CALL CsvFormatter::getImplementationName() throw(RuntimeException, std::exception)
     321             :     {
     322           0 :         return getImplementationName_static();
     323             :     }
     324             : 
     325           0 :     Sequence< OUString > SAL_CALL CsvFormatter::getSupportedServiceNames() throw(RuntimeException, std::exception)
     326             :     {
     327           0 :         return getSupportedServiceNames_static();
     328             :     }
     329             : 
     330           0 :     OUString SAL_CALL CsvFormatter::getImplementationName_static()
     331             :     {
     332           0 :         return OUString( "com.sun.star.comp.extensions.CsvFormatter" );
     333             :     }
     334             : 
     335           0 :     Sequence< OUString > SAL_CALL CsvFormatter::getSupportedServiceNames_static()
     336             :     {
     337           0 :         Sequence< OUString > aServiceNames(1);
     338           0 :         aServiceNames[0] = "com.sun.star.logging.CsvFormatter";
     339           0 :         return aServiceNames;
     340             :     }
     341             : 
     342           0 :     Reference< XInterface > CsvFormatter::Create( const Reference< XComponentContext >& )
     343             :     {
     344           0 :         return *( new CsvFormatter );
     345             :     }
     346             : 
     347           0 :     void createRegistryInfo_CsvFormatter()
     348             :     {
     349           0 :         static OAutoRegistration< CsvFormatter > aAutoRegistration;
     350           0 :     }
     351           0 : } // namespace logging
     352             : 
     353             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10