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