changeset 178:62aa8845edd2

Coloured log output to the console.
author Diggory Hardy <diggory.hardy@gmail.com>
date Tue, 15 Sep 2009 10:36:37 +0200
parents af40e9679436
children 1f9d00f392bd
files data/L10n/en-GB.mtt mde/setup/Init.d mde/setup/LogLayout.d mde/setup/logger.d
diffstat 4 files changed, 64 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/data/L10n/en-GB.mtt	Sat Sep 12 09:50:33 2009 +0200
+++ b/data/L10n/en-GB.mtt	Tue Sep 15 10:36:37 2009 +0200
@@ -42,6 +42,7 @@
 <entry|logOutput.console={0:"Console"}>
 <entry|logOutput.file={0:"Log-file"}>
 <entry|logOutput.both={0:"Console and log-file"}>
+<entry|logColour={0:"Coloured logging",1:"Console log output is coloured"}>
 <entry|l10n={0:"Localisation",1:"Specifies the language to use."}>
 <entry|pollInterval={0:"Polling interval",1:"Delay in main loop to limit CPU usage"}>
 <entry|exitImmediately={0:"Exit immediately",1:"Load files and exit immediately, without running main loop (for debugging)"}>
--- a/mde/setup/Init.d	Sat Sep 12 09:50:33 2009 +0200
+++ b/mde/setup/Init.d	Tue Sep 15 10:36:37 2009 +0200
@@ -43,6 +43,7 @@
 import mde.setup.InitStage;     // Controls external delegates run by init
 import mde.setup.exception;
 import mde.setup.logger;
+import mde.setup.LogLayout;
 
 import mde.content.AStringContent;
 import mde.content.ContentLoader;
@@ -89,6 +90,7 @@
                                     ["Trace", "Info", "Warn", "Error", "Fatal", "None"]);
         logOutput = new EnumContent ("MiscOptions.logOutput",
                                      ["both", "file", "console", "none"]);
+	logColour = new BoolContent ("MiscOptions.logColour");
 	
 	// Callback to set the logging level on change:
 	logLevel.addCallback (&setLogLevel);
@@ -160,6 +162,9 @@
 	    // Where logging is done to is determined at compile-time, currently just via static ifs.
             root = Log.root;
 	    root.clear;			// we may no longer want to log to the console
+	    Appender.Layout layout = null;
+	    if (logColour())
+		layout = new LayoutTimerColour();
 	    
 	    // logOutput == 0 enables both outputs, in case options aren't read
             if (!(logOutput() & 2)) {     // first appender so root seperator messages don't show on console
@@ -169,7 +174,7 @@
                 root.append (Level.None, "");
             }
             if (!(logOutput() & 1))
-                root.add(new AppendConsole);
+                root.add(new AppendConsole (layout));
         } catch (Exception e) {
             // Presumably it was only adding a file appender which failed; set up a new console
             // logger and if that fails let the exception kill the program.
@@ -399,7 +404,7 @@
     
     private static {
         Logger logger;
-        BoolContent exitImmediately;
+        BoolContent exitImmediately, logColour;
         IntContent maxThreads;
         EnumContent logLevel, logOutput;
         
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mde/setup/LogLayout.d	Tue Sep 15 10:36:37 2009 +0200
@@ -0,0 +1,54 @@
+/* LICENSE BLOCK
+Part of mde: a Modular D game-oriented Engine
+Copyright © 2007-2008 Diggory Hardy
+
+This program is free software: you can redistribute it and/or modify it under the terms
+of the GNU General Public License as published by the Free Software Foundation, either
+version 2 of the License, or (at your option) any later version.
+
+This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+See the GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>. */
+
+/**************************************************************************************************
+ * Provides coloured log output.
+ *************************************************************************************************/
+module mde.setup.LogLayout;
+
+import tango.util.log.Log;
+
+public class LayoutTimerColour : Appender.Layout
+{
+        this () {
+	    colour = ["\033[0;37m", "\033[0;0m", "\033[0;33m", "\033[0;31m", "\033[0;35m"];
+	    bold   = ["\033[1;30m", "\033[1;1m", "\033[1;33m", "\033[1;31m", "\033[1;35m"];
+	}
+	
+        void format (LogEvent event, size_t delegate(void[]) dg)
+        {
+                char[20] tmp = void;
+		size_t i = cast(size_t)event.level;
+		if (i >= 5) i = 1;
+		
+		dg ("\033[1;97m");
+                dg (event.toMilli (tmp, event.span));
+                dg ("\t");
+//                dg (Thread.getThis.name);
+//                dg (" ");
+                dg (bold[i]);
+		dg (event.levelName);
+		dg (colour[i]);
+                dg (event.name);
+                dg (event.host.context.label);
+		dg (bold[i]);
+		dg (" - ");
+		dg (colour[i]);
+                dg (event.toString);
+		dg ("\033[0;00m");
+        }
+	
+	char[][] colour, bold;
+}
--- a/mde/setup/logger.d	Sat Sep 12 09:50:33 2009 +0200
+++ b/mde/setup/logger.d	Tue Sep 15 10:36:37 2009 +0200
@@ -21,6 +21,7 @@
  *****************************************************************************/
 module mde.setup.logger;
 
+import mde.setup.LogLayout;
 import tango.util.log.Log;
 import tango.util.log.AppendConsole;
 
@@ -29,5 +30,5 @@
     Logger root = Log.root;
     debug root.level(Logger.Trace);
     else  root.level(Logger.Info);
-    root.add(new AppendConsole);
+    root.add(new AppendConsole (new LayoutTimerColour()));
 }