Skip to content

Commit

Permalink
Insert stream to avoid name conflicts
Browse files Browse the repository at this point in the history
Close the stream as supposed to in main and tests
  • Loading branch information
deltamarnix committed Dec 18, 2023
1 parent abe6e6a commit d90e616
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 45 deletions.
20 changes: 4 additions & 16 deletions core/src/logging.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ function is_current_module(log::LogMessageType)::Bool
log._module == OrdinaryDiffEq# for the progress bar
end

function setup_logger(
config::Config;
function setup_logger(;
verbosity::LogLevel,
stream::IOStream,
module_filter_function::Function = is_current_module,
)::AbstractLogger
file_logger = LoggingExtras.MinLevelLogger(
LoggingExtras.FileLogger(results_path(config, "ribasim.log")),
config.logging.verbosity,
)
file_logger = LoggingExtras.MinLevelLogger(LoggingExtras.FileLogger(stream), verbosity)
terminal_logger = LoggingExtras.MinLevelLogger(
TerminalLogger(),
LogLevel(-1), # To include progress bar
Expand All @@ -30,13 +28,3 @@ function setup_logger(
LoggingExtras.TeeLogger(file_logger, terminal_logger),
)
end

function close(logger::AbstractLogger)
if hasfield(typeof(logger), :logger)
close(logger.logger)
elseif hasfield(typeof(logger), :loggers)
foreach(close, logger.loggers)
elseif hasfield(typeof(logger), :stream) && logger isa SimpleLogger # FileLogger contains a SimpleLogger that contains the Stream.
Base.close(logger.stream)
end
end
9 changes: 5 additions & 4 deletions core/src/main.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ function main(ARGS)::Cint
try
# show progress bar in terminal
config = Config(arg)
logger = setup_logger(config)
model = with_logger(logger) do
Ribasim.run(config)
open(results_path(config, "ribasim.log"), "w") do io
logger = Ribasim.setup_logger(; verbosity = config.verbosity, stream = io)
model = with_logger(logger) do
Ribasim.run(config)
end
end
Ribasim.close(logger)
return if successful_retcode(model)
println("The model finished successfully")
0
Expand Down
53 changes: 28 additions & 25 deletions core/test/logging_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
)
config = Ribasim.Config(normpath(dir, "ribasim.toml"))
mkdir(Ribasim.results_path(config, "."))
logger = Ribasim.setup_logger(config)
@test Logging.shouldlog(logger, Logging.Error, Ribasim, :group, :message)
@test Logging.shouldlog(logger, Logging.Info, Ribasim, :group, :message)
@test Logging.shouldlog(logger, Logging.Info - 1, Ribasim, :group, :message) # progress bar
@test !Logging.shouldlog(logger, Logging.Debug, Ribasim, :group, :message)

Ribasim.close(logger)
open(Ribasim.results_path(config, "ribasim.log"), "w") do io
logger =
Ribasim.setup_logger(; verbosity = config.logging.verbosity, stream = io)
@test Logging.shouldlog(logger, Logging.Error, Ribasim, :group, :message)
@test Logging.shouldlog(logger, Logging.Info, Ribasim, :group, :message)
@test Logging.shouldlog(logger, Logging.Info - 1, Ribasim, :group, :message) # progress bar
@test !Logging.shouldlog(logger, Logging.Debug, Ribasim, :group, :message)
end
end
end

Expand All @@ -28,13 +29,14 @@ end
)
config = Ribasim.Config(normpath(dir, "ribasim.toml"))
mkdir(Ribasim.results_path(config, "."))
logger = Ribasim.setup_logger(config)
@test Logging.shouldlog(logger, Logging.Error, Ribasim, :group, :message)
@test Logging.shouldlog(logger, Logging.Info, Ribasim, :group, :message)
@test Logging.shouldlog(logger, Logging.Info - 1, Ribasim, :group, :message) # progress bar
@test Logging.shouldlog(logger, Logging.Debug, Ribasim, :group, :message)

Ribasim.close(logger)
open(Ribasim.results_path(config, "ribasim.log"), "w") do io
logger =
Ribasim.setup_logger(; verbosity = config.logging.verbosity, stream = io)
@test Logging.shouldlog(logger, Logging.Error, Ribasim, :group, :message)
@test Logging.shouldlog(logger, Logging.Info, Ribasim, :group, :message)
@test Logging.shouldlog(logger, Logging.Info - 1, Ribasim, :group, :message) # progress bar
@test Logging.shouldlog(logger, Logging.Debug, Ribasim, :group, :message)
end
end
end

Expand All @@ -49,20 +51,21 @@ end
)
config = Ribasim.Config(normpath(dir, "ribasim.toml"))
mkdir(Ribasim.results_path(config, "."))
logger = Ribasim.setup_logger(
config;
module_filter_function = log::Ribasim.LogMessageType ->
log._module == @__MODULE__,
)
open(Ribasim.results_path(config, "ribasim.log"), "w") do io
logger = Ribasim.setup_logger(;
verbosity = Logging.Debug,
stream = io,
module_filter_function = log::Ribasim.LogMessageType ->
log._module == @__MODULE__,
)

with_logger(logger) do
@info "foo"
@warn "bar"
@debug "baz"
with_logger(logger) do
@info "foo"
@warn "bar"
@debug "baz"
end
end

Ribasim.close(logger)

open(normpath(dir, "results", "ribasim.log"), "r") do io
result = read(io, String)
@test occursin("Info: foo", result)
Expand Down

0 comments on commit d90e616

Please sign in to comment.