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