Universita' di GenovaINFN Sezione di Genova  
AIRWATCH / EUSO Genova

EsafMsgDispatcher - source file

// $Id: EsafMsgDispatcher.cc,v 1.18 2005/09/22 16:41:03 naumov Exp $
// Author M. Pallavicini created Sep, 24 2004

/*****************************************************************************
 * ESAF: Euso Simulation and Analysis Framework                              *
 *                                                                           *
 *  Id: EsafMsgDispatcher                                                    *
 *  Package: Base                                                            *
 *  Coordinator: Marco.Pallavicini                                           *
 *                                                                           *
 *****************************************************************************/

//______________________________________________________________________________
// 
//  ESAF Message Dispatcher
//  =======================
// 
//  ESAF Message Dispatcher
//  This is a single to really dispatch the messages according to the
//  configuration. 
//  The user can specify the default message level for screen and log file
//  Besides, the user can also change the message level for specific classes
//  that inherit from EsafMsgSource class
//  See EsafMsgSource.cc for further details
//

#include <iostream>
#include <stdexcept>
#include "EsafMsgDispatcher.hh"

ClassImp(EsafMsgDispatcher)

EsafMsgDispatcher* EsafMsgDispatcher::fMe = 0;

const char *const EsafMsgDispatcher::kEsc       = "\033[";
const char *const EsafMsgDispatcher::kReset     = "\033[0m";
const char *const EsafMsgDispatcher::kRed       = "\033[31m";
const char *const EsafMsgDispatcher::kGreen     = "\033[32m";
const char *const EsafMsgDispatcher::kYellow    = "\033[33m";
const char *const EsafMsgDispatcher::kBlue      = "\033[34m";
const char *const EsafMsgDispatcher::kUnderline = "\033[4m";
const char *const EsafMsgDispatcher::kBlink     = "\033[5m";
const char *const EsafMsgDispatcher::kBright    = "\033[1m";
const char *const EsafMsgDispatcher::kDark      = "\033[2m";

const char *     EsafMsgDispatcher::fgColors[] = { EsafMsgDispatcher::kReset,
                                                  EsafMsgDispatcher::kBlue,
                                                  EsafMsgDispatcher::kGreen,
                                                  EsafMsgDispatcher::kYellow,
                                                  EsafMsgDispatcher::kRed
                                                };

//______________________________________________________________________________
 EsafMsgDispatcher::EsafMsgDispatcher() : EsafConfigurable(),
    fHeaderLenght(40), fIsScreenFlushed(kFALSE), fIsLogFlushed(kFALSE),
    fUseColors(kTRUE),
    fDefaultLogSeverity(EsafMsg::Info), fDefaultScreenSeverity(EsafMsg::Info) {
    //
    // ctor
    //
}

//______________________________________________________________________________
 void EsafMsgDispatcher::LoadConfig() {
    //
    //
    //

    string screen = Conf()->GetStr("EsafMsgDispatcher.fScreenSeverity");
    string logfile = Conf()->GetStr("EsafMsgDispatcher.fLogFileSeverity");

    if ( screen == "Debug" )
        fDefaultScreenSeverity = EsafMsg::Debug;
    else if ( screen == "Info" )
        fDefaultScreenSeverity = EsafMsg::Info;
    else if ( screen == "Warning" )
        fDefaultScreenSeverity = EsafMsg::Warning;
    else if ( screen == "Panic" )
        fDefaultScreenSeverity = EsafMsg::Panic;
    else if ( screen == "None" )
        fDefaultScreenSeverity = EsafMsg::None;
    else
        throw runtime_error("Invalid severity type in EsafMsgDispatcher::EsafMsgDispatcher()");

    if ( logfile == "Debug" )
        fDefaultLogSeverity = EsafMsg::Debug;
    else if ( logfile == "Info" )
        fDefaultLogSeverity = EsafMsg::Info;
    else if ( logfile == "Warning" )
        fDefaultLogSeverity = EsafMsg::Warning;
    else if ( logfile == "Panic" )
        fDefaultLogSeverity = EsafMsg::Panic;
    else if ( logfile == "None" )
        fDefaultLogSeverity = EsafMsg::None;
    else 
        throw runtime_error("Invalid severity type in EsafMsgDispatcher::EsafMsgDispatcher()");


    fUseColors = Conf()->GetBool("EsafMsgDispatcher.fUseColors");
}


//______________________________________________________________________________
 void EsafMsgDispatcher::OpenLog( const char* name ) {
    //
    // Open a new log file
    //
    if (fLogFile.is_open()) fLogFile.close();

    string ext = ".log";
    
    if ( name )
        fLogFileName = name;
    else
        fLogFileName = Conf()->GetStr("EsafMsgDispatcher.fLogFileName");

    if (fLogFileName.rfind(ext) != (fLogFileName.size()-ext.size()))
        fLogFileName += ext;

    fLogFile.open( fLogFileName.c_str() );
    if (!fLogFile.is_open())
        throw runtime_error("Cannot open message output file "+fLogFileName);

}

//______________________________________________________________________________
 EsafMsgDispatcher::~EsafMsgDispatcher() {
    fLogFile.close();
}

//______________________________________________________________________________
 EsafMsgDispatcher* EsafMsgDispatcher::Get() {
    
    // singleton
    
    if (!fMe)
        fMe = new EsafMsgDispatcher();
    return fMe;
}

//______________________________________________________________________________
const char* MsgStringType[] = {"",
                               "Debug:  ",
                               "Info:   ",
                               "Warning:",
                               "Panic:  "
                              };
//______________________________________________________________________________
 void EsafMsgDispatcher::Dispatch(const EsafMsg& msg, Bool_t endline, Bool_t ProcCount) {
  
  // dispatch message
  // Message is written on screen and/or log file according to severity 
  // and parameter settings
  
  // check if message must be displayed on screen
  // default threshold is used, unless user has changed this specific sender (class name)
  bool doScreen = (msg.GetSeverity() >= fDefaultScreenSeverity);

  map<string,EsafMsg::MsgSeverity>::const_iterator it;
  if ( ( it = fUserMap.find( msg.GetSender() ) ) != fUserMap.end() ) {
    doScreen = ( msg.GetSeverity() >= it->second );
  }


  // prepare the header
  string header;
  
  header = string(MsgStringType[msg.GetSeverity()])+"["+msg.GetSender()+"]"; 

  if ( header.size() < (size_t)fHeaderLenght)
    header.resize(fHeaderLenght,' ');

  Int_t progress;

  if (ProcCount) {
    progress = msg.GetProgress();    // Current status of counting progress
    cout << "r";                    // Move cursor to the line begining
    fIsScreenFlushed = kFALSE;       // Always print info message
  }
    
  if ( doScreen ) {
    if ( !fIsScreenFlushed ) 
      if ( fUseColors ) {
	cout << fgColors[msg.GetSeverity()];
	cout << header;
	
	cout << EsafMsgDispatcher::kReset;
      } else {
	cout << header;
      }

    // Standart output

    cout << msg.GetText();

    // Printing status of progress message

    if (ProcCount) {
      if (progress != -1) cout << setw(4) << progress << "%";
      else cout << " ... ";
      if (progress >= 100) endline = kTRUE; // for the end of counting
    }

    cout << flush;

    if ( endline ) 
      cout << endl;
    
    fIsScreenFlushed = !endline;
  }
  
  if ( fLogFile.is_open()) {
    // check if message must be displayed on log file
    // default threshold is used, unless user has changed this specific sender (class name)
    bool doLog = ( msg.GetSeverity() >= fDefaultLogSeverity );
    if ( ( it = fUserMap.find( msg.GetSender() ) ) != fUserMap.end() ) {
      doLog = ( msg.GetSeverity() >= it->second );
    }
    
    if ( doLog ) {
      if ( !fIsLogFlushed )
	fLogFile << header;
      
      // Normaly write to file

      if (!ProcCount) fLogFile << msg.GetText() << flush;

      // In the begining of counting write all information

      else if (ProcCount && !progress)
	fLogFile << msg.GetText() << " " << progress << "%" << flush;

      // Writing only counting progress to file

      else if (ProcCount && progress > 0) 
	fLogFile << ".." << progress << "%" << flush;

      if ( endline ) 
	fLogFile << endl;
      
      fIsLogFlushed = !endline;
    }
  }

  if ( msg.GetSeverity() >= EsafMsg::Panic )
    exit(999);
}
About Us | EUSO Official Website | Web pages created by Roberto Pesce and Alessandro Thea - Last Update Wed Nov 16 16:57:39 2005 Wed Nov 16 16:29:22 2005