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