1*caf5cd79SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*caf5cd79SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*caf5cd79SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*caf5cd79SAndrew Rist  * distributed with this work for additional information
6*caf5cd79SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*caf5cd79SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*caf5cd79SAndrew Rist  * "License"); you may not use this file except in compliance
9*caf5cd79SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*caf5cd79SAndrew Rist  *
11*caf5cd79SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*caf5cd79SAndrew Rist  *
13*caf5cd79SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*caf5cd79SAndrew Rist  * software distributed under the License is distributed on an
15*caf5cd79SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*caf5cd79SAndrew Rist  * KIND, either express or implied.  See the License for the
17*caf5cd79SAndrew Rist  * specific language governing permissions and limitations
18*caf5cd79SAndrew Rist  * under the License.
19*caf5cd79SAndrew Rist  *
20*caf5cd79SAndrew Rist  *************************************************************/
21*caf5cd79SAndrew Rist 
22*caf5cd79SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef CONNECTIVITY_HSQLDB_ACCESSLOG_HXX
25cdf0e10cSrcweir #define CONNECTIVITY_HSQLDB_ACCESSLOG_HXX
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #ifdef HSQLDB_DBG
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <stdio.h>
30cdf0e10cSrcweir #include <jni.h>
31cdf0e10cSrcweir #include <rtl/ustring.hxx>
32cdf0e10cSrcweir #include <rtl/string.hxx>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir namespace connectivity { namespace hsqldb
35cdf0e10cSrcweir {
36cdf0e10cSrcweir     class LogFile
37cdf0e10cSrcweir     {
38cdf0e10cSrcweir     private:
39cdf0e10cSrcweir         ::rtl::OUString     m_sFileName;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir     public:
42cdf0e10cSrcweir         LogFile( JNIEnv* env, jstring streamName, const sal_Char* _pAsciiSuffix );
43cdf0e10cSrcweir 
44cdf0e10cSrcweir     public:
45cdf0e10cSrcweir                 void    writeString( const sal_Char* _pString, bool _bEndLine = true );
create()46cdf0e10cSrcweir                 void    create() { getLogFile(); }
47cdf0e10cSrcweir         virtual void    close();
48cdf0e10cSrcweir 
49cdf0e10cSrcweir     protected:
50cdf0e10cSrcweir         FILE*&  getLogFile();
51cdf0e10cSrcweir     };
52cdf0e10cSrcweir 
53cdf0e10cSrcweir     class OperationLogFile : public LogFile
54cdf0e10cSrcweir     {
55cdf0e10cSrcweir     public:
OperationLogFile(JNIEnv * env,jstring streamName,const sal_Char * _pAsciiSuffix)56cdf0e10cSrcweir         OperationLogFile( JNIEnv* env, jstring streamName, const sal_Char* _pAsciiSuffix )
57cdf0e10cSrcweir             :LogFile( env, streamName, ( ::rtl::OString( _pAsciiSuffix ) += ".op" ).getStr() )
58cdf0e10cSrcweir         {
59cdf0e10cSrcweir         }
60cdf0e10cSrcweir 
logOperation(const sal_Char * _pOp)61cdf0e10cSrcweir         void logOperation( const sal_Char* _pOp )
62cdf0e10cSrcweir         {
63cdf0e10cSrcweir             writeString( _pOp, true );
64cdf0e10cSrcweir         }
65cdf0e10cSrcweir 
logOperation(const sal_Char * _pOp,jlong _nLongArg)66cdf0e10cSrcweir         void logOperation( const sal_Char* _pOp, jlong _nLongArg )
67cdf0e10cSrcweir         {
68cdf0e10cSrcweir             ::rtl::OString sLine( _pOp );
69cdf0e10cSrcweir             sLine += "( ";
70cdf0e10cSrcweir             sLine += ::rtl::OString::valueOf( _nLongArg );
71cdf0e10cSrcweir             sLine += " )";
72cdf0e10cSrcweir             writeString( sLine.getStr(), true );
73cdf0e10cSrcweir         }
74cdf0e10cSrcweir 
logReturn(jlong _nRetVal)75cdf0e10cSrcweir         void logReturn( jlong _nRetVal )
76cdf0e10cSrcweir         {
77cdf0e10cSrcweir             ::rtl::OString sLine( " -> " );
78cdf0e10cSrcweir             sLine += ::rtl::OString::valueOf( _nRetVal );
79cdf0e10cSrcweir             writeString( sLine.getStr(), true );
80cdf0e10cSrcweir         }
81cdf0e10cSrcweir 
logReturn(jint _nRetVal)82cdf0e10cSrcweir         void logReturn( jint _nRetVal )
83cdf0e10cSrcweir         {
84cdf0e10cSrcweir             ::rtl::OString sLine( " -> " );
85cdf0e10cSrcweir             sLine += ::rtl::OString::valueOf( _nRetVal );
86cdf0e10cSrcweir             writeString( sLine.getStr(), true );
87cdf0e10cSrcweir         }
88cdf0e10cSrcweir 
close()89cdf0e10cSrcweir         virtual void close()
90cdf0e10cSrcweir         {
91cdf0e10cSrcweir             writeString( "-------------------------------", true );
92cdf0e10cSrcweir             writeString( "", true );
93cdf0e10cSrcweir             LogFile::close();
94cdf0e10cSrcweir         }
95cdf0e10cSrcweir     };
96cdf0e10cSrcweir 
97cdf0e10cSrcweir     class DataLogFile : public LogFile
98cdf0e10cSrcweir     {
99cdf0e10cSrcweir     public:
DataLogFile(JNIEnv * env,jstring streamName,const sal_Char * _pAsciiSuffix)100cdf0e10cSrcweir         DataLogFile( JNIEnv* env, jstring streamName, const sal_Char* _pAsciiSuffix )
101cdf0e10cSrcweir             :LogFile( env, streamName, _pAsciiSuffix )
102cdf0e10cSrcweir         {
103cdf0e10cSrcweir         }
104cdf0e10cSrcweir 
write(jint value)105cdf0e10cSrcweir         void write( jint value )
106cdf0e10cSrcweir         {
107cdf0e10cSrcweir 			fputc( value, getLogFile() );
108cdf0e10cSrcweir             fflush( getLogFile() );
109cdf0e10cSrcweir         }
110cdf0e10cSrcweir 
write(const sal_Int8 * buffer,sal_Int32 bytesRead)111cdf0e10cSrcweir         void write( const sal_Int8* buffer, sal_Int32 bytesRead )
112cdf0e10cSrcweir         {
113cdf0e10cSrcweir 			fwrite( buffer, sizeof(sal_Int8), bytesRead, getLogFile() );
114cdf0e10cSrcweir             fflush( getLogFile() );
115cdf0e10cSrcweir         }
116cdf0e10cSrcweir 
seek(sal_Int64 pos)117cdf0e10cSrcweir         sal_Int64 seek( sal_Int64 pos )
118cdf0e10cSrcweir         {
119cdf0e10cSrcweir             FILE* pFile = getLogFile();
120cdf0e10cSrcweir             fseek( pFile, 0, SEEK_END );
121cdf0e10cSrcweir             if ( ftell( pFile ) < pos )
122cdf0e10cSrcweir             {
123cdf0e10cSrcweir                 sal_Int8 filler( 0 );
124cdf0e10cSrcweir                 while ( ftell( pFile ) < pos )
125cdf0e10cSrcweir                     fwrite( &filler, sizeof( sal_Int8 ), 1, pFile );
126cdf0e10cSrcweir                 fflush( pFile );
127cdf0e10cSrcweir             }
128cdf0e10cSrcweir             fseek( pFile, pos, SEEK_SET );
129cdf0e10cSrcweir             return ftell( pFile );
130cdf0e10cSrcweir         }
131cdf0e10cSrcweir 
tell()132cdf0e10cSrcweir         sal_Int64 tell()
133cdf0e10cSrcweir         {
134cdf0e10cSrcweir             return ftell( getLogFile() );
135cdf0e10cSrcweir         }
136cdf0e10cSrcweir     };
137cdf0e10cSrcweir 
138cdf0e10cSrcweir } }
139cdf0e10cSrcweir #endif
140cdf0e10cSrcweir 
141cdf0e10cSrcweir #endif // CONNECTIVITY_HSQLDB_ACCESSLOG_HXX
142