[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[myserver-commit] [3004] Set up console colors through the `myserver.xml
From: |
Francesco Pipita |
Subject: |
[myserver-commit] [3004] Set up console colors through the `myserver.xml' configuration file. |
Date: |
Wed, 18 Feb 2009 11:06:57 +0000 |
Revision: 3004
http://svn.sv.gnu.org/viewvc/?view=rev&root=myserver&revision=3004
Author: francesco_pipita
Date: 2009-02-18 11:06:56 +0000 (Wed, 18 Feb 2009)
Log Message:
-----------
Set up console colors through the `myserver.xml' configuration file.
Modified Paths:
--------------
trunk/myserver/binaries/myserver.xml.default
trunk/myserver/binaries/myserver.xml.unix.default
trunk/myserver/documentation/log_management.texi
trunk/myserver/include/filter/console.h
trunk/myserver/include/log/log_manager.h
trunk/myserver/include/log/stream/console_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/server/server.cpp
Modified: trunk/myserver/binaries/myserver.xml.default
===================================================================
--- trunk/myserver/binaries/myserver.xml.default 2009-02-16 13:32:19 UTC
(rev 3003)
+++ trunk/myserver/binaries/myserver.xml.default 2009-02-18 11:06:56 UTC
(rev 3004)
@@ -50,6 +50,10 @@
<!--DEFINE THE MAX NUMBER OF CONNECTIONS TO ACCEPT.-->
<MAX_CONNECTIONS_TO_ACCEPT>0</MAX_CONNECTIONS_TO_ACCEPT>
+ <!-- TEXT AND BACKGROUND ATTRIBUTES FOR CONSOLE LOG MESSAGES. -->
+ <CONSOLE_COLORS info_fg="white" info_bg="black" warning_fg="yellow"
+ warning_bg="black" error_fg="red" error_bg="black"/>
+
<!--MAX SIZE OF THE LOG FILE IN BYTES -->
<MAX_LOG_FILE_SIZE>1048576</MAX_LOG_FILE_SIZE>
Modified: trunk/myserver/binaries/myserver.xml.unix.default
===================================================================
--- trunk/myserver/binaries/myserver.xml.unix.default 2009-02-16 13:32:19 UTC
(rev 3003)
+++ trunk/myserver/binaries/myserver.xml.unix.default 2009-02-18 11:06:56 UTC
(rev 3004)
@@ -50,6 +50,10 @@
<!--DEFINE THE MAX NUMBER OF CONNECTIONS TO ACCEPT.-->
<MAX_CONNECTIONS_TO_ACCEPT>0</MAX_CONNECTIONS_TO_ACCEPT>
+ <!-- TEXT AND BACKGROUND ATTRIBUTES FOR CONSOLE LOG MESSAGES. -->
+ <CONSOLE_COLORS info_fg="white" info_bg="black" warning_fg="yellow"
+ warning_bg="black" error_fg="red" error_bg="black"/>
+
<!--MAX SIZE OF THE LOG FILE IN BYTES -->
<MAX_LOG_FILE_SIZE>1048576</MAX_LOG_FILE_SIZE>
Modified: trunk/myserver/documentation/log_management.texi
===================================================================
--- trunk/myserver/documentation/log_management.texi 2009-02-16 13:32:19 UTC
(rev 3003)
+++ trunk/myserver/documentation/log_management.texi 2009-02-18 11:06:56 UTC
(rev 3004)
@@ -13,17 +13,17 @@
features.
@subsection Location strings
-A location string must be provided in the form
address@hidden://resource}, where @code{protocol} can ben choosen
-between :
+A location string can be provided as an URI; the basic schema is in
+the form @code{protocol://resource}. @code{protocol} can be chosen
+between
@enumerate
@item @code{console}
@item @code{file}
@item @code{socket}
@end enumerate
-while @code{resource} must be a valid filename or URL according to its
address@hidden The @code{console} protocol, can take as its
address@hidden argument, either @code{stdout} or @code{stderr}.
+and @code{resource} can be either a valid filename or an hostname according
+to its @code{protocol}. The @code{console} protocol, can take as its
address@hidden argument, either @code{stdout} or @code{stderr}.
@subsubsection Location string examples
@enumerate
@@ -62,7 +62,7 @@
the accesses log configuration, contains some @code{STREAM} nodes, and
each @code{STREAM} contains two attributes, the mandatory
@code{location} and the optional @code{cycle}. A @code{STREAM} node, can
-also contain optional @code{FILTER} subnodes, one for each filter that
+also contain optional @code{FILTER} sub-nodes, one for each filter that
we want to add to the @code{STREAM}.
@example
<ACCESSLOG type="combined">
@@ -90,5 +90,28 @@
<STREAM location="file://MyServerHTTP.err"/>
</WARNINGLOG>
@end example
-If a wrong filter is specified, its relative @code{STREAM} willn't
+If a wrong filter is specified, its relative @code{STREAM} will not
be added.
+
address@hidden Console colors
+It is possible to set color attributes for the text written over the
+console. In this way, messages can look different according to the
+logging level which they belong to.
+For example, you may wish to write error messages with a red text over a
+black background, info messages with a white text over a black
+background and warning messages with a yellow text over a black
+background. You can easily tell MyServer about that information by
+editing the @file{myserver.xml} configuration file and adding the
+following code:
address@hidden
+<CONSOLE_COLORS info_fg="white" info_bg="black" warning_fg="yellow"
+ warning_bg="black" error_fg="red" error_bg="black"/>
address@hidden example
+Allowed values for colors are @code{black}, @code{red}, @code{green},
address@hidden, @code{blue}, @code{magenta}, @code{cyan} and
address@hidden The two common suffixes @code{fg} and @code{bg} represent
+respectively the foreground attribute and the background one, while the
+three prefixes, @code{info}, @code{warning} and @code{error} represent
+kinds of logging messages managed by MyServer.
+If the @code{CONSOLE_COLORS} information is missing or it is partially
+provided, MyServer will use default values.
Modified: trunk/myserver/include/filter/console.h
===================================================================
--- trunk/myserver/include/filter/console.h 2009-02-16 13:32:19 UTC (rev
3003)
+++ trunk/myserver/include/filter/console.h 2009-02-18 11:06:56 UTC (rev
3004)
@@ -20,44 +20,12 @@
#define CONSOLE_H
#include <iostream>
+#include <map>
#include <include/filter/stream.h>
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,
- MYSERVER_FG_COLOR_NONE,
- /* 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,
- MYSERVER_BG_COLOR_NONE
- };
-
-#ifdef WIN32
-extern WORD colors[];
-#endif
-#ifdef NOT_WIN
-extern char const* colors[];
-#endif
-
class Console : public Stream
{
public:
@@ -67,11 +35,18 @@
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 openConsole (string fd);
- int setColor (MyServerColor color[]);
+ int setColor (string fg_color, string bg_color);
int reset ();
protected:
- int checkColors (MyServerColor c[]);
ostream* fd;
+#ifdef WIN32
+ map<string, WORD> fg_colors;
+ map<string, WORD> bg_colors;
+#endif
+#ifdef NOT_WIN
+ map<string, string> fg_colors;
+ map<string, string> bg_colors;
+#endif
};
#endif
Modified: trunk/myserver/include/log/log_manager.h
===================================================================
--- trunk/myserver/include/log/log_manager.h 2009-02-16 13:32:19 UTC (rev
3003)
+++ trunk/myserver/include/log/log_manager.h 2009-02-18 11:06:56 UTC (rev
3004)
@@ -72,7 +72,9 @@
int count (void* owner, string type, string location);
int clear ();
int getOwnersList (string, list<void*>*);
-protected:
+ list<string> getLoggingLevelsByNames ();
+ map<LoggingLevel, string>& getLoggingLevels () { return loggingLevels; }
+private:
int notify (void* owner, string type, string location, LogStreamEvent evt,
void* msg = 0, void* reply = 0);
int notify (void* owner, string type, LogStreamEvent evt, void* msg = 0,
@@ -82,8 +84,9 @@
int add (void* owner, string type);
int add (void* owner, string type, string location, LogStream* ls);
int computeNewLine ();
+ void associateLoggingLevelsWithNames ();
int logWriteln (string, LoggingLevel);
-private:
+
LoggingLevel level;
Mutex* mutex;
LogStreamFactory* lsf;
@@ -110,6 +113,11 @@
* Store the newline string for the host operating system.
*/
string newline;
+
+ /*!
+ * Associate each LoggingLevel with its string representation.
+ */
+ map<LoggingLevel, string> loggingLevels;
};
#endif
Modified: trunk/myserver/include/log/stream/console_stream.h
===================================================================
--- trunk/myserver/include/log/stream/console_stream.h 2009-02-16 13:32:19 UTC
(rev 3003)
+++ trunk/myserver/include/log/stream/console_stream.h 2009-02-18 11:06:56 UTC
(rev 3004)
@@ -20,13 +20,12 @@
#define CONSOLE_STREAM_H
#include <list>
+#include <map>
#include <string>
#include <include/log/stream/log_stream.h>
#include <include/filter/console.h>
-extern MyServerColor defaultColors[][2];
-
class ConsoleStream : public LogStream
{
public:
Modified: trunk/myserver/include/server/server.h
===================================================================
--- trunk/myserver/include/server/server.h 2009-02-16 13:32:19 UTC (rev
3003)
+++ trunk/myserver/include/server/server.h 2009-02-18 11:06:56 UTC (rev
3004)
@@ -160,7 +160,10 @@
AuthMethodFactory *getAuthMethodFactory () {return &authMethodFactory;}
ValidatorFactory *getValidatorFactory (){return &validatorFactory;}
+ map<string, string>& getConsoleColors () { return consoleColors; }
+ LogManager* getLogManager () { return logManager; }
+
private:
friend class ClientsThread;
#ifdef WIN32
@@ -262,6 +265,7 @@
AuthMethodFactory authMethodFactory;
ValidatorFactory validatorFactory;
SecurityManager securityManager;
+ map<string, string> consoleColors;
};
#endif
Modified: trunk/myserver/src/filter/console.cpp
===================================================================
--- trunk/myserver/src/filter/console.cpp 2009-02-16 13:32:19 UTC (rev
3003)
+++ trunk/myserver/src/filter/console.cpp 2009-02-18 11:06:56 UTC (rev
3004)
@@ -17,64 +17,51 @@
#include <include/filter/console.h>
+Console::Console () : Stream ()
+{
+ fd = 0;
#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
- -1, // None
- /* 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
- -1 // None
- };
+ fg_colors["black"] = 0;
+ fg_colors["red"] = FOREGROUND_RED;
+ fg_colors["green"] = FOREGROUND_GREEN;
+ fg_colors["yellow"] = FOREGROUND_RED | FOREGROUND_GREEN;
+ fg_colors["blue"] = FOREGROUND_BLUE;
+ fg_colors["magenta"] = FOREGROUND_RED | FOREGROUND_BLUE;
+ fg_colors["cyan"] = FOREGROUND_BLUE | FOREGROUND_GREEN;
+ fg_colors["white"] = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
+ fg_colors["reset"] = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
+
+ bg_colors["black"] = 0;
+ bg_colors["red"] = BACKGROUND_RED;
+ bg_colors["green"] = BACKGROUND_GREEN;
+ bg_colors["yellow"] = BACKGROUND_RED | BACKGROUND_GREEN;
+ bg_colors["blue"] = BACKGROUND_BLUE;
+ bg_colors["magenta"] = BACKGROUND_RED | BACKGROUND_BLUE;
+ bg_colors["cyan"] = BACKGROUND_BLUE | BACKGROUND_GREEN;
+ bg_colors["white"] = BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
+ bg_colors["reset"] = 0;
#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
- "NO_COLOR", // None
- /* 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
- "NO_COLOR" // None
- };
-#endif
+ fg_colors["black"] = "\033[30m";
+ fg_colors["red"] = "\033[31m";
+ fg_colors["green"] = "\033[32m";
+ fg_colors["yellow"] = "\033[33m";
+ fg_colors["blue"] = "\033[34m";
+ fg_colors["magenta"] = "\033[35m";
+ fg_colors["cyan"] = "\033[36m";
+ fg_colors["white"] = "\033[37m";
+ fg_colors["reset"] = "\033[0m";
-Console::Console () : Stream ()
-{
- fd = 0;
+ bg_colors["black"] = "\033[40m";
+ bg_colors["red"] = "\033[41m";
+ bg_colors["green"] = "\033[42m";
+ bg_colors["yellow"] = "\033[43m";
+ bg_colors["blue"] = "\033[44m";
+ bg_colors["magenta"] = "\033[45m";
+ bg_colors["cyan"] = "033[46m";
+ bg_colors["white"] = "\033[47m";
+ bg_colors["reset"] = "\033[0m";
+#endif
}
Console::~Console ()
@@ -135,52 +122,50 @@
}
/*!
- * 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::checkColors (MyServerColor c[])
-{
- return
- c[0] < MYSERVER_FG_COLOR_BLACK ||
- c[0] > MYSERVER_FG_COLOR_NONE ||
- c[1] < MYSERVER_BG_COLOR_BLACK ||
- c[1] > MYSERVER_BG_COLOR_NONE;
-}
-
-/*!
- * Set the attributes for the console text.
- * \param c c[0] holds the foreground color, c[1] the background one.
+ * Set the attributes for the console text. If a not valid color is provided,
+ * the console attribute for both background and foreground text will be
+ * reset.
+ * \param fg_color the foreground text attribute.
+ * \param bg_color the background text attribute.
* \return 0 on success, 1 on error.
*/
int
-Console::setColor (MyServerColor c[])
+Console::setColor (string fg_color, string bg_color)
{
- if (!checkColors (c))
- {
#ifdef WIN32
- WORD attrs =
- ((c[0] != MYSERVER_FG_COLOR_NONE) ? colors[c[0]] : 0) |
- ((c[1] != MYSERVER_BG_COLOR_NONE) ? colors[c[1]] : 0);
- DWORD nStdHandle = (fd == &cout) ? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE;
- HANDLE h = GetStdHandle (nStdHandle);
- SetConsoleTextAttribute (attrs, h);
+ WORD attrs;
+ DWORD nStdHandle;
+ HANDLE h;
+ if (fg_colors.count (fg_color) && bg_colors.count (bg_color))
+ {
+ attrs = fg_colors[fg_color] | bg_colors[bg_color];
+ }
+ else
+ {
+ attrs = fg_colors["reset"] | bg_colors["reset"];
+ }
+ if (fd == &cout)
+ {
+ nStdHandle = STD_OUTPUT_HANDLE;
+ }
+ else
+ {
+ nStdHandle = STD_ERROR_HANDLE;
+ }
+ h = GetStdHandle (nStdHandle);
+ SetConsoleTextAttribute (attrs, h);
#endif
#ifdef NOT_WIN
- if (c[0] != MYSERVER_FG_COLOR_NONE)
- {
- *fd << colors[c[0]];
- }
- if (c[1] != MYSERVER_BG_COLOR_NONE)
- {
- *fd << colors[c[1]];
- }
+ if (fg_colors.count (fg_color) && bg_colors.count (bg_color))
+ {
+ *fd << fg_colors[fg_color].c_str () << bg_colors[bg_color].c_str ();
+ }
+ else
+ {
+ *fd << fg_colors["reset"].c_str () << bg_colors["reset"].c_str ();
+ }
#endif
return 0;
- }
- return 1;
}
/*!
@@ -191,6 +176,5 @@
int
Console::reset ()
{
- MyServerColor c[] = { MYSERVER_FG_COLOR_RESET, MYSERVER_BG_COLOR_RESET };
- return setColor (c);
+ return setColor ("reset", "reset");
}
Modified: trunk/myserver/src/log/log_manager.cpp
===================================================================
--- trunk/myserver/src/log/log_manager.cpp 2009-02-16 13:32:19 UTC (rev
3003)
+++ trunk/myserver/src/log/log_manager.cpp 2009-02-18 11:06:56 UTC (rev
3004)
@@ -32,6 +32,7 @@
mutex = new Mutex ();
mutex->init ();
computeNewLine ();
+ associateLoggingLevelsWithNames ();
}
/*!
@@ -957,3 +958,27 @@
}
return 1;
}
+
+/*!
+ * \return a list of strings each representing one of logging levels that
+ * MyServer understands.
+ */
+list<string>
+LogManager::getLoggingLevelsByNames ()
+{
+ list<string> l;
+ for (map<LoggingLevel, string>::iterator it = loggingLevels.begin ();
+ it != loggingLevels.end (); it++)
+ {
+ l.push_back (it->second);
+ }
+ return l;
+}
+
+void
+LogManager::associateLoggingLevelsWithNames ()
+{
+ loggingLevels[MYSERVER_LOG_MSG_INFO] = "info";
+ loggingLevels[MYSERVER_LOG_MSG_WARNING] = "warning";
+ loggingLevels[MYSERVER_LOG_MSG_ERROR] = "error";
+}
Modified: trunk/myserver/src/log/stream/console_stream.cpp
===================================================================
--- trunk/myserver/src/log/stream/console_stream.cpp 2009-02-16 13:32:19 UTC
(rev 3003)
+++ trunk/myserver/src/log/stream/console_stream.cpp 2009-02-18 11:06:56 UTC
(rev 3004)
@@ -16,34 +16,8 @@
*/
#include <include/log/stream/console_stream.h>
+#include <include/server/server.h>
-/*!
- * Default color values to use when outputting log messages over the
- * console. Change them according to your tastes :)
- *
- * defaultColors[i][0] = Foreground color for the LoggingLevel `i'
- * defaultColors[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_NONE // }
- },
- {
- MYSERVER_FG_COLOR_YELLOW, // } MYSERVER_LOG_MSG_WARNING
- MYSERVER_BG_COLOR_NONE // }
- },
- {
- MYSERVER_FG_COLOR_RED, // } MYSERVER_LOG_MSG_ERROR
- MYSERVER_BG_COLOR_NONE // }
- }
- };
-
ConsoleStream::ConsoleStream (FiltersFactory* ff, u_long cycle, Stream* out,
FiltersChain* fc) :
LogStream (ff, cycle, out, fc)
@@ -59,7 +33,22 @@
ConsoleStream::setMode (LoggingLevel level)
{
mutex->lock ();
- int success = dynamic_cast<Console*>(out)->setColor (defaultColors[level]);
+ int success = 1;
+ Console* c = dynamic_cast<Console*>(out);
+ if (level == MYSERVER_LOG_MSG_PLAIN)
+ {
+ success = c->setColor ("reset", "reset");
+ }
+ else
+ {
+ Server* server = Server::getInstance ();
+ LogManager* lm = server->getLogManager ();
+ map<string, string> userColors = server->getConsoleColors ();
+ map<LoggingLevel, string> levels = lm->getLoggingLevels ();
+ string fg_color = userColors[levels[level] + "_fg"];
+ string bg_color = userColors[levels[level] + "_bg"];
+ success = c->setColor (fg_color, bg_color);
+ }
mutex->unlock ();
return success;
}
Modified: trunk/myserver/src/server/server.cpp
===================================================================
--- trunk/myserver/src/server/server.cpp 2009-02-16 13:32:19 UTC (rev
3003)
+++ trunk/myserver/src/server/server.cpp 2009-02-18 11:06:56 UTC (rev
3004)
@@ -983,7 +983,6 @@
*/
int Server::initialize()
{
- initLogManager ();
char *data;
#ifdef WIN32
envString = GetEnvironmentStrings();
@@ -1011,6 +1010,28 @@
if(configurationFileManager.open(mainConfigurationFile->c_str()))
return -1;
+ /*
+ * Process console colors information.
+ */
+ list<string> levels = logManager->getLoggingLevelsByNames ();
+ for (list<string>::iterator it = levels.begin (); it != levels.end (); it++)
+ {
+ string fg (*it + "_fg");
+ string bg (*it + "_bg");
+ data = configurationFileManager.getAttr ("CONSOLE_COLORS", fg.c_str ());
+ if (data)
+ {
+ consoleColors[fg] = string (data);
+ }
+ data = configurationFileManager.getAttr ("CONSOLE_COLORS", bg.c_str ());
+ if (data)
+ {
+ consoleColors[bg] = string (data);
+ }
+ }
+
+ initLogManager ();
+
data = configurationFileManager.getValue("LANGUAGE");
if(languageFile)
delete languageFile;
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [myserver-commit] [3004] Set up console colors through the `myserver.xml' configuration file.,
Francesco Pipita <=