LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/extensions/source/logging - csvformatter.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 13 133 9.8 %
Date: 2013-07-09 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             : 
      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             : 
      33             : #include <rtl/ustrbuf.hxx>
      34             : 
      35             : #include <osl/thread.h>
      36             : 
      37             : namespace logging
      38             : {
      39             : 
      40             :     using ::com::sun::star::logging::XCsvLogFormatter;
      41             :     using ::com::sun::star::logging::XLogFormatter;
      42             :     using ::com::sun::star::uno::XComponentContext;
      43             :     using ::com::sun::star::uno::Reference;
      44             :     using ::com::sun::star::uno::Sequence;
      45             :     using ::com::sun::star::lang::XServiceInfo;
      46             :     using ::com::sun::star::uno::RuntimeException;
      47             :     using ::com::sun::star::logging::LogRecord;
      48             :     using ::com::sun::star::uno::XInterface;
      49             : 
      50             :     //= CsvFormatter - declaration
      51             :     //= formats for csv files as defined by RFC4180
      52             :     typedef ::cppu::WeakImplHelper2 <   XCsvLogFormatter
      53             :                                     ,   XServiceInfo
      54             :                                     >   CsvFormatter_Base;
      55             :     class CsvFormatter : public CsvFormatter_Base
      56             :     {
      57             :     public:
      58             :         virtual OUString SAL_CALL formatMultiColumn(const Sequence< OUString>& column_data) throw (RuntimeException);
      59             : 
      60             :         // XServiceInfo - static version
      61             :         static OUString SAL_CALL getImplementationName_static();
      62             :         static Sequence< OUString > SAL_CALL getSupportedServiceNames_static();
      63             :         static Reference< XInterface > Create( const Reference< XComponentContext >& context );
      64             : 
      65             :     protected:
      66             :         CsvFormatter();
      67             :         virtual ~CsvFormatter();
      68             : 
      69             :         // XCsvLogFormatter
      70             :         virtual ::sal_Bool SAL_CALL getLogEventNo() throw (RuntimeException);
      71             :         virtual ::sal_Bool SAL_CALL getLogThread() throw (RuntimeException);
      72             :         virtual ::sal_Bool SAL_CALL getLogTimestamp() throw (RuntimeException);
      73             :         virtual ::sal_Bool SAL_CALL getLogSource() throw (RuntimeException);
      74             :         virtual Sequence< OUString > SAL_CALL getColumnnames() throw (RuntimeException);
      75             : 
      76             :         virtual void SAL_CALL setLogEventNo( ::sal_Bool log_event_no ) throw (RuntimeException);
      77             :         virtual void SAL_CALL setLogThread( ::sal_Bool log_thread ) throw (RuntimeException);
      78             :         virtual void SAL_CALL setLogTimestamp( ::sal_Bool log_timestamp ) throw (RuntimeException);
      79             :         virtual void SAL_CALL setLogSource( ::sal_Bool log_source ) throw (RuntimeException);
      80             :         virtual void SAL_CALL setColumnnames( const Sequence< OUString>& column_names) throw (RuntimeException);
      81             : 
      82             :         // XLogFormatter
      83             :         virtual OUString SAL_CALL getHead(  ) throw (RuntimeException);
      84             :         virtual OUString SAL_CALL format( const LogRecord& Record ) throw (RuntimeException);
      85             :         virtual OUString SAL_CALL getTail(  ) throw (RuntimeException);
      86             : 
      87             :         // XServiceInfo
      88             :         virtual OUString SAL_CALL getImplementationName() throw(RuntimeException);
      89             :         virtual ::sal_Bool SAL_CALL supportsService( const OUString& service_name ) throw(RuntimeException);
      90             :         virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() throw(RuntimeException);
      91             : 
      92             :     private:
      93             :         ::sal_Bool m_LogEventNo;
      94             :         ::sal_Bool m_LogThread;
      95             :         ::sal_Bool m_LogTimestamp;
      96             :         ::sal_Bool m_LogSource;
      97             :         ::sal_Bool m_MultiColumn;
      98             :         ::com::sun::star::uno::Sequence< OUString > m_Columnnames;
      99             :     };
     100             : } // namespace logging
     101             : 
     102             : //= private helpers
     103             : namespace
     104             : {
     105           5 :     const sal_Unicode quote_char = OUString("\"").toChar();
     106           5 :     const sal_Unicode comma_char = OUString(",").toChar();
     107           5 :     const OUString dos_newline = OUString("\r\n");
     108             : 
     109           0 :     inline bool needsQuoting(const OUString& str)
     110             :     {
     111           0 :         static const OUString quote_trigger_chars = OUString( "\",\n\r");
     112           0 :         sal_Int32 len = str.getLength();
     113           0 :         for(sal_Int32 i=0; i<len; i++)
     114           0 :             if(quote_trigger_chars.indexOf(str[i])!=-1)
     115           0 :                 return true;
     116           0 :         return false;
     117             :     };
     118             : 
     119           0 :     inline void appendEncodedString(OUStringBuffer& buf, const OUString& str)
     120             :     {
     121           0 :         if(needsQuoting(str))
     122             :         {
     123             :             // each double-quote will get replaced by two double-quotes
     124           0 :             buf.append(quote_char);
     125           0 :             const sal_Int32 buf_offset = buf.getLength();
     126           0 :             const sal_Int32 str_length = str.getLength();
     127           0 :             buf.append(str);
     128             :             // special treatment for the last character
     129           0 :             if(quote_char==str[str_length-1])
     130           0 :                 buf.append(quote_char);
     131             :             // iterating backwards because the index at which we insert wont be shifted
     132             :             // when moving that way.
     133           0 :             for(sal_Int32 i = str_length; i>=0; )
     134             :             {
     135           0 :                 i=str.lastIndexOf(quote_char, --i);
     136           0 :                 if(i!=-1)
     137           0 :                     buf.insert(buf_offset + i, quote_char);
     138             :             }
     139           0 :             buf.append(quote_char);
     140             :         }
     141             :         else
     142           0 :             buf.append(str);
     143           0 :     };
     144             : 
     145           0 :     ::com::sun::star::uno::Sequence< OUString> initialColumns()
     146             :     {
     147           0 :         com::sun::star::uno::Sequence< OUString> result = ::com::sun::star::uno::Sequence< OUString>(1);
     148           0 :         result[0] = OUString("message");
     149           0 :         return result;
     150             :     };
     151             : }
     152             : 
     153             : //= CsvFormatter - implementation
     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)
     169             :     {
     170           0 :         return m_LogEventNo;
     171             :     }
     172             : 
     173           0 :     ::sal_Bool CsvFormatter::getLogThread() throw (RuntimeException)
     174             :     {
     175           0 :         return m_LogThread;
     176             :     }
     177             : 
     178           0 :     ::sal_Bool CsvFormatter::getLogTimestamp() throw (RuntimeException)
     179             :     {
     180           0 :         return m_LogTimestamp;
     181             :     }
     182             : 
     183           0 :     ::sal_Bool CsvFormatter::getLogSource() throw (RuntimeException)
     184             :     {
     185           0 :         return m_LogSource;
     186             :     }
     187             : 
     188           0 :     Sequence< OUString > CsvFormatter::getColumnnames() throw (RuntimeException)
     189             :     {
     190           0 :         return m_Columnnames;
     191             :     }
     192             : 
     193           0 :     void CsvFormatter::setLogEventNo(::sal_Bool log_event_no) throw (RuntimeException)
     194             :     {
     195           0 :         m_LogEventNo = log_event_no;
     196           0 :     }
     197             : 
     198           0 :     void CsvFormatter::setLogThread(::sal_Bool log_thread) throw (RuntimeException)
     199             :     {
     200           0 :         m_LogThread = log_thread;
     201           0 :     }
     202             : 
     203           0 :     void CsvFormatter::setLogTimestamp(::sal_Bool log_timestamp) throw (RuntimeException)
     204             :     {
     205           0 :         m_LogTimestamp = log_timestamp;
     206           0 :     }
     207             : 
     208           0 :     void CsvFormatter::setLogSource(::sal_Bool log_source) throw (RuntimeException)
     209             :     {
     210           0 :         m_LogSource = log_source;
     211           0 :     }
     212             : 
     213           0 :     void CsvFormatter::setColumnnames(const Sequence< OUString >& columnnames) throw (RuntimeException)
     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)
     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)
     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)
     297             :     {
     298           0 :         return OUString();
     299             :     }
     300             : 
     301           0 :     OUString SAL_CALL CsvFormatter::formatMultiColumn(const Sequence< OUString>& column_data) throw (RuntimeException)
     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)
     315             :     {
     316           0 :         const Sequence< OUString > aServiceNames( getSupportedServiceNames() );
     317           0 :         for (   const OUString* pServiceNames = aServiceNames.getConstArray();
     318           0 :                 pServiceNames != aServiceNames.getConstArray() + aServiceNames.getLength();
     319             :                 ++pServiceNames
     320             :             )
     321           0 :             if ( service_name == *pServiceNames )
     322           0 :                 return sal_True;
     323           0 :         return sal_False;
     324             :     }
     325             : 
     326           0 :     OUString SAL_CALL CsvFormatter::getImplementationName() throw(RuntimeException)
     327             :     {
     328           0 :         return getImplementationName_static();
     329             :     }
     330             : 
     331           0 :     Sequence< OUString > SAL_CALL CsvFormatter::getSupportedServiceNames() throw(RuntimeException)
     332             :     {
     333           0 :         return getSupportedServiceNames_static();
     334             :     }
     335             : 
     336           5 :     OUString SAL_CALL CsvFormatter::getImplementationName_static()
     337             :     {
     338           5 :         return OUString( "com.sun.star.comp.extensions.CsvFormatter" );
     339             :     }
     340             : 
     341           5 :     Sequence< OUString > SAL_CALL CsvFormatter::getSupportedServiceNames_static()
     342             :     {
     343           5 :         Sequence< OUString > aServiceNames(1);
     344           5 :         aServiceNames[0] = OUString( "com.sun.star.logging.CsvFormatter" );
     345           5 :         return aServiceNames;
     346             :     }
     347             : 
     348           0 :     Reference< XInterface > CsvFormatter::Create( const Reference< XComponentContext >& )
     349             :     {
     350           0 :         return *( new CsvFormatter );
     351             :     }
     352             : 
     353          15 :     void createRegistryInfo_CsvFormatter()
     354             :     {
     355          15 :         static OAutoRegistration< CsvFormatter > aAutoRegistration;
     356          15 :     }
     357          15 : } // namespace logging
     358             : 
     359             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10