fintp_base
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
InstrumentedObject.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 INSTRUMENTEDOBJECT_H
22 #define INSTRUMENTEDOBJECT_H
23 
24 #include "DllMain.h"
25 
26 #include <vector>
27 #include <map>
28 #include <iostream>
29 #include <sstream>
30 
31 #include <pthread.h>
32 
33 #define INIT_COUNTERS( instrumented_obj, intrumented_object_name ) \
34 {\
35  int mutexLockResult##intrumented_object_name = pthread_mutex_lock( &( InstrumentedObject::ObjMutex ) ); \
36  if ( 0 != mutexLockResult##intrumented_object_name ) \
37  { \
38  TRACE( "Unable to lock mutex for "#intrumented_object_name" [" << mutexLockResult##intrumented_object_name << "]" ); \
39  }; \
40  InstrumentedObject::Instance.registerCounter( typeid( *this ).name(), "_"#instrumented_obj, instrumented_obj ); \
41  int mutexUnlockResult##intrumented_object_name = pthread_mutex_unlock( &( InstrumentedObject::ObjMutex ) ); \
42  if ( 0 != mutexUnlockResult##intrumented_object_name ) \
43  { \
44  TRACE( "Unable to unlock mutex for "#intrumented_object_name" [" << mutexUnlockResult##intrumented_object_name << "]" ); \
45  }; \
46 }
47 
48 typedef map< string, unsigned long > IntstrumentedObject_CounterType;
49 
50 #define INIT_COUNTER( counterName ) ( void )m_Counters.insert( pair< string, unsigned long >( #counterName, 0 ) )
51 #define DESTROY_COUNTER( counterName ) if ( m_Counters.find( #counterName ) != m_Counters.end() ) ( void )m_Counters.erase( #counterName )
52 #define COUNTER( counterName ) m_Counters[ #counterName ]
53 
54 #define INCREMENT_COUNTER( counterName ) COUNTER( counterName ) = COUNTER( counterName )+1
55 #define INCREMENT_COUNTER_ON( instance, counterName ) instance->COUNTER( counterName ) = instance->COUNTER( counterName )+1
56 #define INCREMENT_COUNTER_ON_T( instance, counterName )
57 #define RESET_COUNTER( counterName ) COUNTER( counterName ) = 0;
58 #define ASSIGN_COUNTER( counterName, value ) COUNTER( counterName ) = value;
59 
60 namespace FinTP
61 {
63  {
64  private :
66  InstrumentedObject& operator=( const InstrumentedObject& obj );
67 
68  protected :
69  map< string, unsigned long > m_Counters;
70 
71  map< string, const InstrumentedObject* > m_RegisteredObjects;
72  unsigned int m_RegisterCounter;
73  vector< string > m_CollectedReports;
74 
76  virtual ~InstrumentedObject(){};
77 
78  string InternalGetCounters() const;
79 
80  public:
81 
83  static pthread_mutex_t ObjMutex;
84 
85  static unsigned int GetIntrumentedInstanceNo()
86  {
87  return Instance.m_RegisteredObjects.size();
88  }
89 
90  static void RemoveAllCounters()
91  {
92  Instance.m_RegisteredObjects.clear();
93  Instance.m_RegisterCounter = 0;
94  }
95 
96  void registerCounter( const string& ownerTypeName, const string& counterName, const InstrumentedObject* iobject );
97  static string Report();
98  static string Collect();
99  };
100 }
101 
102 #endif //INSTRUMENTEDOBJECT_H