Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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\ingroup Base
15Handle messages that might be generated by the system.
16By default a handler only keeps track of the different messages
17generated for a specific class. By deriving from this class and
18overriding Notify() one can implement custom message handling.
19In Notify() one has access to the message id and the object
20generating the message. One can install more than one message
21handler per class. A message handler can be removed or again
22added when needed.
23
24 - All Root "Warnings" are logged as message 1001
25 - All Root "Errors" are logged as message 1002
26 - All Root "SysErrors" are logged as message 1003
27 - All Root "Fatals" are logged as message 1004
28*/
29
30#include "TMessageHandler.h"
31#include "TClass.h"
32#include "TROOT.h"
33#include "TVirtualMutex.h"
34
36
37////////////////////////////////////////////////////////////////////////////////
38/// Create a new message handler for class cl and add it to the list
39/// of message handlers.
40
42{
43 fClass = cl;
44 fMessObj = nullptr;
45 fMessId = 0;
46 fSize = 0;
47 fCnts = nullptr;
48 fMessIds = nullptr;
49 fDerived = derived;
50
51 if (fClass)
53 else
54 SetName("DefaultMessageHandler");
55
56 Add();
57}
58
59////////////////////////////////////////////////////////////////////////////////
60/// Create a new message handler for class named cl and add it to the list
61/// of message handlers.
62
64{
66 fMessObj = nullptr;
67 fMessId = 0;
68 fSize = 0;
69 fCnts = nullptr;
70 fMessIds = nullptr;
71 fDerived = derived;
72
73 SetName(cl);
74
76 Add();
77}
78
79////////////////////////////////////////////////////////////////////////////////
80/// Clean up the message handler.
81
83{
84 Remove();
85 if (fSize <= 0) return;
86 delete [] fCnts;
87 delete [] fMessIds;
88}
89
90////////////////////////////////////////////////////////////////////////////////
91/// Add this message handler to the list of messages handlers.
92
94{
96 gROOT->GetListOfMessageHandlers()->Add(this);
97 if (fClass) {
98 // don't emit signal when the default message handler is added
99 // as this happens in the TROOT ctor and the TQObject stuff is
100 // not yet properly initialized on some platforms
101 Added(); // emit Added() signal
102 }
103}
104
105////////////////////////////////////////////////////////////////////////////////
106/// Return counter for message with ID=messid.
107
109{
110 if (fSize <= 0) return 0;
111 for (Int_t i = 0; i < fSize; i++) {
112 if (fMessIds[i] == messId) return fCnts[i];
113 }
114 return 0;
115}
116
117////////////////////////////////////////////////////////////////////////////////
118/// Return total number of messages.
119
121{
122 if (fSize <= 0) return 0;
123 Int_t count = 0;
124 for (Int_t i = 0; i < fSize; i++) {
125 count += fCnts[i];
126 }
127 return count;
128}
129
130////////////////////////////////////////////////////////////////////////////////
131/// Store message origin, keep statistics and call Notify().
132
134{
135 // check if message must be managed by this message handler
136 if (fClass) {
137 if (fDerived) {
138 if(!obj->InheritsFrom(fClass)) return;
139 } else {
140 if (obj->IsA() != fClass) return;
141 }
142 }
143
144 fMessId = id;
145 fMessObj = obj;
146
147 Notify();
148
149 // increment statistics
150 Int_t i;
151 // first message
152 if (fSize <= 0) {
153 fSize = 1;
154 fCnts = new Int_t[fSize];
155 fMessIds = new Long_t[fSize];
156 } else {
157 // already existing message
158 for (i = 0; i < fSize; i++) {
159 if (fMessIds[i] == fMessId) {
160 fCnts[i]++;
161 return;
162 }
163 }
164 // new message
165 fSize++;
166 Int_t *newCnts = new Int_t[fSize];
167 Long_t *newMessIds = new Long_t[fSize];
168 for (i = 0; i < fSize-1; i++) {
169 newCnts[i] = fCnts[i];
170 newMessIds[i] = fMessIds[i];
171 }
172 delete [] fCnts;
173 delete [] fMessIds;
174 fCnts = newCnts;
175 fMessIds = newMessIds;
176 }
177 fCnts[fSize-1] = 1;
179}
180
181////////////////////////////////////////////////////////////////////////////////
182/// This method must be overridden to handle object notification.
183
185{
186 if (fClass) return kFALSE;
187 // case of default handler
188 // encode class number in message id
189 if (!fMessObj) return kFALSE;
190 Long_t uid = Long_t(fMessObj->IsA()->GetUniqueID());
191 fMessId += 10000*uid;
192 fMessId = -fMessId;
193 Notified(); // emit Notified() signal
194 return kFALSE;
195}
196
197////////////////////////////////////////////////////////////////////////////////
198/// Print statistics for this message handler.
199
201{
202 printf("\n ****** Message Handler: %s has a total of %d messages\n",GetName(),GetTotalMessageCount());
203 if (fSize <= 0) return;
204 Long_t id;
205 Int_t uid;
206 const TClass *cl;
207 TIter next(gROOT->GetListOfClasses());
208 for (Int_t i = 0; i < fSize; i++) {
209 id = fMessIds[i];
210 cl = fClass;
211 if (id < 0) {
212 id = -id;
213 uid = id/10000;
214 id = id%10000;
215 next.Reset();
216 while ((cl = (TClass*)next())) {
217 if (cl->GetUniqueID() == UInt_t(uid)) break;
218 }
219 }
220 if (!cl) cl = gROOT->IsA();
221 if (id == 1001) {
222 printf(" Class: %-20s WARNINGs has %d counts\n",cl->GetName(),fCnts[i]);
223 continue;
224 }
225 if (id == 1002) {
226 printf(" Class: %-20s ERRORs has %d counts\n",cl->GetName(),fCnts[i]);
227 continue;
228 }
229 printf(" Class: %-20s MessID = %5ld has %d counts\n",cl->GetName(),id,fCnts[i]);
230 }
231}
232
233////////////////////////////////////////////////////////////////////////////////
234/// Remove this message handler from the list of messages handlers.
235
237{
239 gROOT->GetListOfMessageHandlers()->Remove(this);
240 Removed(); // emit Removed() signal
241}
long Long_t
Definition RtypesCore.h:54
constexpr Bool_t kFALSE
Definition RtypesCore.h:101
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:377
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize id
R__EXTERN TVirtualMutex * gROOTMutex
Definition TROOT.h:63
#define gROOT
Definition TROOT.h:406
#define R__LOCKGUARD(mutex)
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:81
TClass * IsA() const override
Definition TClass.h:614
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:2968
void Reset()
Handle messages that might be generated by the system.
virtual void Removed()
virtual Int_t GetMessageCount(Long_t messId) const
Return counter for message with ID=messid.
const TObject * fMessObj
TMessageHandler(const TClass *cl, Bool_t derived=kTRUE)
Create a new message handler for class cl and add it to the list of message handlers.
virtual ~TMessageHandler()
Clean up the message handler.
virtual void Remove()
Remove this message handler from the list of messages handlers.
virtual void HandleMessage(Long_t id, const TObject *obj)
Store message origin, keep statistics and call Notify().
void Print(Option_t *option="") const override
Print statistics for this message handler.
virtual void Added()
virtual Int_t GetTotalMessageCount() const
Return total number of messages.
virtual void Add()
Add this message handler to the list of messages handlers.
const TClass * fClass
virtual void Notified()
Bool_t Notify() override
This method must be overridden to handle object notification.
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition TNamed.cxx:140
Mother of all ROOT objects.
Definition TObject.h:41
virtual UInt_t GetUniqueID() const
Return the unique object id.
Definition TObject.cxx:457
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition TObject.cxx:525
virtual TClass * IsA() const
Definition TObject.h:245