1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_filter.hxx"
26 #include "filtertracer.hxx"
27 #include <uno/mapping.hxx>
28 #include <unotools/streamwrap.hxx>
29 #include <unotools/ucbstreamhelper.hxx>
30 // ----------------
31 // - FILTERTRACER -
32 // ----------------
33 
FilterTracer_getImplementationName()34 rtl::OUString FilterTracer_getImplementationName()
35 	throw( NMSP_UNO::RuntimeException )
36 {
37 	return B2UCONST( "com.sun.star.util.FilterTracer" );
38 }
FilterTracer_supportsService(const rtl::OUString & ServiceName)39 sal_Bool SAL_CALL FilterTracer_supportsService( const rtl::OUString& ServiceName )
40 	throw( NMSP_UNO::RuntimeException )
41 {
42     return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "com.sun.star.util.logging.Logger" ) );
43 }
SEQ(rtl::OUString)44 SEQ( rtl::OUString ) SAL_CALL FilterTracer_getSupportedServiceNames()
45 	throw( NMSP_UNO::RuntimeException )
46 {
47 	SEQ( rtl::OUString ) aRet(1);
48     rtl::OUString* pArray = aRet.getArray();
49     pArray[0] = B2UCONST( "com.sun.star.util.logging.Logger" );
50     return aRet;
51 }
52 
53 // -----------------------------------------------------------------------------
54 
FilterTracer(const REF (NMSP_LANG::XMultiServiceFactory)& rxMgr)55 FilterTracer::FilterTracer( const REF( NMSP_LANG::XMultiServiceFactory )& rxMgr ) :
56 	xFact		( rxMgr ),
57 	mpStream	( NULL ),
58 	mnLogLevel	( NMSP_LOGGING::LogLevel::ALL )
59 {
60 	REF( NMSP_UNO::XInterface ) xObj( rxMgr->createInstance( ::rtl::OUString::createFromAscii( "com.sun.star.util.TextSearch" ) ) );
61 	mxTextSearch = REF( NMSP_UTIL::XTextSearch )( xObj, ::com::sun::star::uno::UNO_QUERY );
62 }
~FilterTracer()63 FilterTracer::~FilterTracer()
64 {
65 	if ( mpStream )
66 	{
67 		mxOutputStream = NULL;
68 		delete mpStream;
69 	}
70 }
71 
72 // -----------------------------------------------------------------------------
73 
74 // XInterface
acquire()75 void SAL_CALL FilterTracer::acquire() throw()
76 {
77 	OWeakObject::acquire();
78 }
release()79 void SAL_CALL FilterTracer::release() throw()
80 {
81 	OWeakObject::release();
82 }
83 
84 // -----------------------------------------------------------------------------
85 
86 // checks if the tokens of rFilter can be found in rString
ImplFilter(const rtl::OUString & rFilter,const rtl::OUString & rString)87 sal_Bool FilterTracer::ImplFilter( const rtl::OUString& rFilter, const rtl::OUString& rString )
88 {
89 	sal_Bool bFilter = sal_False;
90 	if ( mxTextSearch.is() )
91 	{
92 		maSearchOptions.searchString = rFilter;
93 		mxTextSearch->setOptions( maSearchOptions );
94 		NMSP_UTIL::SearchResult aSearchResult = mxTextSearch->searchForward( rString, 0, rString.getLength() );
95 		bFilter = aSearchResult.subRegExpressions != 0;
96 	}
97 	return bFilter;
98 }
99 
100 // -----------------------------------------------------------------------------
101 
102 // XInitialization
initialize(const SEQ (NMSP_UNO::Any)& aArguments)103 void SAL_CALL FilterTracer::initialize( const SEQ( NMSP_UNO::Any )& aArguments )
104 	throw ( NMSP_UNO::Exception, NMSP_UNO::RuntimeException )
105 {
106 	sal_Int32 i;
107 	SEQ( NMSP_BEANS::PropertyValue ) aParameter;
108 	for ( i = 0; i < aArguments.getLength(); i++ )
109 	{
110 		if ( aArguments[ i ] >>= aParameter )
111 			break;
112 	}
113 	for ( i = 0; i < aParameter.getLength(); i++ )
114 	{
115 		const NMSP_BEANS::PropertyValue& rProp = aParameter[ i ];
116 		if ( rProp.Name.equalsAscii( "LogLevel" ) )
117 			rProp.Value >>= mnLogLevel;
118 		else if ( rProp.Name.equalsAscii( "ClassFilter" ) )
119 			rProp.Value >>= msClassFilter;
120 		else if ( rProp.Name.equalsAscii( "MethodFilter" ) )
121 			rProp.Value >>= msMethodFilter;
122 		else if ( rProp.Name.equalsAscii( "MessageFilter" ) )
123 			rProp.Value >>= msMessageFilter;
124 		else if ( rProp.Name.equalsAscii( "OutputStream" ) )
125 			rProp.Value >>= mxOutputStream;
126 		else if ( rProp.Name.equalsAscii( "URL" ) )
127 			rProp.Value >>= msURL;
128 		else if ( rProp.Name.equalsAscii( "DocumentHandler" ) )
129 			rProp.Value >>= mxDocumentHandler;
130 	}
131 
132 	// check if we have to create the XOutputStream
133 	if ( !mxOutputStream.is() && msURL.getLength() )
134 	{
135 		mpStream = ::utl::UcbStreamHelper::CreateStream( msURL, STREAM_WRITE | STREAM_TRUNC | STREAM_SHARE_DENYNONE );
136 		if ( mpStream )
137 		{
138 			::utl::OOutputStreamWrapper* pHelper = new ::utl::OOutputStreamWrapper( *mpStream );
139 			mxOutputStream = pHelper;
140 		}
141 	}
142 }
143 
144 // -----------------------------------------------------------------------------
145 
146 // XServiceInfo
getImplementationName()147 rtl::OUString SAL_CALL FilterTracer::getImplementationName()
148 	throw( NMSP_UNO::RuntimeException )
149 {
150 	return FilterTracer_getImplementationName();
151 }
supportsService(const rtl::OUString & rServiceName)152 sal_Bool SAL_CALL FilterTracer::supportsService( const rtl::OUString& rServiceName )
153 	throw( NMSP_UNO::RuntimeException )
154 {
155     return FilterTracer_supportsService( rServiceName );
156 }
SEQ(rtl::OUString)157 SEQ( rtl::OUString ) SAL_CALL FilterTracer::getSupportedServiceNames()
158 	throw ( NMSP_UNO::RuntimeException )
159 {
160     return FilterTracer_getSupportedServiceNames();
161 }
162 
163 // -----------------------------------------------------------------------------
164 
165 // XLogger
REF(NMSP_LOGGING::XLogger)166 REF( NMSP_LOGGING::XLogger ) SAL_CALL  FilterTracer::getLogger( const rtl::OUString& /* rName */ )
167      throw (::com::sun::star::uno::RuntimeException)
168 {
169 	REF( NMSP_LOGGING::XLogger ) xLog( this );
170 	return xLog;
171 }
getLevel()172 sal_Int32 SAL_CALL FilterTracer::getLevel() throw (::com::sun::star::uno::RuntimeException)
173 {
174 	return mnLogLevel;
175 }
getName()176 rtl::OUString SAL_CALL FilterTracer::getName() throw (::com::sun::star::uno::RuntimeException)
177 {
178 	rtl::OUString aName;
179 	return aName;
180 }
isLoggable(sal_Int32 nLevel)181 sal_Bool SAL_CALL FilterTracer::isLoggable( sal_Int32 nLevel )
182      throw (::com::sun::star::uno::RuntimeException)
183 {
184 	return mnLogLevel <= nLevel;
185 }
logp(sal_Int32,const rtl::OUString & rSourceClass,const rtl::OUString & rSourceMethod,const rtl::OUString & rMessage)186 void SAL_CALL FilterTracer::logp( sal_Int32 /* nLevel */, const rtl::OUString& rSourceClass,
187 		const rtl::OUString& rSourceMethod, const rtl::OUString& rMessage )
188      throw (::com::sun::star::uno::RuntimeException)
189 {
190 	if ( mxOutputStream.is() || mxDocumentHandler.is() )
191 	{
192 		if ( ! ( ImplFilter( msClassFilter, rSourceClass ) || ImplFilter( msMethodFilter, rSourceMethod )
193 			|| ImplFilter( msMessageFilter, rMessage ) ) )
194 		{
195 			rtl::OString sClass( rtl::OUStringToOString( rSourceClass, RTL_TEXTENCODING_UTF8 ) );
196 			rtl::OString sMethod( rtl::OUStringToOString( rSourceMethod, RTL_TEXTENCODING_UTF8 ) );
197 			rtl::OString sMessage( rtl::OUStringToOString( rMessage, RTL_TEXTENCODING_UTF8 ) );
198 			try
199 			{
200 				SEQ( sal_Int8 ) aData( sClass.getLength() + sMethod.getLength() + sMessage.getLength() );
201 				sal_Int8* pPtr = aData.getArray();
202 				memcpy( pPtr, sClass.getStr(), sClass.getLength() );
203 				pPtr += sClass.getLength();
204 				memcpy( pPtr, sMethod.getStr(), sMethod.getLength() );
205 				pPtr += sMethod.getLength();
206 				memcpy( pPtr, sMessage.getStr(), sMessage.getLength() );
207 				pPtr += sMessage.getLength();
208 				if ( mxOutputStream.is() )
209 					mxOutputStream->writeBytes( aData );
210 				if ( mxDocumentHandler.is() )
211 					mxDocumentHandler->characters( ::rtl::OUString( (sal_Char*)aData.getArray(), aData.getLength(), RTL_TEXTENCODING_UTF8 ) );
212 			}
213 			catch ( ... )
214 			{
215 
216 			}
217 		}
218 	}
219 }
220 
221 // -----------------------------------------------------------------------------
222 
223 // XTextSearch
setOptions(const NMSP_UTIL::SearchOptions & rSearchOptions)224 void SAL_CALL  FilterTracer::setOptions( const NMSP_UTIL::SearchOptions& rSearchOptions )
225     throw (::com::sun::star::uno::RuntimeException)
226 {
227 	maSearchOptions = rSearchOptions;
228 }
229 
230 // -----------------------------------------------------------------------------
231 
searchForward(const rtl::OUString & rSearchStr,sal_Int32 nStartPos,sal_Int32 nEndPos)232 NMSP_UTIL::SearchResult SAL_CALL FilterTracer::searchForward( const rtl::OUString& rSearchStr,
233 		sal_Int32 nStartPos, sal_Int32 nEndPos ) throw (::com::sun::star::uno::RuntimeException)
234 {
235 	NMSP_UTIL::SearchResult nSearchResult;
236 	if ( mxTextSearch.is() )
237 		mxTextSearch->searchForward( rSearchStr, nStartPos, nEndPos );
238 	return nSearchResult;
239 }
240 
241 // -----------------------------------------------------------------------------
242 
searchBackward(const rtl::OUString & rSearchStr,sal_Int32 nStartPos,sal_Int32 nEndPos)243 NMSP_UTIL::SearchResult SAL_CALL FilterTracer::searchBackward( const rtl::OUString& rSearchStr,
244 		sal_Int32 nStartPos, sal_Int32 nEndPos ) throw (::com::sun::star::uno::RuntimeException)
245 {
246 	NMSP_UTIL::SearchResult nSearchResult;
247 	if ( mxTextSearch.is() )
248 		mxTextSearch->searchBackward( rSearchStr, nStartPos, nEndPos );
249 	return nSearchResult;
250 }
251 
252 
253