![]() |
|
|
Generated: 18 Jul 2008 |
00001 // $Header: /local/reps/Gaudi/GaudiKernel/src/Lib/MsgStream.cpp,v 1.12 2007/04/04 08:22:27 hmd 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 if ( isActive() ) { 00054 Message msg(m_source,m_currLevel,m_stream.str()); 00055 if ( m_service != 0 ) { 00056 MSG::Level lvl = MSG::Level(m_service->outputLevel(m_source)); 00057 m_service->setOutputLevel(m_source, m_currLevel); 00058 m_service->reportMessage (msg); 00059 m_service->setOutputLevel(m_source, lvl); 00060 } 00061 else { 00062 std::cout << msg << std::endl; 00063 } 00064 } 00065 00066 m_stream.str(""); 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 m_stream << "[" << fc << ";1m"; 00075 } 00076 #endif 00077 } 00078 00079 void MsgStream::setColor(MSG::Color fg, MSG::Color bg) { 00080 #ifndef _WIN32 00081 if ( m_useColors ) { 00082 int fc = 90 + fg; 00083 m_stream << "[" << fc; 00084 int bc = 100 + bg; 00085 m_stream << ";" << bc; 00086 m_stream << ";1m"; 00087 } 00088 #endif 00089 } 00090 00091 void MsgStream::resetColor() { 00092 #ifndef _WIN32 00093 if ( m_useColors ) { 00094 m_stream << "[m" << m_service->getLogColor(m_currLevel); 00095 } 00096 #endif 00097 } 00098 00099 std::string format( const char* fmt, ... ) 00100 { 00101 const int buffsize = 2048; 00102 static char buffer[buffsize]; 00103 va_list arguments; 00104 va_start( arguments, fmt ); 00105 if( vsprintf(buffer, fmt, arguments) >= buffsize ) 00106 throw GaudiException("Insufficient buffer size (2048) when formatting message", 00107 "MsgStream", 0); 00108 return std::string(buffer); 00109 }