1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include "log_module.hxx"
22 #include "log_services.hxx"
26 #include <com/sun/star/logging/XLogFormatter.hpp>
27 #include <com/sun/star/uno/XComponentContext.hpp>
28 #include <com/sun/star/lang/XServiceInfo.hpp>
30 #include <cppuhelper/implbase2.hxx>
31 #include <cppuhelper/supportsservice.hxx>
33 #include <rtl/ustrbuf.hxx>
35 #include <osl/thread.h>
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
;
51 typedef ::cppu::WeakImplHelper2
< XLogFormatter
53 > PlainTextFormatter_Base
;
54 class PlainTextFormatter
: public PlainTextFormatter_Base
58 virtual ~PlainTextFormatter();
61 virtual OUString SAL_CALL
getHead( ) throw (RuntimeException
, std::exception
) SAL_OVERRIDE
;
62 virtual OUString SAL_CALL
format( const LogRecord
& Record
) throw (RuntimeException
, std::exception
) SAL_OVERRIDE
;
63 virtual OUString SAL_CALL
getTail( ) throw (RuntimeException
, std::exception
) SAL_OVERRIDE
;
66 virtual OUString SAL_CALL
getImplementationName() throw(RuntimeException
, std::exception
) SAL_OVERRIDE
;
67 virtual sal_Bool SAL_CALL
supportsService( const OUString
& _rServiceName
) throw(RuntimeException
, std::exception
) SAL_OVERRIDE
;
68 virtual Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() throw(RuntimeException
, std::exception
) SAL_OVERRIDE
;
71 // XServiceInfo - static version
72 static OUString SAL_CALL
getImplementationName_static();
73 static Sequence
< OUString
> SAL_CALL
getSupportedServiceNames_static();
74 static Reference
< XInterface
> Create( const Reference
< XComponentContext
>& _rxContext
);
77 PlainTextFormatter::PlainTextFormatter()
82 PlainTextFormatter::~PlainTextFormatter()
87 OUString SAL_CALL
PlainTextFormatter::getHead( ) throw (RuntimeException
, std::exception
)
89 OUStringBuffer aHeader
;
90 aHeader
.appendAscii( " event no" ); // column 1: the event number
91 aHeader
.appendAscii( " " );
92 aHeader
.appendAscii( "thread " ); // column 2: the thread ID
93 aHeader
.appendAscii( " " );
94 aHeader
.appendAscii( "date " ); // column 3: date
95 aHeader
.appendAscii( " " );
96 aHeader
.appendAscii( "time " ); // column 4: time
97 aHeader
.appendAscii( " " );
98 aHeader
.appendAscii( "(class/method:) message" ); // column 5: class/method/message
99 aHeader
.appendAscii( "\n" );
100 return aHeader
.makeStringAndClear();
104 OUString SAL_CALL
PlainTextFormatter::format( const LogRecord
& _rRecord
) throw (RuntimeException
, std::exception
)
107 const int buffer_size
= sizeof( buffer
);
108 int used
= snprintf( buffer
, buffer_size
, "%10i", (int)_rRecord
.SequenceNumber
);
109 if ( used
>= buffer_size
|| used
< 0 )
110 buffer
[ buffer_size
- 1 ] = 0;
112 OUStringBuffer aLogEntry
;
113 aLogEntry
.appendAscii( buffer
);
114 aLogEntry
.appendAscii( " " );
116 OString
sThreadID( OUStringToOString( _rRecord
.ThreadID
, osl_getThreadTextEncoding() ) );
117 snprintf( buffer
, buffer_size
, "%8s", sThreadID
.getStr() );
118 aLogEntry
.appendAscii( buffer
);
119 aLogEntry
.appendAscii( " " );
121 snprintf( buffer
, buffer_size
, "%04i-%02i-%02i %02i:%02i:%02i.%09i",
122 (int)_rRecord
.LogTime
.Year
, (int)_rRecord
.LogTime
.Month
, (int)_rRecord
.LogTime
.Day
,
123 (int)_rRecord
.LogTime
.Hours
, (int)_rRecord
.LogTime
.Minutes
, (int)_rRecord
.LogTime
.Seconds
, (int)_rRecord
.LogTime
.NanoSeconds
);
124 aLogEntry
.appendAscii( buffer
);
125 aLogEntry
.appendAscii( " " );
127 if ( !(_rRecord
.SourceClassName
.isEmpty() || _rRecord
.SourceMethodName
.isEmpty()) )
129 aLogEntry
.append( _rRecord
.SourceClassName
);
130 aLogEntry
.appendAscii( "::" );
131 aLogEntry
.append( _rRecord
.SourceMethodName
);
132 aLogEntry
.appendAscii( ": " );
135 aLogEntry
.append( _rRecord
.Message
);
136 aLogEntry
.appendAscii( "\n" );
138 return aLogEntry
.makeStringAndClear();
142 OUString SAL_CALL
PlainTextFormatter::getTail( ) throw (RuntimeException
, std::exception
)
148 sal_Bool SAL_CALL
PlainTextFormatter::supportsService( const OUString
& _rServiceName
) throw(RuntimeException
, std::exception
)
150 return cppu::supportsService(this, _rServiceName
);
154 OUString SAL_CALL
PlainTextFormatter::getImplementationName() throw(RuntimeException
, std::exception
)
156 return getImplementationName_static();
160 Sequence
< OUString
> SAL_CALL
PlainTextFormatter::getSupportedServiceNames() throw(RuntimeException
, std::exception
)
162 return getSupportedServiceNames_static();
166 OUString SAL_CALL
PlainTextFormatter::getImplementationName_static()
168 return OUString( "com.sun.star.comp.extensions.PlainTextFormatter" );
172 Sequence
< OUString
> SAL_CALL
PlainTextFormatter::getSupportedServiceNames_static()
174 Sequence
< OUString
> aServiceNames(1);
175 aServiceNames
[0] = "com.sun.star.logging.PlainTextFormatter";
176 return aServiceNames
;
180 Reference
< XInterface
> PlainTextFormatter::Create( const Reference
< XComponentContext
>& )
182 return *( new PlainTextFormatter
);
186 void createRegistryInfo_PlainTextFormatter()
188 static OAutoRegistration
< PlainTextFormatter
> aAutoRegistration
;
192 } // namespace logging
195 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */