From b2e064d842a133a53e790eace2b217cae9eddfa3 Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Thu, 23 Oct 2025 18:35:30 -0400 Subject: [PATCH] Add a new `LOG_WARNING` helper Signed-off-by: Juan Cruz Viotti --- src/command_bundle.cc | 24 +++++++++++++----------- src/command_lint.cc | 4 ++-- src/command_validate.cc | 7 ++++--- src/logger.h | 10 ++++++++-- src/resolver.h | 7 +++---- test/validate/pass_jsonl_empty.sh | 1 + 6 files changed, 31 insertions(+), 22 deletions(-) diff --git a/src/command_bundle.cc b/src/command_bundle.cc index be961cdd..45722bfb 100644 --- a/src/command_bundle.cc +++ b/src/command_bundle.cc @@ -9,6 +9,7 @@ #include "command.h" #include "configuration.h" #include "error.h" +#include "logger.h" #include "resolver.h" #include "utils.h" @@ -35,17 +36,18 @@ auto sourcemeta::jsonschema::bundle(const sourcemeta::core::Options &options) .recompose()); if (options.contains("without-id")) { - std::cerr << "warning: You are opting in to remove schema identifiers in " - "the bundled schema.\n"; - std::cerr << "The only legit use case of this advanced feature we know of " - "is to workaround\n"; - std::cerr << "non-compliant JSON Schema implementations such as Visual " - "Studio Code.\n"; - std::cerr << "Otherwise, this is not needed and may harm other use " - "cases. For example,\n"; - std::cerr << "you will be unable to reference the resulting schema from " - "other schemas\n"; - std::cerr << "using the --resolve/-r option.\n"; + sourcemeta::jsonschema::LOG_WARNING() + << "You are opting in to remove schema identifiers in " + "the bundled schema.\n" + << "The only legit use case of this advanced feature we know of " + "is to workaround\n" + << "non-compliant JSON Schema implementations such as Visual " + "Studio Code.\n" + << "Otherwise, this is not needed and may harm other use " + "cases. For example,\n" + << "you will be unable to reference the resulting schema from " + "other schemas\n" + << "using the --resolve/-r option.\n"; sourcemeta::core::for_editor(schema, sourcemeta::core::schema_official_walker, custom_resolver, dialect); diff --git a/src/command_lint.cc b/src/command_lint.cc index 88ad2624..97558ba7 100644 --- a/src/command_lint.cc +++ b/src/command_lint.cc @@ -28,8 +28,8 @@ static auto disable_lint_rules(sourcemeta::core::SchemaTransformer &bundle, sourcemeta::jsonschema::LOG_VERBOSE(options) << "Disabling rule: " << *iterator << "\n"; } else { - sourcemeta::jsonschema::LOG_VERBOSE(options) - << "warning: Cannot exclude unknown rule: " << *iterator << "\n"; + sourcemeta::jsonschema::LOG_WARNING() + << "Cannot exclude unknown rule: " << *iterator << "\n"; } } } diff --git a/src/command_validate.cc b/src/command_validate.cc index ed89de77..299edcf6 100644 --- a/src/command_validate.cc +++ b/src/command_validate.cc @@ -54,8 +54,9 @@ auto get_schema_template(const sourcemeta::core::JSON &bundled, if (precompiled_result.has_value()) { return precompiled_result.value(); } else { - std::cerr << "warning: Failed to parse pre-compiled schema template. " - "Compiling from scratch\n"; + sourcemeta::jsonschema::LOG_WARNING() + << "Failed to parse pre-compiled schema template. " + "Compiling from scratch\n"; } } @@ -285,7 +286,7 @@ auto sourcemeta::jsonschema::validate(const sourcemeta::core::Options &options) } if (index == 0) { - LOG_VERBOSE(options) << "warning: The JSONL file is empty\n"; + sourcemeta::jsonschema::LOG_WARNING() << "The JSONL file is empty\n"; } } else { sourcemeta::core::PointerPositionTracker tracker; diff --git a/src/logger.h b/src/logger.h index ac183d58..2790255c 100644 --- a/src/logger.h +++ b/src/logger.h @@ -3,8 +3,9 @@ #include -#include // std::ofstream -#include // std::ostream +#include // std::ofstream +#include // std::cerr +#include // std::ostream namespace sourcemeta::jsonschema { @@ -18,6 +19,11 @@ inline auto LOG_VERBOSE(const sourcemeta::core::Options &options) return null_stream; } +inline auto LOG_WARNING() -> std::ostream & { + std::cerr << "warning: "; + return std::cerr; +} + } // namespace sourcemeta::jsonschema #endif diff --git a/src/resolver.h b/src/resolver.h index 1d1a4d26..5d537cd0 100644 --- a/src/resolver.h +++ b/src/resolver.h @@ -96,10 +96,9 @@ class CustomResolver { << identifier << "\n"; }); if (!result) { - std::cerr - << "warning: No schema resources were imported from this file\n"; - std::cerr << " at " << entry.first.string() << "\n"; - std::cerr << "Are you sure this schema sets any identifiers?\n"; + LOG_WARNING() << "No schema resources were imported from this file\n" + << " at " << entry.first.string() << "\n" + << "Are you sure this schema sets any identifiers?\n"; } } } diff --git a/test/validate/pass_jsonl_empty.sh b/test/validate/pass_jsonl_empty.sh index 15526a96..9f8dbad4 100755 --- a/test/validate/pass_jsonl_empty.sh +++ b/test/validate/pass_jsonl_empty.sh @@ -23,6 +23,7 @@ touch "$TMP/instance.jsonl" "$1" validate "$TMP/schema.json" "$TMP/instance.jsonl" 2> "$TMP/output.txt" 1>&2 cat << EOF > "$TMP/expected.txt" +warning: The JSONL file is empty EOF diff "$TMP/output.txt" "$TMP/expected.txt"