From 795be6701196450bff920b5a7032e4898071823d Mon Sep 17 00:00:00 2001 From: jClugstor Date: Thu, 20 Nov 2025 15:41:44 -0500 Subject: [PATCH 1/2] add toggle printing to macro --- src/utils.jl | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/utils.jl b/src/utils.jl index 7e2dc0e..3504df8 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -31,39 +31,42 @@ function logging_message_level(option::Silent) end function emit_message( - f::Function, level, file, line, + f::Function, level, option, file, line, _module) message = f() + msg = "Verbosity toggle: $option \n $message" @static if LOGGING_BACKEND == "core" - Core.println(message) + Core.println(msg) else - Base.@logmsg level message _file=file _line=line _module=_module + Base.@logmsg level msg _file=file _line=line _module=_module end if level == Logging.Error - throw(ErrorException(message)) + throw(ErrorException(msg)) end end function emit_message(message::AbstractString, - level, file, line, _module) + level, option, file, line, _module) + + msg = "Verbosity toggle: $option \n $message" @static if LOGGING_BACKEND == "core" - Core.println(message) + Core.println(msg) else - Base.@logmsg level message _file=file _line=line _module=_module + Base.@logmsg level msg _file=file _line=line _module=_module end if level == Logging.Error - throw(ErrorException(message)) + throw(ErrorException(msg)) end end function emit_message(message::AbstractString, - level::Nothing, file, line, _module) + level::Nothing, option, file, line, _module) end function emit_message( - f::Function, level::Nothing, file, line, _module) + f::Function, level::Nothing, option, file, line, _module) end function get_message_level(verb::AbstractVerbositySpecifier, option) @@ -146,6 +149,7 @@ macro SciMLMessage(f_or_message, verb, option) expr = quote emit_message($(esc(f_or_message)), get_message_level($(esc(verb)), $(esc(option))), + $(esc(option)), $file, $line, $_module) From 7c76685f75d9b73c2dea069da999c068f488b3b2 Mon Sep 17 00:00:00 2001 From: jClugstor Date: Thu, 20 Nov 2025 15:55:30 -0500 Subject: [PATCH 2/2] fix the tests --- test/basics.jl | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/test/basics.jl b/test/basics.jl index 188db73..7de0c0c 100644 --- a/test/basics.jl +++ b/test/basics.jl @@ -51,9 +51,9 @@ end @testset "Basic tests" begin verbose = TestVerbosity() - @test_logs (:warn, "Test1") @SciMLMessage("Test1", verbose, :test1) - @test_logs (:info, "Test2") @SciMLMessage("Test2", verbose, :test2) - @test_logs (:error, "Test3") @test_throws "Test3" begin + @test_logs (:warn, r"Test1") @SciMLMessage("Test1", verbose, :test1) + @test_logs (:info, r"Test2") @SciMLMessage("Test2", verbose, :test2) + @test_logs (:error, r"Test3") @test_throws "Test3" begin @SciMLMessage("Test3", verbose, :test3) end @test_logs min_level = Logging.Debug @SciMLMessage("Test4", verbose, :test4) @@ -61,7 +61,7 @@ end x = 30 y = 30 - @test_logs (:warn, "Test1: 60") @SciMLMessage(verbose, :test1) do + @test_logs (:warn, r"Test1: 60") @SciMLMessage(verbose, :test1) do z = x + y "Test1: $z" end @@ -74,15 +74,15 @@ end verbose_none = TestVerbosity(None()) # All preset should log info level messages - @test_logs (:info, "All preset test") @SciMLMessage("All preset test", verbose_all, :test1) + @test_logs (:info, r"All preset test") @SciMLMessage("All preset test", verbose_all, :test1) # Minimal preset should only log errors and throw for error messages - @test_logs (:error, "Minimal preset test") @test_throws ErrorException("Minimal preset test") begin + @test_logs (:error, r"Minimal preset test") @test_throws ErrorException("Verbosity toggle: test1 \n Minimal preset test") begin @SciMLMessage("Minimal preset test", verbose_minimal, :test1) end # Test that minimal preset throws for test3 (which is ErrorLevel) - @test_logs (:error, "Minimal error on test3") @test_throws ErrorException("Minimal error on test3") begin + @test_logs (:error, r"Minimal error on test3") @test_throws ErrorException("Verbosity toggle: test3 \n Minimal error on test3") begin @SciMLMessage("Minimal error on test3", verbose_minimal, :test3) end @@ -107,7 +107,7 @@ end verbose = TestVerbosity() # Test that @SciMLMessage can be called inside another @SciMLMessage function block - @test_logs (:warn, "Inner message from nested call") (:info, "Outer message with nested result") begin + @test_logs (:warn, r"Inner message from nested call") (:info, r"Outer message with nested result") begin result = @SciMLMessage(verbose, :test2) do @SciMLMessage("Inner message from nested call", verbose, :test1) "Outer message with nested result" @@ -116,7 +116,7 @@ end # Test nested with both function-based inner and outer counter = 0 - @test_logs (:info, "Inner computation: 5") (:warn, "Outer result: 5") begin + @test_logs (:info, r"Inner computation: 5") (:warn, r"Outer result: 5") begin @SciMLMessage(verbose, :test1) do inner_result = @SciMLMessage(verbose, :test2) do counter = 5 @@ -129,13 +129,13 @@ end @testset "Boolean verbosity" begin # 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_logs (:warn, r"Message with verbose=true") @SciMLMessage("Message with verbose=true", true, :ignored) # 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 (three-arg form) - @test_logs (:warn, "Computed message: 42") @SciMLMessage(true, :ignored) do + @test_logs (:warn, r"Computed message: 42") @SciMLMessage(true, :ignored) do x = 40 + 2 "Computed message: $x" end @@ -149,13 +149,13 @@ 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_logs (:warn, r"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 + @test_logs (:warn, r"Two-arg computed: 100") @SciMLMessage(true) do y = 50 * 2 "Two-arg computed: $y" end