mana-core-gaudikernel / src / Lib / MsgStream.cpp

//====================================================================
//	MsgStream.cpp
//--------------------------------------------------------------------
//
//	Package    : System ( The LHCb Offline System)
//
//  Description: Class to ease error logging to the message service
//
//	Author     : M.Frank
//  History    :
// +---------+----------------------------------------------+---------
// |    Date |                 Comment                      | Who
// +---------+----------------------------------------------+---------
// | 29/10/98| Initial version                              | MF
// +---------+----------------------------------------------+---------
//
//====================================================================
#define KERNEL_MSGSTREAM_CPP


#include "GaudiKernel/MsgStream.h"
#include "GaudiKernel/Message.h"
#include "GaudiKernel/GaudiException.h"
#include "GaudiKernel/IMessageSvc.h"

#include <iostream>
#include <stdarg.h>
#include <stdio.h>

bool MsgStream::m_countInactive = false;

bool MsgStream::enableCountInactive(bool value) {
  bool old = m_countInactive;
  m_countInactive = value;
  return old;
}

bool MsgStream::countInactive() {
  return m_countInactive;
}


MsgStream::MsgStream(IMessageSvc* svc, int)
: m_service(svc),
  m_source(""),
  m_active(false),
  m_inactCounter(0)
{
  setLevel((0==svc) ? MSG::INFO : svc->outputLevel());
  m_useColors = (0==svc) ? false : svc->useColor();
#ifndef NDEBUG
  m_inactCounter = svc ? Gaudi::Cast<IInactiveMessageCounter>(svc) : 0;
#endif
}

MsgStream::MsgStream(IMessageSvc* svc, const std::string& source, int)
: m_service(svc),
  m_source(source),
  m_active(false),
  m_inactCounter(0)
{
  setLevel((0==svc) ? MSG::INFO : svc->outputLevel(source));
  m_useColors = (0==svc) ? false : svc->useColor();
#ifndef NDEBUG
  m_inactCounter = svc ? Gaudi::Cast<IInactiveMessageCounter>(svc) : 0;
#endif
}

MsgStream::~MsgStream()    {
}

MsgStream& MsgStream::doOutput()       {
  try {
    // This piece of code may throw and we cannot afford it when we print a message
    // in the middle of a catch block.
    if ( isActive() )   {
      Message msg(m_source,m_currLevel,m_stream.str());
      if ( m_service != 0 )   {
        m_service->reportMessage (msg, m_currLevel);
      }
      else     {
        std::cout << msg << std::endl;
      }
    }
    m_stream.str("");
  } catch(...) {}
  return *this;
}

void MsgStream::setColor(MSG::Color col) {
#ifndef _WIN32
  if ( m_useColors)   {
    int fc = 90 + col;
    try { // this may throw and we must not do it
      m_stream << "[" << fc << ";1m";
    }
    catch(...) {}
  }
#endif
}

void MsgStream::setColor(MSG::Color fg, MSG::Color bg) {
#ifndef _WIN32
  if ( m_useColors )   {
    try { // this may throw and we must not do it
      int fc = 90 + fg;
      m_stream << "[" << fc;
      int bc = 100 + bg;
      m_stream << ";" << bc;
      m_stream << ";1m";
    }
    catch(...) {}
  }
#endif
}

void MsgStream::resetColor() {
#ifndef _WIN32
  if ( m_useColors )   {
    try { // this may throw and we must not do it
      m_stream << "" << m_service->getLogColor(m_currLevel);
    }
    catch(...) {}
  }
#endif
}

std::string format( const char* fmt, ... )
{
  const int buffsize = 2048;
  static char buffer[buffsize];
  va_list arguments;
  va_start( arguments, fmt );
  if( vsprintf(buffer, fmt, arguments) >= buffsize )
    throw GaudiException("Insufficient buffer size (2048) when formatting message",
                         "MsgStream", 0);
  return std::string(buffer);
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.