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 *
10 /// @file AliHLTErrorGuard.h
11 /// @author Matthias Richter
13 /// @brief Helper class for suppression of error floods.
15 #include "AliHLTLogging.h"
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.
29 * ALIHLTERRORGUARD(5, "nasty error, first occurence in event %d", event);
34 * static AliHLTErrorGuard g("classname", "functionname", "message");
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.
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.
47 * @ingroup alihlt_base
49 class AliHLTErrorGuard : public AliHLTLogging {
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) {}
55 /// set variable message
56 void SetMessage( int dummy, ... )
59 va_start(args, dummy);
60 fMessage=AliHLTLogging::BuildLogString(NULL, args );
65 virtual ~AliHLTErrorGuard() {
66 Throw(-1, "Postponed message: %s - %d time(s)");
69 /// prefix increment operator
70 AliHLTErrorGuard& operator++() {fOccurrence++; return *this;}
72 int GetOccurrence() const {return fOccurrence;}
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());
82 /** standard constructor prohibited */
84 /** copy constructor prohibited */
85 AliHLTErrorGuard(const AliHLTErrorGuard&);
86 /** assignment operator prohibited */
87 AliHLTErrorGuard& operator=(const AliHLTErrorGuard&);
89 TString fClass; //! transient
90 TString fFunction; //! transient
91 TString fFile; //! transient
92 TString fMessage; //! transient
93 int fLine; //!transient
94 int fOccurrence; //!transient
96 ClassDef(AliHLTErrorGuard, 0)
99 #define ALIHLTERRORGUARD( max, ... ) { \
100 static AliHLTErrorGuard g(Class_Name() , FUNCTIONNAME() , "", __FILE__, __LINE__); \
101 if (g.GetOccurrence()==0) g.SetMessage( 0, __VA_ARGS__ ); \