| # Gaudi Framework, version v23r1 |

Home |  | Generated: Wed Feb 29 2012

# MsgStream.cpp

Go to the documentation of this file.

    00001 //====================================================================
    00002 //      MsgStream.cpp
    00003 //--------------------------------------------------------------------
    00004 //
    00005 //      Package    : System ( The LHCb Offline System)
    00006 //
    00007 //  Description: Class to ease error logging to the message service
    00008 //
    00009 //      Author     : M.Frank
    00010 //  History    :
    00011 // +---------+----------------------------------------------+---------
    00012 // |    Date |                 Comment                      | Who
    00013 // +---------+----------------------------------------------+---------
    00014 // | 29/10/98| Initial version                              | MF
    00015 // +---------+----------------------------------------------+---------
    00016 //
    00017 //====================================================================
    00018 #define KERNEL_MSGSTREAM_CPP
    00019
    00020
    00021 #include "GaudiKernel/MsgStream.h"
    00022 #include "GaudiKernel/Message.h"
    00023 #include "GaudiKernel/GaudiException.h"
    00024 #include "GaudiKernel/IMessageSvc.h"
    00025
    00026 #include <iostream>
    00027 #include <stdarg.h>
    00028 #include <stdio.h>
    00029
    00030 bool MsgStream::m_countInactive = false;
    00031
    00032 bool MsgStream::enableCountInactive(bool value) {
    00033   bool old = m_countInactive;
    00034   m_countInactive = value;
    00035   return old;
    00036 }
    00037
    00038 bool MsgStream::countInactive() {
    00039   return m_countInactive;
    00040 }
    00041
    00042
    00043 MsgStream::MsgStream(IMessageSvc* svc, int)
    00044 : m_service(svc),
    00045   m_source(""),
    00046   m_active(false),
    00047   m_inactCounter(0)
    00048 {
    00049   setLevel((0==svc) ? MSG::INFO : svc->outputLevel());
    00050   m_useColors = (0==svc) ? false : svc->useColor();
    00051 #ifndef NDEBUG
    00052   m_inactCounter = svc ? Gaudi::Cast<IInactiveMessageCounter>(svc) : 0;
    00053 #endif
    00054 }
    00055
    00056 MsgStream::MsgStream(IMessageSvc* svc, const std::string& source, int)
    00057 : m_service(svc),
    00058   m_source(source),
    00059   m_active(false),
    00060   m_inactCounter(0)
    00061 {
    00062   setLevel((0==svc) ? MSG::INFO : svc->outputLevel(source));
    00063   m_useColors = (0==svc) ? false : svc->useColor();
    00064 #ifndef NDEBUG
    00065   m_inactCounter = svc ? Gaudi::Cast<IInactiveMessageCounter>(svc) : 0;
    00066 #endif
    00067 }
    00068
    00069 MsgStream::~MsgStream()    {
    00070 }
    00071
    00072 MsgStream& MsgStream::doOutput()       {
    00073   try {
    00074     // This piece of code may throw and we cannot afford it when we print a message
    00075     // in the middle of a catch block.
    00076     if ( isActive() )   {
    00077       Message msg(m_source,m_currLevel,m_stream.str());
    00078       if ( m_service != 0 )   {
    00079         m_service->reportMessage (msg, m_currLevel);
    00080       }
    00081       else     {
    00082         std::cout << msg << std::endl;
    00083       }
    00084     }
    00085     m_stream.str("");
    00086   } catch(...) {}
    00087   return *this;
    00088 }
    00089
    00090 void MsgStream::setColor(MSG::Color col) {
    00091 #ifndef _WIN32
    00092   if ( m_useColors)   {
    00093     int fc = 90 + col;
    00094     try { // this may throw and we must not do it
    00095       m_stream << "[" << fc << ";1m";
    00096     }
    00097     catch(...) {}
    00098   }
    00099 #endif
    00100 }
    00101
    00102 void MsgStream::setColor(MSG::Color fg, MSG::Color bg) {
    00103 #ifndef _WIN32
    00104   if ( m_useColors )   {
    00105     try { // this may throw and we must not do it
    00106       int fc = 90 + fg;
    00107       m_stream << "[" << fc;
    00108       int bc = 100 + bg;
    00109       m_stream << ";" << bc;
    00110       m_stream << ";1m";
    00111     }
    00112     catch(...) {}
    00113   }
    00114 #endif
    00115 }
    00116
    00117 void MsgStream::resetColor() {
    00118 #ifndef _WIN32
    00119   if ( m_useColors )   {
    00120     try { // this may throw and we must not do it
    00121       m_stream << "[m" << m_service->getLogColor(m_currLevel);
    00122     }
    00123     catch(...) {}
    00124   }
    00125 #endif
    00126 }
    00127
    00128 #ifdef WIN32
    00129 // Disable warning
    00130 //   C4996: 'vsprintf': This function or variable may be unsafe.
    00131 #pragma warning(disable:4996)
    00132 #endif
    00133 std::string format( const char* fmt, ... )
    00134 {
    00135   const int buffsize = 2048;
    00136   static char buffer[buffsize];
    00137   va_list arguments;
    00138   va_start( arguments, fmt );
    00139   if( vsprintf(buffer, fmt, arguments) >= buffsize )
    00140     throw GaudiException("Insufficient buffer size (2048) when formatting message",
    00141                          "MsgStream", 0);
    00142   return std::string(buffer);
    00143 }

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines