[icinga-checkins] icinga.org: icinga2/master: Implemented colored log messages.

git at icinga.org git at icinga.org
Fri Feb 1 14:12:45 CET 2013


Module: icinga2
Branch: master
Commit: 886fd3a5b87b29179757fb627288c6f341f4dd28
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=886fd3a5b87b29179757fb627288c6f341f4dd28

Author: Gunnar Beutner <gunnar.beutner at netways.de>
Date:   Fri Feb  1 14:12:24 2013 +0100

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 c9c9e7e..c5e9635 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 f66e320..d88b681 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 3061639..c1719a6 100644
--- a/lib/base/streamlogger.h
+++ b/lib/base/streamlogger.h
@@ -35,12 +35,13 @@ public:
 	typedef weak_ptr<StreamLogger> 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 278c38e..ebe6f74 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);
 				}
 			}





More information about the icinga-checkins mailing list