Add red color to stderr logging
This commit is contained in:
parent
04243db779
commit
f6377045c9
6 changed files with 131 additions and 16 deletions
10
libs/log/include/psemek/log/colored_stream.hpp
Normal file
10
libs/log/include/psemek/log/colored_stream.hpp
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <psemek/io/stream.hpp>
|
||||||
|
|
||||||
|
namespace psemek::log
|
||||||
|
{
|
||||||
|
|
||||||
|
std::unique_ptr<io::ostream> colored_stream(std::unique_ptr<io::ostream> stream, std::string open, std::string close);
|
||||||
|
|
||||||
|
}
|
||||||
45
libs/log/source/colored_stream.cpp
Normal file
45
libs/log/source/colored_stream.cpp
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
#include <psemek/log/colored_stream.hpp>
|
||||||
|
|
||||||
|
namespace psemek::log
|
||||||
|
{
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
|
||||||
|
struct colored_stream_impl
|
||||||
|
: io::ostream
|
||||||
|
{
|
||||||
|
colored_stream_impl(std::unique_ptr<io::ostream> stream, std::string open, std::string close)
|
||||||
|
: stream_(std::move(stream))
|
||||||
|
, open_(std::move(open))
|
||||||
|
, close_(std::move(close))
|
||||||
|
{}
|
||||||
|
|
||||||
|
std::size_t write(char const * p, std::size_t size) override
|
||||||
|
{
|
||||||
|
std::size_t result = 0;
|
||||||
|
stream_->write(open_.data(), open_.size());
|
||||||
|
result += stream_->write(p, size);
|
||||||
|
stream_->write(close_.data(), close_.size());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
void flush() override
|
||||||
|
{
|
||||||
|
stream_->flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::unique_ptr<io::ostream> stream_;
|
||||||
|
std::string open_;
|
||||||
|
std::string close_;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<io::ostream> colored_stream(std::unique_ptr<io::ostream> stream, std::string open, std::string close)
|
||||||
|
{
|
||||||
|
return std::make_unique<colored_stream_impl>(std::move(stream), std::move(open), std::move(close));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -6,8 +6,10 @@
|
||||||
#include <psemek/util/clock.hpp>
|
#include <psemek/util/clock.hpp>
|
||||||
#include <psemek/util/pretty_print.hpp>
|
#include <psemek/util/pretty_print.hpp>
|
||||||
#include <psemek/log/log.hpp>
|
#include <psemek/log/log.hpp>
|
||||||
|
#include <psemek/log/colored_stream.hpp>
|
||||||
#include <psemek/io/synchronized.hpp>
|
#include <psemek/io/synchronized.hpp>
|
||||||
#include <psemek/util/exception.hpp>
|
#include <psemek/util/exception.hpp>
|
||||||
|
#include <psemek/util/terminal_color.hpp>
|
||||||
|
|
||||||
#undef main
|
#undef main
|
||||||
|
|
||||||
|
|
@ -28,6 +30,9 @@ int main(int argc, char ** argv) try
|
||||||
synchronized_stdout_stderr.push_back(io::std_err());
|
synchronized_stdout_stderr.push_back(io::std_err());
|
||||||
synchronized_stdout_stderr = io::synchronized(std::move(synchronized_stdout_stderr));
|
synchronized_stdout_stderr = io::synchronized(std::move(synchronized_stdout_stderr));
|
||||||
|
|
||||||
|
if (util::terminal_color::supported())
|
||||||
|
synchronized_stdout_stderr[1] = log::colored_stream(std::move(synchronized_stdout_stderr[1]), std::string{util::terminal_color::red()}, std::string{util::terminal_color::normal()});
|
||||||
|
|
||||||
log::add_sink(log::default_sink(std::move(synchronized_stdout_stderr[0]), stdio_log_level, log::level::info));
|
log::add_sink(log::default_sink(std::move(synchronized_stdout_stderr[0]), stdio_log_level, log::level::info));
|
||||||
log::add_sink(log::default_sink(std::move(synchronized_stdout_stderr[1]), log::level::warning, log::level::error));
|
log::add_sink(log::default_sink(std::move(synchronized_stdout_stderr[1]), log::level::warning, log::level::error));
|
||||||
log::register_thread("main");
|
log::register_thread("main");
|
||||||
|
|
|
||||||
16
libs/util/include/psemek/util/terminal_color.hpp
Normal file
16
libs/util/include/psemek/util/terminal_color.hpp
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
|
namespace psemek::util::terminal_color
|
||||||
|
{
|
||||||
|
|
||||||
|
bool supported();
|
||||||
|
std::string_view bold();
|
||||||
|
std::string_view red();
|
||||||
|
std::string_view green();
|
||||||
|
std::string_view yellow();
|
||||||
|
std::string_view cyan();
|
||||||
|
std::string_view normal();
|
||||||
|
|
||||||
|
}
|
||||||
47
libs/util/source/terminal_color.cpp
Normal file
47
libs/util/source/terminal_color.cpp
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
#include <psemek/util/terminal_color.hpp>
|
||||||
|
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
|
namespace psemek::util::terminal_color
|
||||||
|
{
|
||||||
|
|
||||||
|
bool supported()
|
||||||
|
{
|
||||||
|
std::string_view term;
|
||||||
|
if (auto t = std::getenv("TERM"))
|
||||||
|
term = t;
|
||||||
|
|
||||||
|
return term == "linux" || term.starts_with("xterm");
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string_view bold()
|
||||||
|
{
|
||||||
|
return "\033[1m";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string_view red()
|
||||||
|
{
|
||||||
|
return "\033[1;31m";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string_view green()
|
||||||
|
{
|
||||||
|
return "\033[1;32m";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string_view yellow()
|
||||||
|
{
|
||||||
|
return "\033[33m";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string_view cyan()
|
||||||
|
{
|
||||||
|
return "\033[36m";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string_view normal()
|
||||||
|
{
|
||||||
|
return "\033[0m";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
#include <psemek/test/test.hpp>
|
#include <psemek/test/test.hpp>
|
||||||
#include <psemek/util/pretty_print.hpp>
|
#include <psemek/util/pretty_print.hpp>
|
||||||
#include <psemek/util/exception.hpp>
|
#include <psemek/util/exception.hpp>
|
||||||
|
#include <psemek/util/terminal_color.hpp>
|
||||||
#include <psemek/log/log.hpp>
|
#include <psemek/log/log.hpp>
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
|
|
@ -63,15 +64,6 @@ namespace psemek::test
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool terminal_has_color()
|
|
||||||
{
|
|
||||||
std::string_view term;
|
|
||||||
if (auto t = std::getenv("TERM"))
|
|
||||||
term = t;
|
|
||||||
|
|
||||||
return term == "linux" || term.starts_with("xterm");
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char ** argv)
|
int main(int argc, char ** argv)
|
||||||
{
|
{
|
||||||
std::string_view bold;
|
std::string_view bold;
|
||||||
|
|
@ -81,14 +73,14 @@ int main(int argc, char ** argv)
|
||||||
std::string_view cyan;
|
std::string_view cyan;
|
||||||
std::string_view normal;
|
std::string_view normal;
|
||||||
|
|
||||||
if (terminal_has_color())
|
if (psemek::util::terminal_color::supported())
|
||||||
{
|
{
|
||||||
bold = "\033[1m";
|
bold = psemek::util::terminal_color::bold();
|
||||||
red = "\033[1;31m";
|
red = psemek::util::terminal_color::red();
|
||||||
green = "\033[1;32m";
|
green = psemek::util::terminal_color::green();
|
||||||
yellow = "\033[33m";
|
yellow = psemek::util::terminal_color::yellow();
|
||||||
cyan = "\033[36m";
|
cyan = psemek::util::terminal_color::cyan();
|
||||||
normal = "\033[0m";
|
normal = psemek::util::terminal_color::normal();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::set<std::string> tests;
|
std::set<std::string> tests;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue