Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@MartinNowak
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MartinNowak/443f11aa017d14007c35 to your computer and use it in GitHub Desktop.
Save MartinNowak/443f11aa017d14007c35 to your computer and use it in GitHub Desktop.
configure log level
import logger;
// compile time log level
enum logLevel = LogLevel.critical;
void foo()
{
info("foo info"); // optimized out
critical("foo crit");
fatal("foo fat");
}
template modLogLevel(string moduleName) if (!moduleName.length)
{
// default
enum modLogLevel = LogLevel.all;
}
template modLogLevel(string moduleName) if (moduleName.length)
{
import std.string : format;
mixin(q{
static if (__traits(compiles, {import %1$s : logLevel;}))
{
import %1$s : logLevel;
static assert(is(typeof(logLevel) : LogLevel),
"Expect 'logLevel' to be of Type 'LogLevel'.");
// don't enforce enum here
alias modLogLevel = logLevel;
}
else
// use logLevel of package or default
alias modLogLevel = modLogLevel!(parentOf(moduleName));
}.format(moduleName));
}
private string parentOf(string mod)
{
foreach_reverse (i, c; mod)
if (c == '.') return mod[0 .. i];
return null;
}
void log(int line = __LINE__, string file = __FILE__,
string funcName = __FUNCTION__, string prettyFuncName = __PRETTY_FUNCTION__,
string moduleName = __MODULE__, A...)(const LogLevel ll, lazy A args)
if (args.length > 1)
{
import std.stdio;
if (ll >= modLogLevel!moduleName)
writeln(args);
}
template defaultLogFunction(LogLevel ll)
{
void defaultLogFunction(int line = __LINE__, string file = __FILE__,
string funcName = __FUNCTION__,
string prettyFuncName = __PRETTY_FUNCTION__,
string moduleName = __MODULE__, A...)(lazy A args)
if (args.length > 0)
{
import std.stdio;
if (ll >= modLogLevel!moduleName)
writeln(args);
}
}
/// Ditto
alias trace = defaultLogFunction!(LogLevel.trace);
/// Ditto
alias info = defaultLogFunction!(LogLevel.info);
/// Ditto
alias warning = defaultLogFunction!(LogLevel.warning);
/// Ditto
alias error = defaultLogFunction!(LogLevel.error);
/// Ditto
alias critical = defaultLogFunction!(LogLevel.critical);
/// Ditto
alias fatal = defaultLogFunction!(LogLevel.fatal);
enum LogLevel : ubyte
{
all = 1, /** Lowest possible assignable $(D LogLevel). */
trace = 32, /** $(D LogLevel) for tracing the execution of the program. */
info = 64, /** This level is used to display information about the
program. */
warning = 96, /** warnings about the program should be displayed with this
level. */
error = 128, /** Information about errors should be logged with this
level.*/
critical = 160, /** Messages that inform about critical errors should be
logged with this level. */
fatal = 192, /** Log messages that describe fatal errors should use this
level. */
off = ubyte.max /** Highest possible $(D LogLevel). */
}
import logger, foo, pkg;
// runtime logLevel (thread local actually)
LogLevel logLevel;
void main()
{
info("main1");
logLevel = LogLevel.fatal;
info("main2");
logLevel = LogLevel.all;
info("main3");
foo.foo();
pkg.bar();
}
module pkg.bar;
import logger;
void bar()
{
// logLevel in pkg.package is LogLevel.info
trace("bar trace");
info("bar info");
}
module pkg;
import logger;
public import pkg.bar;
// loglevel for whole package
enum logLevel = LogLevel.info;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment