Skip to content

Commit 0abaffe

Browse files
Merge pull request #39 from jClugstor/tags
Add verbosity toggle to message
2 parents 4b9930c + 7c76685 commit 0abaffe

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

src/utils.jl

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,39 +31,42 @@ function logging_message_level(option::Silent)
3131
end
3232

3333
function emit_message(
34-
f::Function, level, file, line,
34+
f::Function, level, option, file, line,
3535
_module)
3636
message = f()
37+
msg = "Verbosity toggle: $option \n $message"
3738
@static if LOGGING_BACKEND == "core"
38-
Core.println(message)
39+
Core.println(msg)
3940
else
40-
Base.@logmsg level message _file=file _line=line _module=_module
41+
Base.@logmsg level msg _file=file _line=line _module=_module
4142
end
4243

4344
if level == Logging.Error
44-
throw(ErrorException(message))
45+
throw(ErrorException(msg))
4546
end
4647
end
4748

4849
function emit_message(message::AbstractString,
49-
level, file, line, _module)
50+
level, option, file, line, _module)
51+
52+
msg = "Verbosity toggle: $option \n $message"
5053
@static if LOGGING_BACKEND == "core"
51-
Core.println(message)
54+
Core.println(msg)
5255
else
53-
Base.@logmsg level message _file=file _line=line _module=_module
56+
Base.@logmsg level msg _file=file _line=line _module=_module
5457
end
5558

5659
if level == Logging.Error
57-
throw(ErrorException(message))
60+
throw(ErrorException(msg))
5861
end
5962
end
6063

6164
function emit_message(message::AbstractString,
62-
level::Nothing, file, line, _module)
65+
level::Nothing, option, file, line, _module)
6366
end
6467

6568
function emit_message(
66-
f::Function, level::Nothing, file, line, _module)
69+
f::Function, level::Nothing, option, file, line, _module)
6770
end
6871

6972
function get_message_level(verb::AbstractVerbositySpecifier, option)
@@ -146,6 +149,7 @@ macro SciMLMessage(f_or_message, verb, option)
146149
expr = quote
147150
emit_message($(esc(f_or_message)),
148151
get_message_level($(esc(verb)), $(esc(option))),
152+
$(esc(option)),
149153
$file,
150154
$line,
151155
$_module)

test/basics.jl

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,17 @@ end
5151
@testset "Basic tests" begin
5252
verbose = TestVerbosity()
5353

54-
@test_logs (:warn, "Test1") @SciMLMessage("Test1", verbose, :test1)
55-
@test_logs (:info, "Test2") @SciMLMessage("Test2", verbose, :test2)
56-
@test_logs (:error, "Test3") @test_throws "Test3" begin
54+
@test_logs (:warn, r"Test1") @SciMLMessage("Test1", verbose, :test1)
55+
@test_logs (:info, r"Test2") @SciMLMessage("Test2", verbose, :test2)
56+
@test_logs (:error, r"Test3") @test_throws "Test3" begin
5757
@SciMLMessage("Test3", verbose, :test3)
5858
end
5959
@test_logs min_level = Logging.Debug @SciMLMessage("Test4", verbose, :test4)
6060

6161
x = 30
6262
y = 30
6363

64-
@test_logs (:warn, "Test1: 60") @SciMLMessage(verbose, :test1) do
64+
@test_logs (:warn, r"Test1: 60") @SciMLMessage(verbose, :test1) do
6565
z = x + y
6666
"Test1: $z"
6767
end
@@ -74,15 +74,15 @@ end
7474
verbose_none = TestVerbosity(None())
7575

7676
# All preset should log info level messages
77-
@test_logs (:info, "All preset test") @SciMLMessage("All preset test", verbose_all, :test1)
77+
@test_logs (:info, r"All preset test") @SciMLMessage("All preset test", verbose_all, :test1)
7878

7979
# Minimal preset should only log errors and throw for error messages
80-
@test_logs (:error, "Minimal preset test") @test_throws ErrorException("Minimal preset test") begin
80+
@test_logs (:error, r"Minimal preset test") @test_throws ErrorException("Verbosity toggle: test1 \n Minimal preset test") begin
8181
@SciMLMessage("Minimal preset test", verbose_minimal, :test1)
8282
end
8383

8484
# Test that minimal preset throws for test3 (which is ErrorLevel)
85-
@test_logs (:error, "Minimal error on test3") @test_throws ErrorException("Minimal error on test3") begin
85+
@test_logs (:error, r"Minimal error on test3") @test_throws ErrorException("Verbosity toggle: test3 \n Minimal error on test3") begin
8686
@SciMLMessage("Minimal error on test3", verbose_minimal, :test3)
8787
end
8888

@@ -107,7 +107,7 @@ end
107107
verbose = TestVerbosity()
108108

109109
# Test that @SciMLMessage can be called inside another @SciMLMessage function block
110-
@test_logs (:warn, "Inner message from nested call") (:info, "Outer message with nested result") begin
110+
@test_logs (:warn, r"Inner message from nested call") (:info, r"Outer message with nested result") begin
111111
result = @SciMLMessage(verbose, :test2) do
112112
@SciMLMessage("Inner message from nested call", verbose, :test1)
113113
"Outer message with nested result"
@@ -116,7 +116,7 @@ end
116116

117117
# Test nested with both function-based inner and outer
118118
counter = 0
119-
@test_logs (:info, "Inner computation: 5") (:warn, "Outer result: 5") begin
119+
@test_logs (:info, r"Inner computation: 5") (:warn, r"Outer result: 5") begin
120120
@SciMLMessage(verbose, :test1) do
121121
inner_result = @SciMLMessage(verbose, :test2) do
122122
counter = 5
@@ -129,13 +129,13 @@ end
129129

130130
@testset "Boolean verbosity" begin
131131
# Test with true - should emit at WarnLevel (three-arg form)
132-
@test_logs (:warn, "Message with verbose=true") @SciMLMessage("Message with verbose=true", true, :ignored)
132+
@test_logs (:warn, r"Message with verbose=true") @SciMLMessage("Message with verbose=true", true, :ignored)
133133

134134
# Test with false - should not emit anything (three-arg form)
135135
@test_logs min_level = Logging.Debug @SciMLMessage("Message with verbose=false", false, :ignored)
136136

137137
# Test with function form and true (three-arg form)
138-
@test_logs (:warn, "Computed message: 42") @SciMLMessage(true, :ignored) do
138+
@test_logs (:warn, r"Computed message: 42") @SciMLMessage(true, :ignored) do
139139
x = 40 + 2
140140
"Computed message: $x"
141141
end
@@ -149,13 +149,13 @@ end
149149
@test !computation_ran # Verify function was never called when verbose=false
150150

151151
# Test two-argument form with true
152-
@test_logs (:warn, "Two-arg form with true") @SciMLMessage("Two-arg form with true", true)
152+
@test_logs (:warn, r"Two-arg form with true") @SciMLMessage("Two-arg form with true", true)
153153

154154
# Test two-argument form with false
155155
@test_logs min_level = Logging.Debug @SciMLMessage("Two-arg form with false", false)
156156

157157
# Test two-argument form with function and true
158-
@test_logs (:warn, "Two-arg computed: 100") @SciMLMessage(true) do
158+
@test_logs (:warn, r"Two-arg computed: 100") @SciMLMessage(true) do
159159
y = 50 * 2
160160
"Two-arg computed: $y"
161161
end

0 commit comments

Comments
 (0)