|
1 |
| -"""Calls @debug with the passed verbosity level""" |
| 1 | +function restore_callsite_source_position!(expr, src) |
| 2 | + # used to fix the logging source file + line |
| 3 | + # since we're lowering our verbose logging macros to the |
| 4 | + # Logging.jl macros; otherwise, they would always report this (verbosity.jl) |
| 5 | + # file as the logging callsite |
| 6 | + expr.args[1].args[2] = src |
| 7 | + return expr |
| 8 | +end |
| 9 | + |
| 10 | +vlogmacrodocs = """ |
| 11 | + @debugv N msg args... |
| 12 | + @infov N msg args... |
| 13 | + @warnv N msg args... |
| 14 | + @errorv N msg args... |
| 15 | +
|
| 16 | +"Verbose" logging macros. Drop in replacements of standard logging macros, but an |
| 17 | +additional verbosity level `N` is passed to indicate differing verbosity levels |
| 18 | +for a given log level. The verbosity argument is passed as the `group` argument |
| 19 | +to the core logging logic, so care should be taken if other loggers are being used |
| 20 | +that also use the group argument (which by default is passed the source file). |
| 21 | +
|
| 22 | +The default logger doesn't have support for filtering on the `group` argument, |
| 23 | +so an `LoggingExtras.EarlyFilteredLogger` can be used. For convenience, a |
| 24 | +[`LoggintExtras.with(f; level, verbosity)`](@ref) function is provided to temporarily |
| 25 | +wrap the current logger with a level/verbosity filter while `f` is executed. |
| 26 | +""" |
| 27 | + |
2 | 28 | macro debugv(verbosity::Int64, msg, exs...)
|
3 |
| - return esc(:($Base.@debug $msg verbosity=$verbosity $(exs...))) |
| 29 | + return restore_callsite_source_position!( |
| 30 | + esc(:($Base.@debug $msg _group=$verbosity $(exs...))), |
| 31 | + __source__, |
| 32 | + ) |
4 | 33 | end
|
5 | 34 |
|
6 |
| -"""Calls @info with the passed verbosity level""" |
7 | 35 | macro infov(verbosity::Int64, msg, exs...)
|
8 |
| - return esc(:($Base.@info $msg verbosity=$verbosity $(exs...))) |
| 36 | + return restore_callsite_source_position!( |
| 37 | + esc(:($Base.@info $msg _group=$verbosity $(exs...))), |
| 38 | + __source__, |
| 39 | + ) |
9 | 40 | end
|
10 | 41 |
|
11 |
| -"""Calls @warn with the passed verbosity level""" |
12 | 42 | macro warnv(verbosity::Int64, msg, exs...)
|
13 |
| - return esc(:($Base.@warn $msg verbosity=$verbosity $(exs...))) |
| 43 | + return restore_callsite_source_position!( |
| 44 | + esc(:($Base.@warn $msg _group=$verbosity $(exs...))), |
| 45 | + __source__, |
| 46 | + ) |
14 | 47 | end
|
15 | 48 |
|
16 |
| -"""Calls @error with the passed verbosity level""" |
17 | 49 | macro errorv(verbosity::Int64, msg, exs...)
|
18 |
| - return esc(:($Base.@error $msg verbosity=$verbosity $(exs...))) |
| 50 | + return restore_callsite_source_position!( |
| 51 | + esc(:($Base.@error $msg _group=$verbosity $(exs...))), |
| 52 | + __source__, |
| 53 | + ) |
19 | 54 | end
|
20 | 55 |
|
21 |
| -"""Calls @error with the passed verbosity level""" |
22 | 56 | macro logmsgv(verbosity::Int64, level, msg, exs...)
|
23 |
| - return esc(:($Base.@logmsgv $level $msg verbosity=$verbosity $(exs...))) |
| 57 | + return restore_callsite_source_position!( |
| 58 | + esc(:($Base.@logmsg $level $msg _group=$verbosity $(exs...))), |
| 59 | + __source__, |
| 60 | + ) |
| 61 | +end |
| 62 | + |
| 63 | +@eval @doc $vlogmacrodocs :(@logmsgv) |
| 64 | +@eval @doc $vlogmacrodocs :(@debugv) |
| 65 | +@eval @doc $vlogmacrodocs :(@infov) |
| 66 | +@eval @doc $vlogmacrodocs :(@warnv) |
| 67 | +@eval @doc $vlogmacrodocs :(@errorv) |
| 68 | + |
| 69 | +""" |
| 70 | + LoggingExtras.with(f; level=Info, verbosity=0) |
| 71 | +
|
| 72 | +Convenience function like `Logging.with_logger` to temporarily wrap |
| 73 | +the current logger with a level/verbosity filter while `f` is executed. |
| 74 | +That is, the current logger will still be used for actual logging, but |
| 75 | +log messages will first be checked that they meet the `level` and |
| 76 | +`verbosity` levels before being passed on to be logged. |
| 77 | +""" |
| 78 | +function with(f; level::Union{Int, LogLevel}=Info, verbosity::Int=0) |
| 79 | + with_logger(EarlyFilteredLogger( |
| 80 | + args -> !(args.group isa Int) || verbosity >= args.group, |
| 81 | + LevelOverrideLogger(level, current_logger())) |
| 82 | + ) do |
| 83 | + f() |
| 84 | + end |
24 | 85 | end
|
0 commit comments