]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/AliHLTErrorGuard.h
5db098786a2f182b415f6a922a13716bbfa2d73d
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTErrorGuard.h
1 //-*- Mode: C++ -*-
2 // $Id$
3
4 #ifndef ALIHLTERRORGUARD_H
5 #define ALIHLTERRORGUARD_H
6 //* This file is property of and copyright by the ALICE HLT Project        * 
7 //* ALICE Experiment at CERN, All rights reserved.                         *
8 //* See cxx source for full Copyright notice                               *
9
10 /// @file   AliHLTErrorGuard.h
11 /// @author Matthias Richter
12 /// @date   01.07.2010
13 /// @brief  Helper class for suppression of error floods.
14
15 #include "AliHLTLogging.h"
16 #include "TString.h"
17 #include "Varargs.h"
18
19 /**
20  * @class AliHLTErrorGuard
21  * Helper class for suppression of error message floods caused by error messages
22  * occurring rather frequently, e.g. for every event. The class suppresses the
23  * printout after an adjustable number of occurences and prints an error summary
24  * when the instance gets out of scope.
25  *
26  * Examples:
27  * <pre>
28  * if (nastyerror) {
29  *   ALIHLTERRORGUARD(5, "nasty error, first occurence in event %d", event);
30  * }
31  * </pre>
32  * <pre>
33  * if (nastyerror) {
34  *   static AliHLTErrorGuard g("classname", "functionname", "message");
35  *   (++g).Throw(5);
36  * }
37  * </pre>
38  * Both examples will throw the error for the first 5 occurrences. The macro
39  * ALIHLTERRORGUARD handles also class and function name, source file and line
40  * number, and supports variable messages through variadic macros.
41  *
42  * The second example illustrates usage of the class directly. The 'static'
43  * attribute causes the object not to be destroyed at run time, only when the
44  * program is terminated the object is deleted. This will print the error summary
45  * at the very end of the program execution.
46  *
47  * @ingroup alihlt_base
48  */
49 class AliHLTErrorGuard : public AliHLTLogging {
50  public:
51   /// constructor
52  AliHLTErrorGuard(const char* classname, const char* functionname, const char* message, const char* file=NULL, int line=0)
53    : fClass(classname), fFunction(functionname), fFile(file?file:""), fMessage(message), fLine(line), fOccurrence(0) {}
54
55   /// set variable message
56   void SetMessage( int dummy, ... )
57   {
58     va_list args;
59     va_start(args, dummy);
60     fMessage=AliHLTLogging::BuildLogString(NULL, args );
61     va_end(args);
62   }
63
64   /// destructor
65   virtual ~AliHLTErrorGuard() {
66     Throw(-1, "Postponed message: %s - %d time(s)");
67   }
68
69   /// prefix increment operator
70   AliHLTErrorGuard& operator++() {fOccurrence++; return *this;}
71
72   int GetOccurrence() const {return fOccurrence;}
73
74   void Throw(int maxoccurrence=1, const char* format="%s") {
75     if (fOccurrence<=maxoccurrence || maxoccurrence<0) 
76       LoggingVarargs(kHLTLogError, fClass.Data(), fFunction.Data(), fFile.Data(), fLine, format, fMessage.Data(), GetOccurrence());
77   }
78
79  protected:
80
81  private:
82   /** standard constructor prohibited */
83   AliHLTErrorGuard();
84   /** copy constructor prohibited */
85   AliHLTErrorGuard(const AliHLTErrorGuard&);
86   /** assignment operator prohibited */
87   AliHLTErrorGuard& operator=(const AliHLTErrorGuard&);
88
89   TString fClass; //! transient
90   TString fFunction; //! transient
91   TString fFile; //! transient
92   TString fMessage; //! transient
93   int fLine; //!transient
94   int fOccurrence; //!transient
95
96   ClassDef(AliHLTErrorGuard, 0)
97 };
98
99 #define ALIHLTERRORGUARD( max, ... )  {                                                \
100     static AliHLTErrorGuard g(Class_Name() , FUNCTIONNAME() , "", __FILE__, __LINE__); \
101   if (g.GetOccurrence()==0) g.SetMessage( 0, __VA_ARGS__ );                            \
102   (++g).Throw(max);                                                                    \
103 }
104
105 #endif