fintp_log
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Trace.h
Go to the documentation of this file.
1 /*
2 * FinTP - Financial Transactions Processing Application
3 * Copyright (C) 2013 Business Information Systems (Allevo) S.R.L.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>
17 * or contact Allevo at : 031281 Bucuresti, 23C Calea Vitan, Romania,
18 * phone +40212554577, office@allevo.ro <mailto:office@allevo.ro>, www.allevo.ro.
19 */
20 
21 #ifndef QPAYTRACE_H
22 #define QPAYTRACE_H
23 
24 #include "DllMainLog.h"
25 
26 #include <map>
27 #include <vector>
28 
29 #include <fstream>
30 #include <string>
31 #include <iostream>
32 #include <sstream>
33 
34 #include <pthread.h>
35 
36 using namespace std;
37 
38 #ifdef WIN32
39 #define __MSXML_LIBRARY_DEFINED__
40 #include <windows.h>
41 #include <string.h>
42 #define QPIFILE ( ( strrchr(__FILE__, '\\') != NULL ) ? strrchr(__FILE__, '\\') + 1 : __FILE__ )
43 #else
44 #define QPIFILE (__FILE__)
45 #endif
46 
47 #if defined( WIN32 ) && defined ( _DEBUG ) && defined( CHECK_MEM_LEAKS )
48 
49 #define MEM_CHECKPOINT_INIT() \
50 { \
51  _CrtMemState state1, state2; \
52  _CrtMemCheckpoint( &state1 ); \
53  _CrtMemState memDiff; \
54  {
55 
56 #define MEM_CHECKPOINT_COLLECT() \
57  _CrtMemCheckpoint( &state2 );
58 
59 #define MEM_CHECKPOINT_END( clause, module ) \
60  } \
61  MEM_CHECKPOINT_COLLECT() \
62  if ( _CrtMemDifference( &memDiff, &state1, &state2 ) ) \
63  { \
64  _CrtDbgReport( _CRT_WARN, QPIFILE, __LINE__, module, \
65  "Memory leaks detected during "##clause". Dumping objects... \n" ); \
66  _CrtMemDumpAllObjectsSince( &state1 ); \
67  _CrtMemDumpStatistics( &memDiff ); \
68  _CrtDbgReport( _CRT_WARN, QPIFILE, __LINE__, module, "Dumping done. \n" ); \
69  } \
70 }
71 
72 #define MEM_CHECKPOINT_END_REPORT( clause, module ) \
73  } \
74  if ( _CrtMemDifference( &memDiff, &state1, &state2 ) ) \
75  { \
76  _CrtDbgReport( _CRT_WARN, QPIFILE, __LINE__, module, \
77  "Memory leaks detected during "##clause". Dumping objects... \n" ); \
78  _CrtMemDumpAllObjectsSince( &state1 ); \
79  _CrtMemDumpStatistics( &memDiff ); \
80  _CrtDbgReport( _CRT_WARN, QPIFILE, __LINE__, module, "Dumping done. \n" ); \
81  } \
82 }
83 #define MEM_CHECKPOINT_END_FAIL( clause, module ) \
84  } \
85  MEM_CHECKPOINT_COLLECT() \
86  if ( _CrtMemDifference( &memDiff, &state1, &state2 ) ) \
87  { \
88  _CrtDbgReport( _CRT_WARN, QPIFILE, __LINE__, module, \
89  "Memory leaks detected during "##clause". Dumping objects... \n" ); \
90  _CrtMemDumpAllObjectsSince( &state1 ); \
91  _CrtMemDumpStatistics( &memDiff ); \
92  _CrtDbgReport( _CRT_WARN, QPIFILE, __LINE__, module, "Dumping done. \n" ); \
93  CPPUNIT_FAIL( "Memory leaks detected during "##clause ); \
94  } \
95 }
96 
97 #else
98 #define MEM_CHECKPOINT_INIT() {
99 #define MEM_CHECKPOINT_END( clause, module ) }
100 #define MEM_CHECKPOINT_END_REPORT( clause, module ) }
101 #define MEM_CHECKPOINT_END_FAIL( clause, module ) }
102 #define MEM_CHECKPOINT_COLLECT()
103 #endif
104 
105 #define cnull std::ofstream( "DebugOutput.out", ios::app )
106 
107 #ifdef DEBUG_ENABLED
108 //EXPIMP_TEMPLATE template class ExportedLogObject std::map< pthread_t, ofstream* >;
109 namespace FinTP
110 {
111  class ExportedLogObject FileOutputter
112  {
113  private :
114 
115  FileOutputter();
116 
117  static pthread_once_t KeysCreate;
118  static pthread_key_t StreamKey;
119  static pthread_key_t LinesKey;
120  static pthread_key_t NamesKey;
121  static pthread_key_t ExtraFilesKey;
122 
123  static string Prefix;
124  static unsigned long MaxLines;
125  static unsigned long MaxExtraFiles;
126 
127  static FileOutputter m_Instance;
128 
129  static bool m_Terminated;
130  static pthread_mutex_t m_OutputtersSyncMutex;
131 
132  static void CreateKeys();
133  static void DeleteStreams( void* data );
134  static void DeleteLines( void* data );
135  static void DeleteNames( void* data );
136  static void DeleteExtraFiles( void* data );
137 
138  static ostream& internalGetStream();
139 
140  public :
141 
142  ~FileOutputter();
143 
144  static void setLogMaxLines( const unsigned long maxLines = 0 );
145 
146  static void setLogPrefix( const string prefix = "Unnamed" );
147 
148  static ostream& getStream();
149  static bool wasTerminated();
150  static void setTerminated( const bool value );
151 
152  static void setLogMaxExtraFiles( const unsigned long maxLogs = 0 );
153  };
154 }
155 
156 //#define DEBUG_GLOBAL( expr ) { stringstream *_ostrlog = new stringstream(); *_ostrlog << "DEBUG [" << QPIFILE << "] - " << __LINE__ << " : " << expr << endl << flush; cerr.write( _ostrlog->str().c_str(), _ostrlog->str().length() ); cerr << flush; delete _ostrlog; }
157 #define DEBUG_GLOBAL( expr ) { if ( !FileOutputter::wasTerminated() ) { FileOutputter::getStream() << "DEBUG [" << QPIFILE << "] - " << __LINE__ << " : " << expr << endl << flush; } }
158 #define DEBUG( expr ) DEBUG_GLOBAL( expr )
159 
160 #ifdef WIN32
161 #define DEBUG_PRINT_STACK ;
162 #else
163 #define DEBUG_PRINT_STACK ;
164 #endif
165 
166 #ifdef EXTENDED_DEBUG
167 #define DEBUG2( expr ) DEBUG( expr )
168 #define DEBUG_GLOBAL2( expr ) DEBUG_GLOBAL( expr )
169 #else
170 #define DEBUG2( expr ) ;
171 #define DEBUG_GLOBAL2( expr ) ;
172 #endif
173 
174 
175 #ifdef WIN32_SERVICE
176 #define TRACE_SERVICE( expr ) { stringstream _errormessage; _errormessage << expr; OutputDebugString( TEXT( _errormessage.str().c_str() ) ); FileOutputter::getStream() << "TRACE [" << QPIFILE << "] - " << __LINE__ << " : " << expr << endl << flush; }
177 #define TRACE_GLOBAL( expr ) TRACE_SERVICE( expr )
178 #define TRACE( expr ) TRACE_SERVICE( expr )
179 #define TRACE_NOLOG( expr ) \
180  { \
181  try \
182  { \
183  stringstream _errormessage; _errormessage << expr; OutputDebugString( TEXT( _errormessage.str().c_str() ) ); FileOutputter::getStream() << "TRACE [" << QPIFILE << "] - " << __LINE__ << " : " << expr << endl << flush; \
184  } \
185  catch( ... ){} \
186  }
187 #define DEBUG_NOLOG( expr ) { cout << "DEBUG [" << QPIFILE << "] - " << __LINE__ << " : " << expr << endl << flush; }
188 #else
189 #define TRACE_GLOBAL( expr ) { if ( !FileOutputter::wasTerminated() ) { FileOutputter::getStream() << "TRACE [" << QPIFILE << "] - " << __LINE__ << " : " << expr << endl << flush; } }
190 #define TRACE( expr ) TRACE_GLOBAL( expr )
191 #define TRACE_SERVICE( expr ) TRACE_GLOBAL( expr )
192 #define TRACE_NOLOG( expr ) \
193  { \
194  try \
195  { \
196  FileOutputter::getStream() << "TRACE [" << QPIFILE << "] - " << __LINE__ << " : " << expr << endl << flush; \
197  } \
198  catch( ... ){} \
199  try \
200  { \
201  cerr << "TRACE [" << QPIFILE << "] - " << __LINE__ << " : " << expr << endl << flush; \
202  } catch( ... ){}; \
203  }
204 #define DEBUG_NOLOG( expr ) { cout << "DEBUG [" << QPIFILE << "] - " << __LINE__ << " : " << expr << endl << flush; }
205 #endif // WIN32_SERVICE
206 
207 #else //DEBUG_ENABLED
208 
209 #define DEBUG( expr ) ;
210 #define DEBUG2( expr ) ;
211 #define DEBUG_GLOBAL( expr ) ;
212 #define DEBUG_GLOBAL2( expr ) ;
213 #define DEBUG_PRINT_STACK ;
214 #define TRACE_GLOBAL( expr ) ;
215 #define TRACE( expr ) ;
216 #define TRACE_NOLOG( expr ) ;
217 
218 #endif //DEBUG_ENABLED
219 
220 #endif // QPAYTRACE_H
221