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