Skip to content

Do not log already handled progress log messages #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ version = "0.1.0"

[deps]
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
LoggingExtras = "e6f89c97-d47a-5376-807f-9c37f3926c36"
ProgressMeter = "92933f4c-e287-5a05-a399-4b506db050ca"

[compat]
LoggingExtras = "0.3"
ProgressMeter = "1"
julia = "1"

Expand Down
21 changes: 6 additions & 15 deletions src/ConsoleProgressMonitor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ module ConsoleProgressMonitor
# Use README as the docstring of the module:
@doc read(joinpath(dirname(@__DIR__), "README.md"), String) ConsoleProgressMonitor

import Logging
import LoggingExtras
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,
Expand Down Expand Up @@ -102,25 +103,15 @@ with_progresslogger(f; options...) =
install_logger(; options...)
install_logger(logger::ProgressLogger)

Install `ProgressLogger` using `LoggingExtras.DemuxLogger`.
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
#=
pkgid = Base.PkgId(
Base.UUID("e6f89c97-d47a-5376-807f-9c37f3926c36"),
"LoggingExtras",
)
LoggingExtras = Base.require(pkgid)
previous_logger = Base.invokelatest() do
Logging.global_logger(LoggingExtras.DemuxLogger(logger))
end
=#
previous_logger = Logging.global_logger(LoggingExtras.DemuxLogger(logger))
previous_logger = global_logger(ProgressLogRouter(global_logger(), logger))
end

"""
Expand All @@ -131,7 +122,7 @@ Rollback the global logger to the one before last call of `install_logger`.
function uninstall_logger()
global previous_logger
previous_logger === nothing && return
ans = Logging.global_logger(previous_logger)
ans = global_logger(previous_logger)
previous_logger = nothing
return ans
end
Expand Down
27 changes: 27 additions & 0 deletions src/router.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Logging: Logging, AbstractLogger

struct ProgressLogRouter{D<:AbstractLogger,P<:AbstractLogger} <: AbstractLogger
defaultlogger::D
progresslogger::P
end

_hasprogress(; progress = nothing, _...) = progress === "done" || progress isa Real

function Logging.handle_message(logger::ProgressLogRouter, args...; kwargs...)
level, _, _module, group, id = args
if _hasprogress(; kwargs...)
Logging.handle_message(logger.progresslogger, args...; kwargs...)
elseif level >= Logging.min_enabled_level(logger) &&
Logging.shouldlog(logger, level, _module, group, id)
Logging.handle_message(logger.defaultlogger, args...; kwargs...)
end
end

Logging.shouldlog(logger::ProgressLogRouter, args...) =
Logging.shouldlog(logger.defaultlogger, args...) ||
Logging.shouldlog(logger.progresslogger, args...)

Logging.min_enabled_level(logger::ProgressLogRouter) = min(
Logging.min_enabled_level(logger.defaultlogger),
Logging.min_enabled_level(logger.progresslogger),
)