#ifndef HMESSAGEMGR__H
#define HMESSAGEMGR__H
#include "TObject.h"
#include "TString.h"
#include "TSocket.h"
#include "TError.h"
#include "hades.h"
using namespace std;
#include <fstream>
#include <iostream>
#define LEVELMAX 10
#if HDL > 0
 
#define DEBUG_msg(level, det, text) \
  ((HMessageMgr *)gHades->getMsg())->setL(__LINE__); \
  ((HMessageMgr *)gHades->getMsg())->setF(__FILE__); \
  ((HMessageMgr *)gHades->getMsg())->debug(level,det, this->GetName(), text);
#else
#define DEBUG_msg(level, det, text) \
  ((HMessageMgr *)gHades->getMsg())->setL(0); \
  ((HMessageMgr *)gHades->getMsg())->setF(""); \
  ((HMessageMgr *)gHades->getMsg())->debug(level,det, this->GetName(), text);
#endif // HDL > 0
#define ERROR_msg(det, text) \
  if(((HMessageMgr *)gHades->getMsg()) != NULL) \
  ((HMessageMgr *)gHades->getMsg())->error(1, det, this->GetName(), text); \
  else cerr << text << endl;
#define WARNING_msg(level, det, text) \
  if(((HMessageMgr *)gHades->getMsg()) != NULL) \
  ((HMessageMgr *)gHades->getMsg())->warning(level,det, this->GetName(), text); \
  else cout << text << endl;
#define INFO_msg(level, det, text) \
  if(((HMessageMgr *)gHades->getMsg()) != NULL) \
  ((HMessageMgr *)gHades->getMsg())->info((level),(det), (this->GetName()), (text)); \
  else cout << text << endl;
#define SEPERATOR_msg(sep, num) \
  if(((HMessageMgr *)gHades->getMsg()) != NULL) \
  ((HMessageMgr *)gHades->getMsg())->messageSeperator(sep,num); \
  else for(Int_t i=0;i<num;i++) \
    cout << sep; \
    cout << endl;
class HMessageMgr : public TNamed
{
 public:
  HMessageMgr(const Char_t *name, const Char_t *title);
  ~HMessageMgr(void);
  Bool_t setDefault(void);
  Bool_t setFile(const Char_t *dir);
  Bool_t setPort(const Char_t *host, Int_t port);
  Bool_t switchOff(void);
  void setDebugLevel(Char_t level);
  void setInfoLevel(Char_t level);
  void setWarningLevel(Char_t level);
  void enableDetectorMsg(Int_t det);
  void messageSeperator(const Char_t *seperator, Int_t num);
  static void hydraErrorHandler(Int_t level, Bool_t abort, const Char_t *location, const Char_t *msg);
	
  void debug(Char_t level, Int_t det, const Char_t *className, const Char_t *text);
  void error(Char_t level, Int_t det, const Char_t *className, const Char_t *text);
  void warning(Char_t level, Int_t det, const Char_t *className, const Char_t *text);
  void info(Char_t level, Int_t det, const Char_t *className, const Char_t *text);
  
  void debug(Int_t level, Int_t det, const Char_t *className, const Char_t *text, ...);
  void error(Int_t level, Int_t det, const Char_t *className, const Char_t *text, ...);
  void warning(Int_t level, Int_t det, const Char_t *className, const Char_t *text, ...);
  void info(Int_t level, Int_t det, const Char_t *className, const Char_t *text, ...);
  
	
  void debugB(Char_t level, Int_t det, const Char_t *text, ...);
  void errorB(Char_t level, Int_t det, const Char_t *text, ...);
  void warningB(Char_t level, Int_t det, const Char_t *text, ...);
  void infoB(Char_t level, Int_t det, const Char_t *text, ...);
  
  void debug(Int_t level, Int_t det, const Char_t *className, const Char_t *file, Int_t line, const Char_t *text, ...);
  void setF(const Char_t *name);
  void setL(Int_t num);
	
  void setScreenOutput(Bool_t f = kTRUE){screen = f;};
	enum Detector {
			DET_RICH       = 0x001,
			DET_START      = 0x002,
			DET_MDC        = 0x004,
			DET_TOF        = 0x008,
			DET_TOFINO     = 0x010,
			DET_SHOWER     = 0x020,
			DET_TRIGGER    = 0x040,
			DET_RUNTIMEDB  = 0x080,
			DET_KICKPLANE  = 0x100,
			DET_QA         = 0x200,
			DET_ALL        = 0x3ff
	};
 protected:
  void write(const Char_t *text);
  void ewrite(const Char_t *text);
 private:
  Char_t debugLevel;       
  Char_t warningLevel;     
  Char_t infoLevel;        
  ofstream *msgFile;       
  ofstream *errorFile;     
  TSocket *msgSocket;      
  TSocket *errorSocket;    
  TString *thisHost;       
  Int_t detectorMsg;       
  Char_t *msg1;            
	Char_t *msg2;            
  Char_t *file;            
  Int_t line;              
  Bool_t screen;           
  ClassDef(HMessageMgr,0)  
};
#endif