-
Notifications
You must be signed in to change notification settings - Fork 1
Logging interface #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from 3 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
11facf1
add logging file
lkdvos 2d740b8
add logging statements
lkdvos 81c23a1
some progress
lkdvos e11636f
some actual progress
lkdvos 0b7ea5d
small fixes
lkdvos ad95017
fix Printf compat
lkdvos 195a9c7
minor optimizations
lkdvos 2e9d3df
one more optimization attempt
lkdvos b9c9b26
various improvements and rework interface.md
lkdvos e293ccd
small docstring typos
lkdvos 0eb141a
stopping criterion docs and small fixes
lkdvos 8d5f452
WIP
lkdvos c40765e
collapse docstrings by default
lkdvos 464882c
write logging docs
lkdvos d285b04
clean up and finish logging docs
lkdvos ba8739a
fix test
lkdvos 78006a1
add iteration test
lkdvos e4fe20e
add error test
lkdvos 51f7a68
Add test for `IfAction`
lkdvos c356152
Add GroupAction test
lkdvos bd1d900
add global toggle test
lkdvos 703284a
`GroupAction` -> `ActionGroup`
lkdvos 042a369
Update interface.md
lkdvos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,127 @@ | ||
| """ | ||
| log!(problem::Problem, algorithm::Algorithm, state::State; context::Symbol) -> nothing | ||
|
|
||
| Generate a log record for the given `(problem, algorithm, state)`, using the logging action associated to `context`. | ||
| By default, the following events trigger a logging action with the given `context`: | ||
|
|
||
| | context | event | | ||
| | --------- | ------------------------------------- | | ||
| | :Start | The solver has been initialized. | | ||
| | :PreStep | The solver is about to take a step. | | ||
| | :PostStep | The solver has taken a step. | | ||
| | :Stop | The solver has finished. | | ||
|
|
||
| Specific algorithms can associate other events with other contexts. | ||
|
|
||
| See also [`register_action!`](@ref) to associate custom actions with these contexts. | ||
| """ | ||
| function log!(problem::Problem, algorithm::Algorithm, state::State; context)::Nothing | ||
| action = @something(logging_action(problem, algorithm, state, context), return nothing) | ||
| # TODO: filter out `nothing` logdata | ||
| @logmsg( | ||
| loglevel(action), logdata!(action, problem, algorithm, state), | ||
| _id = logid(action), _group = loggroup(algorithm) | ||
| ) | ||
| return nothing | ||
| end | ||
|
|
||
| """ | ||
| logging_action(problem::Problem, algorithm::Algorithm, state::State, context::Symbol) | ||
| -> Union{Nothing,LoggingAction} | ||
|
|
||
| Obtain the registered logging action associated to an event identified by `context`. | ||
| The default implementation assumes a dictionary-like object `state.logging_action`, which holds | ||
| the different registered actions. | ||
| """ | ||
| function logging_action(::Problem, ::Algorithm, state::State, context::Symbol) | ||
| return get(state.logging_actions, context, nothing) | ||
| end | ||
|
|
||
| @doc """ | ||
| loggroup(algorithm::Algorithm) -> Symbol | ||
| loggroup(::Type{<:Algorithm}) -> Symbol | ||
|
|
||
| Generate a group id to attach to all log records for a given algorithm. | ||
| """ loggroup | ||
|
|
||
| loggroup(algorithm::Algorithm) = loggroup(typeof(algorithm)) | ||
| loggroup(Alg::Type{<:Algorithm}) = Base.nameof(Alg) | ||
|
|
||
| # Sources | ||
| # ------- | ||
| """ | ||
| LoggingAction | ||
|
|
||
| Abstract supertype for defining an action that generates a log record. | ||
|
|
||
| ## Methods | ||
| Any concrete subtype should at least implement the following method: | ||
| - [`logdata!(action, problem, algorithm, state)`](@ref logdata!) : generate the data for the log record. | ||
|
|
||
| Addionally, the following methods can be specialized to alter the default behavior: | ||
| - [`loglevel(action) = Logging.Info`](@ref loglevel) : specify the logging level of the generated record. | ||
| - [`logid(action) = objectid(action)`](@ref logid) : specify a unique identifier to associate with the record. | ||
| """ | ||
| abstract type LoggingAction end | ||
|
|
||
| logdata!(::LoggingAction, ::Problem, ::Algorithm, ::State) = missing | ||
| loglevel(::LoggingAction) = Logging.Info | ||
| logid(action::LoggingAction) = objectid(action) | ||
|
|
||
| struct LogCallback{F} <: LoggingAction | ||
| f::F | ||
| end | ||
|
|
||
| logdata!(action::LogCallback, problem, algorithm, state) = | ||
| action.f(problem, algorithm, state) | ||
|
|
||
| struct LogGroup{A <: LoggingAction} <: LoggingAction | ||
| actions::Vector{A} | ||
| end | ||
|
|
||
| loglevel(alg::LogGroup) = maximum(loglevel, alg.actions) | ||
|
|
||
| logdata!(action::LogGroup, problem, algorithm, state) = map(action.actions) do action | ||
| return logdata!(action, problem, algorithm, state) | ||
| end | ||
|
|
||
| struct LogLvl{F, A <: LoggingAction} <: LoggingAction | ||
| action::A | ||
| lvl::LogLevel | ||
| end | ||
|
|
||
| loglevel(alg::LogLvl) = alg.lvl | ||
|
|
||
| struct LogIf{F, A <: LoggingAction} <: LoggingAction | ||
| predicate::F | ||
| action::A | ||
| end | ||
|
|
||
| # first cheap check through the level | ||
| loglevel(alg::LogIf) = loglevel(alg.action) | ||
|
|
||
| # second check through the predicate | ||
| logdata!(action::LogIf, problem::Problem, algorithm::Algorithm, state::State) = | ||
| action.predicate(problem, algorithm, state) ? logdata!(action.action, problem, algorithm, state) : nothing | ||
|
|
||
| # Sinks | ||
| # ----- | ||
| struct StringFormat{F <: PrintF.Format} | ||
| format::F | ||
| end | ||
| function (formatter::StringFormat)(io::IO, lvl, msg, _module, group, id, file = nothing, line = nothing; indent::Integer = 0, kwargs...) | ||
| iob = IOBuffer() | ||
| return ioc = IOContext(iob, io) | ||
|
|
||
| end | ||
|
|
||
| struct FormatterGroup{K, V} | ||
| rules::Dict{K, V} | ||
| end | ||
|
|
||
| function (formatter::AlgorithmFormatter)(io::IO, log) | ||
| rule = get(formatter.rules, log.id, nothing) | ||
| isnothing(rule) ? println(io, log) : | ||
| rule(io, log.level, log.message, log._module, log.group, log.id, log.file, log.line; log.kwargs...) | ||
| return nothing | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.