MsgStream.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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 m_service->reportMessage (msg, m_currLevel);
00057 }
00058 else {
00059 std::cout << msg << std::endl;
00060 }
00061 }
00062
00063 m_stream.str("");
00064 return *this;
00065 }
00066
00067 void MsgStream::setColor(MSG::Color col) {
00068 #ifndef _WIN32
00069 if ( m_useColors) {
00070 int fc = 90 + col;
00071 m_stream << "[" << fc << ";1m";
00072 }
00073 #endif
00074 }
00075
00076 void MsgStream::setColor(MSG::Color fg, MSG::Color bg) {
00077 #ifndef _WIN32
00078 if ( m_useColors ) {
00079 int fc = 90 + fg;
00080 m_stream << "[" << fc;
00081 int bc = 100 + bg;
00082 m_stream << ";" << bc;
00083 m_stream << ";1m";
00084 }
00085 #endif
00086 }
00087
00088 void MsgStream::resetColor() {
00089 #ifndef _WIN32
00090 if ( m_useColors ) {
00091 m_stream << "[m" << m_service->getLogColor(m_currLevel);
00092 }
00093 #endif
00094 }
00095
00096 std::string format( const char* fmt, ... )
00097 {
00098 const int buffsize = 2048;
00099 static char buffer[buffsize];
00100 va_list arguments;
00101 va_start( arguments, fmt );
00102 if( vsprintf(buffer, fmt, arguments) >= buffsize )
00103 throw GaudiException("Insufficient buffer size (2048) when formatting message",
00104 "MsgStream", 0);
00105 return std::string(buffer);
00106 }