xref: /aoo42x/main/sal/qa/rtl/logfile/rtl_logfile.cxx (revision e6348c9c)
187d2adbcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
387d2adbcSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
487d2adbcSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
587d2adbcSAndrew Rist  * distributed with this work for additional information
687d2adbcSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
787d2adbcSAndrew Rist  * to you under the Apache License, Version 2.0 (the
887d2adbcSAndrew Rist  * "License"); you may not use this file except in compliance
987d2adbcSAndrew Rist  * with the License.  You may obtain a copy of the License at
1087d2adbcSAndrew Rist  *
1187d2adbcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
1287d2adbcSAndrew Rist  *
1387d2adbcSAndrew Rist  * Unless required by applicable law or agreed to in writing,
1487d2adbcSAndrew Rist  * software distributed under the License is distributed on an
1587d2adbcSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1687d2adbcSAndrew Rist  * KIND, either express or implied.  See the License for the
1787d2adbcSAndrew Rist  * specific language governing permissions and limitations
1887d2adbcSAndrew Rist  * under the License.
1987d2adbcSAndrew Rist  *
2087d2adbcSAndrew Rist  *************************************************************/
2187d2adbcSAndrew Rist 
2287d2adbcSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir 
25cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
26cdf0e10cSrcweir #include "precompiled_sal.hxx"
27cdf0e10cSrcweir // LLA:
28cdf0e10cSrcweir // this file is converted to use with testshl2
29cdf0e10cSrcweir // original was placed in sal/test/textenc.cxx
30cdf0e10cSrcweir 
31cdf0e10cSrcweir 
32cdf0e10cSrcweir // -----------------------------------------------------------------------------
33cdf0e10cSrcweir #include <stdio.h>
34cdf0e10cSrcweir #include <stdlib.h>
35cdf0e10cSrcweir #include <string.h>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #if defined(UNX) || defined(OS2)
38cdf0e10cSrcweir #       include <unistd.h>
39cdf0e10cSrcweir #endif
40cdf0e10cSrcweir 
41cdf0e10cSrcweir #include <rtl/logfile.hxx>
4298e2c44aSDamjan Jovanovic #include "gtest/gtest.h"
43cdf0e10cSrcweir 
44cdf0e10cSrcweir // #ifndef      _OSL_MODULE_HXX_
45cdf0e10cSrcweir // #include <osl/module.hxx>
46cdf0e10cSrcweir // #endif
47cdf0e10cSrcweir #include <osl/file.hxx>
48cdf0e10cSrcweir #if ( defined WNT )                     // Windows
49cdf0e10cSrcweir #include <tchar.h>
50cdf0e10cSrcweir #endif
51cdf0e10cSrcweir 
52cdf0e10cSrcweir using namespace ::osl;
53cdf0e10cSrcweir 
printUString(const::rtl::OUString & str,const sal_Char * msg="")54cdf0e10cSrcweir inline void printUString( const ::rtl::OUString & str, const sal_Char * msg = "" )
55cdf0e10cSrcweir {
56cdf0e10cSrcweir 
57cdf0e10cSrcweir     if (strlen(msg) > 0)
58cdf0e10cSrcweir     {
5998e2c44aSDamjan Jovanovic         printf("%s: ", msg );
60cdf0e10cSrcweir     }
61cdf0e10cSrcweir     rtl::OString aString;
62cdf0e10cSrcweir     aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US );
6398e2c44aSDamjan Jovanovic     printf("%s\n", (char *)aString.getStr( ) );
64cdf0e10cSrcweir }
65cdf0e10cSrcweir 
66cdf0e10cSrcweir /** get the absolute source file URL "file:///.../sal/qa/rtl/logfile/"
67cdf0e10cSrcweir   */
68cdf0e10cSrcweir 
getTempPath(void)69cdf0e10cSrcweir inline ::rtl::OUString getTempPath( void )
70cdf0e10cSrcweir {
71cdf0e10cSrcweir #ifdef UNX
72cdf0e10cSrcweir     rtl::OUString suDirURL(rtl::OUString::createFromAscii("file:///tmp/"));
73cdf0e10cSrcweir #else /* Windows */
74cdf0e10cSrcweir     rtl::OUString suDirURL(rtl::OUString::createFromAscii("file:///c:/temp/"));
75cdf0e10cSrcweir #endif
76cdf0e10cSrcweir     return suDirURL;
77cdf0e10cSrcweir }
78cdf0e10cSrcweir 
79cdf0e10cSrcweir /** if the file exist
80cdf0e10cSrcweir  */
t_fileExist(rtl::OUString const & _sFilename)81cdf0e10cSrcweir bool t_fileExist(rtl::OUString const& _sFilename)
82cdf0e10cSrcweir {
83cdf0e10cSrcweir     ::osl::FileBase::RC   nError1;
84cdf0e10cSrcweir     ::osl::File aTestFile( _sFilename );
85cdf0e10cSrcweir     nError1 = aTestFile.open ( OpenFlag_Read );
86cdf0e10cSrcweir     if ( ( ::osl::FileBase::E_NOENT != nError1 ) && ( ::osl::FileBase::E_ACCES != nError1 ) )
87cdf0e10cSrcweir     {
88cdf0e10cSrcweir         aTestFile.close( );
89cdf0e10cSrcweir         return true;
90cdf0e10cSrcweir     }
91cdf0e10cSrcweir     return false;
92cdf0e10cSrcweir }
93cdf0e10cSrcweir /** get Current PID.
94cdf0e10cSrcweir */
getCurrentPID()95cdf0e10cSrcweir inline ::rtl::OUString getCurrentPID(  )
96cdf0e10cSrcweir {
97cdf0e10cSrcweir         //~ Get current PID and turn it into OUString;
98cdf0e10cSrcweir         int nPID = 0;
99cdf0e10cSrcweir #ifdef WNT
100cdf0e10cSrcweir         nPID = GetCurrentProcessId();
101cdf0e10cSrcweir #else
102cdf0e10cSrcweir         nPID = getpid();
103cdf0e10cSrcweir #endif
104cdf0e10cSrcweir         return ( ::rtl::OUString::valueOf( ( long )nPID ) );
105cdf0e10cSrcweir }
106cdf0e10cSrcweir 
107cdf0e10cSrcweir 
108cdf0e10cSrcweir // -----------------------------------------------------------------------------
109cdf0e10cSrcweir /*
110cdf0e10cSrcweir  * LLA:
111cdf0e10cSrcweir  * check if logfile is create
112cdf0e10cSrcweir  * be careful with relative logfiles they will create near the source, maybe it's no write access to it.
113cdf0e10cSrcweir  * use absolute path to logfile instead.
114cdf0e10cSrcweir  */
115cdf0e10cSrcweir namespace rtl_logfile
116cdf0e10cSrcweir {
11798e2c44aSDamjan Jovanovic     class logfile : public ::testing::Test
118cdf0e10cSrcweir     {
11998e2c44aSDamjan Jovanovic     };
120cdf0e10cSrcweir 
12198e2c44aSDamjan Jovanovic     //directly call rtl_logfile_trace
TEST_F(logfile,logfile_001)12298e2c44aSDamjan Jovanovic     TEST_F(logfile, logfile_001)
12398e2c44aSDamjan Jovanovic     {
124cdf0e10cSrcweir #ifdef SOLARIS
12598e2c44aSDamjan Jovanovic         putenv(const_cast< char * >("RTL_LOGFILE=/tmp/logfile1"));
12698e2c44aSDamjan Jovanovic #elif WNT
12798e2c44aSDamjan Jovanovic         putenv("RTL_LOGFILE=c:\\temp\\logfile1");
12898e2c44aSDamjan Jovanovic #else
12998e2c44aSDamjan Jovanovic         setenv("RTL_LOGFILE", "/tmp/logfile1", 0);
130cdf0e10cSrcweir #endif
13198e2c44aSDamjan Jovanovic         rtl_logfile_trace("trace %d\n", 2 );
13298e2c44aSDamjan Jovanovic         rtl_logfile_trace("trace %d %d\n" , 1,2 );
13398e2c44aSDamjan Jovanovic         rtl_logfile_trace("trace %d %d %d\n" , 1 , 2 ,3 );
13498e2c44aSDamjan Jovanovic         for (int i = 0; i < 1024; i++)
13598e2c44aSDamjan Jovanovic             rtl_logfile_trace("rubbish to flush the log\n");
13698e2c44aSDamjan Jovanovic 
13798e2c44aSDamjan Jovanovic         rtl::OUString suFilePath = getTempPath();
13898e2c44aSDamjan Jovanovic         suFilePath +=  rtl::OUString::createFromAscii("logfile1_") + getCurrentPID( );
13998e2c44aSDamjan Jovanovic         suFilePath +=  rtl::OUString::createFromAscii(".log");
14098e2c44aSDamjan Jovanovic 
14198e2c44aSDamjan Jovanovic         ::osl::FileBase::RC   nError1;
14298e2c44aSDamjan Jovanovic         ::osl::File aTestFile( suFilePath );
14398e2c44aSDamjan Jovanovic         printUString( suFilePath );
14498e2c44aSDamjan Jovanovic         nError1 = aTestFile.open ( OpenFlag_Read );
14598e2c44aSDamjan Jovanovic         ASSERT_TRUE(( ::osl::FileBase::E_NOENT != nError1 ) &&
14698e2c44aSDamjan Jovanovic                     ( ::osl::FileBase::E_ACCES != nError1 ) ) << "create the log file: but the logfile does not exist";
14798e2c44aSDamjan Jovanovic         sal_Char       buffer_read[400];
14898e2c44aSDamjan Jovanovic         sal_uInt64      nCount_read;
14998e2c44aSDamjan Jovanovic         nError1 = aTestFile.read( buffer_read, 400, nCount_read );
15098e2c44aSDamjan Jovanovic         //print("buffer is %s\n", buffer_read );
15198e2c44aSDamjan Jovanovic         ASSERT_TRUE( strstr( buffer_read, "trace 1 2 3") != NULL ) << "write right logs";
15298e2c44aSDamjan Jovanovic         aTestFile.sync();
15398e2c44aSDamjan Jovanovic         aTestFile.close();
15498e2c44aSDamjan Jovanovic         /*// delete logfile on the disk
15598e2c44aSDamjan Jovanovic 
15698e2c44aSDamjan Jovanovic         nError1 = osl::File::remove( suFilePath );
15798e2c44aSDamjan Jovanovic         printError( nError1 );
158*4b40cfcdSDamjan Jovanovic         ASSERT_TRUE( ( ::osl::FileBase::E_None == nError1 ) || ( nError1 == ::osl::FileBase::E_NOENT ) ) << "In deleteTestFile Function: remove ";
15998e2c44aSDamjan Jovanovic         */
16098e2c44aSDamjan Jovanovic     }
161cdf0e10cSrcweir 
16298e2c44aSDamjan Jovanovic     //Profiling output should only be generated for a special product version of OpenOffice
16398e2c44aSDamjan Jovanovic     // which is compiled with a defined preprocessor symbol 'TIMELOG'. Now, the symbol not defined
TEST_F(logfile,logfile_002)16498e2c44aSDamjan Jovanovic     TEST_F(logfile, logfile_002)
16598e2c44aSDamjan Jovanovic     {
166cdf0e10cSrcweir #ifdef SOLARIS
16798e2c44aSDamjan Jovanovic         putenv(const_cast< char * >("RTL_LOGFILE=/tmp/logfile2"));
168cdf0e10cSrcweir #endif
169cdf0e10cSrcweir #ifdef WNT
17098e2c44aSDamjan Jovanovic         putenv("RTL_LOGFILE=c:\\temp\\logfile2");
171cdf0e10cSrcweir #endif
172cdf0e10cSrcweir #ifdef LINUX
17398e2c44aSDamjan Jovanovic         setenv("RTL_LOGFILE", "/tmp/logfile2", 0);
174cdf0e10cSrcweir #endif
17598e2c44aSDamjan Jovanovic         RTL_LOGFILE_TRACE( "trace the log" );
17698e2c44aSDamjan Jovanovic         RTL_LOGFILE_TRACE1( "trace %d" , 1 );
17798e2c44aSDamjan Jovanovic         RTL_LOGFILE_TRACE2( "trace %d %d" , 1,2 );
17898e2c44aSDamjan Jovanovic         RTL_LOGFILE_TRACE3( "trace %d %d %d" , 1 , 2 ,3 );
179cdf0e10cSrcweir // TODO: assertion test!
18098e2c44aSDamjan Jovanovic     }
181cdf0e10cSrcweir 
TEST_F(logfile,logfile_003)18298e2c44aSDamjan Jovanovic     TEST_F(logfile, logfile_003)
18398e2c44aSDamjan Jovanovic     {
184cdf0e10cSrcweir #ifdef SOLARIS
18598e2c44aSDamjan Jovanovic         putenv(const_cast< char * >("RTL_LOGFILE=/tmp/logfile2"));
186cdf0e10cSrcweir #endif
187cdf0e10cSrcweir #ifdef WNT
18898e2c44aSDamjan Jovanovic         putenv("RTL_LOGFILE=c:\\temp\\logfile2");
189cdf0e10cSrcweir #endif
190cdf0e10cSrcweir #ifdef LINUX
19198e2c44aSDamjan Jovanovic         setenv("RTL_LOGFILE", "/tmp/logfile2", 0);
192cdf0e10cSrcweir #endif
19398e2c44aSDamjan Jovanovic         RTL_LOGFILE_CONTEXT ( foo , "foo-function" );
19498e2c44aSDamjan Jovanovic         RTL_LOGFILE_CONTEXT_TRACE ( foo , "trace" );
19598e2c44aSDamjan Jovanovic         RTL_LOGFILE_CONTEXT_TRACE1 ( foo , "trace %d" , 1 );
19698e2c44aSDamjan Jovanovic         RTL_LOGFILE_CONTEXT_TRACE2 ( foo , "trace %d %d" , 1 , 2 );
19798e2c44aSDamjan Jovanovic         RTL_LOGFILE_CONTEXT_TRACE3 ( foo , "trace %d %d %d" , 1 , 2 , 3);
198cdf0e10cSrcweir // TODO: assertion test!
19998e2c44aSDamjan Jovanovic     }
200cdf0e10cSrcweir 
201cdf0e10cSrcweir } // namespace rtl_logfile
202cdf0e10cSrcweir 
203cdf0e10cSrcweir 
204cdf0e10cSrcweir // -----------------------------------------------------------------------------
205cdf0e10cSrcweir 
206cdf0e10cSrcweir //~ do some clean up work after all test completed.
207cdf0e10cSrcweir class GlobalObject
208cdf0e10cSrcweir {
209cdf0e10cSrcweir public:
~GlobalObject()210cdf0e10cSrcweir     ~GlobalObject()
211cdf0e10cSrcweir         {
212cdf0e10cSrcweir             try
213cdf0e10cSrcweir             {
21498e2c44aSDamjan Jovanovic                 printf( "\n#Do some clean-ups ... only delete logfile1_*.log here!\n" );
215cdf0e10cSrcweir                 rtl::OUString suFilePath = getTempPath();
216cdf0e10cSrcweir                 suFilePath +=  rtl::OUString::createFromAscii("logfile1_") + getCurrentPID( );
217cdf0e10cSrcweir                 suFilePath +=  rtl::OUString::createFromAscii(".log");
218cdf0e10cSrcweir 
219cdf0e10cSrcweir                 //if ( ifFileExist( suFilePath )  == sal_True )
220cdf0e10cSrcweir                 ::osl::FileBase::RC nError1;
221cdf0e10cSrcweir                 nError1 = osl::File::remove( suFilePath );
222cdf0e10cSrcweir #ifdef WNT
22398e2c44aSDamjan Jovanovic                 printf("Please remove logfile* manully! Error is Permision denied!");
224cdf0e10cSrcweir #endif
225cdf0e10cSrcweir             }
226cdf0e10cSrcweir             catch (...)
227cdf0e10cSrcweir             {
22898e2c44aSDamjan Jovanovic                 printf("Exception caught (...) in GlobalObject dtor()\n");
229cdf0e10cSrcweir             }
230cdf0e10cSrcweir         }
231cdf0e10cSrcweir };
232cdf0e10cSrcweir 
233cdf0e10cSrcweir GlobalObject theGlobalObject;
234cdf0e10cSrcweir 
main(int argc,char ** argv)23598e2c44aSDamjan Jovanovic int main(int argc, char **argv)
23698e2c44aSDamjan Jovanovic {
23798e2c44aSDamjan Jovanovic     ::testing::InitGoogleTest(&argc, argv);
23898e2c44aSDamjan Jovanovic     return RUN_ALL_TESTS();
23998e2c44aSDamjan Jovanovic }
240