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
35
36////////////////////////////////////////////////////////////////////////////////
37/// Create a new message handler for class cl and add it to the list
38/// of message handlers.
39
41{
42 fClass = cl;
43 fMessObj = nullptr;
44 fMessId = 0;
45 fSize = 0;
46 fCnts = nullptr;
47 fMessIds = nullptr;
49
50 if (fClass)
51 SetName(fClass->GetName());
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
63{
65 fMessObj = nullptr;
66 fMessId = 0;
67 fSize = 0;
68 fCnts = nullptr;
69 fMessIds = nullptr;
71
72 SetName(cl);
73
74 SetName(fClass->GetName());
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 Long_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];
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;
175 }
176 fCnts[fSize-1] = 1;
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 Long_t uid = Long_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 Long_t id;
204 Int_t uid;
205 const TClass *cl;
206 TIter next(gROOT->GetListOfClasses());
207 for (Int_t i = 0; i < fSize; i++) {
208 id = fMessIds[i];
209 cl = fClass;
210 if (id < 0) {
211 id = -id;
212 uid = id/10000;
213 id = id%10000;
214 next.Reset();
215 while ((cl = (TClass*)next())) {
216 if (cl->GetUniqueID() == UInt_t(uid)) break;
217 }
218 }
219 if (!cl) cl = gROOT->IsA();
220 if (id == 1001) {
221 printf(" Class: %-20s WARNINGs has %d counts\n",cl->GetName(),fCnts[i]);
222 continue;
223 }
224 if (id == 1002) {
225 printf(" Class: %-20s ERRORs has %d counts\n",cl->GetName(),fCnts[i]);
226 continue;
227 }
228 printf(" Class: %-20s MessID = %5ld has %d counts\n",cl->GetName(),id,fCnts[i]);
229 }
230}
231
232////////////////////////////////////////////////////////////////////////////////
233/// Remove this message handler from the list of messages handlers.
234
236{
238 gROOT->GetListOfMessageHandlers()->Remove(this);
239 Removed(); // emit Removed() signal
240}
long Long_t
Signed long integer 4 bytes (long). Size depends on architecture.
Definition RtypesCore.h:68
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
const char Option_t
Option string (const char)
Definition RtypesCore.h:80
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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:411
#define R__LOCKGUARD(mutex)
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:84
TClass * IsA() const override
Definition TClass.h:634
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:2973
void Reset()
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:49
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition TNamed.cxx:149
Mother of all ROOT objects.
Definition TObject.h:41
virtual UInt_t GetUniqueID() const
Return the unique object id.
Definition TObject.cxx:475
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition TObject.cxx:543
virtual TClass * IsA() const
Definition TObject.h:246