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 :
25 : #include <com/sun/star/logging/XLogFormatter.hpp>
26 : #include <com/sun/star/uno/XComponentContext.hpp>
27 : #include <com/sun/star/lang/XServiceInfo.hpp>
28 :
29 : #include <cppuhelper/implbase2.hxx>
30 : #include <cppuhelper/supportsservice.hxx>
31 :
32 : #include <rtl/ustrbuf.hxx>
33 :
34 : #include <osl/thread.h>
35 :
36 :
37 : namespace logging
38 : {
39 :
40 :
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 :
51 : //= PlainTextFormatter - declaration
52 :
53 : typedef ::cppu::WeakImplHelper2 < XLogFormatter
54 : , XServiceInfo
55 : > PlainTextFormatter_Base;
56 : class PlainTextFormatter : public PlainTextFormatter_Base
57 : {
58 : protected:
59 : PlainTextFormatter();
60 : virtual ~PlainTextFormatter();
61 :
62 : // XLogFormatter
63 : virtual OUString SAL_CALL getHead( ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
64 : virtual OUString SAL_CALL format( const LogRecord& Record ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
65 : virtual OUString SAL_CALL getTail( ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
66 :
67 : // XServiceInfo
68 : virtual OUString SAL_CALL getImplementationName() throw(RuntimeException, std::exception) SAL_OVERRIDE;
69 : virtual sal_Bool SAL_CALL supportsService( const OUString& _rServiceName ) throw(RuntimeException, std::exception) SAL_OVERRIDE;
70 : virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() throw(RuntimeException, std::exception) SAL_OVERRIDE;
71 :
72 : public:
73 : // XServiceInfo - static version
74 : static OUString SAL_CALL getImplementationName_static();
75 : static Sequence< OUString > SAL_CALL getSupportedServiceNames_static();
76 : static Reference< XInterface > Create( const Reference< XComponentContext >& _rxContext );
77 : };
78 :
79 :
80 : //= PlainTextFormatter - implementation
81 :
82 :
83 0 : PlainTextFormatter::PlainTextFormatter()
84 : {
85 0 : }
86 :
87 :
88 0 : PlainTextFormatter::~PlainTextFormatter()
89 : {
90 0 : }
91 :
92 :
93 0 : OUString SAL_CALL PlainTextFormatter::getHead( ) throw (RuntimeException, std::exception)
94 : {
95 0 : OUStringBuffer aHeader;
96 0 : aHeader.appendAscii( " event no" ); // column 1: the event number
97 0 : aHeader.appendAscii( " " );
98 0 : aHeader.appendAscii( "thread " ); // column 2: the thread ID
99 0 : aHeader.appendAscii( " " );
100 0 : aHeader.appendAscii( "date " ); // column 3: date
101 0 : aHeader.appendAscii( " " );
102 0 : aHeader.appendAscii( "time " ); // column 4: time
103 0 : aHeader.appendAscii( " " );
104 0 : aHeader.appendAscii( "(class/method:) message" ); // column 5: class/method/message
105 0 : aHeader.appendAscii( "\n" );
106 0 : return aHeader.makeStringAndClear();
107 : }
108 :
109 :
110 0 : OUString SAL_CALL PlainTextFormatter::format( const LogRecord& _rRecord ) throw (RuntimeException, std::exception)
111 : {
112 : char buffer[ 30 ];
113 0 : const int buffer_size = sizeof( buffer );
114 0 : int used = snprintf( buffer, buffer_size, "%10i", (int)_rRecord.SequenceNumber );
115 0 : if ( used >= buffer_size || used < 0 )
116 0 : buffer[ buffer_size - 1 ] = 0;
117 :
118 0 : OUStringBuffer aLogEntry;
119 0 : aLogEntry.appendAscii( buffer );
120 0 : aLogEntry.appendAscii( " " );
121 :
122 0 : OString sThreadID( OUStringToOString( _rRecord.ThreadID, osl_getThreadTextEncoding() ) );
123 0 : snprintf( buffer, buffer_size, "%8s", sThreadID.getStr() );
124 0 : aLogEntry.appendAscii( buffer );
125 0 : aLogEntry.appendAscii( " " );
126 :
127 : snprintf( buffer, buffer_size, "%04i-%02i-%02i %02i:%02i:%02i.%09i",
128 : (int)_rRecord.LogTime.Year, (int)_rRecord.LogTime.Month, (int)_rRecord.LogTime.Day,
129 0 : (int)_rRecord.LogTime.Hours, (int)_rRecord.LogTime.Minutes, (int)_rRecord.LogTime.Seconds, (int)_rRecord.LogTime.NanoSeconds );
130 0 : aLogEntry.appendAscii( buffer );
131 0 : aLogEntry.appendAscii( " " );
132 :
133 0 : if ( !(_rRecord.SourceClassName.isEmpty() || _rRecord.SourceMethodName.isEmpty()) )
134 : {
135 0 : aLogEntry.append( _rRecord.SourceClassName );
136 0 : aLogEntry.appendAscii( "::" );
137 0 : aLogEntry.append( _rRecord.SourceMethodName );
138 0 : aLogEntry.appendAscii( ": " );
139 : }
140 :
141 0 : aLogEntry.append( _rRecord.Message );
142 0 : aLogEntry.appendAscii( "\n" );
143 :
144 0 : return aLogEntry.makeStringAndClear();
145 : }
146 :
147 :
148 0 : OUString SAL_CALL PlainTextFormatter::getTail( ) throw (RuntimeException, std::exception)
149 : {
150 : // no tail
151 0 : return OUString();
152 : }
153 :
154 0 : sal_Bool SAL_CALL PlainTextFormatter::supportsService( const OUString& _rServiceName ) throw(RuntimeException, std::exception)
155 : {
156 0 : return cppu::supportsService(this, _rServiceName);
157 : }
158 :
159 :
160 0 : OUString SAL_CALL PlainTextFormatter::getImplementationName() throw(RuntimeException, std::exception)
161 : {
162 0 : return getImplementationName_static();
163 : }
164 :
165 :
166 0 : Sequence< OUString > SAL_CALL PlainTextFormatter::getSupportedServiceNames() throw(RuntimeException, std::exception)
167 : {
168 0 : return getSupportedServiceNames_static();
169 : }
170 :
171 :
172 0 : OUString SAL_CALL PlainTextFormatter::getImplementationName_static()
173 : {
174 0 : return OUString( "com.sun.star.comp.extensions.PlainTextFormatter" );
175 : }
176 :
177 :
178 0 : Sequence< OUString > SAL_CALL PlainTextFormatter::getSupportedServiceNames_static()
179 : {
180 0 : Sequence< OUString > aServiceNames(1);
181 0 : aServiceNames[0] = "com.sun.star.logging.PlainTextFormatter";
182 0 : return aServiceNames;
183 : }
184 :
185 :
186 0 : Reference< XInterface > PlainTextFormatter::Create( const Reference< XComponentContext >& )
187 : {
188 0 : return *( new PlainTextFormatter );
189 : }
190 :
191 :
192 0 : void createRegistryInfo_PlainTextFormatter()
193 : {
194 0 : static OAutoRegistration< PlainTextFormatter > aAutoRegistration;
195 0 : }
196 :
197 :
198 : } // namespace logging
199 :
200 :
201 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|