From 886fd3a5b87b29179757fb627288c6f341f4dd28 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Fri, 1 Feb 2013 14:12:24 +0100 Subject: [PATCH] Implemented colored log messages. --- lib/base/logger.cpp | 7 +++++-- lib/base/streamlogger.cpp | 40 +++++++++++++++++++++++++++++++++++---- lib/base/streamlogger.h | 6 ++++-- lib/remoting/endpoint.cpp | 2 +- 4 files changed, 46 insertions(+), 9 deletions(-) diff --git a/lib/base/logger.cpp b/lib/base/logger.cpp index c9c9e7e13..c5e9635d8 100644 --- a/lib/base/logger.cpp +++ b/lib/base/logger.cpp @@ -125,8 +125,11 @@ void Logger::ForwardLogEntry(const LogEntry& entry) processed = true; } - if (!processed && entry.Severity >= LogInformation) - StreamLogger::ProcessLogEntry(std::cout, entry); + if (!processed && entry.Severity >= LogInformation) { + static bool tty = StreamLogger::IsTty(std::cout); + + StreamLogger::ProcessLogEntry(std::cout, tty, entry); + } } /** diff --git a/lib/base/streamlogger.cpp b/lib/base/streamlogger.cpp index f66e320a4..d88b68188 100644 --- a/lib/base/streamlogger.cpp +++ b/lib/base/streamlogger.cpp @@ -25,7 +25,7 @@ using namespace icinga; * Constructor for the StreamLogger class. */ StreamLogger::StreamLogger(void) - : ILogger(), m_Stream(NULL), m_OwnsStream(false) + : ILogger(), m_Stream(NULL), m_OwnsStream(false), m_Tty(false) { } /** @@ -34,7 +34,7 @@ StreamLogger::StreamLogger(void) * @param stream The stream. */ StreamLogger::StreamLogger(ostream *stream) - : ILogger(), m_Stream(stream), m_OwnsStream(false) + : ILogger(), m_Stream(stream), m_OwnsStream(false), m_Tty(IsTty(*stream)) { } /** @@ -62,15 +62,17 @@ void StreamLogger::OpenFile(const String& filename) m_Stream = stream; m_OwnsStream = true; + m_Tty = false; } /** * Processes a log entry and outputs it to a stream. * * @param stream The output stream. + * @param tty Whether the output stream is a TTY. * @param entry The log entry. */ -void StreamLogger::ProcessLogEntry(std::ostream& stream, const LogEntry& entry) +void StreamLogger::ProcessLogEntry(ostream& stream, bool tty, const LogEntry& entry) { char timestamp[100]; @@ -79,9 +81,26 @@ void StreamLogger::ProcessLogEntry(std::ostream& stream, const LogEntry& entry) strftime(timestamp, sizeof(timestamp), "%Y/%m/%d %H:%M:%S %z", &tmnow); + if (tty) { + String colorCode; + switch (entry.Severity) { + case LogWarning: + colorCode = "\x1b[33m"; // yellow; + break; + case LogCritical: + colorCode = "\x1b[31m"; // red + break; + } + + stream << colorCode; + } + stream << "[" << timestamp << "] " << Logger::SeverityToString(entry.Severity) << "/" << entry.Facility << ": " << entry.Message << std::endl; + + if (tty) + stream << "\x1b[0m"; // clear colors } /** @@ -91,6 +110,19 @@ void StreamLogger::ProcessLogEntry(std::ostream& stream, const LogEntry& entry) */ void StreamLogger::ProcessLogEntry(const LogEntry& entry) { - ProcessLogEntry(*m_Stream, entry); + ProcessLogEntry(*m_Stream, m_Tty, entry); } +bool StreamLogger::IsTty(ostream& stream) +{ +#ifndef _WIN32 + /* Eww... */ + if (stream == std::cout) + return isatty(fileno(stdout)); + + if (stream == std::cerr) + return isatty(fileno(stderr)); +#endif /*_ WIN32 */ + + return false; +} diff --git a/lib/base/streamlogger.h b/lib/base/streamlogger.h index 306163903..c1719a6e1 100644 --- a/lib/base/streamlogger.h +++ b/lib/base/streamlogger.h @@ -35,12 +35,13 @@ public: typedef weak_ptr WeakPtr; StreamLogger(void); - StreamLogger(std::ostream *stream); + StreamLogger(ostream *stream); ~StreamLogger(void); void OpenFile(const String& filename); - static void ProcessLogEntry(std::ostream& stream, const LogEntry& entry); + static void ProcessLogEntry(ostream& stream, bool tty, const LogEntry& entry); + static bool IsTty(ostream& stream); protected: virtual void ProcessLogEntry(const LogEntry& entry); @@ -48,6 +49,7 @@ protected: private: ostream *m_Stream; bool m_OwnsStream; + bool m_Tty; }; } diff --git a/lib/remoting/endpoint.cpp b/lib/remoting/endpoint.cpp index 278c38e90..ebe6f74a1 100644 --- a/lib/remoting/endpoint.cpp +++ b/lib/remoting/endpoint.cpp @@ -252,7 +252,7 @@ void Endpoint::OnAttributeChanged(const String& name, const Value& oldValue) String subscription; BOOST_FOREACH(tie(tuples::ignore, subscription), newSubscriptions) { if (!oldSubscriptions || !oldSubscriptions->Contains(subscription)) { - Logger::Write(LogInformation, "remoting", "New subscription for '" + GetName() + "': " + subscription); + Logger::Write(LogDebug, "remoting", "New subscription for '" + GetName() + "': " + subscription); OnSubscriptionRegistered(GetSelf(), subscription); } }