Skip to content

Commit 4fb8130

Browse files
committed
rearrange
1 parent 36361f2 commit 4fb8130

File tree

1 file changed

+90
-92
lines changed

1 file changed

+90
-92
lines changed

src/verbosity.jl

Lines changed: 90 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,15 @@
11
@data Verbosity begin
22
None
3-
Edge
43
Info
54
Warn
65
Error
6+
Level(Int)
7+
Edge
78
All
89
Default
9-
Level(Int)
10-
end
11-
12-
13-
function message_level(verbose::AbstractVerbositySpecifier{true}, option, group)
14-
group = getproperty(verbose, group)
15-
opt_level = getproperty(group, option)
16-
17-
@match opt_level begin
18-
Verbosity.None() => nothing
19-
Verbosity.Info() => Logging.Info
20-
Verbosity.Warn() => Logging.Warn
21-
Verbosity.Error() => Logging.Error
22-
Verbosity.Level(i) => Logging.LogLevel(i)
23-
end
24-
end
25-
26-
function emit_message(
27-
f::Function, verbose::AbstractVerbositySpecifier{true}, option, group, file, line, _module)
28-
level = message_level(verbose, option, group)
2910

30-
if !isnothing(level)
31-
message = f()
32-
Base.@logmsg level message _file=file _line=line _module=_module
33-
end
3411
end
3512

36-
function emit_message(message::String, verbose::AbstractVerbositySpecifier{true}, option, group, file, line, _module)
37-
level = message_level(verbose, option, group)
38-
39-
if !isnothing(level)
40-
Base.@logmsg level message _file=file _line=line _module=_module
41-
end
42-
end
43-
44-
function emit_message(f, verbose::AbstractVerbositySpecifier{false}, option, group, file, line, _module)
45-
end
46-
47-
@doc doc"""
48-
A macro that emits a log message based on the log level specified in the `option` and `group` of the `AbstractVerbositySpecifier` supplied.
49-
50-
`f_or_message` may be a message String, or a 0-argument function that returns a String.
51-
52-
## Usage
53-
To emit a simple string, `@SciMLMessage("message", verbosity, :option, :group)` will emit a log message with the LogLevel specified in `verbosity`, at the appropriate `option` and `group`.
54-
55-
`@SciMLMessage` can also be used to emit a log message coming from the evaluation of a 0-argument function. This function is resolved in the environment of the macro call.
56-
Therefore it can use variables from the surrounding evironment. This may be useful if the log message writer wishes to carry out some calculations using existing variables
57-
and use them.
58-
59-
```julia
60-
x = 10
61-
y = 20
62-
63-
@SciMLMessage(verbosity, :option, :group) do
64-
z = x + y
65-
"Message is: x + y = \$z"
66-
end
67-
```
68-
"""
69-
macro SciMLMessage(f_or_message, verb, option, group)
70-
line = __source__.line
71-
file = string(__source__.file)
72-
_module = __module__
73-
return :(emit_message($(esc(f_or_message)), $(esc(verb)), $toggle, $group, $file, $line, $_module))
74-
end
75-
76-
7713
# Linear Verbosity
7814

7915
mutable struct LinearErrorControlVerbosity
@@ -88,9 +24,9 @@ function LinearErrorControlVerbosity(verbose::Verbosity.Type)
8824

8925
Verbosity.Error() => LinearErrorControlVerbosity(fill(Verbosity.Error(), nfields(LinearErrorControlVerbosity))...)
9026

91-
Verbosity.Default() => LinearErrorControlVerbosity(Verbosity.Info(), Verbosity.Error())
27+
Verbosity.Default() => LinearErrorControlVerbosity()
9228

93-
Verbosity.Edge() => LinearErrorControlVerbosity(Verbosity.Info(), Verbosity.Warn())
29+
Verbosity.Edge() => LinearErrorControlVerbosity()
9430

9531
_ => @error "Not a valid choice for verbosity."
9632
end
@@ -116,10 +52,9 @@ function LinearPerformanceVerbosity(verbose::Verbosity.Type)
11652
Verbosity.Error() => LinearPerformanceVerbosity(fill(
11753
Verbosity.Error(), nfields(LinearPerformanceVerbosity))...)
11854

119-
Verbosity.Default() => LinearPerformanceVerbosity(
120-
Verbosity.Info(), Verbosity.Error())
55+
Verbosity.Default() => LinearPerformanceVerbosity()
12156

122-
Verbosity.Edge() => LinearPerformanceVerbosity(Verbosity.Info(), Verbosity.Warn())
57+
Verbosity.Edge() => LinearPerformanceVerbosity()
12358

12459
_ => @error "Not a valid choice for verbosity."
12560
end
@@ -144,10 +79,9 @@ function LinearNumericalVerbosity(verbose::Verbosity.Type)
14479
Verbosity.Error() => LinearNumericalVerbosity(fill(
14580
Verbosity.Error(), nfields(LinearNumericalVerbosity))...)
14681

147-
Verbosity.Default() => LinearNumericalVerbosity(
148-
Verbosity.Info(), Verbosity.Error())
82+
Verbosity.Default() => LinearNumericalVerbosity()
14983

150-
Verbosity.Edge() => LinearNumericalVerbosity(Verbosity.Info(), Verbosity.Warn())
84+
Verbosity.Edge() => LinearNumericalVerbosity()
15185

15286
_ => @error "Not a valid choice for verbosity."
15387
end
@@ -203,18 +137,17 @@ function NonlinearErrorControlVerbosity(verbose::Verbosity.Type)
203137
Verbosity.Error() => NonlinearNumericalVerbosity(fill(
204138
Verbosity.Error(), nfields(NonlinearErrorControlVerbosity))...)
205139

206-
Verbosity.Default() => NonlinearErrorControlVerbosity(
207-
Verbosity.Info(), Verbosity.Error())
140+
Verbosity.Default() => NonlinearErrorControlVerbosity()
208141

209-
Verbosity.Edge() => NonlinearErrorControlVerbosity(Verbosity.Info(), Verbosity.Warn())
142+
Verbosity.Edge() => NonlinearErrorControlVerbosity()
210143

211144
_ => @error "Not a valid choice for verbosity."
212145
end
213146
end
214147

215148
mutable struct NonlinearPerformanceVerbosity
216149
@add_kwonly function NonlinearPerformanceVerbosity()
217-
NonlinearPerformanceVerbosity()
150+
new()
218151
end
219152

220153
end
@@ -231,11 +164,9 @@ function NonlinearPerformanceVerbosity(verbose::Verbosity.Type)
231164
Verbosity.Error() => NonlinearPerformanceVerbosity(fill(
232165
Verbosity.Error(), nfields(NonlinearPerformanceVerbosity))...)
233166

234-
Verbosity.Default() => NonlinearPerformanceVerbosity(
235-
Verbosity.Info(), Verbosity.Error())
167+
Verbosity.Default() => NonlinearPerformanceVerbosity()
236168

237-
Verbosity.Edge() => NonlinearPerformanceVerbosity(
238-
Verbosity.Info(), Verbosity.Warn())
169+
Verbosity.Edge() => NonlinearPerformanceVerbosity()
239170

240171
_ => @error "Not a valid choice for verbosity."
241172
end
@@ -244,7 +175,7 @@ end
244175

245176
mutable struct NonlinearNumericalVerbosity
246177
@add_kwonly function NonlinearNumericalVerbosity()
247-
NonlinearNumericalVerbosity()
178+
new()
248179
end
249180
end
250181

@@ -259,11 +190,9 @@ function NonlinearNumericalVerbosity(verbose::Verbosity.Type)
259190
Verbosity.Error() => NonlinearNumericalVerbosity(fill(
260191
Verbosity.Error(), nfields(NonlinearPerformanceVerbosity))...)
261192

262-
Verbosity.Default() => NonlinearNumericalVerbosity(
263-
Verbosity.Info(), Verbosity.Error())
193+
Verbosity.Default() => NonlinearNumericalVerbosity()
264194

265-
Verbosity.Edge() => NonlinearNumericalVerbosity(
266-
Verbosity.Info(), Verbosity.Warn())
195+
Verbosity.Edge() => NonlinearNumericalVerbosity()
267196

268197
_ => @error "Not a valid choice for verbosity."
269198
end
@@ -310,7 +239,7 @@ mutable struct ODEErrorControlVerbosity
310239
init_NaN::Verbosity.Type
311240

312241
@add_kwonly function ODEErrorControlVerbosity(dt_NaN, init_NaN)
313-
ODEErrorControlVerbosity(dt_NaN, init_NaN)
242+
new(dt_NaN, init_NaN)
314243
end
315244
end
316245

@@ -337,7 +266,7 @@ end
337266

338267
mutable struct ODEPerformanceVerbosity
339268
@add_kwonly function ODEPerformanceVerbosity(dt_NaN, init_NaN)
340-
ODEPerformanceVerbosity(dt_NaN, init_NaN)
269+
new(dt_NaN, init_NaN)
341270
end
342271
end
343272

@@ -354,15 +283,15 @@ function ODEPerformanceVerbosity(verbose::Verbosity.Type)
354283
Verbosity.Error() => ODEPerformanceVerbosity(fill(
355284
Verbosity.Error(), nfields(ODEPerformanceVerbosity))...)
356285

357-
Verbosity.Default() => ODEPerformanceVerbosity(Verbosity.Warn(), Verbosity.Error())
286+
Verbosity.Default() => ODEPerformanceVerbosity()
358287

359288
_ => @error "Not a valid choice for verbosity."
360289
end
361290
end
362291

363292
mutable struct ODENumericalVerbosity
364293
@add_kwonly function ODENumericalVerbosity(dt_NaN, init_NaN)
365-
ODEErrorControlVerbosity(dt_NaN, init_NaN)
294+
new(dt_NaN, init_NaN)
366295
end
367296
end
368297

@@ -379,7 +308,7 @@ function ODENumericalVerbosity(verbose::Verbosity.Type)
379308
Verbosity.Error() => ODENumericalVerbosity(fill(
380309
Verbosity.Error(), nfields(ODENumericalVerbosity))...)
381310

382-
Verbosity.Default() => ODENumericalVerbosity(Verbosity.Warn(), Verbosity.Error())
311+
Verbosity.Default() => ODENumericalVerbosity()
383312

384313
_ => @error "Not a valid choice for verbosity."
385314
end
@@ -458,3 +387,72 @@ function ODEVerbosity(; error_control = Verbosity.Default(), performance = Verbo
458387

459388
ODEVerbosity(nonlinear, linear, error_control_verbosity, performance_verbosity, numerical_verbosity)
460389
end
390+
391+
392+
# Utilities
393+
394+
function message_level(verbose::AbstractVerbositySpecifier{true}, option, group)
395+
group = getproperty(verbose, group)
396+
opt_level = getproperty(group, option)
397+
398+
@match opt_level begin
399+
Verbosity.None() => nothing
400+
Verbosity.Info() => Logging.Info
401+
Verbosity.Warn() => Logging.Warn
402+
Verbosity.Error() => Logging.Error
403+
Verbosity.Level(i) => Logging.LogLevel(i)
404+
end
405+
end
406+
407+
function emit_message(
408+
f::Function, verbose::AbstractVerbositySpecifier{true}, option, group, file, line, _module)
409+
level = message_level(verbose, option, group)
410+
411+
if !isnothing(level)
412+
message = f()
413+
Base.@logmsg level message _file=file _line=line _module=_module
414+
end
415+
end
416+
417+
function emit_message(message::String, verbose::AbstractVerbositySpecifier{true},
418+
option, group, file, line, _module)
419+
level = message_level(verbose, option, group)
420+
421+
if !isnothing(level)
422+
Base.@logmsg level message _file=file _line=line _module=_module
423+
end
424+
end
425+
426+
function emit_message(
427+
f, verbose::AbstractVerbositySpecifier{false}, option, group, file, line, _module)
428+
end
429+
430+
@doc doc"""
431+
A macro that emits a log message based on the log level specified in the `option` and `group` of the `AbstractVerbositySpecifier` supplied.
432+
433+
`f_or_message` may be a message String, or a 0-argument function that returns a String.
434+
435+
## Usage
436+
To emit a simple string, `@SciMLMessage("message", verbosity, :option, :group)` will emit a log message with the LogLevel specified in `verbosity`, at the appropriate `option` and `group`.
437+
438+
`@SciMLMessage` can also be used to emit a log message coming from the evaluation of a 0-argument function. This function is resolved in the environment of the macro call.
439+
Therefore it can use variables from the surrounding environment. This may be useful if the log message writer wishes to carry out some calculations using existing variables
440+
and use them in the log message.
441+
442+
```julia
443+
x = 10
444+
y = 20
445+
446+
@SciMLMessage(verbosity, :option, :group) do
447+
z = x + y
448+
"Message is: x + y = \$z"
449+
end
450+
```
451+
"""
452+
macro SciMLMessage(f_or_message, verb, option, group)
453+
line = __source__.line
454+
file = string(__source__.file)
455+
_module = __module__
456+
return :(emit_message(
457+
$(esc(f_or_message)), $(esc(verb)), $toggle, $group, $file, $line, $_module))
458+
end

0 commit comments

Comments
 (0)