Last active
April 18, 2017 11:11
-
-
Save juanjux/16f0027ee797d679c16be6c3c1a7db7f to your computer and use it in GitHub Desktop.
Simple mixin to add some log messages to every function call (D language)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module logmixin.d; | |
/** | |
This mixin will log at the start and exit | |
of every call that injects it, including parameter | |
namess and values. Enter/exit/param messages are | |
logged only on debug mode. Failures are always logged. | |
*/ | |
string LogBoilerPlate(string caller=__FUNCTION__)() { | |
import std.traits: ParameterIdentifierTuple; | |
mixin("alias callerSymbol = " ~ caller ~ ";"); | |
string logArgs; | |
foreach(i; ParameterIdentifierTuple!callerSymbol) { | |
logArgs ~= `trace("Arg: ` ~ i ~ `, value: ", ` ~ i ~ `);`; | |
} | |
return ` | |
import std.experimental.logger: trace, warning; | |
trace("Entering: ", __PRETTY_FUNCTION__); | |
` ~ logArgs ~ ` | |
scope(success) | |
trace("Exiting: ", __PRETTY_FUNCTION__); | |
scope(failure) | |
warning("FAILED: ", __PRETTY_FUNCTION__);`; | |
} | |
/// Example usage: | |
void loggedFunc(int someParam, int otherParam) | |
{ | |
mixin(LogBoilerPlate); | |
// ...normal code | |
} | |
unittest // not really unittest, just an example | |
{ | |
loggedFunc(3, 4); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment