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 "gtest/gtest.h" 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 printf("%s: ", msg ); 65 } 66 rtl::OString aString; 67 aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US ); 68 printf("%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 ::testing::Test 123 { 124 }; 125 126 //directly call rtl_logfile_trace 127 TEST_F(logfile, logfile_001) 128 { 129 #ifdef SOLARIS 130 putenv(const_cast< char * >("RTL_LOGFILE=/tmp/logfile1")); 131 #elif WNT 132 putenv("RTL_LOGFILE=c:\\temp\\logfile1"); 133 #else 134 setenv("RTL_LOGFILE", "/tmp/logfile1", 0); 135 #endif 136 rtl_logfile_trace("trace %d\n", 2 ); 137 rtl_logfile_trace("trace %d %d\n" , 1,2 ); 138 rtl_logfile_trace("trace %d %d %d\n" , 1 , 2 ,3 ); 139 for (int i = 0; i < 1024; i++) 140 rtl_logfile_trace("rubbish to flush the log\n"); 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 ASSERT_TRUE(( ::osl::FileBase::E_NOENT != nError1 ) && 151 ( ::osl::FileBase::E_ACCES != nError1 ) ) << "create the log file: but the logfile does not exist"; 152 sal_Char buffer_read[400]; 153 sal_uInt64 nCount_read; 154 nError1 = aTestFile.read( buffer_read, 400, nCount_read ); 155 //print("buffer is %s\n", buffer_read ); 156 ASSERT_TRUE( strstr( buffer_read, "trace 1 2 3") != NULL ) << "write right logs"; 157 aTestFile.sync(); 158 aTestFile.close(); 159 /*// delete logfile on the disk 160 161 nError1 = osl::File::remove( suFilePath ); 162 printError( nError1 ); 163 CPPUNIT_ASSERT_MESSAGE( "In deleteTestFile Function: remove ", ( ::osl::FileBase::E_None == nError1 ) || ( nError1 == ::osl::FileBase::E_NOENT ) ); 164 */ 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 TEST_F(logfile, 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 TEST_F(logfile, 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 } // namespace rtl_logfile 207 208 209 // ----------------------------------------------------------------------------- 210 211 //~ do some clean up work after all test completed. 212 class GlobalObject 213 { 214 public: 215 ~GlobalObject() 216 { 217 try 218 { 219 printf( "\n#Do some clean-ups ... only delete logfile1_*.log here!\n" ); 220 rtl::OUString suFilePath = getTempPath(); 221 suFilePath += rtl::OUString::createFromAscii("logfile1_") + getCurrentPID( ); 222 suFilePath += rtl::OUString::createFromAscii(".log"); 223 224 //if ( ifFileExist( suFilePath ) == sal_True ) 225 ::osl::FileBase::RC nError1; 226 nError1 = osl::File::remove( suFilePath ); 227 #ifdef WNT 228 printf("Please remove logfile* manully! Error is Permision denied!"); 229 #endif 230 } 231 catch (...) 232 { 233 printf("Exception caught (...) in GlobalObject dtor()\n"); 234 } 235 } 236 }; 237 238 GlobalObject theGlobalObject; 239 240 int main(int argc, char **argv) 241 { 242 ::testing::InitGoogleTest(&argc, argv); 243 return RUN_ALL_TESTS(); 244 } 245