std.experimental.logger formal review round 3
Martin Nowak via Digitalmars-d
digitalmars-d at puremagic.com
Tue Oct 28 11:22:06 PDT 2014
On Tuesday, 28 October 2014 at 12:02:16 UTC, Robert burner
Schadek wrote:
> It is a design goal to disable certain LogLevel at CT of a
> compile unit (CU).
> e.g. make all logs to trace function template do nothing
One idea to make this working is to use prefixed version
identifiers.
Obviously this is all boilerplate so it could be done in a mixin
template in std.log.
module mylib.log;
version (MyLibLogInfo)
enum logLevel = LogLevel.info;
version (MyLibLogTrace)
enum logLevel = LogLevel.trace;
version (MyLibLogWarning)
enum logLevel = LogLevel.warning;
version (MyLibLogError)
enum logLevel = LogLevel.error;
static if (is(typeof(logLevel)))
{
// wrapper around Logger interface/class for CT logLevel
static struct FixedLogLevel
{
enum logLevel = logLevel;
Logger impl;
alias impl this;
}
void setLogger(Logger logger)
{
logger.impl = logger;
}
private __gshared FixedLogLevel logger;
}
module mylib.foo;
void foo()
{
import mylib.log;
logger.info("bla");
logger.error("wat");
}
module client.bar;
void bar()
{
import mylib.log, mylib.foo;
auto logger = new FileLogger("log.txt", LogLevel.warning);
setLogger(logger);
foo(); // CT check for LogLevel
logger.info("runtime check whether info logging is on");
}
More information about the Digitalmars-d
mailing list