-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConsoleProgressMonitor.jl
143 lines (115 loc) · 3.6 KB
/
ConsoleProgressMonitor.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
module ConsoleProgressMonitor
# Use README as the docstring of the module:
@doc read(joinpath(dirname(@__DIR__), "README.md"), String) ConsoleProgressMonitor
using Logging: Logging, global_logger
using ProgressMeter: Progress, finish!, update!
include("router.jl")
const default_colors = [
:green,
:blue,
:magenta,
:cyan,
:yellow,
:red,
:light_black,
:light_green,
:light_blue,
:light_magenta,
:light_cyan,
:light_yellow,
:light_red,
]
"""
ProgressLogger(; colors, progress_options...)
# Keyword Arguments
- `colors :: Vector{Symbol}`: a list of colors used for progress meters.
- Other keyword arguments are used for constructing `ProgressMeter.Progress`.
"""
struct ProgressLogger <: Logging.AbstractLogger
options::NamedTuple
colors::Vector{Symbol}
bars::Dict{Any,Progress}
lastid::typeof(Ref{Any}(:_))
end
const _noid = gensym(:_noid)
ProgressLogger(options::NamedTuple, colors::Vector{Symbol} = default_colors) =
ProgressLogger(options, colors, Dict(), Ref{Any}(_noid))
ProgressLogger(; colors = default_colors, options...) =
ProgressLogger((; options...), colors)
# https://docs.julialang.org/en/latest/stdlib/Logging/#AbstractLogger-interface-1
likelytoprint(p) = time() + 1e-3 - p.tlast > p.dt
pickcycle(xs, i) = xs[mod1(i, length(xs))]
somestring(x) = x
somestring(x, xs...) = x isa AbstractString && !isempty(x) ? x : somestring(xs...)
function Logging.handle_message(
logger::ProgressLogger,
level,
title,
_module,
group,
id,
file,
line;
progress = nothing,
message = nothing,
_...,
)
n = 1000
if progress isa Real && (progress <= 1 || isnan(progress))
p = get!(logger.bars, id) do
color = pickcycle(logger.colors, length(logger.bars) + 1)
Progress(n; color = color, logger.options...)
end
progress = isnan(progress) ? 0.0 : progress
counter = floor(Int, progress * n)
desc = somestring(string(title), message, p.desc)
if !endswith(desc, " ")
desc = string(desc, ": ")
end
p.desc = desc
if logger.lastid[] ∉ (id, _noid) && likelytoprint(p)
# Switched from unfinished progress bar:
println(p.output)
end
update!(p, counter)
if p.printed
logger.lastid[] = id
end
elseif progress == "done" || (progress isa Real && progress > 1)
p = pop!(logger.bars, id, nothing)
p === nothing && return
finish!(p)
logger.lastid[] = _noid
end
return
end
Logging.shouldlog(::ProgressLogger, level, _module, group, id) = true
Logging.min_enabled_level(::ProgressLogger) = Logging.LogLevel(-1)
"""
with_progresslogger(f; options...)
Run `f` with `ProgressLogger` enabled.
"""
with_progresslogger(f; options...) = Logging.with_logger(f, ProgressLogger(; options...))
"""
install_logger(; options...)
install_logger(logger::ProgressLogger)
Install `ProgressLogger` to global logger.
Keyword arguments `options` are passed to `ProgressLogger` constructor.
"""
install_logger(; options...) = install_logger(ProgressLogger(; options...))
function install_logger(logger::ProgressLogger)
global previous_logger
previous_logger = global_logger(ProgressLogRouter(global_logger(), logger))
end
"""
uninstall_logger()
Rollback the global logger to the one before last call of `install_logger`.
"""
function uninstall_logger()
global previous_logger
previous_logger === nothing && return
ans = global_logger(previous_logger)
previous_logger = nothing
return ans
end
end # module