| # Gaudi Framework, version v21r7p1 |

Home |  | Generated: 15 Feb 2010

# MsgStream.cpp

Go to the documentation of this file.

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