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