On Friday, 14 August 2015 at 12:40:08 UTC, Steven Schveighoffer wrote:

I would do it this way:

// at module level
debug(logging) {
    Logger logger;
    static this() { logger = new Logger;}
}

If you want to have the logger be global, add 'shared' to both the logger and the static this.

This initializes the data before main() is run, so there is no need for singletons.

The added bonus here is that you can't accidentally log stuff when debug(logging) isn't enabled, as the variable will not exist.

-Steve

Ahh, that is a much cleaner way to do it rather than using a singleton.

By 'module level', I assume you mean in the module that defines the Logger class? An 'import debug.logger' or somesuch would then give all relevant modules access, correct?

Is the compiler smart enough to compile out all the symbols associated with the logger if it is never instantiated?

Reply via email to