]> git.uio.no Git - u/mrichter/AliRoot.git/commitdiff
Adding helper class for suppression of error message floods caused by error messages...
authorrichterm <richterm@f7af4fe6-9843-0410-8265-dc069ae4e863>
Mon, 5 Jul 2010 08:42:20 +0000 (08:42 +0000)
committerrichterm <richterm@f7af4fe6-9843-0410-8265-dc069ae4e863>
Mon, 5 Jul 2010 08:42:20 +0000 (08:42 +0000)
The class suppresses the printout after an adjustable number of occurences and prints an error summary when the instance gets out of scope.
Simple usage by predefined macro
 * if (nastyerror) {
 *   ALIHLTERRORGUARD(5, "nasty error, first occurence in event %d", event);
 * }

HLT/BASE/AliHLTErrorGuard.cxx [new file with mode: 0644]
HLT/BASE/AliHLTErrorGuard.h [new file with mode: 0644]
HLT/BASE/HLTbaseLinkDef.h
HLT/libHLTbase.pkg

diff --git a/HLT/BASE/AliHLTErrorGuard.cxx b/HLT/BASE/AliHLTErrorGuard.cxx
new file mode 100644 (file)
index 0000000..c2c75f2
--- /dev/null
@@ -0,0 +1,28 @@
+// $Id$
+
+//**************************************************************************
+//* This file is property of and copyright by the ALICE HLT Project        * 
+//* ALICE Experiment at CERN, All rights reserved.                         *
+//*                                                                        *
+//* Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no>        *
+//*                  for The ALICE HLT Project.                            *
+//*                                                                        *
+//* Permission to use, copy, modify and distribute this software and its   *
+//* documentation strictly for non-commercial purposes is hereby granted   *
+//* without fee, provided that the above copyright notice appears in all   *
+//* copies and that both the copyright notice and this permission notice   *
+//* appear in the supporting documentation. The authors make no claims     *
+//* about the suitability of this software for any purpose. It is          *
+//* provided "as is" without express or implied warranty.                  *
+//**************************************************************************
+
+/// @file   AliHLTErrorGuard.cxx
+/// @author Matthias Richter
+/// @date   01.07.2010
+/// @brief  Helper class for suppression of error floods.
+/// @note
+
+#include "AliHLTErrorGuard.h"
+
+/** ROOT macro for the implementation of ROOT specific class methods */
+ClassImp(AliHLTErrorGuard)
diff --git a/HLT/BASE/AliHLTErrorGuard.h b/HLT/BASE/AliHLTErrorGuard.h
new file mode 100644 (file)
index 0000000..ee42183
--- /dev/null
@@ -0,0 +1,105 @@
+//-*- Mode: C++ -*-
+// $Id$
+
+#ifndef ALIHLTERRORGUARD_H
+#define ALIHLTERRORGUARD_H
+//* This file is property of and copyright by the ALICE HLT Project        * 
+//* ALICE Experiment at CERN, All rights reserved.                         *
+//* See cxx source for full Copyright notice                               *
+
+/// @file   AliHLTErrorGuard.h
+/// @author Matthias Richter
+/// @date   01.07.2010
+/// @brief  Helper class for suppression of error floods.
+
+#include "AliHLTLogging.h"
+#include "TString.h"
+#include "Varargs.h"
+
+/**
+ * @class AliHLTErrorGuard
+ * Helper class for suppression of error message floods caused by error messages
+ * occurring rather frequently, e.g. for every event. The class suppresses the
+ * printout after an adjustable number of occurences and prints an error summary
+ * when the instance gets out of scope.
+ *
+ * Examples:
+ * <pre>
+ * if (nastyerror) {
+ *   ALIHLTERRORGUARD(5, "nasty error, first occurence in event %d", event);
+ * }
+ * </pre>
+ * <pre>
+ * if (nastyerror) {
+ *   static AliHLTErrorGuard g("classname", "functionname", "message");
+ *   g.Throw(5);
+ * }
+ * </pre>
+ * Both examples will throw the error for the first 5 occurrences. The macro
+ * ALIHLTERRORGUARD handles also class and function name, source file and line
+ * number, and supports variable messages through variadic macros.
+ *
+ * The second example illustrates usage of the class directly. The 'static'
+ * attribute causes the object not to be destroyed at run time, only when the
+ * program is terminated the object is deleted. This will print the error summary
+ * at the very end of the program execution.
+ *
+ * @ingroup alihlt_base
+ */
+class AliHLTErrorGuard : public AliHLTLogging {
+ public:
+  /// constructor
+ AliHLTErrorGuard(const char* classname, const char* functionname, const char* message, const char* file=NULL, int line=0)
+   : fClass(classname), fFunction(functionname), fFile(file?file:""), fMessage(message), fLine(line), fOccurrence(0) {}
+
+  /// set variable message
+  void SetMessage( int dummy, ... )
+  {
+    va_list args;
+    va_start(args, dummy);
+    fMessage=AliHLTLogging::BuildLogString(NULL, args );
+    va_end(args);
+  }
+
+  /// destructor
+  virtual ~AliHLTErrorGuard() {
+    Throw(-1, "Postponed message: %s - %d time(s)");
+  }
+
+  /// prefix increment operator
+  AliHLTErrorGuard& operator++() {fOccurrence++; return *this;}
+
+  int GetOccurrence() const {return fOccurrence;}
+
+  void Throw(int maxoccurrence=1, const char* format="%s") {
+    if (fOccurrence<=maxoccurrence || maxoccurrence<0) 
+      LoggingVarargs(kHLTLogError, fClass.Data(), fFunction.Data(), fFile.Data(), fLine, format, fMessage.Data(), GetOccurrence());
+  }
+
+ protected:
+
+ private:
+  /** standard constructor prohibited */
+  AliHLTErrorGuard();
+  /** copy constructor prohibited */
+  AliHLTErrorGuard(const AliHLTErrorGuard&);
+  /** assignment operator prohibited */
+  AliHLTErrorGuard& operator=(const AliHLTErrorGuard&);
+
+  TString fClass; //! transient
+  TString fFunction; //! transient
+  TString fFile; //! transient
+  TString fMessage; //! transient
+  int fLine; //!transient
+  int fOccurrence; //!transient
+
+  ClassDef(AliHLTErrorGuard, 0)
+};
+
+#define ALIHLTERRORGUARD( max, ... )  {                                                \
+    static AliHLTErrorGuard g(Class_Name() , FUNCTIONNAME() , "", __FILE__, __LINE__); \
+  if (g.GetOccurrence()==0) g.SetMessage( 0, __VA_ARGS__ );                            \
+  (++g).Throw(max);                                                                   \
+}
+
+#endif
index 1c0c00fd3fa032c72e2f7dd4790a876493bac099..cd42e9d6d284730fdbbd5c7a7c5e2ce72a258b20 100644 (file)
@@ -16,6 +16,7 @@
 #pragma link C++ class AliHLTDumpTask+;
 #pragma link C++ class AliHLTControlTask+;
 #pragma link C++ class AliHLTLogging+;
+#pragma link C++ class AliHLTErrorGuard+;
 #pragma link C++ class AliHLTDataBuffer+;
 #pragma link C++ class AliHLTDataBuffer::AliHLTRawBuffer+;
 #pragma link C++ class AliHLTDataBuffer::AliHLTRawPage+;
index 84fce534ae974ff6bf95c65b9b9415c2c672a9aa..198a9a52ca62e1cfd859751977ffa21fb4536b7d 100644 (file)
@@ -17,6 +17,7 @@ CLASS_HDRS:=          AliHLTComponent.h \
                AliHLTDumpTask.h \
                AliHLTControlTask.h \
                AliHLTLogging.h \
+               AliHLTErrorGuard.h \
                AliHLTDataBuffer.h \
                AliHLTConsumerDescriptor.h \
                AliHLTDataSource.h \