xref: /aoo42x/main/sal/qa/rtl/logfile/rtl_logfile.cxx (revision 4b40cfcd)
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 <tools/prewin.h>
50cdf0e10cSrcweir // #define UNICODE
51cdf0e10cSrcweir // #define WIN32_LEAN_AND_MEAN
52cdf0e10cSrcweir // #include <windows.h>
53cdf0e10cSrcweir #include <tchar.h>
54cdf0e10cSrcweir #include <tools/postwin.h>
55cdf0e10cSrcweir #endif
56cdf0e10cSrcweir 
57cdf0e10cSrcweir using namespace ::osl;
58cdf0e10cSrcweir 
59cdf0e10cSrcweir inline void printUString( const ::rtl::OUString & str, const sal_Char * msg = "" )
60cdf0e10cSrcweir {
61cdf0e10cSrcweir 
62cdf0e10cSrcweir     if (strlen(msg) > 0)
63cdf0e10cSrcweir     {
6498e2c44aSDamjan Jovanovic         printf("%s: ", msg );
65cdf0e10cSrcweir     }
66cdf0e10cSrcweir     rtl::OString aString;
67cdf0e10cSrcweir     aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US );
6898e2c44aSDamjan Jovanovic     printf("%s\n", (char *)aString.getStr( ) );
69cdf0e10cSrcweir }
70cdf0e10cSrcweir 
71cdf0e10cSrcweir /** get the absolute source file URL "file:///.../sal/qa/rtl/logfile/"
72cdf0e10cSrcweir   */
73cdf0e10cSrcweir 
74cdf0e10cSrcweir inline ::rtl::OUString getTempPath( void )
75cdf0e10cSrcweir {
76cdf0e10cSrcweir #ifdef UNX
77cdf0e10cSrcweir     rtl::OUString suDirURL(rtl::OUString::createFromAscii("file:///tmp/"));
78cdf0e10cSrcweir #else /* Windows */
79cdf0e10cSrcweir     rtl::OUString suDirURL(rtl::OUString::createFromAscii("file:///c:/temp/"));
80cdf0e10cSrcweir #endif
81cdf0e10cSrcweir     return suDirURL;
82cdf0e10cSrcweir }
83cdf0e10cSrcweir 
84cdf0e10cSrcweir /** if the file exist
85cdf0e10cSrcweir  */
86cdf0e10cSrcweir bool t_fileExist(rtl::OUString const& _sFilename)
87cdf0e10cSrcweir {
88cdf0e10cSrcweir     ::osl::FileBase::RC   nError1;
89cdf0e10cSrcweir     ::osl::File aTestFile( _sFilename );
90cdf0e10cSrcweir     nError1 = aTestFile.open ( OpenFlag_Read );
91cdf0e10cSrcweir     if ( ( ::osl::FileBase::E_NOENT != nError1 ) && ( ::osl::FileBase::E_ACCES != nError1 ) )
92cdf0e10cSrcweir     {
93cdf0e10cSrcweir         aTestFile.close( );
94cdf0e10cSrcweir         return true;
95cdf0e10cSrcweir     }
96cdf0e10cSrcweir     return false;
97cdf0e10cSrcweir }
98cdf0e10cSrcweir /** get Current PID.
99cdf0e10cSrcweir */
100cdf0e10cSrcweir inline ::rtl::OUString getCurrentPID(  )
101cdf0e10cSrcweir {
102cdf0e10cSrcweir         //~ Get current PID and turn it into OUString;
103cdf0e10cSrcweir         int nPID = 0;
104cdf0e10cSrcweir #ifdef WNT
105cdf0e10cSrcweir         nPID = GetCurrentProcessId();
106cdf0e10cSrcweir #else
107cdf0e10cSrcweir         nPID = getpid();
108cdf0e10cSrcweir #endif
109cdf0e10cSrcweir         return ( ::rtl::OUString::valueOf( ( long )nPID ) );
110cdf0e10cSrcweir }
111cdf0e10cSrcweir 
112cdf0e10cSrcweir 
113cdf0e10cSrcweir // -----------------------------------------------------------------------------
114cdf0e10cSrcweir /*
115cdf0e10cSrcweir  * LLA:
116cdf0e10cSrcweir  * check if logfile is create
117cdf0e10cSrcweir  * be careful with relative logfiles they will create near the source, maybe it's no write access to it.
118cdf0e10cSrcweir  * use absolute path to logfile instead.
119cdf0e10cSrcweir  */
120cdf0e10cSrcweir namespace rtl_logfile
121cdf0e10cSrcweir {
12298e2c44aSDamjan Jovanovic     class logfile : public ::testing::Test
123cdf0e10cSrcweir     {
12498e2c44aSDamjan Jovanovic     };
125cdf0e10cSrcweir 
12698e2c44aSDamjan Jovanovic     //directly call rtl_logfile_trace
12798e2c44aSDamjan Jovanovic     TEST_F(logfile, logfile_001)
12898e2c44aSDamjan Jovanovic     {
129cdf0e10cSrcweir #ifdef SOLARIS
13098e2c44aSDamjan Jovanovic         putenv(const_cast< char * >("RTL_LOGFILE=/tmp/logfile1"));
13198e2c44aSDamjan Jovanovic #elif WNT
13298e2c44aSDamjan Jovanovic         putenv("RTL_LOGFILE=c:\\temp\\logfile1");
13398e2c44aSDamjan Jovanovic #else
13498e2c44aSDamjan Jovanovic         setenv("RTL_LOGFILE", "/tmp/logfile1", 0);
135cdf0e10cSrcweir #endif
13698e2c44aSDamjan Jovanovic         rtl_logfile_trace("trace %d\n", 2 );
13798e2c44aSDamjan Jovanovic         rtl_logfile_trace("trace %d %d\n" , 1,2 );
13898e2c44aSDamjan Jovanovic         rtl_logfile_trace("trace %d %d %d\n" , 1 , 2 ,3 );
13998e2c44aSDamjan Jovanovic         for (int i = 0; i < 1024; i++)
14098e2c44aSDamjan Jovanovic             rtl_logfile_trace("rubbish to flush the log\n");
14198e2c44aSDamjan Jovanovic 
14298e2c44aSDamjan Jovanovic         rtl::OUString suFilePath = getTempPath();
14398e2c44aSDamjan Jovanovic         suFilePath +=  rtl::OUString::createFromAscii("logfile1_") + getCurrentPID( );
14498e2c44aSDamjan Jovanovic         suFilePath +=  rtl::OUString::createFromAscii(".log");
14598e2c44aSDamjan Jovanovic 
14698e2c44aSDamjan Jovanovic         ::osl::FileBase::RC   nError1;
14798e2c44aSDamjan Jovanovic         ::osl::File aTestFile( suFilePath );
14898e2c44aSDamjan Jovanovic         printUString( suFilePath );
14998e2c44aSDamjan Jovanovic         nError1 = aTestFile.open ( OpenFlag_Read );
15098e2c44aSDamjan Jovanovic         ASSERT_TRUE(( ::osl::FileBase::E_NOENT != nError1 ) &&
15198e2c44aSDamjan Jovanovic                     ( ::osl::FileBase::E_ACCES != nError1 ) ) << "create the log file: but the logfile does not exist";
15298e2c44aSDamjan Jovanovic         sal_Char       buffer_read[400];
15398e2c44aSDamjan Jovanovic         sal_uInt64      nCount_read;
15498e2c44aSDamjan Jovanovic         nError1 = aTestFile.read( buffer_read, 400, nCount_read );
15598e2c44aSDamjan Jovanovic         //print("buffer is %s\n", buffer_read );
15698e2c44aSDamjan Jovanovic         ASSERT_TRUE( strstr( buffer_read, "trace 1 2 3") != NULL ) << "write right logs";
15798e2c44aSDamjan Jovanovic         aTestFile.sync();
15898e2c44aSDamjan Jovanovic         aTestFile.close();
15998e2c44aSDamjan Jovanovic         /*// delete logfile on the disk
16098e2c44aSDamjan Jovanovic 
16198e2c44aSDamjan Jovanovic         nError1 = osl::File::remove( suFilePath );
16298e2c44aSDamjan Jovanovic         printError( nError1 );
163*4b40cfcdSDamjan Jovanovic         ASSERT_TRUE( ( ::osl::FileBase::E_None == nError1 ) || ( nError1 == ::osl::FileBase::E_NOENT ) ) << "In deleteTestFile Function: remove ";
16498e2c44aSDamjan Jovanovic         */
16598e2c44aSDamjan Jovanovic     }
166cdf0e10cSrcweir 
16798e2c44aSDamjan Jovanovic     //Profiling output should only be generated for a special product version of OpenOffice
16898e2c44aSDamjan Jovanovic     // which is compiled with a defined preprocessor symbol 'TIMELOG'. Now, the symbol not defined
16998e2c44aSDamjan Jovanovic     TEST_F(logfile, logfile_002)
17098e2c44aSDamjan Jovanovic     {
171cdf0e10cSrcweir #ifdef SOLARIS
17298e2c44aSDamjan Jovanovic         putenv(const_cast< char * >("RTL_LOGFILE=/tmp/logfile2"));
173cdf0e10cSrcweir #endif
174cdf0e10cSrcweir #ifdef WNT
17598e2c44aSDamjan Jovanovic         putenv("RTL_LOGFILE=c:\\temp\\logfile2");
176cdf0e10cSrcweir #endif
177cdf0e10cSrcweir #ifdef LINUX
17898e2c44aSDamjan Jovanovic         setenv("RTL_LOGFILE", "/tmp/logfile2", 0);
179cdf0e10cSrcweir #endif
18098e2c44aSDamjan Jovanovic         RTL_LOGFILE_TRACE( "trace the log" );
18198e2c44aSDamjan Jovanovic         RTL_LOGFILE_TRACE1( "trace %d" , 1 );
18298e2c44aSDamjan Jovanovic         RTL_LOGFILE_TRACE2( "trace %d %d" , 1,2 );
18398e2c44aSDamjan Jovanovic         RTL_LOGFILE_TRACE3( "trace %d %d %d" , 1 , 2 ,3 );
184cdf0e10cSrcweir // TODO: assertion test!
18598e2c44aSDamjan Jovanovic     }
186cdf0e10cSrcweir 
18798e2c44aSDamjan Jovanovic     TEST_F(logfile, logfile_003)
18898e2c44aSDamjan Jovanovic     {
189cdf0e10cSrcweir #ifdef SOLARIS
19098e2c44aSDamjan Jovanovic         putenv(const_cast< char * >("RTL_LOGFILE=/tmp/logfile2"));
191cdf0e10cSrcweir #endif
192cdf0e10cSrcweir #ifdef WNT
19398e2c44aSDamjan Jovanovic         putenv("RTL_LOGFILE=c:\\temp\\logfile2");
194cdf0e10cSrcweir #endif
195cdf0e10cSrcweir #ifdef LINUX
19698e2c44aSDamjan Jovanovic         setenv("RTL_LOGFILE", "/tmp/logfile2", 0);
197cdf0e10cSrcweir #endif
19898e2c44aSDamjan Jovanovic         RTL_LOGFILE_CONTEXT ( foo , "foo-function" );
19998e2c44aSDamjan Jovanovic         RTL_LOGFILE_CONTEXT_TRACE ( foo , "trace" );
20098e2c44aSDamjan Jovanovic         RTL_LOGFILE_CONTEXT_TRACE1 ( foo , "trace %d" , 1 );
20198e2c44aSDamjan Jovanovic         RTL_LOGFILE_CONTEXT_TRACE2 ( foo , "trace %d %d" , 1 , 2 );
20298e2c44aSDamjan Jovanovic         RTL_LOGFILE_CONTEXT_TRACE3 ( foo , "trace %d %d %d" , 1 , 2 , 3);
203cdf0e10cSrcweir // TODO: assertion test!
20498e2c44aSDamjan Jovanovic     }
205cdf0e10cSrcweir 
206cdf0e10cSrcweir } // namespace rtl_logfile
207cdf0e10cSrcweir 
208cdf0e10cSrcweir 
209cdf0e10cSrcweir // -----------------------------------------------------------------------------
210cdf0e10cSrcweir 
211cdf0e10cSrcweir //~ do some clean up work after all test completed.
212cdf0e10cSrcweir class GlobalObject
213cdf0e10cSrcweir {
214cdf0e10cSrcweir public:
215cdf0e10cSrcweir     ~GlobalObject()
216cdf0e10cSrcweir         {
217cdf0e10cSrcweir             try
218cdf0e10cSrcweir             {
21998e2c44aSDamjan Jovanovic                 printf( "\n#Do some clean-ups ... only delete logfile1_*.log here!\n" );
220cdf0e10cSrcweir                 rtl::OUString suFilePath = getTempPath();
221cdf0e10cSrcweir                 suFilePath +=  rtl::OUString::createFromAscii("logfile1_") + getCurrentPID( );
222cdf0e10cSrcweir                 suFilePath +=  rtl::OUString::createFromAscii(".log");
223cdf0e10cSrcweir 
224cdf0e10cSrcweir                 //if ( ifFileExist( suFilePath )  == sal_True )
225cdf0e10cSrcweir                 ::osl::FileBase::RC nError1;
226cdf0e10cSrcweir                 nError1 = osl::File::remove( suFilePath );
227cdf0e10cSrcweir #ifdef WNT
22898e2c44aSDamjan Jovanovic                 printf("Please remove logfile* manully! Error is Permision denied!");
229cdf0e10cSrcweir #endif
230cdf0e10cSrcweir             }
231cdf0e10cSrcweir             catch (...)
232cdf0e10cSrcweir             {
23398e2c44aSDamjan Jovanovic                 printf("Exception caught (...) in GlobalObject dtor()\n");
234cdf0e10cSrcweir             }
235cdf0e10cSrcweir         }
236cdf0e10cSrcweir };
237cdf0e10cSrcweir 
238cdf0e10cSrcweir GlobalObject theGlobalObject;
239cdf0e10cSrcweir 
24098e2c44aSDamjan Jovanovic int main(int argc, char **argv)
24198e2c44aSDamjan Jovanovic {
24298e2c44aSDamjan Jovanovic     ::testing::InitGoogleTest(&argc, argv);
24398e2c44aSDamjan Jovanovic     return RUN_ALL_TESTS();
24498e2c44aSDamjan Jovanovic }
245