1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: rtl_logfile.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 // MARKER(update_precomp.py): autogen include statement, do not remove
33 #include "precompiled_sal.hxx"
35 // this file is converted to use with testshl2
36 // original was placed in sal/test/textenc.cxx
39 // -----------------------------------------------------------------------------
44 #if defined(UNX) || defined(OS2)
48 #include <rtl/logfile.hxx>
49 #include <cppunit/simpleheader.hxx>
51 // #ifndef _OSL_MODULE_HXX_
52 // #include <osl/module.hxx>
54 #include <osl/file.hxx>
55 #if ( defined WNT ) // Windows
56 #include <tools/prewin.h>
58 // #define WIN32_LEAN_AND_MEAN
59 // #include <windows.h>
61 #include <tools/postwin.h>
64 using namespace ::osl
;
66 inline void printUString( const ::rtl::OUString
& str
, const sal_Char
* msg
= "" )
71 t_print("%s: ", msg
);
74 aString
= ::rtl::OUStringToOString( str
, RTL_TEXTENCODING_ASCII_US
);
75 t_print("%s\n", (char *)aString
.getStr( ) );
78 /** get the absolute source file URL "file:///.../sal/qa/rtl/logfile/"
81 inline ::rtl::OUString
getTempPath( void )
84 rtl::OUString
suDirURL(rtl::OUString::createFromAscii("file:///tmp/"));
86 rtl::OUString
suDirURL(rtl::OUString::createFromAscii("file:///c:/temp/"));
93 bool t_fileExist(rtl::OUString
const& _sFilename
)
95 ::osl::FileBase::RC nError1
;
96 ::osl::File
aTestFile( _sFilename
);
97 nError1
= aTestFile
.open ( OpenFlag_Read
);
98 if ( ( ::osl::FileBase::E_NOENT
!= nError1
) && ( ::osl::FileBase::E_ACCES
!= nError1
) )
107 inline ::rtl::OUString
getCurrentPID( )
109 //~ Get current PID and turn it into OUString;
112 nPID
= GetCurrentProcessId();
116 return ( ::rtl::OUString::valueOf( ( long )nPID
) );
120 // -----------------------------------------------------------------------------
123 * check if logfile is create
124 * be careful with relative logfiles they will create near the source, maybe it's no write access to it.
125 * use absolute path to logfile instead.
127 namespace rtl_logfile
129 class logfile
: public CppUnit::TestFixture
133 //directly call rtl_logfile_trace
137 putenv(const_cast< char * >("RTL_LOGFILE=/tmp/logfile1"));
140 putenv("RTL_LOGFILE=c:\\temp\\logfile1");
143 setenv("RTL_LOGFILE", "/tmp/logfile1", 0);
145 rtl_logfile_trace("trace %d\n", 2 );
146 rtl_logfile_trace("trace %d %d\n" , 1,2 );
147 rtl_logfile_trace("trace %d %d %d\n" , 1 , 2 ,3 );
149 rtl::OUString suFilePath
= getTempPath();
150 suFilePath
+= rtl::OUString::createFromAscii("logfile1_") + getCurrentPID( );
151 suFilePath
+= rtl::OUString::createFromAscii(".log");
153 ::osl::FileBase::RC nError1
;
154 ::osl::File
aTestFile( suFilePath
);
155 printUString( suFilePath
);
156 nError1
= aTestFile
.open ( OpenFlag_Read
);
157 CPPUNIT_ASSERT_MESSAGE("create the log file: but the logfile does not exist",
158 ( ::osl::FileBase::E_NOENT
!= nError1
) &&
159 ( ::osl::FileBase::E_ACCES
!= nError1
) );
160 sal_Char buffer_read
[400];
161 sal_uInt64 nCount_read
;
162 nError1
= aTestFile
.read( buffer_read
, 400, nCount_read
);
163 //t_print("buffer is %s\n", buffer_read );
164 CPPUNIT_ASSERT_MESSAGE("write right logs", strstr( buffer_read
, "trace 1 2 3") != NULL
);
167 /*// delete logfile on the disk
169 nError1 = osl::File::remove( suFilePath );
170 printError( nError1 );
171 CPPUNIT_ASSERT_MESSAGE( "In deleteTestFile Function: remove ", ( ::osl::FileBase::E_None == nError1 ) || ( nError1 == ::osl::FileBase::E_NOENT ) );
174 //Profiling output should only be generated for a special product version of OpenOffice
175 // which is compiled with a defined preprocessor symbol 'TIMELOG'. Now, the symbol not defined
179 putenv(const_cast< char * >("RTL_LOGFILE=/tmp/logfile2"));
182 putenv("RTL_LOGFILE=c:\\temp\\logfile2");
185 setenv("RTL_LOGFILE", "/tmp/logfile2", 0);
187 RTL_LOGFILE_TRACE( "trace the log" );
188 RTL_LOGFILE_TRACE1( "trace %d" , 1 );
189 RTL_LOGFILE_TRACE2( "trace %d %d" , 1,2 );
190 RTL_LOGFILE_TRACE3( "trace %d %d %d" , 1 , 2 ,3 );
191 // TODO: assertion test!
197 putenv(const_cast< char * >("RTL_LOGFILE=/tmp/logfile2"));
200 putenv("RTL_LOGFILE=c:\\temp\\logfile2");
203 setenv("RTL_LOGFILE", "/tmp/logfile2", 0);
205 RTL_LOGFILE_CONTEXT ( foo
, "foo-function" );
206 RTL_LOGFILE_CONTEXT_TRACE ( foo
, "trace" );
207 RTL_LOGFILE_CONTEXT_TRACE1 ( foo
, "trace %d" , 1 );
208 RTL_LOGFILE_CONTEXT_TRACE2 ( foo
, "trace %d %d" , 1 , 2 );
209 RTL_LOGFILE_CONTEXT_TRACE3 ( foo
, "trace %d %d %d" , 1 , 2 , 3);
210 // TODO: assertion test!
214 CPPUNIT_TEST_SUITE( logfile
);
215 CPPUNIT_TEST( logfile_001
);
216 CPPUNIT_TEST( logfile_002
);
217 CPPUNIT_TEST( logfile_003
);
218 CPPUNIT_TEST_SUITE_END( );
221 } // namespace rtl_logfile
223 // -----------------------------------------------------------------------------
224 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( rtl_logfile::logfile
, "rtl_logfile" );
226 // -----------------------------------------------------------------------------
229 //~ do some clean up work after all test completed.
237 t_print( "\n#Do some clean-ups ... only delete logfile1_*.log here!\n" );
238 rtl::OUString suFilePath
= getTempPath();
239 suFilePath
+= rtl::OUString::createFromAscii("logfile1_") + getCurrentPID( );
240 suFilePath
+= rtl::OUString::createFromAscii(".log");
242 //if ( ifFileExist( suFilePath ) == sal_True )
243 ::osl::FileBase::RC nError1
;
244 nError1
= osl::File::remove( suFilePath
);
246 t_print("Please remove logfile* manully! Error is Permision denied!");
249 catch (CppUnit::Exception
&e
)
251 t_print("Exception caught in GlobalObject dtor(). Exception message: '%s'. Source line: %d\n", e
.what(), e
.sourceLine().lineNumber());
255 t_print("Exception caught (...) in GlobalObject dtor()\n");
260 GlobalObject theGlobalObject
;