myserver-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[myserver-commit] [2948] Added the possibility to colorize console loggi


From: Francesco Pipita
Subject: [myserver-commit] [2948] Added the possibility to colorize console logging messages depending on their logging level .
Date: Sat, 08 Nov 2008 17:35:34 +0000

Revision: 2948
          http://svn.sv.gnu.org/viewvc/?view=rev&root=myserver&revision=2948
Author:   francesco_pipita
Date:     2008-11-08 17:35:26 +0000 (Sat, 08 Nov 2008)

Log Message:
-----------
Added the possibility to colorize console logging messages depending on their 
logging level.

Modified Paths:
--------------
    trunk/myserver/include/filter/console.h
    trunk/myserver/include/log/log_manager.h
    trunk/myserver/include/log/stream/console_stream.h
    trunk/myserver/include/log/stream/log_stream.h
    trunk/myserver/include/server/server.h
    trunk/myserver/src/filter/console.cpp
    trunk/myserver/src/log/log_manager.cpp
    trunk/myserver/src/log/stream/console_stream.cpp
    trunk/myserver/src/log/stream/log_stream.cpp
    trunk/myserver/tests/test_log_manager.cpp

Modified: trunk/myserver/include/filter/console.h
===================================================================
--- trunk/myserver/include/filter/console.h     2008-11-08 10:34:53 UTC (rev 
2947)
+++ trunk/myserver/include/filter/console.h     2008-11-08 17:35:26 UTC (rev 
2948)
@@ -25,18 +25,50 @@
 
 using namespace std;
 
+enum MyServerColor
+  {
+    /* Foreground colors. */
+    MYSERVER_FG_COLOR_BLACK,
+    MYSERVER_FG_COLOR_RED,
+    MYSERVER_FG_COLOR_GREEN,
+    MYSERVER_FG_COLOR_YELLOW,
+    MYSERVER_FG_COLOR_BLUE,
+    MYSERVER_FG_COLOR_MAGENTA,
+    MYSERVER_FG_COLOR_CYAN,
+    MYSERVER_FG_COLOR_WHITE,
+    MYSERVER_FG_COLOR_RESET,
+    /* Background colors */
+    MYSERVER_BG_COLOR_BLACK,
+    MYSERVER_BG_COLOR_RED,
+    MYSERVER_BG_COLOR_GREEN,
+    MYSERVER_BG_COLOR_YELLOW,
+    MYSERVER_BG_COLOR_BLUE,
+    MYSERVER_BG_COLOR_MAGENTA,
+    MYSERVER_BG_COLOR_CYAN,
+    MYSERVER_BG_COLOR_WHITE,
+    MYSERVER_BG_COLOR_RESET
+  };
+
+#ifdef WIN32
+extern WORD colors[];
+#endif
+#ifdef NOT_WIN
+extern char const* colors[];
+#endif
+
 class Console : public Stream
 {
 public:
-  Console();
-  virtual ~Console();
+  Console ();
+  virtual ~Console ();
   virtual int flush (u_long* nbw);
   virtual int read (char* buffer, u_long len, u_long* nbr);
   virtual int write (const char* buffer, u_long len, u_long* nbw);
-  virtual int enterErrorMode ();
-  virtual int exitErrorMode ();
   virtual int openConsole (string fd);
+  int setColor (MyServerColor color[]);
+  int reset ();
 protected:
+  int checkColors (MyServerColor c[]);
   ostream* fd;
 };
 

Modified: trunk/myserver/include/log/log_manager.h
===================================================================
--- trunk/myserver/include/log/log_manager.h    2008-11-08 10:34:53 UTC (rev 
2947)
+++ trunk/myserver/include/log/log_manager.h    2008-11-08 17:35:26 UTC (rev 
2948)
@@ -29,27 +29,20 @@
 
 using namespace std;
 
-enum LoggingLevel
-  {
-    MYSERVER_LOG_MSG_INFO,
-    MYSERVER_LOG_MSG_WARNING,
-    MYSERVER_LOG_MSG_ERROR
-  };
-
 class LogManager
 {
 public:
-  LogManager (FiltersFactory* ff, LoggingLevel level = 
MYSERVER_LOG_MSG_WARNING);
+  LogManager (FiltersFactory* ff, LoggingLevel level = MYSERVER_LOG_MSG_INFO);
   ~LogManager ();
   int add (void* owner, string type, string location, 
            list<string>& filters, u_long cycle);
   int remove (void* owner);
   int log (void* owner, string message, bool appendNL = false,
-           LoggingLevel level = MYSERVER_LOG_MSG_WARNING);
+           LoggingLevel level = MYSERVER_LOG_MSG_INFO);
   int log (void* owner, string type, string message, bool appendNL = false,
-           LoggingLevel level = MYSERVER_LOG_MSG_WARNING);
+           LoggingLevel level = MYSERVER_LOG_MSG_INFO);
   int log (void* owner, string type, string location, string message, 
-           bool appendNL = false, LoggingLevel level = 
MYSERVER_LOG_MSG_WARNING);
+           bool appendNL = false, LoggingLevel level = MYSERVER_LOG_MSG_INFO);
   int close (void* owner);
   int close (void* owner, string type);
   int close (void* owner, string type, string location);
@@ -86,6 +79,7 @@
   int add (void* owner);
   int add (void* owner, string type);
   int add (void* owner, string type, string location, LogStream* ls);
+  int computeNewLine ();
 private:
   LoggingLevel level;
   Mutex* mutex;
@@ -93,6 +87,7 @@
   FiltersFactory* ff;
   map<string, LogStream*> logStreams;
   map<void*, map<string, map<string, LogStream*> > > owners;
+  string newline;
 };
 
 #endif

Modified: trunk/myserver/include/log/stream/console_stream.h
===================================================================
--- trunk/myserver/include/log/stream/console_stream.h  2008-11-08 10:34:53 UTC 
(rev 2947)
+++ trunk/myserver/include/log/stream/console_stream.h  2008-11-08 17:35:26 UTC 
(rev 2948)
@@ -25,12 +25,13 @@
 #include <include/log/stream/log_stream.h>
 #include <include/filter/console.h>
 
+extern MyServerColor defaultColors[][2];
+
 class ConsoleStream : public LogStream
 {
 public:
   ConsoleStream (FiltersFactory*, u_long, Stream*, FiltersChain*);
-  virtual int enterErrorMode ();
-  virtual int exitErrorMode ();
+  virtual int setMode (LoggingLevel level);
 };
 
 #endif

Modified: trunk/myserver/include/log/stream/log_stream.h
===================================================================
--- trunk/myserver/include/log/stream/log_stream.h      2008-11-08 10:34:53 UTC 
(rev 2947)
+++ trunk/myserver/include/log/stream/log_stream.h      2008-11-08 17:35:26 UTC 
(rev 2948)
@@ -29,6 +29,18 @@
 
 using namespace std;
 
+enum LoggingLevel
+  {
+    /* The MYSERVER_LOG_MSG_PLAIN is only used within the
+     * LogManager class to print new lines with normal text 
+     * attributes over the ConsoleStream.
+     */
+    MYSERVER_LOG_MSG_PLAIN,
+    MYSERVER_LOG_MSG_INFO,
+    MYSERVER_LOG_MSG_WARNING,
+    MYSERVER_LOG_MSG_ERROR
+  };
+
 enum LogStreamEvent
   {
     MYSERVER_LOG_EVT_SET_CYCLE,
@@ -36,8 +48,7 @@
     MYSERVER_LOG_EVT_CLOSE,
     MYSERVER_LOG_EVT_ADD_FILTER,
     MYSERVER_LOG_EVT_CHOWN,
-    MYSERVER_LOG_EVT_ENTER_ERROR_MODE,
-    MYSERVER_LOG_EVT_EXIT_ERROR_MODE
+    MYSERVER_LOG_EVT_SET_MODE
   };
 
 class LogStream
@@ -60,8 +71,7 @@
   virtual int log (string message);
   virtual u_long streamSize ();
   virtual int chown (int uid, int gid);
-  virtual int enterErrorMode ();
-  virtual int exitErrorMode ();
+  virtual int setMode (LoggingLevel level);
   virtual ~LogStream ();
 protected:
   virtual int streamCycle ();

Modified: trunk/myserver/include/server/server.h
===================================================================
--- trunk/myserver/include/server/server.h      2008-11-08 10:34:53 UTC (rev 
2947)
+++ trunk/myserver/include/server/server.h      2008-11-08 17:35:26 UTC (rev 
2948)
@@ -132,7 +132,7 @@
   void stop();
   void finalCleanup();
   int terminate();
-  int logWriteln(char const*, LoggingLevel level = MYSERVER_LOG_MSG_WARNING);
+  int logWriteln(char const*, LoggingLevel level = MYSERVER_LOG_MSG_INFO);
   int logWriteln(string const &str)
     {return logWriteln(str.c_str());};
   int setLogLocation(string);

Modified: trunk/myserver/src/filter/console.cpp
===================================================================
--- trunk/myserver/src/filter/console.cpp       2008-11-08 10:34:53 UTC (rev 
2947)
+++ trunk/myserver/src/filter/console.cpp       2008-11-08 17:35:26 UTC (rev 
2948)
@@ -17,13 +17,68 @@
 
 #include <include/filter/console.h>
 
+#ifdef WIN32
+WORD colors[] =
+  {
+    /* Foreground colors */
+    0, // Black
+    FOREGROUND_RED, // Red
+    FOREGROUND_GREEN, // Green
+    FOREGROUND_RED | FOREGROUND_GREEN, // Yellow
+    FOREGROUND_BLUE, // Blue
+    FOREGROUND_RED | FOREGROUND_BLUE // Magenta
+    FOREGROUND_BLUE | FOREGROUND_GREEN // Cyan
+    FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, // White
+    FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE // Reset
+    /* Background colors */
+    0, // Black
+    BACKGROUND_RED, // Red
+    BACKGROUND_GREEN, // Green
+    BACKGROUND_RED | BACKGROUND_GREEN, // Yellow
+    BACKGROUND_BLUE, // Blue
+    BACKGROUND_RED | BACKGROUND_BLUE, // Magenta
+    BACKGROUND_BLUE | BACKGROUND_GREEN, // Cyan
+    BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE, // White
+    0 // Reset
+  };
+#endif
+#ifdef NOT_WIN
+char const* colors[] =
+  {
+    /* Foreground colors */
+    "\033[30m", // Black
+    "\033[31m", // Red
+    "\033[32m", // Green
+    "\033[33m", // Yellow
+    "\033[34m", // Blue
+    "\033[35m", // Magenta
+    "\033[36m", // Cyan
+    "\033[37m", // White
+    "\033[0m", // Reset
+    /* Background colors */
+    "\033[40m", // Black
+    "\033[41m", // Red
+    "\033[42m", // Green
+    "\033[43m", // Yellow
+    "\033[44m", // Blue
+    "\033[45m", // Magenta
+    "\033[46m", // Cyan
+    "\033[47m", // White
+    "\033[0m" // Reset
+  };
+#endif
+
 Console::Console () : Stream ()
 {
+  fd = 0;
 }
 
 Console::~Console ()
 {
-  
+  if (fd) 
+    {
+      reset ();
+    }
 }
 
 int 
@@ -58,7 +113,7 @@
   return 1;
 }
 
-int 
+int
 Console::openConsole (string fd)
 {
   int success = 1;
@@ -75,38 +130,52 @@
   return success;
 }
 
+/*!
+ * Check that only allowed values are provided for background and 
+ * foreground colors.
+ * \param c the array to validate.
+ * \return 0 if c is a valid array, 1 else.
+ */
 int
-Console::enterErrorMode ()
+Console::checkColors (MyServerColor c[])
 {
-#ifdef WIN32
-
-  int success = 
-    SetConsoleTextAttribute (GetStdHandle ((fd == &cout) ? STD_OUTPUT_HANDLE : 
STD_ERROR_HANDLE),
-                             FOREGROUND_RED |
-                             FOREGROUND_INTENSITY);
-  if (success)
-    return 0;
-#endif
-#ifdef NOT_WIN
-  *fd << "\033[31;1m";
-  return 0;
-#endif
+  return 
+    c[0] < MYSERVER_FG_COLOR_BLACK || 
+    c[0] > MYSERVER_FG_COLOR_RESET ||
+    c[1] < MYSERVER_BG_COLOR_BLACK || 
+    c[1] > MYSERVER_BG_COLOR_RESET;
 }
 
+/*!
+ * Set the attributes for the console text.
+ * \param c[0] holds the foreground color, c[1] the background one.
+ * \return 0 on success, 1 on error.
+ */
 int
-Console::exitErrorMode ()
+Console::setColor (MyServerColor c[])
 {
+  if (!checkColors (c))
+    {
 #ifdef WIN32
-  int success = 
-    SetConsoleTextAttribute (GetStdHandle ((fd == &cout) ? STD_OUTPUT_HANDLE : 
STD_ERROR_HANDLE),
-                             FOREGROUND_RED |
-                             FOREGROUND_GREEN |
-                             FOREGROUND_BLUE);
-  if (success)
-    return 0;
+      SetConsoleTextAttribute (GetStdHandle ((fd == &cout) ? STD_OUTPUT_HANDLE 
: STD_ERROR_HANDLE),
+                               colors[c[0]] | colors[c[1]]);
 #endif
 #ifdef NOT_WIN
-  *fd << "\033[0m";
-  return 0;
+      *fd << colors[c[0]] << colors[c[1]];
 #endif
+      return 0;
+    }
+  return 1;
 }
+
+/*!
+ * Restore the original console colors (white text on black background on
+ * WIN32).
+ * \return 0 on success, 1 on error.
+ */
+int
+Console::reset ()
+{
+  MyServerColor c[] = { MYSERVER_FG_COLOR_RESET, MYSERVER_BG_COLOR_RESET };
+  return setColor (c);
+}

Modified: trunk/myserver/src/log/log_manager.cpp
===================================================================
--- trunk/myserver/src/log/log_manager.cpp      2008-11-08 10:34:53 UTC (rev 
2947)
+++ trunk/myserver/src/log/log_manager.cpp      2008-11-08 17:35:26 UTC (rev 
2948)
@@ -24,6 +24,7 @@
   lsf = new LogStreamFactory ();
   mutex = new Mutex ();
   mutex->init ();
+  computeNewLine ();
 }
 
 LogManager::~LogManager ()
@@ -34,7 +35,19 @@
   delete lsf;
 }
 
+/*!
+ * Precalculate the newline string for the host operating system.
+ * \return 0 on success, 1 on error.
+ */
 int
+LogManager::computeNewLine ()
+{
+  ostringstream oss;
+  oss << endl;
+  newline.assign (oss.str ());
+}
+
+int
 LogManager::clear ()
 {
   mutex->lock ();
@@ -227,24 +240,16 @@
   int success = 1;
   if (level >= this->level)
     {
-      success = 0;
+      success = 
+        notify (owner, MYSERVER_LOG_EVT_SET_MODE, static_cast<void*>(&level)) 
||
+        notify (owner, MYSERVER_LOG_EVT_LOG, static_cast<void*>(&message));
       if (appendNL)
         {
-          ostringstream oss;
-          oss << message << endl;
-          message.assign (oss.str ());
+          LoggingLevel l = MYSERVER_LOG_MSG_PLAIN;
+          success |= 
+            (notify (owner, MYSERVER_LOG_EVT_SET_MODE, 
(static_cast<void*>(&l))) ||
+             notify (owner, MYSERVER_LOG_EVT_LOG, 
(static_cast<void*>(&newline))));
         }
-      if (level == MYSERVER_LOG_MSG_ERROR)
-        {
-          success = 
-            notify (owner, MYSERVER_LOG_EVT_ENTER_ERROR_MODE) ||
-            notify (owner, MYSERVER_LOG_EVT_LOG, static_cast<void*>(&message)) 
||
-            notify (owner, MYSERVER_LOG_EVT_EXIT_ERROR_MODE);
-        }
-      else
-        {
-          success = notify (owner, MYSERVER_LOG_EVT_LOG, 
static_cast<void*>(&message));
-        }
     }
   return success;
 }
@@ -256,24 +261,16 @@
   int success = 1;
   if (level >= this->level)
     {
-      success = 0;
+      success = 
+        notify (owner, type, MYSERVER_LOG_EVT_SET_MODE, 
static_cast<void*>(&level)) ||
+        notify (owner, type, MYSERVER_LOG_EVT_LOG, 
static_cast<void*>(&message));
       if (appendNL)
         {
-          ostringstream oss;
-          oss << message << endl;
-          message.assign (oss.str ());
+          LoggingLevel l = MYSERVER_LOG_MSG_PLAIN;
+          success |= 
+            (notify (owner, MYSERVER_LOG_EVT_SET_MODE, 
(static_cast<void*>(&l))) ||
+             notify (owner, MYSERVER_LOG_EVT_LOG, 
(static_cast<void*>(&newline))));
         }
-      if (level == MYSERVER_LOG_MSG_ERROR)
-        {
-          success = 
-            notify (owner, type, MYSERVER_LOG_EVT_ENTER_ERROR_MODE) ||
-            notify (owner, type, MYSERVER_LOG_EVT_LOG, 
static_cast<void*>(&message)) ||
-            notify (owner, type, MYSERVER_LOG_EVT_EXIT_ERROR_MODE);
-        }
-      else
-        {
-          success = notify (owner, type, MYSERVER_LOG_EVT_LOG, 
static_cast<void*>(&message));
-        }
     }
   return success;
 }
@@ -285,24 +282,16 @@
   int success = 1;
   if (level >= this->level)
     {
+      success = 
+        notify (owner, type, location, MYSERVER_LOG_EVT_SET_MODE, 
static_cast<void*>(&level)) ||
+        notify (owner, type, location, MYSERVER_LOG_EVT_LOG, 
static_cast<void*>(&message));
       if (appendNL)
         {
-          ostringstream oss;
-          oss << message << endl;
-          message.assign (oss.str ());
+          LoggingLevel l = MYSERVER_LOG_MSG_PLAIN;
+          success |=
+            (notify (owner, MYSERVER_LOG_EVT_SET_MODE, 
(static_cast<void*>(&l))) ||
+             notify (owner, MYSERVER_LOG_EVT_LOG, 
(static_cast<void*>(&newline))));
         }
-      if (level == MYSERVER_LOG_MSG_ERROR)
-        {
-          success = 
-            notify (owner, type, location, MYSERVER_LOG_EVT_ENTER_ERROR_MODE) 
||
-            notify (owner, type, location, MYSERVER_LOG_EVT_LOG, 
static_cast<void*>(&message)) ||
-            notify (owner, type, location, MYSERVER_LOG_EVT_EXIT_ERROR_MODE);
-        }
-      else
-        {
-          success = notify (owner, type, location, MYSERVER_LOG_EVT_LOG, 
-                            static_cast<void*>(&message));
-        }
     }
   return success;
 }

Modified: trunk/myserver/src/log/stream/console_stream.cpp
===================================================================
--- trunk/myserver/src/log/stream/console_stream.cpp    2008-11-08 10:34:53 UTC 
(rev 2947)
+++ trunk/myserver/src/log/stream/console_stream.cpp    2008-11-08 17:35:26 UTC 
(rev 2948)
@@ -17,26 +17,49 @@
 
 #include <include/log/stream/console_stream.h>
 
+/*!
+ * Default color values to use when outputting log messages over the
+ * console. Change them according to your tastes :)
+ *
+ * c[i][0] = Foreground color for the LoggingLevel `i'
+ * c[i][1] = Background color for the LoggingLevel `i'
+ */
+MyServerColor defaultColors[][2] =
+  {
+    {
+      MYSERVER_FG_COLOR_RESET, // } MYSERVER_LOG_MSG_PLAIN
+      MYSERVER_BG_COLOR_RESET  // } Don't modify this
+    },
+    {
+      MYSERVER_FG_COLOR_WHITE, // } MYSERVER_LOG_MSG_INFO
+      MYSERVER_BG_COLOR_BLACK  // }
+    },
+    {
+      MYSERVER_FG_COLOR_YELLOW, // } MYSERVER_LOG_MSG_WARNING
+      MYSERVER_BG_COLOR_BLACK   // }
+    },
+    {
+      MYSERVER_FG_COLOR_RED,   // } MYSERVER_LOG_MSG_ERROR
+      MYSERVER_BG_COLOR_BLACK  // }
+    }
+  };
+
 ConsoleStream::ConsoleStream (FiltersFactory* ff, u_long cycle, Stream* out,
                               FiltersChain* fc) :  
   LogStream (ff, cycle, out, fc)
 {
 }
 
+/*!
+ * Change the console text attributes according to the logging level.
+ * \param level The logging level.
+ * \return 0 on success, 1 on error.
+ */
 int
-ConsoleStream::enterErrorMode ()
+ConsoleStream::setMode (LoggingLevel level)
 {
   mutex->lock ();
-  int success = dynamic_cast<Console*>(out)->enterErrorMode ();
+  int success = dynamic_cast<Console*>(out)->setColor (defaultColors[level]);
   mutex->unlock ();
   return success;
 }
-
-int
-ConsoleStream::exitErrorMode ()
-{
-  mutex->lock ();
-  int success = dynamic_cast<Console*>(out)->exitErrorMode ();
-  mutex->unlock ();
-  return success;
-}

Modified: trunk/myserver/src/log/stream/log_stream.cpp
===================================================================
--- trunk/myserver/src/log/stream/log_stream.cpp        2008-11-08 10:34:53 UTC 
(rev 2947)
+++ trunk/myserver/src/log/stream/log_stream.cpp        2008-11-08 17:35:26 UTC 
(rev 2948)
@@ -153,16 +153,11 @@
         return chown (static_cast<int*>(message)[0], 
                       static_cast<int*>(message)[1]);
       }
-    case MYSERVER_LOG_EVT_ENTER_ERROR_MODE:
-      {
-        return enterErrorMode ();
-      }
       break;
-    case MYSERVER_LOG_EVT_EXIT_ERROR_MODE:
+    case MYSERVER_LOG_EVT_SET_MODE:
       {
-        return exitErrorMode ();
+        return setMode (*static_cast<LoggingLevel*>(message));
       }
-      break;
     default:
       return 1;
     }
@@ -229,13 +224,7 @@
 }
 
 int
-LogStream::enterErrorMode ()
+LogStream::setMode (LoggingLevel level)
 {
   return 0;
 }
-
-int
-LogStream::exitErrorMode ()
-{
-  return 0;
-}

Modified: trunk/myserver/tests/test_log_manager.cpp
===================================================================
--- trunk/myserver/tests/test_log_manager.cpp   2008-11-08 10:34:53 UTC (rev 
2947)
+++ trunk/myserver/tests/test_log_manager.cpp   2008-11-08 17:35:26 UTC (rev 
2948)
@@ -149,6 +149,7 @@
   {
     list<string> filters;
     lm->add (this, "test", "file://foo", filters, 0);
+    lm->setLevel (MYSERVER_LOG_MSG_WARNING);
     CPPUNIT_ASSERT (lm->log (this, "test", "a message", false, 
MYSERVER_LOG_MSG_INFO));
     CPPUNIT_ASSERT (!lm->log (this, "test", "a message", false, 
MYSERVER_LOG_MSG_ERROR));
     CPPUNIT_ASSERT (!lm->log (this, "test", "a message", false, 
MYSERVER_LOG_MSG_WARNING));






reply via email to

[Prev in Thread] Current Thread [Next in Thread]