1*87d2adbcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*87d2adbcSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*87d2adbcSAndrew Rist * or more contributor license agreements. See the NOTICE file
5*87d2adbcSAndrew Rist * distributed with this work for additional information
6*87d2adbcSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*87d2adbcSAndrew Rist * to you under the Apache License, Version 2.0 (the
8*87d2adbcSAndrew Rist * "License"); you may not use this file except in compliance
9*87d2adbcSAndrew Rist * with the License. You may obtain a copy of the License at
10*87d2adbcSAndrew Rist *
11*87d2adbcSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*87d2adbcSAndrew Rist *
13*87d2adbcSAndrew Rist * Unless required by applicable law or agreed to in writing,
14*87d2adbcSAndrew Rist * software distributed under the License is distributed on an
15*87d2adbcSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*87d2adbcSAndrew Rist * KIND, either express or implied. See the License for the
17*87d2adbcSAndrew Rist * specific language governing permissions and limitations
18*87d2adbcSAndrew Rist * under the License.
19*87d2adbcSAndrew Rist *
20*87d2adbcSAndrew Rist *************************************************************/
21*87d2adbcSAndrew Rist
22*87d2adbcSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir
25cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
26cdf0e10cSrcweir #include "precompiled_sal.hxx"
27cdf0e10cSrcweir // LLA:
28cdf0e10cSrcweir // this file is converted to use with testshl2
29cdf0e10cSrcweir // original was placed in sal/test/textenc.cxx
30cdf0e10cSrcweir
31cdf0e10cSrcweir
32cdf0e10cSrcweir // -----------------------------------------------------------------------------
33cdf0e10cSrcweir #include <stdio.h>
34cdf0e10cSrcweir #include <stdlib.h>
35cdf0e10cSrcweir #include <string.h>
36cdf0e10cSrcweir
37cdf0e10cSrcweir #if defined(UNX) || defined(OS2)
38cdf0e10cSrcweir # include <unistd.h>
39cdf0e10cSrcweir #endif
40cdf0e10cSrcweir
41cdf0e10cSrcweir #include <rtl/logfile.hxx>
42cdf0e10cSrcweir #include <testshl/simpleheader.hxx>
43cdf0e10cSrcweir
44cdf0e10cSrcweir // #ifndef _OSL_MODULE_HXX_
45cdf0e10cSrcweir // #include <osl/module.hxx>
46cdf0e10cSrcweir // #endif
47cdf0e10cSrcweir #include <osl/file.hxx>
48cdf0e10cSrcweir #if ( defined WNT ) // Windows
49cdf0e10cSrcweir #include <tools/prewin.h>
50cdf0e10cSrcweir // #define UNICODE
51cdf0e10cSrcweir // #define WIN32_LEAN_AND_MEAN
52cdf0e10cSrcweir // #include <windows.h>
53cdf0e10cSrcweir #include <tchar.h>
54cdf0e10cSrcweir #include <tools/postwin.h>
55cdf0e10cSrcweir #endif
56cdf0e10cSrcweir
57cdf0e10cSrcweir using namespace ::osl;
58cdf0e10cSrcweir
printUString(const::rtl::OUString & str,const sal_Char * msg="")59cdf0e10cSrcweir inline void printUString( const ::rtl::OUString & str, const sal_Char * msg = "" )
60cdf0e10cSrcweir {
61cdf0e10cSrcweir
62cdf0e10cSrcweir if (strlen(msg) > 0)
63cdf0e10cSrcweir {
64cdf0e10cSrcweir t_print("%s: ", msg );
65cdf0e10cSrcweir }
66cdf0e10cSrcweir rtl::OString aString;
67cdf0e10cSrcweir aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US );
68cdf0e10cSrcweir t_print("%s\n", (char *)aString.getStr( ) );
69cdf0e10cSrcweir }
70cdf0e10cSrcweir
71cdf0e10cSrcweir /** get the absolute source file URL "file:///.../sal/qa/rtl/logfile/"
72cdf0e10cSrcweir */
73cdf0e10cSrcweir
getTempPath(void)74cdf0e10cSrcweir inline ::rtl::OUString getTempPath( void )
75cdf0e10cSrcweir {
76cdf0e10cSrcweir #ifdef UNX
77cdf0e10cSrcweir rtl::OUString suDirURL(rtl::OUString::createFromAscii("file:///tmp/"));
78cdf0e10cSrcweir #else /* Windows */
79cdf0e10cSrcweir rtl::OUString suDirURL(rtl::OUString::createFromAscii("file:///c:/temp/"));
80cdf0e10cSrcweir #endif
81cdf0e10cSrcweir return suDirURL;
82cdf0e10cSrcweir }
83cdf0e10cSrcweir
84cdf0e10cSrcweir /** if the file exist
85cdf0e10cSrcweir */
t_fileExist(rtl::OUString const & _sFilename)86cdf0e10cSrcweir bool t_fileExist(rtl::OUString const& _sFilename)
87cdf0e10cSrcweir {
88cdf0e10cSrcweir ::osl::FileBase::RC nError1;
89cdf0e10cSrcweir ::osl::File aTestFile( _sFilename );
90cdf0e10cSrcweir nError1 = aTestFile.open ( OpenFlag_Read );
91cdf0e10cSrcweir if ( ( ::osl::FileBase::E_NOENT != nError1 ) && ( ::osl::FileBase::E_ACCES != nError1 ) )
92cdf0e10cSrcweir {
93cdf0e10cSrcweir aTestFile.close( );
94cdf0e10cSrcweir return true;
95cdf0e10cSrcweir }
96cdf0e10cSrcweir return false;
97cdf0e10cSrcweir }
98cdf0e10cSrcweir /** get Current PID.
99cdf0e10cSrcweir */
getCurrentPID()100cdf0e10cSrcweir inline ::rtl::OUString getCurrentPID( )
101cdf0e10cSrcweir {
102cdf0e10cSrcweir //~ Get current PID and turn it into OUString;
103cdf0e10cSrcweir int nPID = 0;
104cdf0e10cSrcweir #ifdef WNT
105cdf0e10cSrcweir nPID = GetCurrentProcessId();
106cdf0e10cSrcweir #else
107cdf0e10cSrcweir nPID = getpid();
108cdf0e10cSrcweir #endif
109cdf0e10cSrcweir return ( ::rtl::OUString::valueOf( ( long )nPID ) );
110cdf0e10cSrcweir }
111cdf0e10cSrcweir
112cdf0e10cSrcweir
113cdf0e10cSrcweir // -----------------------------------------------------------------------------
114cdf0e10cSrcweir /*
115cdf0e10cSrcweir * LLA:
116cdf0e10cSrcweir * check if logfile is create
117cdf0e10cSrcweir * be careful with relative logfiles they will create near the source, maybe it's no write access to it.
118cdf0e10cSrcweir * use absolute path to logfile instead.
119cdf0e10cSrcweir */
120cdf0e10cSrcweir namespace rtl_logfile
121cdf0e10cSrcweir {
122cdf0e10cSrcweir class logfile : public CppUnit::TestFixture
123cdf0e10cSrcweir {
124cdf0e10cSrcweir public:
125cdf0e10cSrcweir
126cdf0e10cSrcweir //directly call rtl_logfile_trace
logfile_001()127cdf0e10cSrcweir void logfile_001()
128cdf0e10cSrcweir {
129cdf0e10cSrcweir #ifdef SOLARIS
130cdf0e10cSrcweir putenv(const_cast< char * >("RTL_LOGFILE=/tmp/logfile1"));
131cdf0e10cSrcweir #endif
132cdf0e10cSrcweir #ifdef WNT
133cdf0e10cSrcweir putenv("RTL_LOGFILE=c:\\temp\\logfile1");
134cdf0e10cSrcweir #endif
135cdf0e10cSrcweir #ifdef LINUX
136cdf0e10cSrcweir setenv("RTL_LOGFILE", "/tmp/logfile1", 0);
137cdf0e10cSrcweir #endif
138cdf0e10cSrcweir rtl_logfile_trace("trace %d\n", 2 );
139cdf0e10cSrcweir rtl_logfile_trace("trace %d %d\n" , 1,2 );
140cdf0e10cSrcweir rtl_logfile_trace("trace %d %d %d\n" , 1 , 2 ,3 );
141cdf0e10cSrcweir
142cdf0e10cSrcweir rtl::OUString suFilePath = getTempPath();
143cdf0e10cSrcweir suFilePath += rtl::OUString::createFromAscii("logfile1_") + getCurrentPID( );
144cdf0e10cSrcweir suFilePath += rtl::OUString::createFromAscii(".log");
145cdf0e10cSrcweir
146cdf0e10cSrcweir ::osl::FileBase::RC nError1;
147cdf0e10cSrcweir ::osl::File aTestFile( suFilePath );
148cdf0e10cSrcweir printUString( suFilePath );
149cdf0e10cSrcweir nError1 = aTestFile.open ( OpenFlag_Read );
150cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("create the log file: but the logfile does not exist",
151cdf0e10cSrcweir ( ::osl::FileBase::E_NOENT != nError1 ) &&
152cdf0e10cSrcweir ( ::osl::FileBase::E_ACCES != nError1 ) );
153cdf0e10cSrcweir sal_Char buffer_read[400];
154cdf0e10cSrcweir sal_uInt64 nCount_read;
155cdf0e10cSrcweir nError1 = aTestFile.read( buffer_read, 400, nCount_read );
156cdf0e10cSrcweir //t_print("buffer is %s\n", buffer_read );
157cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("write right logs", strstr( buffer_read, "trace 1 2 3") != NULL );
158cdf0e10cSrcweir aTestFile.sync();
159cdf0e10cSrcweir aTestFile.close();
160cdf0e10cSrcweir /*// delete logfile on the disk
161cdf0e10cSrcweir
162cdf0e10cSrcweir nError1 = osl::File::remove( suFilePath );
163cdf0e10cSrcweir printError( nError1 );
164cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "In deleteTestFile Function: remove ", ( ::osl::FileBase::E_None == nError1 ) || ( nError1 == ::osl::FileBase::E_NOENT ) );
165cdf0e10cSrcweir */
166cdf0e10cSrcweir }
167cdf0e10cSrcweir //Profiling output should only be generated for a special product version of OpenOffice
168cdf0e10cSrcweir // which is compiled with a defined preprocessor symbol 'TIMELOG'. Now, the symbol not defined
logfile_002()169cdf0e10cSrcweir void logfile_002()
170cdf0e10cSrcweir {
171cdf0e10cSrcweir #ifdef SOLARIS
172cdf0e10cSrcweir putenv(const_cast< char * >("RTL_LOGFILE=/tmp/logfile2"));
173cdf0e10cSrcweir #endif
174cdf0e10cSrcweir #ifdef WNT
175cdf0e10cSrcweir putenv("RTL_LOGFILE=c:\\temp\\logfile2");
176cdf0e10cSrcweir #endif
177cdf0e10cSrcweir #ifdef LINUX
178cdf0e10cSrcweir setenv("RTL_LOGFILE", "/tmp/logfile2", 0);
179cdf0e10cSrcweir #endif
180cdf0e10cSrcweir RTL_LOGFILE_TRACE( "trace the log" );
181cdf0e10cSrcweir RTL_LOGFILE_TRACE1( "trace %d" , 1 );
182cdf0e10cSrcweir RTL_LOGFILE_TRACE2( "trace %d %d" , 1,2 );
183cdf0e10cSrcweir RTL_LOGFILE_TRACE3( "trace %d %d %d" , 1 , 2 ,3 );
184cdf0e10cSrcweir // TODO: assertion test!
185cdf0e10cSrcweir }
186cdf0e10cSrcweir
logfile_003()187cdf0e10cSrcweir void logfile_003()
188cdf0e10cSrcweir {
189cdf0e10cSrcweir #ifdef SOLARIS
190cdf0e10cSrcweir putenv(const_cast< char * >("RTL_LOGFILE=/tmp/logfile2"));
191cdf0e10cSrcweir #endif
192cdf0e10cSrcweir #ifdef WNT
193cdf0e10cSrcweir putenv("RTL_LOGFILE=c:\\temp\\logfile2");
194cdf0e10cSrcweir #endif
195cdf0e10cSrcweir #ifdef LINUX
196cdf0e10cSrcweir setenv("RTL_LOGFILE", "/tmp/logfile2", 0);
197cdf0e10cSrcweir #endif
198cdf0e10cSrcweir RTL_LOGFILE_CONTEXT ( foo , "foo-function" );
199cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_TRACE ( foo , "trace" );
200cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_TRACE1 ( foo , "trace %d" , 1 );
201cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_TRACE2 ( foo , "trace %d %d" , 1 , 2 );
202cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_TRACE3 ( foo , "trace %d %d %d" , 1 , 2 , 3);
203cdf0e10cSrcweir // TODO: assertion test!
204cdf0e10cSrcweir }
205cdf0e10cSrcweir
206cdf0e10cSrcweir
207cdf0e10cSrcweir CPPUNIT_TEST_SUITE( logfile );
208cdf0e10cSrcweir CPPUNIT_TEST( logfile_001 );
209cdf0e10cSrcweir CPPUNIT_TEST( logfile_002 );
210cdf0e10cSrcweir CPPUNIT_TEST( logfile_003 );
211cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END( );
212cdf0e10cSrcweir };
213cdf0e10cSrcweir
214cdf0e10cSrcweir } // namespace rtl_logfile
215cdf0e10cSrcweir
216cdf0e10cSrcweir // -----------------------------------------------------------------------------
217cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( rtl_logfile::logfile, "rtl_logfile" );
218cdf0e10cSrcweir
219cdf0e10cSrcweir // -----------------------------------------------------------------------------
220cdf0e10cSrcweir NOADDITIONAL;
221cdf0e10cSrcweir
222cdf0e10cSrcweir //~ do some clean up work after all test completed.
223cdf0e10cSrcweir class GlobalObject
224cdf0e10cSrcweir {
225cdf0e10cSrcweir public:
~GlobalObject()226cdf0e10cSrcweir ~GlobalObject()
227cdf0e10cSrcweir {
228cdf0e10cSrcweir try
229cdf0e10cSrcweir {
230cdf0e10cSrcweir t_print( "\n#Do some clean-ups ... only delete logfile1_*.log here!\n" );
231cdf0e10cSrcweir rtl::OUString suFilePath = getTempPath();
232cdf0e10cSrcweir suFilePath += rtl::OUString::createFromAscii("logfile1_") + getCurrentPID( );
233cdf0e10cSrcweir suFilePath += rtl::OUString::createFromAscii(".log");
234cdf0e10cSrcweir
235cdf0e10cSrcweir //if ( ifFileExist( suFilePath ) == sal_True )
236cdf0e10cSrcweir ::osl::FileBase::RC nError1;
237cdf0e10cSrcweir nError1 = osl::File::remove( suFilePath );
238cdf0e10cSrcweir #ifdef WNT
239cdf0e10cSrcweir t_print("Please remove logfile* manully! Error is Permision denied!");
240cdf0e10cSrcweir #endif
241cdf0e10cSrcweir }
242cdf0e10cSrcweir catch (CppUnit::Exception &e)
243cdf0e10cSrcweir {
244cdf0e10cSrcweir t_print("Exception caught in GlobalObject dtor(). Exception message: '%s'. Source line: %d\n", e.what(), e.sourceLine().lineNumber());
245cdf0e10cSrcweir }
246cdf0e10cSrcweir catch (...)
247cdf0e10cSrcweir {
248cdf0e10cSrcweir t_print("Exception caught (...) in GlobalObject dtor()\n");
249cdf0e10cSrcweir }
250cdf0e10cSrcweir }
251cdf0e10cSrcweir };
252cdf0e10cSrcweir
253cdf0e10cSrcweir GlobalObject theGlobalObject;
254cdf0e10cSrcweir
255cdf0e10cSrcweir
256cdf0e10cSrcweir
257