ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TMessageHandler.cxx
Go to the documentation of this file.
1 // @(#)root/base:$Id$
2 // Author: Rene Brun 11/11/99
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 /** \class TMessageHandler
13 
14 Handle messages that might be generated by the system.
15 By default a handler only keeps track of the different messages
16 generated for a specific class. By deriving from this class and
17 overriding Notify() one can implement custom message handling.
18 In Notify() one has access to the message id and the object
19 generating the message. One can install more than one message
20 handler per class. A message handler can be removed or again
21 added when needed.
22 
23  - All Root "Warnings" are logged as message 1001
24  - All Root "Errors" are logged as message 1002
25  - All Root "SysErrors" are logged as message 1003
26  - All Root "Fatals" are logged as message 1004
27 */
28 
29 #include "TMessageHandler.h"
30 #include "TClass.h"
31 #include "TROOT.h"
32 #include "TVirtualMutex.h"
33 
35 
36 ////////////////////////////////////////////////////////////////////////////////
37 /// Create a new message handler for class cl and add it to the list
38 /// of message handlers.
39 
40 TMessageHandler::TMessageHandler(const TClass *cl, Bool_t derived)
41 {
42  fClass = cl;
43  fMessObj = 0;
44  fMessId = 0;
45  fSize = 0;
46  fCnts = 0;
47  fMessIds = 0;
48  fDerived = derived;
49 
50  if (fClass)
52  else
53  SetName("DefaultMessageHandler");
54 
55  Add();
56 }
57 
58 ////////////////////////////////////////////////////////////////////////////////
59 /// Create a new message handler for class named cl and add it to the list
60 /// of message handlers.
61 
62 TMessageHandler::TMessageHandler(const char *cl, Bool_t derived)
63 {
65  fMessObj = 0;
66  fMessId = 0;
67  fSize = 0;
68  fCnts = 0;
69  fMessIds = 0;
70  fDerived = derived;
71 
72  SetName(cl);
73 
75  Add();
76 }
77 
78 ////////////////////////////////////////////////////////////////////////////////
79 /// Clean up the message handler.
80 
82 {
83  Remove();
84  if (fSize <= 0) return;
85  delete [] fCnts;
86  delete [] fMessIds;
87 }
88 
89 ////////////////////////////////////////////////////////////////////////////////
90 /// Add this message handler to the list of messages handlers.
91 
93 {
95  gROOT->GetListOfMessageHandlers()->Add(this);
96  if (fClass) {
97  // don't emit signal when the default message handler is added
98  // as this happens in the TROOT ctor and the TQObject stuff is
99  // not yet properly initialized on some platforms
100  Added(); // emit Added() signal
101  }
102 }
103 
104 ////////////////////////////////////////////////////////////////////////////////
105 /// Return counter for message with ID=messid.
106 
108 {
109  if (fSize <= 0) return 0;
110  for (Int_t i = 0; i < fSize; i++) {
111  if (fMessIds[i] == messId) return fCnts[i];
112  }
113  return 0;
114 }
115 
116 ////////////////////////////////////////////////////////////////////////////////
117 /// Return total number of messages.
118 
120 {
121  if (fSize <= 0) return 0;
122  Int_t count = 0;
123  for (Int_t i = 0; i < fSize; i++) {
124  count += fCnts[i];
125  }
126  return count;
127 }
128 
129 ////////////////////////////////////////////////////////////////////////////////
130 /// Store message origin, keep statistics and call Notify().
131 
133 {
134  // check if message must be managed by this message handler
135  if (fClass) {
136  if (fDerived) {
137  if(!obj->InheritsFrom(fClass)) return;
138  } else {
139  if (obj->IsA() != fClass) return;
140  }
141  }
142 
143  fMessId = id;
144  fMessObj = obj;
145 
146  Notify();
147 
148  // increment statistics
149  Int_t i;
150  // first message
151  if (fSize <= 0) {
152  fSize = 1;
153  fCnts = new Int_t[fSize];
154  fMessIds = new Int_t[fSize];
155  } else {
156  // already existing message
157  for (i = 0; i < fSize; i++) {
158  if (fMessIds[i] == fMessId) {
159  fCnts[i]++;
160  return;
161  }
162  }
163  // new message
164  fSize++;
165  Int_t *newCnts = new Int_t[fSize];
166  Int_t *newMessIds = new Int_t[fSize];
167  for (i = 0; i < fSize-1; i++) {
168  newCnts[i] = fCnts[i];
169  newMessIds[i] = fMessIds[i];
170  }
171  delete [] fCnts;
172  delete [] fMessIds;
173  fCnts = newCnts;
174  fMessIds = newMessIds;
175  }
176  fCnts[fSize-1] = 1;
177  fMessIds[fSize-1] = fMessId;
178 }
179 
180 ////////////////////////////////////////////////////////////////////////////////
181 /// This method must be overridden to handle object notification.
182 
184 {
185  if (fClass) return kFALSE;
186  // case of default handler
187  // encode class number in message id
188  if (!fMessObj) return kFALSE;
189  Int_t uid = Int_t(fMessObj->IsA()->GetUniqueID());
190  fMessId += 10000*uid;
191  fMessId = -fMessId;
192  Notified(); // emit Notified() signal
193  return kFALSE;
194 }
195 
196 ////////////////////////////////////////////////////////////////////////////////
197 /// Print statistics for this message handler.
198 
200 {
201  printf("\n ****** Message Handler: %s has a total of %d messages\n",GetName(),GetTotalMessageCount());
202  if (fSize <= 0) return;
203  Int_t id, uid;
204  const TClass *cl;
205  TIter next(gROOT->GetListOfClasses());
206  for (Int_t i = 0; i < fSize; i++) {
207  id = fMessIds[i];
208  cl = fClass;
209  if (id < 0) {
210  id = -id;
211  uid = id/10000;
212  id = id%10000;
213  next.Reset();
214  while ((cl = (TClass*)next())) {
215  if (cl->GetUniqueID() == UInt_t(uid)) break;
216  }
217  }
218  if (!cl) cl = gROOT->IsA();
219  if (id == 1001) {
220  printf(" Class: %-20s WARNINGs has %d counts\n",cl->GetName(),fCnts[i]);
221  continue;
222  }
223  if (id == 1002) {
224  printf(" Class: %-20s ERRORs has %d counts\n",cl->GetName(),fCnts[i]);
225  continue;
226  }
227  printf(" Class: %-20s MessID = %5d has %d counts\n",cl->GetName(),id,fCnts[i]);
228  }
229 }
230 
231 ////////////////////////////////////////////////////////////////////////////////
232 /// Remove this message handler from the list of messages handlers.
233 
235 {
237  gROOT->GetListOfMessageHandlers()->Remove(this);
238  Removed(); // emit Removed() signal
239 }
ClassImp(TMessageHandler) TMessageHandler
Create a new message handler for class cl and add it to the list of message handlers.
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition: TObject.cxx:487
ClassImp(TSeqCollection) Int_t TSeqCollection TIter next(this)
Return index of object in collection.
virtual Int_t GetTotalMessageCount() const
Return total number of messages.
TMatrixT< Element > & Add(TMatrixT< Element > &target, Element scalar, const TMatrixT< Element > &source)
Modify addition: target += scalar * source.
Definition: TMatrixT.cxx:2925
Handle messages that might be generated by the system.
const char Option_t
Definition: RtypesCore.h:62
virtual void Removed()
virtual void Add()
Add this message handler to the list of messages handlers.
virtual void HandleMessage(Int_t id, const TObject *obj)
Store message origin, keep statistics and call Notify().
virtual void SetName(const char *name)
Change (i.e.
Definition: TNamed.cxx:128
#define gROOT
Definition: TROOT.h:344
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
R__EXTERN TVirtualMutex * gROOTMutex
Definition: TROOT.h:63
const Bool_t kFALSE
Definition: Rtypes.h:92
UChar_t mod R__LOCKGUARD2(gSrvAuthenticateMutex)
TClass * fClass
pointer to the foreign object
XFontStruct * id
Definition: TGX11.cxx:108
virtual Bool_t Notify()
This method must be overridden to handle object notification.
unsigned int UInt_t
Definition: RtypesCore.h:42
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
The ROOT global object gROOT contains a list of all defined classes.
Definition: TClass.h:81
virtual void Print(Option_t *option="") const
Print statistics for this message handler.
virtual Int_t GetMessageCount(Int_t messId) const
Return counter for message with ID=messid.
virtual void Notified()
virtual ~TMessageHandler()
Clean up the message handler.
virtual void Added()
ClassImp(TMCParticle) void TMCParticle printf(": p=(%7.3f,%7.3f,%9.3f) ;", fPx, fPy, fPz)
static TClass * GetClass(const char *name, Bool_t load=kTRUE, Bool_t silent=kFALSE)
Static method returning pointer to TClass of the specified class name.
Definition: TClass.cxx:2801
virtual UInt_t GetUniqueID() const
Return the unique object id.
Definition: TObject.cxx:433
virtual void Remove()
Remove this message handler from the list of messages handlers.
Mother of all ROOT objects.
Definition: TObject.h:58
TMessageHandler(const TClass *cl, Bool_t derived=kTRUE)
TObject * obj
const TObject * fMessObj
gr SetName("gr")
const TClass * fClass