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

Generated by: LCOV version 1.10