Added command-line option to autodetect, enable, or disable color output.

This commit is contained in:
2016-06-14 18:02:59 +02:00
parent aff396d919
commit eb3d91f085
5 changed files with 73 additions and 21 deletions

View File

@@ -25,40 +25,70 @@ enum class StandardStream
class LogStream
{
public:
enum class ColorPolicy
{
Never,
Auto,
Always
};
private:
using CharacterType = std::ostream::char_type;
using TraitsType = std::ostream::traits_type;
public:
LogStream(StandardStream standardStream)
: m_standardStream{standardStream}
: m_standardStream{standardStream},
m_colorPolicy{ColorPolicy::Never}
{
}
LogStream(const LogStream &other)
: m_standardStream{other.m_standardStream}
: m_standardStream{other.m_standardStream},
m_colorPolicy{ColorPolicy::Never}
{
}
LogStream &operator=(const LogStream &other)
{
m_standardStream = other.m_standardStream;
m_colorPolicy = other.m_colorPolicy;
return *this;
}
LogStream(LogStream &&other)
: m_standardStream{other.m_standardStream}
: m_standardStream{other.m_standardStream},
m_colorPolicy{other.m_colorPolicy}
{
other.m_colorPolicy = ColorPolicy::Never;
}
LogStream &operator=(LogStream &&other)
{
m_standardStream = other.m_standardStream;
m_colorPolicy = other.m_colorPolicy;
other.m_colorPolicy = ColorPolicy::Never;
return *this;
}
void setColorPolicy(ColorPolicy colorPolicy)
{
m_colorPolicy = colorPolicy;
}
bool supportsColor() const
{
if (m_colorPolicy == ColorPolicy::Never)
return false;
if (m_colorPolicy == ColorPolicy::Always)
return true;
// Autodetect by checking whether output goes to a terminal
const auto fileDescriptor =
(m_standardStream == utils::StandardStream::Out)
? STDOUT_FILENO
@@ -95,6 +125,7 @@ class LogStream
private:
StandardStream m_standardStream;
ColorPolicy m_colorPolicy;
};
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@@ -41,6 +41,7 @@ class Logger
LogStream &errorStream();
void setWarningLevel(WarningLevel warningLevel);
void setColorPolicy(LogStream::ColorPolicy colorPolicy);
void logError(const std::string &message);
void logError(const Parser::Coordinate &coordinate, const std::string &message);