53 lines
1.2 KiB
C++
53 lines
1.2 KiB
C++
#include <Void/Log/ConsoleLogger.hpp>
|
|
|
|
#ifdef VOID_HAS_STACKTRACE
|
|
#include <Void/Stacktrace/Color.hpp>
|
|
#endif
|
|
|
|
#include <cstdlib>
|
|
#include <iostream>
|
|
#include <ostream>
|
|
|
|
namespace Void::Logging
|
|
{
|
|
|
|
namespace
|
|
{
|
|
ColorPolicy console_default_policy()
|
|
{
|
|
ColorPolicy p = ColorPolicy::defaults();
|
|
// Colored by default, with the standard opt-outs: NO_COLOR for
|
|
// the environment and the Stacktrace global toggle when present.
|
|
bool enabled = true;
|
|
if (const char* no_color = std::getenv("NO_COLOR");
|
|
no_color != nullptr && *no_color != '\0') {
|
|
enabled = false;
|
|
}
|
|
#ifdef VOID_HAS_STACKTRACE
|
|
enabled = enabled && Void::Stacktrace::Color::g_color_enabled;
|
|
#endif
|
|
p.enabled = enabled;
|
|
return p;
|
|
}
|
|
} // namespace
|
|
|
|
ConsoleLogger::ConsoleLogger(std::ostream& out, Mode mode,
|
|
std::size_t capacity)
|
|
: Logger(mode, capacity), m_out(out)
|
|
{
|
|
m_default.set_color_policy(console_default_policy());
|
|
}
|
|
|
|
ConsoleLogger::ConsoleLogger() : ConsoleLogger(std::cerr) {}
|
|
|
|
ConsoleLogger::~ConsoleLogger() noexcept { flush(); }
|
|
|
|
void ConsoleLogger::write(const Entry& entry)
|
|
{
|
|
const Formatter& fmt = m_formatter ? *m_formatter : m_default;
|
|
m_out << fmt.format(entry) << '\n';
|
|
m_out.flush();
|
|
}
|
|
|
|
} // namespace Void::Logging
|