added ColorConsoleChannel and WindowsColorConsoleChannel classes

This commit is contained in:
Guenter Obiltschnig 2013-08-26 16:27:24 +02:00
parent fd6433eb4e
commit 086721bfe9
5 changed files with 704 additions and 7 deletions

View File

@ -1,7 +1,7 @@
//
// ConsoleChannel.h
//
// $Id: //poco/1.4/Foundation/include/Poco/ConsoleChannel.h#1 $
// $Id: //poco/1.4/Foundation/include/Poco/ConsoleChannel.h#2 $
//
// Library: Foundation
// Package: Logging
@ -66,10 +66,10 @@ class Foundation_API ConsoleChannel: public Channel
{
public:
ConsoleChannel();
/// Creates the channel and attached std::clog.
/// Creates the channel and attaches std::clog.
ConsoleChannel(std::ostream& str);
/// Creates the channel.
/// Creates the channel using the given stream.
void log(const Message& msg);
/// Logs the given message to the channel's stream.
@ -83,6 +83,126 @@ private:
};
class Foundation_API ColorConsoleChannel: public Channel
/// A channel that writes to an ostream.
///
/// Only the message's text is written, followed
/// by a newline.
///
/// Messages can be colored depending on priority.
/// The console device must support ANSI escape codes
/// in order to display colored messages.
///
/// To enable message coloring, set the "enableColors"
/// property to true (default). Furthermore, colors can be
/// configured by setting the following properties
/// (default values are given in parenthesis):
///
/// * traceColor (gray)
/// * debugColor (gray)
/// * informationColor (default)
/// * noticeColor (default)
/// * warningColor (yellow)
/// * errorColor (lightRed)
/// * criticalColor (lightRed)
/// * fatalColor (lightRed)
///
/// The following color values are supported:
///
/// * default
/// * black
/// * red
/// * green
/// * brown
/// * blue
/// * magenta
/// * cyan
/// * gray
/// * darkgray
/// * lightRed
/// * lightGreen
/// * yellow
/// * lightBlue
/// * lightMagenta
/// * lightCyan
/// * white
///
/// Chain this channel to a FormattingChannel with an
/// appropriate Formatter to control what is contained
/// in the text.
///
/// Similar to StreamChannel, except that a static
/// mutex is used to protect against multiple
/// console channels concurrently writing to the
/// same stream.
{
public:
ColorConsoleChannel();
/// Creates the channel and attaches std::clog.
ColorConsoleChannel(std::ostream& str);
/// Creates the channel using the given stream.
void log(const Message& msg);
/// Logs the given message to the channel's stream.
void setProperty(const std::string& name, const std::string& value);
/// Sets the property with the given name.
///
/// The following properties are supported:
/// * enableColors: Enable or disable colors.
/// * traceColor: Specify color for trace messages.
/// * debugColor: Specify color for debug messages.
/// * informationColor: Specify color for information messages.
/// * noticeColor: Specify color for notice messages.
/// * warningColor: Specify color for warning messages.
/// * errorColor: Specify color for error messages.
/// * criticalColor: Specify color for critical messages.
/// * fatalColor: Specify color for fatal messages.
///
/// See the class documentation for a list of supported color values.
std::string getProperty(const std::string& name) const;
/// Returns the value of the property with the given name.
/// See setProperty() for a description of the supported
/// properties.
protected:
enum Color
{
CC_DEFAULT = 0x0027,
CC_BLACK = 0x001e,
CC_RED = 0x001f,
CC_GREEN = 0x0020,
CC_BROWN = 0x0021,
CC_BLUE = 0x0022,
CC_MAGENTA = 0x0023,
CC_CYAN = 0x0024,
CC_GRAY = 0x0025,
CC_DARKGRAY = 0x011e,
CC_LIGHTRED = 0x011f,
CC_LIGHTGREEN = 0x0120,
CC_YELLOW = 0x0121,
CC_LIGHTBLUE = 0x0122,
CC_LIGHTMAGENTA = 0x0123,
CC_LIGHTCYAN = 0x0124,
CC_WHITE = 0x0125
};
~ColorConsoleChannel();
Color parseColor(const std::string& color) const;
std::string formatColor(Color color) const;
void initColors();
private:
std::ostream& _str;
bool _enableColors;
Color _colors[9];
static FastMutex _mutex;
static const std::string CSI;
};
} // namespace Poco

View File

@ -1,7 +1,7 @@
//
// WindowsConsoleChannel.h
//
// $Id: //poco/1.4/Foundation/include/Poco/WindowsConsoleChannel.h#1 $
// $Id: //poco/1.4/Foundation/include/Poco/WindowsConsoleChannel.h#2 $
//
// Library: Foundation
// Package: Logging
@ -83,6 +83,122 @@ private:
};
class Foundation_API WindowsColorConsoleChannel: public Channel
/// A channel that writes to the Windows console.
///
/// Only the message's text is written, followed
/// by a newline.
///
/// If POCO has been compiled with POCO_WIN32_UTF8,
/// log messages are assumed to be UTF-8 encoded, and
/// are converted to UTF-16 prior to writing them to the
/// console. This is the main difference to the ConsoleChannel
/// class, which cannot handle UTF-8 encoded messages on Windows.
///
/// Messages can be colored depending on priority.
///
/// To enable message coloring, set the "enableColors"
/// property to true (default). Furthermore, colors can be
/// configured by setting the following properties
/// (default values are given in parenthesis):
///
/// * traceColor (gray)
/// * debugColor (gray)
/// * informationColor (default)
/// * noticeColor (default)
/// * warningColor (yellow)
/// * errorColor (lightRed)
/// * criticalColor (lightRed)
/// * fatalColor (lightRed)
///
/// The following color values are supported:
///
/// * default
/// * black
/// * red
/// * green
/// * brown
/// * blue
/// * magenta
/// * cyan
/// * gray
/// * darkgray
/// * lightRed
/// * lightGreen
/// * yellow
/// * lightBlue
/// * lightMagenta
/// * lightCyan
/// * white
///
/// Chain this channel to a FormattingChannel with an
/// appropriate Formatter to control what is contained
/// in the text.
///
/// Only available on Windows platforms.
{
public:
WindowsColorConsoleChannel();
/// Creates the WindowsConsoleChannel.
void log(const Message& msg);
/// Logs the given message to the channel's stream.
void setProperty(const std::string& name, const std::string& value);
/// Sets the property with the given name.
///
/// The following properties are supported:
/// * enableColors: Enable or disable colors.
/// * traceColor: Specify color for trace messages.
/// * debugColor: Specify color for debug messages.
/// * informationColor: Specify color for information messages.
/// * noticeColor: Specify color for notice messages.
/// * warningColor: Specify color for warning messages.
/// * errorColor: Specify color for error messages.
/// * criticalColor: Specify color for critical messages.
/// * fatalColor: Specify color for fatal messages.
///
/// See the class documentation for a list of supported color values.
std::string getProperty(const std::string& name) const;
/// Returns the value of the property with the given name.
/// See setProperty() for a description of the supported
/// properties.
protected:
enum Color
{
CC_BLACK = 0x0000,
CC_RED = 0x0004,
CC_GREEN = 0x0002,
CC_BROWN = 0x0006,
CC_BLUE = 0x0001,
CC_MAGENTA = 0x0005,
CC_CYAN = 0x0003,
CC_GRAY = 0x0007,
CC_DARKGRAY = 0x0008,
CC_LIGHTRED = 0x000C,
CC_LIGHTGREEN = 0x000A,
CC_YELLOW = 0x000E,
CC_LIGHTBLUE = 0x0009,
CC_LIGHTMAGENTA = 0x000D,
CC_LIGHTCYAN = 0x000B,
CC_WHITE = 0x000F
};
~WindowsColorConsoleChannel();
WORD parseColor(const std::string& color) const;
std::string formatColor(WORD color) const;
void initColors();
private:
bool _enableColors;
HANDLE _hConsole;
bool _isFile;
WORD _colors[9];
};
} // namespace Poco

View File

@ -1,7 +1,7 @@
//
// ConsoleChannel.cpp
//
// $Id: //poco/1.4/Foundation/src/ConsoleChannel.cpp#1 $
// $Id: //poco/1.4/Foundation/src/ConsoleChannel.cpp#2 $
//
// Library: Foundation
// Package: Logging
@ -36,6 +36,8 @@
#include "Poco/ConsoleChannel.h"
#include "Poco/Message.h"
#include "Poco/String.h"
#include "Poco/Exception.h"
#include <iostream>
@ -68,4 +70,225 @@ void ConsoleChannel::log(const Message& msg)
}
FastMutex ColorConsoleChannel::_mutex;
const std::string ColorConsoleChannel::CSI("\033[");
ColorConsoleChannel::ColorConsoleChannel():
_str(std::clog),
_enableColors(true)
{
initColors();
}
ColorConsoleChannel::ColorConsoleChannel(std::ostream& str):
_str(str),
_enableColors(true)
{
initColors();
}
ColorConsoleChannel::~ColorConsoleChannel()
{
}
void ColorConsoleChannel::log(const Message& msg)
{
FastMutex::ScopedLock lock(_mutex);
if (_enableColors)
{
int color = _colors[msg.getPriority()];
if (color & 0x100)
{
_str << CSI << "1m";
}
color &= 0xff;
_str << CSI << color << "m";
}
_str << msg.getText();
if (_enableColors)
{
_str << CSI << "0m";
}
_str << std::endl;
}
void ColorConsoleChannel::setProperty(const std::string& name, const std::string& value)
{
if (name == "enableColors")
{
_enableColors = icompare(value, "true") == 0;
}
else if (name == "traceColor")
{
_colors[Message::PRIO_TRACE] = parseColor(value);
}
else if (name == "debugColor")
{
_colors[Message::PRIO_DEBUG] = parseColor(value);
}
else if (name == "informationColor")
{
_colors[Message::PRIO_INFORMATION] = parseColor(value);
}
else if (name == "noticeColor")
{
_colors[Message::PRIO_NOTICE] = parseColor(value);
}
else if (name == "warningColor")
{
_colors[Message::PRIO_WARNING] = parseColor(value);
}
else if (name == "errorColor")
{
_colors[Message::PRIO_ERROR] = parseColor(value);
}
else if (name == "criticalColor")
{
_colors[Message::PRIO_CRITICAL] = parseColor(value);
}
else if (name == "fatalColor")
{
_colors[Message::PRIO_FATAL] = parseColor(value);
}
else
{
Channel::setProperty(name, value);
}
}
std::string ColorConsoleChannel::getProperty(const std::string& name) const
{
if (name == "enableColors")
{
return _enableColors ? "true" : "false";
}
else if (name == "traceColor")
{
return formatColor(_colors[Message::PRIO_TRACE]);
}
else if (name == "debugColor")
{
return formatColor(_colors[Message::PRIO_DEBUG]);
}
else if (name == "informationColor")
{
return formatColor(_colors[Message::PRIO_INFORMATION]);
}
else if (name == "noticeColor")
{
return formatColor(_colors[Message::PRIO_NOTICE]);
}
else if (name == "warningColor")
{
return formatColor(_colors[Message::PRIO_WARNING]);
}
else if (name == "errorColor")
{
return formatColor(_colors[Message::PRIO_ERROR]);
}
else if (name == "criticalColor")
{
return formatColor(_colors[Message::PRIO_CRITICAL]);
}
else if (name == "fatalColor")
{
return formatColor(_colors[Message::PRIO_FATAL]);
}
else
{
return Channel::getProperty(name);
}
}
ColorConsoleChannel::Color ColorConsoleChannel::parseColor(const std::string& color) const
{
if (icompare(color, "default") == 0)
return CC_DEFAULT;
else if (icompare(color, "black") == 0)
return CC_BLACK;
else if (icompare(color, "red") == 0)
return CC_RED;
else if (icompare(color, "green") == 0)
return CC_GREEN;
else if (icompare(color, "brown") == 0)
return CC_BROWN;
else if (icompare(color, "blue") == 0)
return CC_BLUE;
else if (icompare(color, "magenta") == 0)
return CC_MAGENTA;
else if (icompare(color, "cyan") == 0)
return CC_CYAN;
else if (icompare(color, "gray") == 0)
return CC_GRAY;
else if (icompare(color, "darkGray") == 0)
return CC_DARKGRAY;
else if (icompare(color, "lightRed") == 0)
return CC_LIGHTRED;
else if (icompare(color, "lightGreen") == 0)
return CC_LIGHTGREEN;
else if (icompare(color, "yellow") == 0)
return CC_YELLOW;
else if (icompare(color, "lightBlue") == 0)
return CC_LIGHTBLUE;
else if (icompare(color, "lightMagenta") == 0)
return CC_LIGHTMAGENTA;
else if (icompare(color, "lightCyan") == 0)
return CC_LIGHTCYAN;
else if (icompare(color, "white") == 0)
return CC_WHITE;
else throw InvalidArgumentException("Invalid color value", color);
}
std::string ColorConsoleChannel::formatColor(Color color) const
{
switch (color)
{
case CC_DEFAULT: return "default";
case CC_BLACK: return "black";
case CC_RED: return "red";
case CC_GREEN: return "green";
case CC_BROWN: return "brown";
case CC_BLUE: return "blue";
case CC_MAGENTA: return "magenta";
case CC_CYAN: return "cyan";
case CC_GRAY: return "gray";
case CC_DARKGRAY: return "darkGray";
case CC_LIGHTRED: return "lightRed";
case CC_LIGHTGREEN: return "lightGreen";
case CC_YELLOW: return "yellow";
case CC_LIGHTBLUE: return "lightBlue";
case CC_LIGHTMAGENTA: return "lightMagenta";
case CC_LIGHTCYAN: return "lightCyan";
case CC_WHITE: return "white";
default: return "invalid";
}
}
void ColorConsoleChannel::initColors()
{
_colors[0] = CC_DEFAULT; // unused
_colors[Message::PRIO_FATAL] = CC_LIGHTRED;
_colors[Message::PRIO_CRITICAL] = CC_LIGHTRED;
_colors[Message::PRIO_ERROR] = CC_LIGHTRED;
_colors[Message::PRIO_WARNING] = CC_YELLOW;
_colors[Message::PRIO_NOTICE] = CC_DEFAULT;
_colors[Message::PRIO_INFORMATION] = CC_DEFAULT;
_colors[Message::PRIO_DEBUG] = CC_GRAY;
_colors[Message::PRIO_FATAL] = CC_GRAY;
}
} // namespace Poco

View File

@ -1,7 +1,7 @@
//
// LoggingFactory.cpp
//
// $Id: //poco/1.4/Foundation/src/LoggingFactory.cpp#1 $
// $Id: //poco/1.4/Foundation/src/LoggingFactory.cpp#3 $
//
// Library: Foundation
// Package: Logging
@ -110,8 +110,10 @@ void LoggingFactory::registerBuiltins()
_channelFactory.registerClass("AsyncChannel", new Instantiator<AsyncChannel, Channel>);
#if defined(POCO_OS_FAMILY_WINDOWS) && !defined(_WIN32_WCE)
_channelFactory.registerClass("ConsoleChannel", new Instantiator<WindowsConsoleChannel, Channel>);
_channelFactory.registerClass("ColorConsoleChannel", new Instantiator<WindowsColorConsoleChannel, Channel>);
#else
_channelFactory.registerClass("ConsoleChannel", new Instantiator<ConsoleChannel, Channel>);
_channelFactory.registerClass("ColorConsoleChannel", new Instantiator<ColorConsoleChannel, Channel>);
#endif
#ifndef POCO_NO_FILECHANNEL
_channelFactory.registerClass("FileChannel", new Instantiator<FileChannel, Channel>);

View File

@ -1,7 +1,7 @@
//
// WindowsConsoleChannel.cpp
//
// $Id: //poco/1.4/Foundation/src/WindowsConsoleChannel.cpp#1 $
// $Id: //poco/1.4/Foundation/src/WindowsConsoleChannel.cpp#2 $
//
// Library: Foundation
// Package: Logging
@ -39,6 +39,8 @@
#if defined(POCO_WIN32_UTF8)
#include "Poco/UnicodeConverter.h"
#endif
#include "Poco/String.h"
#include "Poco/Exception.h"
namespace Poco {
@ -85,4 +87,238 @@ void WindowsConsoleChannel::log(const Message& msg)
}
WindowsColorConsoleChannel::WindowsColorConsoleChannel():
_enableColors(true),
_isFile(false),
_hConsole(INVALID_HANDLE_VALUE)
{
_hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
// check whether the console has been redirected
DWORD mode;
_isFile = (GetConsoleMode(_hConsole, &mode) == 0);
initColors();
}
WindowsColorConsoleChannel::~WindowsColorConsoleChannel()
{
}
void WindowsColorConsoleChannel::log(const Message& msg)
{
std::string text = msg.getText();
text += "\r\n";
if (_enableColors && !_isFile)
{
WORD attr = _colors[0];
attr &= 0xFFF0;
attr |= _colors[msg.getPriority()];
SetConsoleTextAttribute(_hConsole, attr);
}
#if defined(POCO_WIN32_UTF8)
if (_isFile)
{
DWORD written;
WriteFile(_hConsole, text.data(), static_cast<DWORD>(text.size()), &written, NULL);
}
else
{
std::wstring utext;
UnicodeConverter::toUTF16(text, utext);
DWORD written;
WriteConsoleW(_hConsole, utext.data(), static_cast<DWORD>(utext.size()), &written, NULL);
}
#else
DWORD written;
WriteFile(_hConsole, text.data(), text.size(), &written, NULL);
#endif
if (_enableColors && !_isFile)
{
SetConsoleTextAttribute(_hConsole, _colors[0]);
}
}
void WindowsColorConsoleChannel::setProperty(const std::string& name, const std::string& value)
{
if (name == "enableColors")
{
_enableColors = icompare(value, "true") == 0;
}
else if (name == "traceColor")
{
_colors[Message::PRIO_TRACE] = parseColor(value);
}
else if (name == "debugColor")
{
_colors[Message::PRIO_DEBUG] = parseColor(value);
}
else if (name == "informationColor")
{
_colors[Message::PRIO_INFORMATION] = parseColor(value);
}
else if (name == "noticeColor")
{
_colors[Message::PRIO_NOTICE] = parseColor(value);
}
else if (name == "warningColor")
{
_colors[Message::PRIO_WARNING] = parseColor(value);
}
else if (name == "errorColor")
{
_colors[Message::PRIO_ERROR] = parseColor(value);
}
else if (name == "criticalColor")
{
_colors[Message::PRIO_CRITICAL] = parseColor(value);
}
else if (name == "fatalColor")
{
_colors[Message::PRIO_FATAL] = parseColor(value);
}
else
{
Channel::setProperty(name, value);
}
}
std::string WindowsColorConsoleChannel::getProperty(const std::string& name) const
{
if (name == "enableColors")
{
return _enableColors ? "true" : "false";
}
else if (name == "traceColor")
{
return formatColor(_colors[Message::PRIO_TRACE]);
}
else if (name == "debugColor")
{
return formatColor(_colors[Message::PRIO_DEBUG]);
}
else if (name == "informationColor")
{
return formatColor(_colors[Message::PRIO_INFORMATION]);
}
else if (name == "noticeColor")
{
return formatColor(_colors[Message::PRIO_NOTICE]);
}
else if (name == "warningColor")
{
return formatColor(_colors[Message::PRIO_WARNING]);
}
else if (name == "errorColor")
{
return formatColor(_colors[Message::PRIO_ERROR]);
}
else if (name == "criticalColor")
{
return formatColor(_colors[Message::PRIO_CRITICAL]);
}
else if (name == "fatalColor")
{
return formatColor(_colors[Message::PRIO_FATAL]);
}
else
{
return Channel::getProperty(name);
}
}
WORD WindowsColorConsoleChannel::parseColor(const std::string& color) const
{
if (icompare(color, "default") == 0)
return _colors[0];
else if (icompare(color, "black") == 0)
return CC_BLACK;
else if (icompare(color, "red") == 0)
return CC_RED;
else if (icompare(color, "green") == 0)
return CC_GREEN;
else if (icompare(color, "brown") == 0)
return CC_BROWN;
else if (icompare(color, "blue") == 0)
return CC_BLUE;
else if (icompare(color, "magenta") == 0)
return CC_MAGENTA;
else if (icompare(color, "cyan") == 0)
return CC_CYAN;
else if (icompare(color, "gray") == 0)
return CC_GRAY;
else if (icompare(color, "darkGray") == 0)
return CC_DARKGRAY;
else if (icompare(color, "lightRed") == 0)
return CC_LIGHTRED;
else if (icompare(color, "lightGreen") == 0)
return CC_LIGHTGREEN;
else if (icompare(color, "yellow") == 0)
return CC_YELLOW;
else if (icompare(color, "lightBlue") == 0)
return CC_LIGHTBLUE;
else if (icompare(color, "lightMagenta") == 0)
return CC_LIGHTMAGENTA;
else if (icompare(color, "lightCyan") == 0)
return CC_LIGHTCYAN;
else if (icompare(color, "white") == 0)
return CC_WHITE;
else throw InvalidArgumentException("Invalid color value", color);
}
std::string WindowsColorConsoleChannel::formatColor(WORD color) const
{
switch (color)
{
case CC_BLACK: return "black";
case CC_RED: return "red";
case CC_GREEN: return "green";
case CC_BROWN: return "brown";
case CC_BLUE: return "blue";
case CC_MAGENTA: return "magenta";
case CC_CYAN: return "cyan";
case CC_GRAY: return "gray";
case CC_DARKGRAY: return "darkGray";
case CC_LIGHTRED: return "lightRed";
case CC_LIGHTGREEN: return "lightGreen";
case CC_YELLOW: return "yellow";
case CC_LIGHTBLUE: return "lightBlue";
case CC_LIGHTMAGENTA: return "lightMagenta";
case CC_LIGHTCYAN: return "lightCyan";
case CC_WHITE: return "white";
default: return "invalid";
}
}
void WindowsColorConsoleChannel::initColors()
{
if (!_isFile)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(_hConsole, &csbi);
_colors[0] = csbi.wAttributes;
}
else
{
_colors[0] = CC_WHITE;
}
_colors[Message::PRIO_FATAL] = CC_LIGHTRED;
_colors[Message::PRIO_CRITICAL] = CC_LIGHTRED;
_colors[Message::PRIO_ERROR] = CC_LIGHTRED;
_colors[Message::PRIO_WARNING] = CC_YELLOW;
_colors[Message::PRIO_NOTICE] = _colors[0];
_colors[Message::PRIO_INFORMATION] = _colors[0];
_colors[Message::PRIO_DEBUG] = CC_GRAY;
_colors[Message::PRIO_FATAL] = CC_GRAY;
}
} // namespace Poco