January 2020
Intermediate to advanced
532 pages
13h 31m
English
Code generation is one way to tackle this problem, as mentioned in the preceding section. What we will do is to build up the syntax of defining the function and then throw that into a loop to define all three logging functions. Here is how the code may look:
for level in (:info, :warning, :error) lower_level_str = String(level) upper_level_str = uppercase(lower_level_str) upper_level_sym = Symbol(upper_level_str) fn = Symbol(lower_level_str * "!") label = " [" * upper_level_str * "] " @eval function $fn(logger::Logger, args...) if logger.level <= $upper_level_sym let io = logger.handle print(io, trunc(now(), Dates.Second), $label) for (idx, arg) in enumerate(args) idx > 0 && print(io, " ") print(io, ...
Read now
Unlock full access