1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef CONNECTIVITY_HSQLDB_ACCESSLOG_HXX
29 #define CONNECTIVITY_HSQLDB_ACCESSLOG_HXX
30 
31 #ifdef HSQLDB_DBG
32 
33 #include <stdio.h>
34 #include <jni.h>
35 #include <rtl/ustring.hxx>
36 #include <rtl/string.hxx>
37 
38 namespace connectivity { namespace hsqldb
39 {
40     class LogFile
41     {
42     private:
43         ::rtl::OUString     m_sFileName;
44 
45     public:
46         LogFile( JNIEnv* env, jstring streamName, const sal_Char* _pAsciiSuffix );
47 
48     public:
49                 void    writeString( const sal_Char* _pString, bool _bEndLine = true );
50                 void    create() { getLogFile(); }
51         virtual void    close();
52 
53     protected:
54         FILE*&  getLogFile();
55     };
56 
57     class OperationLogFile : public LogFile
58     {
59     public:
60         OperationLogFile( JNIEnv* env, jstring streamName, const sal_Char* _pAsciiSuffix )
61             :LogFile( env, streamName, ( ::rtl::OString( _pAsciiSuffix ) += ".op" ).getStr() )
62         {
63         }
64 
65         void logOperation( const sal_Char* _pOp )
66         {
67             writeString( _pOp, true );
68         }
69 
70         void logOperation( const sal_Char* _pOp, jlong _nLongArg )
71         {
72             ::rtl::OString sLine( _pOp );
73             sLine += "( ";
74             sLine += ::rtl::OString::valueOf( _nLongArg );
75             sLine += " )";
76             writeString( sLine.getStr(), true );
77         }
78 
79         void logReturn( jlong _nRetVal )
80         {
81             ::rtl::OString sLine( " -> " );
82             sLine += ::rtl::OString::valueOf( _nRetVal );
83             writeString( sLine.getStr(), true );
84         }
85 
86         void logReturn( jint _nRetVal )
87         {
88             ::rtl::OString sLine( " -> " );
89             sLine += ::rtl::OString::valueOf( _nRetVal );
90             writeString( sLine.getStr(), true );
91         }
92 
93         virtual void close()
94         {
95             writeString( "-------------------------------", true );
96             writeString( "", true );
97             LogFile::close();
98         }
99     };
100 
101     class DataLogFile : public LogFile
102     {
103     public:
104         DataLogFile( JNIEnv* env, jstring streamName, const sal_Char* _pAsciiSuffix )
105             :LogFile( env, streamName, _pAsciiSuffix )
106         {
107         }
108 
109         void write( jint value )
110         {
111 			fputc( value, getLogFile() );
112             fflush( getLogFile() );
113         }
114 
115         void write( const sal_Int8* buffer, sal_Int32 bytesRead )
116         {
117 			fwrite( buffer, sizeof(sal_Int8), bytesRead, getLogFile() );
118             fflush( getLogFile() );
119         }
120 
121         sal_Int64 seek( sal_Int64 pos )
122         {
123             FILE* pFile = getLogFile();
124             fseek( pFile, 0, SEEK_END );
125             if ( ftell( pFile ) < pos )
126             {
127                 sal_Int8 filler( 0 );
128                 while ( ftell( pFile ) < pos )
129                     fwrite( &filler, sizeof( sal_Int8 ), 1, pFile );
130                 fflush( pFile );
131             }
132             fseek( pFile, pos, SEEK_SET );
133             return ftell( pFile );
134         }
135 
136         sal_Int64 tell()
137         {
138             return ftell( getLogFile() );
139         }
140     };
141 
142 } }
143 #endif
144 
145 #endif // CONNECTIVITY_HSQLDB_ACCESSLOG_HXX
146