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 #ifndef CONNECTIVITY_HSQLDB_ACCESSLOG_HXX
25 #define CONNECTIVITY_HSQLDB_ACCESSLOG_HXX
26 
27 #ifdef HSQLDB_DBG
28 
29 #include <stdio.h>
30 #include <jni.h>
31 #include <rtl/ustring.hxx>
32 #include <rtl/string.hxx>
33 
34 namespace connectivity { namespace hsqldb
35 {
36     class LogFile
37     {
38     private:
39         ::rtl::OUString     m_sFileName;
40 
41     public:
42         LogFile( JNIEnv* env, jstring streamName, const sal_Char* _pAsciiSuffix );
43 
44     public:
45                 void    writeString( const sal_Char* _pString, bool _bEndLine = true );
create()46                 void    create() { getLogFile(); }
47         virtual void    close();
48 
49     protected:
50         FILE*&  getLogFile();
51     };
52 
53     class OperationLogFile : public LogFile
54     {
55     public:
OperationLogFile(JNIEnv * env,jstring streamName,const sal_Char * _pAsciiSuffix)56         OperationLogFile( JNIEnv* env, jstring streamName, const sal_Char* _pAsciiSuffix )
57             :LogFile( env, streamName, ( ::rtl::OString( _pAsciiSuffix ) += ".op" ).getStr() )
58         {
59         }
60 
logOperation(const sal_Char * _pOp)61         void logOperation( const sal_Char* _pOp )
62         {
63             writeString( _pOp, true );
64         }
65 
logOperation(const sal_Char * _pOp,jlong _nLongArg)66         void logOperation( const sal_Char* _pOp, jlong _nLongArg )
67         {
68             ::rtl::OString sLine( _pOp );
69             sLine += "( ";
70             sLine += ::rtl::OString::valueOf( _nLongArg );
71             sLine += " )";
72             writeString( sLine.getStr(), true );
73         }
74 
logReturn(jlong _nRetVal)75         void logReturn( jlong _nRetVal )
76         {
77             ::rtl::OString sLine( " -> " );
78             sLine += ::rtl::OString::valueOf( _nRetVal );
79             writeString( sLine.getStr(), true );
80         }
81 
logReturn(jint _nRetVal)82         void logReturn( jint _nRetVal )
83         {
84             ::rtl::OString sLine( " -> " );
85             sLine += ::rtl::OString::valueOf( _nRetVal );
86             writeString( sLine.getStr(), true );
87         }
88 
close()89         virtual void close()
90         {
91             writeString( "-------------------------------", true );
92             writeString( "", true );
93             LogFile::close();
94         }
95     };
96 
97     class DataLogFile : public LogFile
98     {
99     public:
DataLogFile(JNIEnv * env,jstring streamName,const sal_Char * _pAsciiSuffix)100         DataLogFile( JNIEnv* env, jstring streamName, const sal_Char* _pAsciiSuffix )
101             :LogFile( env, streamName, _pAsciiSuffix )
102         {
103         }
104 
write(jint value)105         void write( jint value )
106         {
107 			fputc( value, getLogFile() );
108             fflush( getLogFile() );
109         }
110 
write(const sal_Int8 * buffer,sal_Int32 bytesRead)111         void write( const sal_Int8* buffer, sal_Int32 bytesRead )
112         {
113 			fwrite( buffer, sizeof(sal_Int8), bytesRead, getLogFile() );
114             fflush( getLogFile() );
115         }
116 
seek(sal_Int64 pos)117         sal_Int64 seek( sal_Int64 pos )
118         {
119             FILE* pFile = getLogFile();
120             fseek( pFile, 0, SEEK_END );
121             if ( ftell( pFile ) < pos )
122             {
123                 sal_Int8 filler( 0 );
124                 while ( ftell( pFile ) < pos )
125                     fwrite( &filler, sizeof( sal_Int8 ), 1, pFile );
126                 fflush( pFile );
127             }
128             fseek( pFile, pos, SEEK_SET );
129             return ftell( pFile );
130         }
131 
tell()132         sal_Int64 tell()
133         {
134             return ftell( getLogFile() );
135         }
136     };
137 
138 } }
139 #endif
140 
141 #endif // CONNECTIVITY_HSQLDB_ACCESSLOG_HXX
142