-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Logger writes to both Terminal and File (#911)
Fixes #502 Fixes #747 Using `LoggingExtras` as a basis to write logs to two places at the same time. The verbosity only applies to the file logger. The terminal logger will always be on Progress level (-1). The settings.json was adjusted to exclude some folders that do not help in our development. --------- Co-authored-by: Martijn Visser <[email protected]>
- Loading branch information
1 parent
a3c91ea
commit ee626c3
Showing
19 changed files
with
287 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,10 @@ authors = ["Deltares and contributors <[email protected]>"] | |
description = "Meta-project used to share the Manifest of Ribasim and its dependents" | ||
|
||
[deps] | ||
ReTestItems = "817f1d60-ba6b-4fd5-9520-3cf149f6a823" | ||
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe" | ||
Ribasim = "aac5e3d9-0b8f-4d4f-8241-b1a7a9632635" | ||
TestEnv = "1e6cf692-eddd-4d53-88a5-2d735e33781b" | ||
TimeZones = "f269a46b-ccf7-5d73-abea-4c690281aa53" | ||
create_binaries = "3cfb6a46-05f0-43df-bb16-bf763deb14b4" | ||
docs = "8daea9ca-fc6c-4731-aa85-717fa0b706bc" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,7 @@ | ||
module ribasim_cli | ||
|
||
using Logging: global_logger, with_logger | ||
using TerminalLoggers: TerminalLogger | ||
using SciMLBase: successful_retcode | ||
using Ribasim | ||
|
||
function help(x)::Cint | ||
println(x) | ||
println("Usage: ribasim path/to/model/ribasim.toml") | ||
return 1 | ||
end | ||
|
||
function julia_main()::Cint | ||
n = length(ARGS) | ||
if n != 1 | ||
return help("Exactly 1 argument expected, got $n") | ||
end | ||
arg = only(ARGS) | ||
|
||
if arg == "--version" | ||
version = pkgversion(Ribasim) | ||
print(version) | ||
return 0 | ||
end | ||
|
||
if !isfile(arg) | ||
return help("File not found: $arg") | ||
end | ||
|
||
try | ||
# show progress bar in terminal | ||
model = with_logger(TerminalLogger()) do | ||
Ribasim.run(arg) | ||
end | ||
return if successful_retcode(model) | ||
println("The model finished successfully") | ||
0 | ||
else | ||
t = Ribasim.datetime_since(model.integrator.t, model.config.starttime) | ||
retcode = model.integrator.sol.retcode | ||
println("The model exited at model time $t with return code $retcode") | ||
println("See https://docs.sciml.ai/DiffEqDocs/stable/basics/solution/#retcodes") | ||
1 | ||
end | ||
catch | ||
Base.invokelatest(Base.display_error, current_exceptions()) | ||
return 1 | ||
end | ||
end | ||
julia_main()::Cint = Ribasim.main(ARGS) | ||
|
||
end # module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
""" | ||
is_current_module(log::LogMessageType)::Bool | ||
Returns true if the log message is from the current module or a submodule. | ||
See https://github.com/JuliaLogging/LoggingExtras.jl/blob/d35e7c8cfc197853ee336ace17182e6ed36dca24/src/CompositionalLoggers/earlyfiltered.jl#L39 | ||
for the information available in log. | ||
""" | ||
function is_current_module(log)::Bool | ||
(log._module == @__MODULE__) || | ||
(parentmodule(log._module) == @__MODULE__) || | ||
log._module == OrdinaryDiffEq # for the progress bar | ||
end | ||
|
||
function setup_logger(; | ||
verbosity::LogLevel, | ||
stream::IOStream, | ||
module_filter_function::Function = is_current_module, | ||
)::AbstractLogger | ||
file_logger = LoggingExtras.MinLevelLogger(LoggingExtras.FileLogger(stream), verbosity) | ||
terminal_logger = LoggingExtras.MinLevelLogger( | ||
TerminalLogger(), | ||
LogLevel(-1), # To include progress bar | ||
) | ||
return LoggingExtras.EarlyFilteredLogger( | ||
module_filter_function, | ||
LoggingExtras.TeeLogger(file_logger, terminal_logger), | ||
) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
function help(x::AbstractString)::Cint | ||
println(x) | ||
println("Usage: ribasim path/to/model/ribasim.toml") | ||
return 1 | ||
end | ||
|
||
main(toml_path::AbstractString)::Cint = main([toml_path]) | ||
|
||
""" | ||
main(ARGS::Vector{String})::Cint | ||
This is the main entry point of the application. | ||
Performs argument parsing and sets up logging for both terminal and file. | ||
Calls Ribasim.run() and handles exceptions to convert to exit codes. | ||
""" | ||
function main(ARGS::Vector{String})::Cint | ||
n = length(ARGS) | ||
if n != 1 | ||
return help("Exactly 1 argument expected, got $n") | ||
end | ||
arg = only(ARGS) | ||
|
||
if arg == "--version" | ||
version = pkgversion(Ribasim) | ||
print(version) | ||
return 0 | ||
end | ||
|
||
if !isfile(arg) | ||
return help("File not found: $arg") | ||
end | ||
|
||
try | ||
# show progress bar in terminal | ||
config = Config(arg) | ||
mkpath(results_path(config, ".")) | ||
open(results_path(config, "ribasim.log"), "w") do io | ||
logger = | ||
Ribasim.setup_logger(; verbosity = config.logging.verbosity, stream = io) | ||
with_logger(logger) do | ||
model = Ribasim.run(config) | ||
if successful_retcode(model) | ||
@info "The model finished successfully" | ||
return 0 | ||
end | ||
|
||
t = Ribasim.datetime_since(model.integrator.t, model.config.starttime) | ||
retcode = model.integrator.sol.retcode | ||
@error "The model exited at model time $t with return code $retcode.\nSee https://docs.sciml.ai/DiffEqDocs/stable/basics/solution/#retcodes" | ||
return 1 | ||
end | ||
end | ||
catch | ||
Base.invokelatest(Base.display_error, current_exceptions()) | ||
return 1 | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
starttime = 2019-01-01 | ||
endtime = 2019-12-31 | ||
input_dir = "." | ||
results_dir = "results" | ||
|
||
[logging] | ||
verbosity = "debug" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
starttime = 2019-01-01 | ||
endtime = 2019-12-31 | ||
input_dir = "." | ||
results_dir = "results" |
Oops, something went wrong.