Using string mixins for logging

From D Wiki
Revision as of 16:53, 13 December 2012 by R m r (talk | contribs) (edit functions for readability)
Jump to: navigation, search
import std.traits : PTT=ParameterTypeTuple;
import std.stdio : stderr;
import std.datetime : Clock;
 
template StringOf(TS...)
{
    static if(TS.length == 0)
    {
        enum StringOf = "";
    }
    else static if(TS.length == 1)
    {
        enum StringOf = (TS[0]).stringof;
    }
    else
    {
        enum StringOf = (TS[0]).stringof ~ "," ~ StringOf!(TS[1..$]) ;
    }
}
 
template ArgStringOf(TS...)
{
    static if(TS.length == 0)
    {
        enum ArgStringOf = "";
    }
    else static if(TS.length == 1)
    {
        enum ArgStringOf = TS[0];
    }
    else
    {
        enum ArgStringOf = TS[0] ~ "," ~ ArgStringOf!(TS[1..$]);
    }
}
 
template info(PS...)
{
    //pragma(msg, info);
    const char[] info = `static if(!__traits(compiles, __FUNCTION__ == ""))` ~ 
                        `{ immutable string __FUNCTION__ = __traits(identifier, __traits(parent, {})) ~` ~
                                                          `"(" ~ StringOf!(PTT!(__traits(parent, {}))) ~ ")";` ~
                        `} stderr.writeln(Clock.currTime(), " [INFO] ", __FILE__, '(', __LINE__, "): ",` ~
                                          `__FUNCTION__, ": ",` ~ ArgStringOf!(PS) ~ `);` ;
}
 
mixin template set_func_name(string fn)
{	
    immutable string __FUNCTION__ = fn;
}
 
auto foo(T)(in T val)
    if(is(T==int) || is(T==string))
{
    mixin set_func_name!("foo(" ~ T.stringof ~ ")"); //workaround for fwd ref issue
    mixin (info!(`"started"`));
    mixin (info!(`"stopped"`));
 
    static if (is(T==int))
        return 1.0123;
    else static if(is(T==string))
        return true;
    else
        static assert(false, "need int or string");	
}

void baz()
{
    mixin (info!(`"called"`));
}

void main(string[] args)
{
    mixin (info!(`"started"`));
 
    mixin (info!(`"foo(1) returned: "`, `foo(1)`));
    mixin (info!(`"foo(\"bar\") returned: "`, `foo("bar")`));
    mixin (info!(`"calling baz()"`));
    baz();
    mixin (info!(`"stopped"`));	
}

▶ Run code

Sample Output

2012-Dec-13 16:48:05.1691321 [INFO] /home/c613/c859.d(74): main(string[]): started
2012-Dec-13 16:48:05.1692541 [INFO] /home/c613/c859.d(56): foo(int): started
2012-Dec-13 16:48:05.169285 [INFO] /home/c613/c859.d(57): foo(int): stopped
2012-Dec-13 16:48:05.1692519 [INFO] /home/c613/c859.d(76): main(string[]): foo(1) returned: 1.0123
2012-Dec-13 16:48:05.1693905 [INFO] /home/c613/c859.d(56): foo(string): started
2012-Dec-13 16:48:05.1694197 [INFO] /home/c613/c859.d(57): foo(string): stopped
2012-Dec-13 16:48:05.1693903 [INFO] /home/c613/c859.d(77): main(string[]): foo("bar") returned: true
2012-Dec-13 16:48:05.169471 [INFO] /home/c613/c859.d(78): main(string[]): calling baz()
2012-Dec-13 16:48:05.1694954 [INFO] /home/c613/c859.d(69): baz(): called
2012-Dec-13 16:48:05.1695198 [INFO] /home/c613/c859.d(80): main(string[]): stopped