Factor out verboseDo to encapsulate magenta output color

This commit is contained in:
Rangi
2026-07-13 18:05:55 -04:00
parent ea3a8ee171
commit 304cbb464f
8 changed files with 59 additions and 95 deletions
+11
View File
@@ -6,6 +6,9 @@
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include "helpers.hpp" // Procedure
#include "style.hpp"
// This macro does not evaluate its arguments unless the condition is true. // This macro does not evaluate its arguments unless the condition is true.
#define verbosePrint(level, ...) \ #define verbosePrint(level, ...) \
do { \ do { \
@@ -30,6 +33,14 @@ bool checkVerbosity(Verbosity level);
[[gnu::format(printf, 1, 2)]] [[gnu::format(printf, 1, 2)]]
void printVerbosely(char const *fmt, ...); void printVerbosely(char const *fmt, ...);
void verboseDo(Verbosity level, Procedure<> auto callback) {
if (checkVerbosity(level)) {
style_Set(stderr, STYLE_MAGENTA, false);
callback();
style_Reset(stderr);
}
}
void printVVVVVVerbosity(); void printVVVVVVerbosity();
#endif // RGBDS_VERBOSITY_HPP #endif // RGBDS_VERBOSITY_HPP
+11 -15
View File
@@ -2315,26 +2315,22 @@ yy::parser::symbol_type yylex() {
return yy::parser::symbol_type(token.type, std::get<uint32_t>(token.value)); return yy::parser::symbol_type(token.type, std::get<uint32_t>(token.value));
} else if (std::holds_alternative<std::string>(token.value)) { } else if (std::holds_alternative<std::string>(token.value)) {
// LCOV_EXCL_START // LCOV_EXCL_START
if (checkVerbosity(VERB_TRACE)) { verboseDo(VERB_TRACE, [&]() {
style_Set(stderr, STYLE_MAGENTA, false);
fprintf(stderr, "Lexed `%s` token (", yy::parser::symbol_type(token.type).name()); fprintf(stderr, "Lexed `%s` token (", yy::parser::symbol_type(token.type).name());
verboseOutputString(std::get<std::string>(token.value)); verboseOutputString(std::get<std::string>(token.value));
fputs(")\n", stderr); fputs(")\n", stderr);
style_Reset(stderr); });
}
// LCOV_EXCL_STOP // LCOV_EXCL_STOP
return yy::parser::symbol_type(token.type, std::get<std::string>(token.value)); return yy::parser::symbol_type(token.type, std::get<std::string>(token.value));
} else if (std::holds_alternative<InternedStr>(token.value)) { } else if (std::holds_alternative<InternedStr>(token.value)) {
// LCOV_EXCL_START // LCOV_EXCL_START
if (checkVerbosity(VERB_TRACE)) { verboseDo(VERB_TRACE, [&]() {
style_Set(stderr, STYLE_MAGENTA, false);
fprintf( fprintf(
stderr, "Lexed `%s` token (interned ", yy::parser::symbol_type(token.type).name() stderr, "Lexed `%s` token (interned ", yy::parser::symbol_type(token.type).name()
); );
verboseOutputString(std::get<InternedStr>(token.value).str()); verboseOutputString(std::get<InternedStr>(token.value).str());
fputs(")\n", stderr); fputs(")\n", stderr);
style_Reset(stderr); });
}
// LCOV_EXCL_STOP // LCOV_EXCL_STOP
return yy::parser::symbol_type(token.type, std::get<InternedStr>(token.value)); return yy::parser::symbol_type(token.type, std::get<InternedStr>(token.value));
} else { } else {
@@ -2392,13 +2388,13 @@ static Capture makeCapture(char const *name, InvocableR<int, int> auto callback)
} }
// LCOV_EXCL_START // LCOV_EXCL_START
if (checkVerbosity(VERB_TRACE) && capture.span.ptr) { verboseDo(VERB_TRACE, [&]() {
style_Set(stderr, STYLE_MAGENTA, false); if (capture.span.ptr) {
fprintf(stderr, "Captured %s (", name); fprintf(stderr, "Captured %s (", name);
verboseOutputString(std::string_view{capture.span.ptr.get(), capture.span.size}); verboseOutputString(std::string_view{capture.span.ptr.get(), capture.span.size});
fputs(")\n", stderr); fputs(")\n", stderr);
style_Reset(stderr); }
} });
// LCOV_EXCL_STOP // LCOV_EXCL_STOP
assume(!lexerState->atLineStart); // `skipToLeadingKeyword` moves past the start of the line assume(!lexerState->atLineStart); // `skipToLeadingKeyword` moves past the start of the line
+2 -10
View File
@@ -24,7 +24,7 @@
#include "helpers.hpp" #include "helpers.hpp"
#include "parser.hpp" // Generated from parser.y #include "parser.hpp" // Generated from parser.y
#include "platform.hpp" #include "platform.hpp"
#include "style.hpp" #include "style.hpp" // style_Parse
#include "usage.hpp" #include "usage.hpp"
#include "util.hpp" // UpperMap #include "util.hpp" // UpperMap
#include "verbosity.hpp" #include "verbosity.hpp"
@@ -373,12 +373,6 @@ static void parseArg(int ch, char *arg) {
// LCOV_EXCL_START // LCOV_EXCL_START
static void verboseOutputConfig() { static void verboseOutputConfig() {
if (!checkVerbosity(VERB_CONFIG)) {
return;
}
style_Set(stderr, STYLE_MAGENTA, false);
usage.printVersion(true); usage.printVersion(true);
printVVVVVVerbosity(); printVVVVVVerbosity();
@@ -490,8 +484,6 @@ static void verboseOutputConfig() {
} }
} }
fputs("Ready for assembly\n", stderr); fputs("Ready for assembly\n", stderr);
style_Reset(stderr);
} }
// LCOV_EXCL_STOP // LCOV_EXCL_STOP
@@ -519,7 +511,7 @@ int main(int argc, char *argv[]) {
options.targetFileName = options.objectFileName; options.targetFileName = options.objectFileName;
} }
verboseOutputConfig(); verboseDo(VERB_CONFIG, verboseOutputConfig);
if (!localOptions.inputFileName) { if (!localOptions.inputFileName) {
usage.printAndExit("No input file specified (pass \"-\" to read from standard input)"); usage.printAndExit("No input file specified (pass \"-\" to read from standard input)");
+2 -10
View File
@@ -19,7 +19,7 @@
#include "file.hpp" #include "file.hpp"
#include "helpers.hpp" #include "helpers.hpp"
#include "platform.hpp" #include "platform.hpp"
#include "style.hpp" #include "style.hpp" // style_Parse
#include "usage.hpp" #include "usage.hpp"
#include "util.hpp" #include "util.hpp"
#include "verbosity.hpp" #include "verbosity.hpp"
@@ -474,12 +474,6 @@ static void parseArg(int ch, char *arg) {
// LCOV_EXCL_START // LCOV_EXCL_START
static void verboseOutputConfig() { static void verboseOutputConfig() {
if (!checkVerbosity(VERB_CONFIG)) {
return;
}
style_Set(stderr, STYLE_MAGENTA, false);
usage.printVersion(true); usage.printVersion(true);
printVVVVVVerbosity(); printVVVVVVerbosity();
@@ -609,8 +603,6 @@ static void verboseOutputConfig() {
fprintf(stderr, "\tReverse image width: %" PRIu16 " tiles\n", options.reversedWidth); fprintf(stderr, "\tReverse image width: %" PRIu16 " tiles\n", options.reversedWidth);
} }
fputs("Ready for conversion\n", stderr); fputs("Ready for conversion\n", stderr);
style_Reset(stderr);
} }
// LCOV_EXCL_STOP // LCOV_EXCL_STOP
@@ -684,7 +676,7 @@ int main(int argc, char *argv[]) {
parseExternalPalSpec(localOptions.externalPalSpec->c_str()); parseExternalPalSpec(localOptions.externalPalSpec->c_str());
} }
verboseOutputConfig(); // LCOV_EXCL_LINE verboseDo(VERB_CONFIG, verboseOutputConfig);
// Do not do anything if option parsing went wrong. // Do not do anything if option parsing went wrong.
requireZeroErrors(); requireZeroErrors();
+12 -17
View File
@@ -18,7 +18,6 @@
#include <vector> #include <vector>
#include "helpers.hpp" #include "helpers.hpp"
#include "style.hpp"
#include "verbosity.hpp" #include "verbosity.hpp"
#include "gfx/color_set.hpp" #include "gfx/color_set.hpp"
@@ -259,22 +258,18 @@ public:
static void verboseOutputAssignments( static void verboseOutputAssignments(
std::vector<AssignedSets> const &assignments, std::vector<ColorSet> const &colorSets std::vector<AssignedSets> const &assignments, std::vector<ColorSet> const &colorSets
) { ) {
if (!checkVerbosity(VERB_INFO)) { verboseDo(VERB_INFO, [&]() {
return; for (AssignedSets const &assignment : assignments) {
} fputs("{ ", stderr);
for (ColorSetAttrs const &attrs : assignment) {
style_Set(stderr, STYLE_MAGENTA, false); fprintf(stderr, "[%zu] ", attrs.colorSetIndex);
for (AssignedSets const &assignment : assignments) { for (uint16_t colorIndex : colorSets[attrs.colorSetIndex]) {
fputs("{ ", stderr); fprintf(stderr, "%04" PRIx16 ", ", colorIndex);
for (ColorSetAttrs const &attrs : assignment) { }
fprintf(stderr, "[%zu] ", attrs.colorSetIndex);
for (uint16_t colorIndex : colorSets[attrs.colorSetIndex]) {
fprintf(stderr, "%04" PRIx16 ", ", colorIndex);
} }
fprintf(stderr, "} (volume = %zu)\n", assignment.volume());
} }
fprintf(stderr, "} (volume = %zu)\n", assignment.volume()); });
}
style_Reset(stderr);
} }
// LCOV_EXCL_STOP // LCOV_EXCL_STOP
@@ -553,13 +548,13 @@ std::pair<std::vector<size_t>, size_t> overloadAndRemove(std::vector<ColorSet> c
} }
} }
verboseOutputAssignments(assignments, colorSets); // LCOV_EXCL_LINE verboseOutputAssignments(assignments, colorSets);
// "Decant" the result // "Decant" the result
decant(assignments, colorSets); decant(assignments, colorSets);
// Note that the result does not contain any empty palettes // Note that the result does not contain any empty palettes
verboseOutputAssignments(assignments, colorSets); // LCOV_EXCL_LINE verboseOutputAssignments(assignments, colorSets);
std::vector<size_t> mappings(colorSets.size()); std::vector<size_t> mappings(colorSets.size());
for (size_t i = 0; i < assignments.size(); ++i) { for (size_t i = 0; i < assignments.size(); ++i) {
+2 -5
View File
@@ -16,7 +16,6 @@
#include "diagnostics.hpp" #include "diagnostics.hpp"
#include "helpers.hpp" #include "helpers.hpp"
#include "style.hpp"
#include "verbosity.hpp" #include "verbosity.hpp"
#include "gfx/rgba.hpp" #include "gfx/rgba.hpp"
@@ -158,15 +157,13 @@ Png::Png(char const *filename, std::streambuf &file) {
); );
} }
if (checkVerbosity(VERB_INFO)) { verboseDo(VERB_INFO, [&]() {
style_Set(stderr, STYLE_MAGENTA, false);
fprintf(stderr, "Embedded PNG palette has %d colors: [", nbColors); fprintf(stderr, "Embedded PNG palette has %d colors: [", nbColors);
for (int i = 0; i < nbColors; ++i) { for (int i = 0; i < nbColors; ++i) {
fprintf(stderr, "%s#%08x", i > 0 ? ", " : "", palette[i].toCSS()); fprintf(stderr, "%s#%08x", i > 0 ? ", " : "", palette[i].toCSS());
} }
fprintf(stderr, "]\n"); fprintf(stderr, "]\n");
style_Reset(stderr); });
}
} else { } else {
verbosePrint(VERB_INFO, "No embedded PNG palette\n"); verbosePrint(VERB_INFO, "No embedded PNG palette\n");
} }
+17 -28
View File
@@ -22,7 +22,6 @@
#include "file.hpp" #include "file.hpp"
#include "helpers.hpp" #include "helpers.hpp"
#include "itertools.hpp" #include "itertools.hpp"
#include "style.hpp"
#include "verbosity.hpp" #include "verbosity.hpp"
#include "gfx/color_set.hpp" #include "gfx/color_set.hpp"
@@ -350,16 +349,16 @@ static std::pair<std::vector<size_t>, std::vector<Palette>>
assume(mappings.size() == colorSets.size()); assume(mappings.size() == colorSets.size());
// LCOV_EXCL_START // LCOV_EXCL_START
if (checkVerbosity(VERB_INFO)) { // Ideally we'd use an implicit `[&]` capture, but C++20 P0588R1 (which allows "reference to
style_Set(stderr, STYLE_MAGENTA, false); // local binding declared in enclosing function") is not sufficiently supported by clang.
verboseDo(VERB_INFO, [&mappingsV = mappings, &nbPalettesV = nbPalettes]() {
fprintf( fprintf(
stderr, "Color set mappings: (%zu palette%s)\n", nbPalettes, nbPalettes != 1 ? "s" : "" stderr, "Color set mappings: (%zu palette%s)\n", nbPalettesV, nbPalettesV != 1 ? "s" : ""
); );
for (size_t i = 0; i < mappings.size(); ++i) { for (size_t i = 0; i < mappingsV.size(); ++i) {
fprintf(stderr, "%zu -> %zu\n", i, mappings[i]); fprintf(stderr, "%zu -> %zu\n", i, mappingsV[i]);
} }
style_Reset(stderr); });
}
// LCOV_EXCL_STOP // LCOV_EXCL_STOP
std::vector<Palette> palettes(nbPalettes); std::vector<Palette> palettes(nbPalettes);
@@ -455,8 +454,7 @@ static std::pair<std::vector<size_t>, std::vector<Palette>>
static void outputPalettes(std::vector<Palette> const &palettes) { static void outputPalettes(std::vector<Palette> const &palettes) {
// LCOV_EXCL_START // LCOV_EXCL_START
if (checkVerbosity(VERB_INFO)) { verboseDo(VERB_INFO, [&]() {
style_Set(stderr, STYLE_MAGENTA, false);
for (Palette const &palette : palettes) { for (Palette const &palette : palettes) {
fputs("{ ", stderr); fputs("{ ", stderr);
for (uint16_t colorIndex : palette) { for (uint16_t colorIndex : palette) {
@@ -464,8 +462,7 @@ static void outputPalettes(std::vector<Palette> const &palettes) {
} }
fputs("}\n", stderr); fputs("}\n", stderr);
} }
style_Reset(stderr); });
}
// LCOV_EXCL_STOP // LCOV_EXCL_STOP
if (palettes.size() > options.nbPalettes) { if (palettes.size() > options.nbPalettes) {
@@ -950,8 +947,7 @@ void process() {
Image image(options.input); // This also sets `hasTransparentPixels` as a side effect Image image(options.input); // This also sets `hasTransparentPixels` as a side effect
// LCOV_EXCL_START // LCOV_EXCL_START
if (checkVerbosity(VERB_INFO)) { verboseDo(VERB_INFO, [&]() {
style_Set(stderr, STYLE_MAGENTA, false);
fputs("Image colors: [ ", stderr); fputs("Image colors: [ ", stderr);
for (std::optional<Rgba> const &slot : image.colors) { for (std::optional<Rgba> const &slot : image.colors) {
if (!slot.has_value()) { if (!slot.has_value()) {
@@ -960,8 +956,7 @@ void process() {
fprintf(stderr, "#%08x, ", slot->toCSS()); fprintf(stderr, "#%08x, ", slot->toCSS());
} }
fputs("]\n", stderr); fputs("]\n", stderr);
style_Reset(stderr); });
}
// LCOV_EXCL_STOP // LCOV_EXCL_STOP
if (options.palSpecType == Options::DMG) { if (options.palSpecType == Options::DMG) {
@@ -1050,8 +1045,7 @@ void process() {
case ColorSet::STRICT_SUPERSET: case ColorSet::STRICT_SUPERSET:
// Override the previous color set that this one is a strict superset of // Override the previous color set that this one is a strict superset of
if (checkVerbosity(VERB_DEBUG)) { verboseDo(VERB_DEBUG, [&]() {
style_Set(stderr, STYLE_MAGENTA, false);
fprintf( fprintf(
stderr, stderr,
"- Tile (%" PRIu32 ", %" PRIu32 ") overrides color set #%zu: [", "- Tile (%" PRIu32 ", %" PRIu32 ") overrides color set #%zu: [",
@@ -1067,8 +1061,7 @@ void process() {
fprintf(stderr, "$%04x, ", color); fprintf(stderr, "$%04x, ", color);
} }
fputs("]\n", stderr); fputs("]\n", stderr);
style_Reset(stderr); });
}
colorSets[n] = colorSet; colorSets[n] = colorSet;
// Remove any other color sets that we are also a strict superset of // Remove any other color sets that we are also a strict superset of
@@ -1116,8 +1109,7 @@ void process() {
attrs.colorSetID = colorSets.size(); attrs.colorSetID = colorSets.size();
colorSets.push_back(colorSet); colorSets.push_back(colorSet);
if (checkVerbosity(VERB_DEBUG)) { verboseDo(VERB_DEBUG, [&]() {
style_Set(stderr, STYLE_MAGENTA, false);
fprintf( fprintf(
stderr, stderr,
"- Tile (%" PRIu32 ", %" PRIu32 ") adds color set #%zu: [", "- Tile (%" PRIu32 ", %" PRIu32 ") adds color set #%zu: [",
@@ -1129,8 +1121,7 @@ void process() {
fprintf(stderr, "$%04x, ", color); fprintf(stderr, "$%04x, ", color);
} }
fputs("]\n", stderr); fputs("]\n", stderr);
style_Reset(stderr); });
}
continue_visiting_tiles:; continue_visiting_tiles:;
} }
@@ -1142,8 +1133,7 @@ continue_visiting_tiles:;
colorSets.size() != 1 ? "s" : "" colorSets.size() != 1 ? "s" : ""
); );
// LCOV_EXCL_START // LCOV_EXCL_START
if (checkVerbosity(VERB_INFO)) { verboseDo(VERB_INFO, [&]() {
style_Set(stderr, STYLE_MAGENTA, false);
for (ColorSet const &colorSet : colorSets) { for (ColorSet const &colorSet : colorSets) {
fputs("[ ", stderr); fputs("[ ", stderr);
for (uint16_t color : colorSet) { for (uint16_t color : colorSet) {
@@ -1151,8 +1141,7 @@ continue_visiting_tiles:;
} }
fputs("]\n", stderr); fputs("]\n", stderr);
} }
style_Reset(stderr); });
}
// LCOV_EXCL_STOP // LCOV_EXCL_STOP
if (colorSets.empty()) { if (colorSets.empty()) {
+2 -10
View File
@@ -17,7 +17,7 @@
#include "diagnostics.hpp" #include "diagnostics.hpp"
#include "linkdefs.hpp" #include "linkdefs.hpp"
#include "script.hpp" // Generated from script.y #include "script.hpp" // Generated from script.y
#include "style.hpp" #include "style.hpp" // style_Parse
#include "usage.hpp" #include "usage.hpp"
#include "util.hpp" // UpperMap, printChar #include "util.hpp" // UpperMap, printChar
#include "verbosity.hpp" #include "verbosity.hpp"
@@ -329,12 +329,6 @@ static void parseArg(int ch, char *arg) {
// LCOV_EXCL_START // LCOV_EXCL_START
static void verboseOutputConfig() { static void verboseOutputConfig() {
if (!checkVerbosity(VERB_CONFIG)) {
return;
}
style_Set(stderr, STYLE_MAGENTA, false);
usage.printVersion(true); usage.printVersion(true);
printVVVVVVerbosity(); printVVVVVVerbosity();
@@ -414,15 +408,13 @@ static void verboseOutputConfig() {
// -n/--sym // -n/--sym
printPath("Output sym file", options.symFileName); printPath("Output sym file", options.symFileName);
fputs("Ready for linking\n", stderr); fputs("Ready for linking\n", stderr);
style_Reset(stderr);
} }
// LCOV_EXCL_STOP // LCOV_EXCL_STOP
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
cli_ParseArgs(argc, argv, optstring, longopts, parseArg, usage); cli_ParseArgs(argc, argv, optstring, longopts, parseArg, usage);
verboseOutputConfig(); verboseDo(VERB_CONFIG, verboseOutputConfig);
if (localOptions.inputFileNames.empty()) { if (localOptions.inputFileNames.empty()) {
usage.printAndExit("No input file specified (pass \"-\" to read from standard input)"); usage.printAndExit("No input file specified (pass \"-\" to read from standard input)");