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