Using_string_mixins_for_logging error
WebFreak001
d.forum at webfreak.org
Wed Jul 15 07:33:33 UTC 2020
On Wednesday, 15 July 2020 at 07:07:55 UTC, Vitalii wrote:
> Hello everyone!
>
> I try to compile this recipe with dmd (2.089.0), ldc2 (1.18.0):
> https://wiki.dlang.org/Using_string_mixins_for_logging
> but get the same error:
>
> mixin_log.d(64): Error: basic type expected, not __FUNCTION__
> mixin_log.d(64): Error: declaration expected, not __FUNCTION__
>
> in that part of code:
>
> mixin template set_func_name(string fn)
> {
> enum __FUNCTION__ = fn;
> }
>
> There is no doubt that this recipe once worked. How to fix it
> now?
that wiki entry is from 2012. __FUNCTION__, __PRETTY_FUNCTION__
and __MODULE__ were introduced in 2013, making that old
__FUNCTION__ definition code obsolete.
In fact all of the code on that page can now be more easily
written as:
enum LogLevel { INFO, WARN, ERROR }
template log(LogLevel level)
{
void log(Args...)(Args args, string fn = __FUNCTION__, string
file = __FILE__, size_t line = __LINE__)
{
writeln(Clock.currTime(), " [", level, "] ", file, '(',
line, "): ", fn, ": ", args);
}
}
alias info = log!(LogLevel.INFO);
alias warn = log!(LogLevel.WARN);
alias error = log!(LogLevel.ERROR);
no longer requiring any mixins or boilerplate code in your
calling functions.
---
void main(string[] args)
{
info("hello", "world");
warn("i am ", "number ", 1);
error(true);
log!(LogLevel.INFO)("manual call");
}
>>>
2020-Jul-15 08:12:00.5900346 [INFO] onlineapp.d(21):
onlineapp.main: helloworld
2020-Jul-15 08:12:00.5901559 [WARN] onlineapp.d(22):
onlineapp.main: i am number 1
2020-Jul-15 08:12:00.5901745 [ERROR] onlineapp.d(23):
onlineapp.main: true
---
This uses default values after variadic parameters which is
allowed since 2.079.0 and also uses a nested template to allow
for the aliases with default LogLevel.
More information about the Digitalmars-d-learn
mailing list