ROOT logo
//-*- 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
 AliHLTErrorGuard.h:1
 AliHLTErrorGuard.h:2
 AliHLTErrorGuard.h:3
 AliHLTErrorGuard.h:4
 AliHLTErrorGuard.h:5
 AliHLTErrorGuard.h:6
 AliHLTErrorGuard.h:7
 AliHLTErrorGuard.h:8
 AliHLTErrorGuard.h:9
 AliHLTErrorGuard.h:10
 AliHLTErrorGuard.h:11
 AliHLTErrorGuard.h:12
 AliHLTErrorGuard.h:13
 AliHLTErrorGuard.h:14
 AliHLTErrorGuard.h:15
 AliHLTErrorGuard.h:16
 AliHLTErrorGuard.h:17
 AliHLTErrorGuard.h:18
 AliHLTErrorGuard.h:19
 AliHLTErrorGuard.h:20
 AliHLTErrorGuard.h:21
 AliHLTErrorGuard.h:22
 AliHLTErrorGuard.h:23
 AliHLTErrorGuard.h:24
 AliHLTErrorGuard.h:25
 AliHLTErrorGuard.h:26
 AliHLTErrorGuard.h:27
 AliHLTErrorGuard.h:28
 AliHLTErrorGuard.h:29
 AliHLTErrorGuard.h:30
 AliHLTErrorGuard.h:31
 AliHLTErrorGuard.h:32
 AliHLTErrorGuard.h:33
 AliHLTErrorGuard.h:34
 AliHLTErrorGuard.h:35
 AliHLTErrorGuard.h:36
 AliHLTErrorGuard.h:37
 AliHLTErrorGuard.h:38
 AliHLTErrorGuard.h:39
 AliHLTErrorGuard.h:40
 AliHLTErrorGuard.h:41
 AliHLTErrorGuard.h:42
 AliHLTErrorGuard.h:43
 AliHLTErrorGuard.h:44
 AliHLTErrorGuard.h:45
 AliHLTErrorGuard.h:46
 AliHLTErrorGuard.h:47
 AliHLTErrorGuard.h:48
 AliHLTErrorGuard.h:49
 AliHLTErrorGuard.h:50
 AliHLTErrorGuard.h:51
 AliHLTErrorGuard.h:52
 AliHLTErrorGuard.h:53
 AliHLTErrorGuard.h:54
 AliHLTErrorGuard.h:55
 AliHLTErrorGuard.h:56
 AliHLTErrorGuard.h:57
 AliHLTErrorGuard.h:58
 AliHLTErrorGuard.h:59
 AliHLTErrorGuard.h:60
 AliHLTErrorGuard.h:61
 AliHLTErrorGuard.h:62
 AliHLTErrorGuard.h:63
 AliHLTErrorGuard.h:64
 AliHLTErrorGuard.h:65
 AliHLTErrorGuard.h:66
 AliHLTErrorGuard.h:67
 AliHLTErrorGuard.h:68
 AliHLTErrorGuard.h:69
 AliHLTErrorGuard.h:70
 AliHLTErrorGuard.h:71
 AliHLTErrorGuard.h:72
 AliHLTErrorGuard.h:73
 AliHLTErrorGuard.h:74
 AliHLTErrorGuard.h:75
 AliHLTErrorGuard.h:76
 AliHLTErrorGuard.h:77
 AliHLTErrorGuard.h:78
 AliHLTErrorGuard.h:79
 AliHLTErrorGuard.h:80
 AliHLTErrorGuard.h:81
 AliHLTErrorGuard.h:82
 AliHLTErrorGuard.h:83
 AliHLTErrorGuard.h:84
 AliHLTErrorGuard.h:85
 AliHLTErrorGuard.h:86
 AliHLTErrorGuard.h:87
 AliHLTErrorGuard.h:88
 AliHLTErrorGuard.h:89
 AliHLTErrorGuard.h:90
 AliHLTErrorGuard.h:91
 AliHLTErrorGuard.h:92
 AliHLTErrorGuard.h:93
 AliHLTErrorGuard.h:94
 AliHLTErrorGuard.h:95
 AliHLTErrorGuard.h:96
 AliHLTErrorGuard.h:97
 AliHLTErrorGuard.h:98
 AliHLTErrorGuard.h:99
 AliHLTErrorGuard.h:100
 AliHLTErrorGuard.h:101
 AliHLTErrorGuard.h:102
 AliHLTErrorGuard.h:103
 AliHLTErrorGuard.h:104
 AliHLTErrorGuard.h:105