Zonemaster::CLI: don’t lose early log messages #416
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.
Purpose
This PR fixes a problem where, in one particular set of circumstances (caused by user error),
zonemaster-cli
fails to display messages on the terminal.Adding fake delegation by means of the --ns command-line option can sometimes elicit early Zonemaster::Engine log messages that are lost because the translator isn’t properly initialized at this stage. For reasons that I do not understand fully yet, this situation causes a condition where the zonemaster-cli script displays no message at all, even though it should.
As a workaround for this problem, we set the callback to Zonemaster::Engine::Logger twice. First, we set it to a closure that simply appends them to a queue of yet-to-be-printed messages. Right after the translator is initialized, we print the header and messages stored by the first callback before switching the callback to the function we actually wanted to use, which prints the messages on the terminal.
A brief test shows no unintended side-effects with other output formats such as JSON. But the run() method in Zonemaster::CLI is long and has a high cyclomatic complexity, which doesn’t really help with testing!
Context
Fixes #402.
Changes
How to test this PR
Run the following command:
zonemaster-cli
should display at least one log message complaining about the fake delegation missing required glue. Previously, it would only display the table header and no message at all.