update dev300-m57
[ooovba.git] / sal / qa / rtl / logfile / rtl_logfile.cxx
blob486520ca9406fff48ace316f3e9df306338da645
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: rtl_logfile.cxx,v $
10 * $Revision: 1.8 $
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"
34 // LLA:
35 // this file is converted to use with testshl2
36 // original was placed in sal/test/textenc.cxx
39 // -----------------------------------------------------------------------------
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
44 #if defined(UNX) || defined(OS2)
45 # include <unistd.h>
46 #endif
48 #include <rtl/logfile.hxx>
49 #include <cppunit/simpleheader.hxx>
51 // #ifndef _OSL_MODULE_HXX_
52 // #include <osl/module.hxx>
53 // #endif
54 #include <osl/file.hxx>
55 #if ( defined WNT ) // Windows
56 #include <tools/prewin.h>
57 // #define UNICODE
58 // #define WIN32_LEAN_AND_MEAN
59 // #include <windows.h>
60 #include <tchar.h>
61 #include <tools/postwin.h>
62 #endif
64 using namespace ::osl;
66 inline void printUString( const ::rtl::OUString & str, const sal_Char * msg = "" )
69 if (strlen(msg) > 0)
71 t_print("%s: ", msg );
73 rtl::OString aString;
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 )
83 #ifdef UNX
84 rtl::OUString suDirURL(rtl::OUString::createFromAscii("file:///tmp/"));
85 #else /* Windows */
86 rtl::OUString suDirURL(rtl::OUString::createFromAscii("file:///c:/temp/"));
87 #endif
88 return suDirURL;
91 /** if the file exist
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 ) )
100 aTestFile.close( );
101 return true;
103 return false;
105 /** get Current PID.
107 inline ::rtl::OUString getCurrentPID( )
109 //~ Get current PID and turn it into OUString;
110 int nPID = 0;
111 #ifdef WNT
112 nPID = GetCurrentProcessId();
113 #else
114 nPID = getpid();
115 #endif
116 return ( ::rtl::OUString::valueOf( ( long )nPID ) );
120 // -----------------------------------------------------------------------------
122 * LLA:
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
131 public:
133 //directly call rtl_logfile_trace
134 void logfile_001()
136 #ifdef SOLARIS
137 putenv(const_cast< char * >("RTL_LOGFILE=/tmp/logfile1"));
138 #endif
139 #ifdef WNT
140 putenv("RTL_LOGFILE=c:\\temp\\logfile1");
141 #endif
142 #ifdef LINUX
143 setenv("RTL_LOGFILE", "/tmp/logfile1", 0);
144 #endif
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 );
165 aTestFile.sync();
166 aTestFile.close();
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
176 void logfile_002()
178 #ifdef SOLARIS
179 putenv(const_cast< char * >("RTL_LOGFILE=/tmp/logfile2"));
180 #endif
181 #ifdef WNT
182 putenv("RTL_LOGFILE=c:\\temp\\logfile2");
183 #endif
184 #ifdef LINUX
185 setenv("RTL_LOGFILE", "/tmp/logfile2", 0);
186 #endif
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!
194 void logfile_003()
196 #ifdef SOLARIS
197 putenv(const_cast< char * >("RTL_LOGFILE=/tmp/logfile2"));
198 #endif
199 #ifdef WNT
200 putenv("RTL_LOGFILE=c:\\temp\\logfile2");
201 #endif
202 #ifdef LINUX
203 setenv("RTL_LOGFILE", "/tmp/logfile2", 0);
204 #endif
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 // -----------------------------------------------------------------------------
227 NOADDITIONAL;
229 //~ do some clean up work after all test completed.
230 class GlobalObject
232 public:
233 ~GlobalObject()
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 );
245 #ifdef WNT
246 t_print("Please remove logfile* manully! Error is Permision denied!");
247 #endif
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());
253 catch (...)
255 t_print("Exception caught (...) in GlobalObject dtor()\n");
260 GlobalObject theGlobalObject;