changeset 38:452915ecd1f4

Basic logging functionality
author David Bryant <bagnose@gmail.com>
date Sun, 27 Sep 2009 22:51:03 +0930
parents bc0035393a81
children b6c34f1fc7f3
files doodle/cairo/routines.d doodle/core/logging.d doodle/dia/grid_layer.d doodle/fig/fig_layer.d doodle/main/prog/doodler.d doodle/tk/types.d
diffstat 6 files changed, 115 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/doodle/cairo/routines.d	Sun Aug 30 23:39:04 2009 +0930
+++ b/doodle/cairo/routines.d	Sun Sep 27 22:51:03 2009 +0930
@@ -14,3 +14,15 @@
     cr.moveTo(x0, y0);
     cr.lineTo(x1, y1);
 }
+
+// Horizontal line
+void hline(scope Context cr, in double y, double x0, double x1) {
+    cr.moveTo(x0, y);
+    cr.lineTo(x1, y);
+}
+
+// Vertical line
+void vline(scope Context cr, in double x, double y0, double y1) {
+    cr.moveTo(x, y0);
+    cr.lineTo(x, y1);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/doodle/core/logging.d	Sun Sep 27 22:51:03 2009 +0930
@@ -0,0 +1,87 @@
+module doodle.core.logging;
+
+private {
+    import std.stdio;
+    import std.typecons;
+}
+
+public {
+    void trace(string file = __FILE__, int line = __LINE__)(in string message) {
+        log(Severity.TRACE,   std.string.format("%s(%d):   TRACE: %s", right(file, 20), line, message));
+    }
+    void info(string file = __FILE__, int line = __LINE__)(in string message) {
+        log(Severity.INFO,    std.string.format("%s(%d):    INFO: %s", right(file, 20), line, message));
+    }
+    void message(string file = __FILE__, int line = __LINE__)(in string message) {
+        log(Severity.MESSAGE, std.string.format("%s(%d): MESSAGE: %s", right(file, 20), line, message));
+    }
+    void warning(string file = __FILE__, int line = __LINE__)(in string message) {
+        log(Severity.WARNING, std.string.format("%s(%d): WARNING: %s", right(file, 20), line, message));
+    }
+    void error(string file = __FILE__, int line = __LINE__)(in string message) {
+        log(Severity.ERROR,   std.string.format("%s(%d):   ERROR: %s", right(file, 20), line, message));
+    }
+    void fatal(string file = __FILE__, int line = __LINE__)(in string message) {
+        log(Severity.FATAL,   std.string.format("%s(%d):   FATAL: %s", right(file, 20), line, message));
+        assert(0);
+    }
+}
+
+private {
+    mixin(defineEnum!("Severity",
+                      "TRACE", "INFO", "MESSAGE", "WARNING", "ERROR", "FATAL"));
+
+    string mod_string(in Severity s) {
+        switch (s) {
+        case Severity.TRACE:
+            return modifier_string(Modifier.DIM) ~ fg_color_string(Color.CYAN);
+        case Severity.INFO:
+            return modifier_string(Modifier.UNDERLINE) ~ fg_color_string(Color.GREEN);
+        case Severity.MESSAGE:
+            return fg_color_string(Color.YELLOW);
+        case Severity.WARNING:
+            return modifier_string(Modifier.BRIGHT) ~ fg_color_string(Color.MAGENTA);
+        case Severity.ERROR:
+            return modifier_string(Modifier.BRIGHT) ~ fg_color_string(Color.RED);
+        case Severity.FATAL:
+            return modifier_string(Modifier.BRIGHT) ~ bg_color_string(Color.RED) ~ fg_color_string(Color.WHITE);
+        }
+        assert(0);
+    }
+
+    void log(in Severity severity, in string message) {
+        write(mod_string(severity));
+        write(message);
+        writeln(modifier_string(Modifier.RESET));
+    }
+
+    enum Modifier {
+        RESET     = 0,
+        BRIGHT    = 1,
+        DIM       = 2,      // does nothing in gnome-terminal
+        UNDERLINE = 3,      // does nothing in gnome-terminal
+        BLINK     = 5,      // does nothing in gnome-terminal
+        REVERSE   = 7,
+        HIDDEN    = 8
+    }
+
+    enum Color {
+        BLACK   = 0,
+        RED     = 1,
+        GREEN   = 2,
+        YELLOW  = 3,
+        BLUE    = 4,
+        MAGENTA = 5,
+        CYAN    = 6,
+        WHITE   = 7
+    }
+
+    string modifier_string(Modifier m) { return std.string.format("\033[%dm", 0 + m); }
+    string fg_color_string(Color c)    { return std.string.format("\033[%dm", 30 + c); }
+    string bg_color_string(Color c)    { return std.string.format("\033[%dm", 40 + c); }
+
+    private const(char)[] right(in char[] str, int n) {
+        int pos = str.length < n ? 0 : str.length - n;
+        return str[pos..$];
+    }
+}
--- a/doodle/dia/grid_layer.d	Sun Aug 30 23:39:04 2009 +0930
+++ b/doodle/dia/grid_layer.d	Sun Sep 27 22:51:03 2009 +0930
@@ -51,7 +51,7 @@
                 double y1 = model_damage.max_corner.y;
 
                 for (;;) {
-                    line(model_cr, x, y0, x, y1);
+                    vline(model_cr, x, y0, y1);
 
                     // Ensure 1 pixel wide FIXME is this naughty? We are sneaking
                     // through cairo to mix model and pixel coordinates...
@@ -75,7 +75,7 @@
                 double x1 = model_damage.max_corner.x;
 
                 for (;;) {
-                    line(model_cr, x0, y, x1, y);
+                    hline(model_cr, y, x0, x1);
 
                     model_cr.save(); {
                         model_cr.scale(1.0 / xx, 1.0 / yy);
--- a/doodle/fig/fig_layer.d	Sun Aug 30 23:39:04 2009 +0930
+++ b/doodle/fig/fig_layer.d	Sun Sep 27 22:51:03 2009 +0930
@@ -9,6 +9,8 @@
         super(name);
     }
 
+    // Layer overrides:
+
     override Rectangle bounds() const {
         return Rectangle.DEFAULT;
     }
--- a/doodle/main/prog/doodler.d	Sun Aug 30 23:39:04 2009 +0930
+++ b/doodle/main/prog/doodler.d	Sun Sep 27 22:51:03 2009 +0930
@@ -1,4 +1,6 @@
 private {
+    import doodle.core.logging;
+
     import doodle.dia.tool_layer;
     import doodle.dia.standard_tools;
     import doodle.dia.grid_layer;
@@ -15,6 +17,15 @@
 }
 
 void main(string[] args) {
+    /+
+    trace("Test trace");
+    info("Test trace");
+    message("Test trace");
+    warning("Test trace");
+    error("Test trace");
+    fatal("Test trace");
+    +/
+
     Main.init(args);
     auto window = new MainWindow("Doodle");
     auto vbox = new VBox(false, 0);
--- a/doodle/tk/types.d	Sun Aug 30 23:39:04 2009 +0930
+++ b/doodle/tk/types.d	Sun Sep 27 22:51:03 2009 +0930
@@ -30,7 +30,7 @@
         }
     }
 
-    string toString() {
+    string toString() const {
         string s;
 
         // FIXME this is terrible