Skip to content

Commit

Permalink
Allow d.msg in DebugIfEntry to contain an identifier to print the f…
Browse files Browse the repository at this point in the history
…ield (#351)

* Make `d.msg` in DebugIfEntry more general and allow to have an identifier to print the field as well.
* Use a check that is independent of the Printf internals.
  • Loading branch information
kellertuer authored Jan 28, 2024
1 parent b389340 commit 89d88ad
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
6 changes: 6 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable Changes to the Julia package `Manopt.jl` will be documented in this
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [unreleased]

### Added

* Allow the `message=` of the `DebugIfEntry` debug action to contain a format element to print the field in the message as well.

## [0.4.50] January 26, 2024

### Fixed
Expand Down
16 changes: 11 additions & 5 deletions src/plans/debug.jl
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,11 @@ end
@doc raw"""
DebugIfEntry <: DebugAction
Issue a warning, info or error if a certain field does _not_ pass a check
Issue a warning, info or error if a certain field does _not_ pass a check.
The `message` is printed in this case. If it contains a `@printf` argument identifier,
that one will be filled with the value of the `field`.
That way you can print the vaule in this case as well.
# Fields
Expand Down Expand Up @@ -400,10 +404,12 @@ mutable struct DebugIfEntry{F} <: DebugAction
end
function (d::DebugIfEntry)(::AbstractManoptProblem, st::AbstractManoptSolverState, i)
if (i >= 0) && (!d.check(getfield(st, d.field)))
d.type === :warn && (@warn "$(d.msg)")
d.type === :info && (@info "$(d.msg)")
d.type === :error && error(d.msg)
d.type === :print && print(d.io, d.msg)
format = Printf.Format(d.msg)
msg = !('%' d.msg) ? d.msg : Printf.format(format, getfield(st, d.field))
d.type === :warn && (@warn "$(msg)")
d.type === :info && (@info "$(msg)")
d.type === :error && error(msg)
d.type === :print && print(d.io, msg)
end
return nothing
end
Expand Down

0 comments on commit 89d88ad

Please sign in to comment.