view doodle/core/logging.d @ 38:452915ecd1f4

Basic logging functionality
author David Bryant <bagnose@gmail.com>
date Sun, 27 Sep 2009 22:51:03 +0930
parents
children 1f97022e5c6d
line wrap: on
line source

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..$];
    }
}