Difference between revisions of "Using string mixins for logging"
(Adding code and dpaste link) |
m (edit functions for readability) |
||
Line 3: | Line 3: | ||
import std.stdio : stderr; | import std.stdio : stderr; | ||
import std.datetime : Clock; | import std.datetime : Clock; | ||
− | + | ||
template StringOf(TS...) | template StringOf(TS...) | ||
{ | { | ||
Line 9: | Line 9: | ||
{ | { | ||
enum StringOf = ""; | enum StringOf = ""; | ||
+ | } | ||
+ | else static if(TS.length == 1) | ||
+ | { | ||
+ | enum StringOf = (TS[0]).stringof; | ||
} | } | ||
else | else | ||
{ | { | ||
− | enum StringOf = (TS[0]).stringof ~ | + | enum StringOf = (TS[0]).stringof ~ "," ~ StringOf!(TS[1..$]) ; |
} | } | ||
} | } | ||
− | + | ||
template ArgStringOf(TS...) | template ArgStringOf(TS...) | ||
{ | { | ||
Line 21: | Line 25: | ||
{ | { | ||
enum ArgStringOf = ""; | enum ArgStringOf = ""; | ||
+ | } | ||
+ | else static if(TS.length == 1) | ||
+ | { | ||
+ | enum ArgStringOf = TS[0]; | ||
} | } | ||
else | else | ||
{ | { | ||
− | enum ArgStringOf = TS[0] ~ | + | enum ArgStringOf = TS[0] ~ "," ~ ArgStringOf!(TS[1..$]); |
} | } | ||
} | } | ||
− | + | ||
template info(PS...) | template info(PS...) | ||
{ | { | ||
Line 37: | Line 45: | ||
`__FUNCTION__, ": ",` ~ ArgStringOf!(PS) ~ `);` ; | `__FUNCTION__, ": ",` ~ ArgStringOf!(PS) ~ `);` ; | ||
} | } | ||
− | + | ||
mixin template set_func_name(string fn) | mixin template set_func_name(string fn) | ||
{ | { | ||
immutable string __FUNCTION__ = fn; | immutable string __FUNCTION__ = fn; | ||
} | } | ||
− | + | ||
auto foo(T)(in T val) | auto foo(T)(in T val) | ||
if(is(T==int) || is(T==string)) | if(is(T==int) || is(T==string)) | ||
Line 49: | Line 57: | ||
mixin (info!(`"started"`)); | mixin (info!(`"started"`)); | ||
mixin (info!(`"stopped"`)); | mixin (info!(`"stopped"`)); | ||
− | + | ||
static if (is(T==int)) | static if (is(T==int)) | ||
return 1.0123; | return 1.0123; | ||
Line 57: | Line 65: | ||
static assert(false, "need int or string"); | static assert(false, "need int or string"); | ||
} | } | ||
− | + | ||
+ | void baz() | ||
+ | { | ||
+ | mixin (info!(`"called"`)); | ||
+ | } | ||
+ | |||
void main(string[] args) | void main(string[] args) | ||
{ | { | ||
mixin (info!(`"started"`)); | mixin (info!(`"started"`)); | ||
− | + | ||
mixin (info!(`"foo(1) returned: "`, `foo(1)`)); | mixin (info!(`"foo(1) returned: "`, `foo(1)`)); | ||
mixin (info!(`"foo(\"bar\") returned: "`, `foo("bar")`)); | mixin (info!(`"foo(\"bar\") returned: "`, `foo("bar")`)); | ||
− | + | mixin (info!(`"calling baz()"`)); | |
+ | baz(); | ||
mixin (info!(`"stopped"`)); | mixin (info!(`"stopped"`)); | ||
} | } | ||
− | |||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | [http://dpaste.dzfl.pl/3ff42a83 ▶ Run code] | ||
− | [ | + | ==Sample Output== |
+ | <pre> | ||
+ | 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 | ||
+ | </pre> |
Revision as of 16:53, 13 December 2012
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"`));
}
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