TMessageHandler.cxx

Go to the documentation of this file.
00001 // @(#)root/base:$Id: TMessageHandler.cxx 22421 2008-03-03 10:54:03Z rdm $
00002 // Author: Rene Brun   11/11/99
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 
00013 //////////////////////////////////////////////////////////////////////////
00014 //                                                                      //
00015 // TMessageHandler                                                      //
00016 //                                                                      //
00017 // Handle messages that might be generated by the system.               //
00018 // By default a handler only keeps track of the different messages      //
00019 // generated for a specific class. By deriving from this class and      //
00020 // overriding Notify() one can implement custom message handling.       //
00021 // In Notify() one has access to the message id and the object          //
00022 // generating the message. One can install more than one message        //
00023 // handler per class. A message handler can be removed or again         //
00024 // added when needed.                                                   //
00025 // All Root "Warnings"  are logged as message 1001                      //
00026 // All Root "Errors"    are logged as message 1002                      //
00027 // All Root "SysErrors" are logged as message 1003                      //
00028 // All Root "Fatals"    are logged as message 1004                      //
00029 //                                                                      //
00030 //////////////////////////////////////////////////////////////////////////
00031 
00032 #include "TMessageHandler.h"
00033 #include "TClass.h"
00034 #include "TROOT.h"
00035 #include "TVirtualMutex.h"
00036 
00037 ClassImp(TMessageHandler)
00038 
00039 //______________________________________________________________________________
00040 TMessageHandler::TMessageHandler(const TClass *cl, Bool_t derived)
00041 {
00042    // Create a new message handler for class cl and add it to the list
00043    // of message handlers.
00044 
00045    fClass   = cl;
00046    fMessObj = 0;
00047    fMessId  = 0;
00048    fSize    = 0;
00049    fCnts    = 0;
00050    fMessIds = 0;
00051    fDerived = derived;
00052 
00053    if (fClass)
00054       SetName(fClass->GetName());
00055    else
00056       SetName("DefaultMessageHandler");
00057 
00058    Add();
00059 }
00060 
00061 //______________________________________________________________________________
00062 TMessageHandler::TMessageHandler(const char *cl, Bool_t derived)
00063 {
00064    // Create a new message handler for class named cl and add it to the list
00065    // of message handlers.
00066 
00067    fClass   = TClass::GetClass(cl);
00068    fMessObj = 0;
00069    fMessId  = 0;
00070    fSize    = 0;
00071    fCnts    = 0;
00072    fMessIds = 0;
00073    fDerived = derived;
00074 
00075    SetName(cl);
00076 
00077    SetName(fClass->GetName());
00078    Add();
00079 }
00080 
00081 //______________________________________________________________________________
00082 TMessageHandler:: ~TMessageHandler()
00083 {
00084    // Clean up the messagehandler.
00085 
00086    Remove();
00087    if (fSize <= 0) return;
00088    delete [] fCnts;
00089    delete [] fMessIds;
00090 }
00091 
00092 //______________________________________________________________________________
00093 void TMessageHandler::Add()
00094 {
00095    // Add this message handler to the list of messages handlers.
00096 
00097    R__LOCKGUARD2(gROOTMutex);
00098    gROOT->GetListOfMessageHandlers()->Add(this);
00099    if (fClass) {
00100       // don't emit signal when the default message handler is added
00101       // as this happens in the TROOT ctor and the TQObject stuff is
00102       // not yet properly initialized on some platforms
00103       Added();  // emit Added() signal
00104    }
00105 }
00106 
00107 //______________________________________________________________________________
00108 Int_t TMessageHandler::GetMessageCount(Int_t messId) const
00109 {
00110    // Return counter for message with ID=messid.
00111 
00112    if (fSize <= 0) return 0;
00113    for (Int_t i = 0; i < fSize; i++) {
00114       if (fMessIds[i] == messId) return fCnts[i];
00115    }
00116    return 0;
00117 }
00118 
00119 //______________________________________________________________________________
00120 Int_t TMessageHandler::GetTotalMessageCount() const
00121 {
00122    // Return total number of messages.
00123 
00124    if (fSize <= 0) return 0;
00125    Int_t count = 0;
00126    for (Int_t i = 0; i < fSize; i++) {
00127       count += fCnts[i];
00128    }
00129    return count;
00130 }
00131 
00132 //______________________________________________________________________________
00133 void TMessageHandler::HandleMessage(Int_t id, const TObject *obj)
00134 {
00135    // Store message origin, keep statistics and call Notify().
00136 
00137    // check if message must be managed by this message handler
00138    if (fClass) {
00139       if (fDerived) {
00140          if(!obj->InheritsFrom(fClass)) return;
00141       } else {
00142          if (obj->IsA() != fClass) return;
00143       }
00144    }
00145 
00146    fMessId  = id;
00147    fMessObj = obj;
00148 
00149    Notify();
00150 
00151    // increment statistics
00152    Int_t i;
00153    // first message
00154    if (fSize <= 0) {
00155       fSize    = 1;
00156       fCnts    = new Int_t[fSize];
00157       fMessIds = new Int_t[fSize];
00158    } else {
00159       // already existing message
00160       for (i = 0; i < fSize; i++) {
00161          if (fMessIds[i] == fMessId) {
00162             fCnts[i]++;
00163             return;
00164          }
00165       }
00166       // new message
00167       fSize++;
00168       Int_t *newCnts    = new Int_t[fSize];
00169       Int_t *newMessIds = new Int_t[fSize];
00170       for (i = 0; i < fSize-1; i++) {
00171          newCnts[i]    = fCnts[i];
00172          newMessIds[i] = fMessIds[i];
00173       }
00174       delete [] fCnts;
00175       delete [] fMessIds;
00176       fCnts    = newCnts;
00177       fMessIds = newMessIds;
00178    }
00179    fCnts[fSize-1]    = 1;
00180    fMessIds[fSize-1] = fMessId;
00181 }
00182 
00183 //______________________________________________________________________________
00184 Bool_t TMessageHandler::Notify()
00185 {
00186    // This method must be overridden to handle object notifcation.
00187 
00188    if (fClass) return kFALSE;
00189    // case of default handler
00190    // encode class number in message id
00191    if (!fMessObj) return kFALSE;
00192    Int_t uid = Int_t(fMessObj->IsA()->GetUniqueID());
00193    fMessId += 10000*uid;
00194    fMessId = -fMessId;
00195    Notified();  // emit Notified() signal
00196    return kFALSE;
00197 }
00198 
00199 //______________________________________________________________________________
00200 void TMessageHandler::Print(Option_t *) const
00201 {
00202    // Print statistics for this message handler.
00203 
00204    printf("\n ****** Message Handler: %s has a total of %d messages\n",GetName(),GetTotalMessageCount());
00205    if (fSize <= 0) return;
00206    Int_t id, uid;
00207    const TClass *cl;
00208    TIter next(gROOT->GetListOfClasses());
00209    for (Int_t i = 0; i < fSize; i++) {
00210       id = fMessIds[i];
00211       cl = fClass;
00212       if (id < 0) {
00213          id = -id;
00214          uid = id/10000;
00215          id = id%10000;
00216          next.Reset();
00217          while ((cl = (TClass*)next())) {
00218             if (cl->GetUniqueID() == UInt_t(uid)) break;
00219          }
00220       }
00221       if (!cl) cl = gROOT->IsA();
00222       if (id == 1001) {
00223          printf("  Class: %-20s WARNINGs       has %d counts\n",cl->GetName(),fCnts[i]);
00224          continue;
00225       }
00226       if (id == 1002) {
00227          printf("  Class: %-20s ERRORs         has %d counts\n",cl->GetName(),fCnts[i]);
00228          continue;
00229       }
00230       printf("  Class: %-20s MessID = %5d has %d counts\n",cl->GetName(),id,fCnts[i]);
00231    }
00232 }
00233 
00234 //______________________________________________________________________________
00235 void TMessageHandler::Remove()
00236 {
00237    // Remove this message handler from the list of messages handlers.
00238 
00239    R__LOCKGUARD2(gROOTMutex);
00240    gROOT->GetListOfMessageHandlers()->Remove(this);
00241    Removed();  // emit Removed() signal
00242 }

Generated on Tue Jul 5 14:11:21 2011 for ROOT_528-00b_version by  doxygen 1.5.1