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