From ab4eb4f8ffbc01e1ef692ba43a3aa916a73ccdad Mon Sep 17 00:00:00 2001 From: jClugstor Date: Fri, 7 Nov 2025 17:30:53 -0500 Subject: [PATCH 1/3] add way to use bool verb --- src/utils.jl | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/utils.jl b/src/utils.jl index 5c8f39e..a776aa7 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -66,6 +66,14 @@ function emit_message( f::Function, level::Nothing, file, line, _module) end +function get_message_level(verb::AbstractVerbositySpecifier, option) + return logging_message_level(getproperty(verb, option)) +end + +function get_message_level(verb::Bool, _) + return verb ? logging_message_level(WarnLevel()) : logging_message_level(Silent()) +end + """ `@SciMLMessage(message, verbosity, option)` @@ -114,18 +122,24 @@ function solve_problem(problem; verbose = SolverVerbosity(Standard())) return result end ``` + +Alternatively, the macro also accepts a boolean value for `verb`: + +When `verb` is a boolean: +- `true` will emit the message at `WarnLevel()` +- `false` will suppress the message (equivalent to `Silent()`) """ macro SciMLMessage(f_or_message, verb, option) line = __source__.line file = string(__source__.file) _module = __module__ - expr = quote + expr = quote emit_message($(esc(f_or_message)), - logging_message_level(getproperty($(esc(verb)), $(esc(option)))), + get_message_level($(esc(verb)), $(esc(option))), $file, $line, $_module) - end + end return expr end From e3e0a6e359e7dd11ad1654e0a7d5d60c183cad90 Mon Sep 17 00:00:00 2001 From: jClugstor Date: Fri, 7 Nov 2025 17:37:54 -0500 Subject: [PATCH 2/3] add tests for bool verb --- test/basics.jl | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/basics.jl b/test/basics.jl index 73c1573..b3a3008 100644 --- a/test/basics.jl +++ b/test/basics.jl @@ -125,4 +125,26 @@ end "Outer result: $counter" end end +end + +@testset "Boolean verbosity" begin + # Test with true - should emit at WarnLevel + @test_logs (:warn, "Message with verbose=true") @SciMLMessage("Message with verbose=true", true, :ignored) + + # Test with false - should not emit anything + @test_logs min_level = Logging.Debug @SciMLMessage("Message with verbose=false", false, :ignored) + + # Test with function form and true + @test_logs (:warn, "Computed message: 42") @SciMLMessage(true, :ignored) do + x = 40 + 2 + "Computed message: $x" + end + + # Test with function form and false - should not compute or emit + computation_ran = false + @test_logs min_level = Logging.Debug @SciMLMessage(false, :ignored) do + computation_ran = true + "This should not be computed" + end + @test !computation_ran # Verify function was never called when verbose=false end \ No newline at end of file From fa606b302d9fac1ef10ddba6602422a35b1cacbd Mon Sep 17 00:00:00 2001 From: jClugstor Date: Fri, 7 Nov 2025 17:42:58 -0500 Subject: [PATCH 3/3] add two arg version of Macro --- src/utils.jl | 16 +++++++++++++++- test/basics.jl | 28 ++++++++++++++++++++++++---- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/src/utils.jl b/src/utils.jl index a776aa7..7e2dc0e 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -76,7 +76,8 @@ end """ - `@SciMLMessage(message, verbosity, option)` + `@SciMLMessage(message, verbosity::AbstracVerbositySpecifier, option::Symbol)` + `@SciMLMessage(message, verbosity::Bool)` A macro that emits a log message based on the log level specified in the `option` of the `AbstractVerbositySpecifier` supplied. @@ -128,6 +129,15 @@ Alternatively, the macro also accepts a boolean value for `verb`: When `verb` is a boolean: - `true` will emit the message at `WarnLevel()` - `false` will suppress the message (equivalent to `Silent()`) + +The two-argument form `@SciMLMessage(message, verbosity)` can be used when `verbosity` is a `Bool`: + +```julia +function solve_problem(problem; verbose::Bool = true) + @SciMLMessage("Starting solver", verbose) + # ... solver logic ... +end +``` """ macro SciMLMessage(f_or_message, verb, option) line = __source__.line @@ -143,6 +153,10 @@ macro SciMLMessage(f_or_message, verb, option) return expr end +macro SciMLMessage(f_or_message, verb) + return esc(:(@SciMLMessage($f_or_message, $verb, :_))) +end + """ `verbosity_to_int(verb::AbstractMessageLevel)` diff --git a/test/basics.jl b/test/basics.jl index b3a3008..188db73 100644 --- a/test/basics.jl +++ b/test/basics.jl @@ -128,23 +128,43 @@ end end @testset "Boolean verbosity" begin - # Test with true - should emit at WarnLevel + # Test with true - should emit at WarnLevel (three-arg form) @test_logs (:warn, "Message with verbose=true") @SciMLMessage("Message with verbose=true", true, :ignored) - # Test with false - should not emit anything + # Test with false - should not emit anything (three-arg form) @test_logs min_level = Logging.Debug @SciMLMessage("Message with verbose=false", false, :ignored) - # Test with function form and true + # Test with function form and true (three-arg form) @test_logs (:warn, "Computed message: 42") @SciMLMessage(true, :ignored) do x = 40 + 2 "Computed message: $x" end - # Test with function form and false - should not compute or emit + # Test with function form and false - should not compute or emit (three-arg form) computation_ran = false @test_logs min_level = Logging.Debug @SciMLMessage(false, :ignored) do computation_ran = true "This should not be computed" end @test !computation_ran # Verify function was never called when verbose=false + + # Test two-argument form with true + @test_logs (:warn, "Two-arg form with true") @SciMLMessage("Two-arg form with true", true) + + # Test two-argument form with false + @test_logs min_level = Logging.Debug @SciMLMessage("Two-arg form with false", false) + + # Test two-argument form with function and true + @test_logs (:warn, "Two-arg computed: 100") @SciMLMessage(true) do + y = 50 * 2 + "Two-arg computed: $y" + end + + # Test two-argument form with function and false + computation_ran_2arg = false + @test_logs min_level = Logging.Debug @SciMLMessage(false) do + computation_ran_2arg = true + "This should not be computed either" + end + @test !computation_ran_2arg # Verify function was never called when verbose=false end \ No newline at end of file