]> granicus.if.org Git - icinga2/commitdiff
Implemented colored log messages.
authorGunnar Beutner <gunnar.beutner@netways.de>
Fri, 1 Feb 2013 13:12:24 +0000 (14:12 +0100)
committerGunnar Beutner <gunnar.beutner@netways.de>
Fri, 1 Feb 2013 13:12:24 +0000 (14:12 +0100)
lib/base/logger.cpp
lib/base/streamlogger.cpp
lib/base/streamlogger.h
lib/remoting/endpoint.cpp

index c9c9e7e13a0e45c59a19b1d7da64bba7dbdef4a5..c5e9635d89ec722dbaaa2da673366421248005e1 100644 (file)
@@ -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);
+       }
 }
 
 /**
index f66e320a4ed6d9543f519e485100b7405255ce6d..d88b681888737727158209de802440a66b9af6f2 100644 (file)
@@ -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;
+}
index 30616390362d16f2217966acbba9b0e5f6eedba3..c1719a6e11e8177edb1e1b52d639dbc461a19079 100644 (file)
@@ -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;
 };
 
 }
index 278c38e906ee92d89cd955a968ef1bdfbe73f676..ebe6f74a14663d7e1acc8998dec70defb00406da 100644 (file)
@@ -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);
                                }
                        }