From 27ef7b906223b712a5e935de747ec41fd289ed61 Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Fri, 12 Jan 2024 13:38:57 +0000 Subject: [PATCH] Debugging: Output list of key/value pairs being set --- src/eccodes_prototypes.h | 2 +- src/grib_value.cc | 31 +++++++++++++++++++++++-------- tests/unit_tests.cc | 6 +++--- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/eccodes_prototypes.h b/src/eccodes_prototypes.h index febe5573e..1a57e46ae 100644 --- a/src/eccodes_prototypes.h +++ b/src/eccodes_prototypes.h @@ -1187,7 +1187,7 @@ int grib_get_long_array(const grib_handle* h, const char* name, long* val, size_ int grib_get_values(grib_handle* h, grib_values* args, size_t count); int grib_set_values(grib_handle* h, grib_values* args, size_t count); int grib_get_nearest_smaller_value(grib_handle* h, const char* name, double val, double* nearest); -void grib_print_values(const char* title, grib_values* values); +void grib_print_values(const char* title, grib_values* values, FILE* out); int grib_values_check(grib_handle* h, grib_values* values, int count); int grib_key_equal(const grib_handle* h1, const grib_handle* h2, const char* key, int type, int* err); int codes_copy_key(grib_handle* h1, grib_handle* h2, const char* key, int type); diff --git a/src/grib_value.cc b/src/grib_value.cc index 2ac9cb3ce..9c0365789 100644 --- a/src/grib_value.cc +++ b/src/grib_value.cc @@ -131,6 +131,11 @@ int grib_set_long(grib_handle* h, const char* name, long val) return ret; } + + if (h->context->debug) { + fprintf(stderr, "ECCODES DEBUG grib_set_long %s=%ld (Key not found)\n", name, val); + } + return GRIB_NOT_FOUND; } @@ -476,6 +481,11 @@ int grib_set_string(grib_handle* h, const char* name, const char* val, size_t* l } return ret; } + + if (h->context->debug) { + fprintf(stderr, "ECCODES DEBUG grib_set_string %s=|%s| (Key not found)\n", name, val); + } + return GRIB_NOT_FOUND; } @@ -1769,6 +1779,12 @@ int grib_set_values(grib_handle* h, grib_values* args, size_t count) for (i = 0; i < count; i++) args[i].error = GRIB_NOT_FOUND; + if (h->context->debug) { + for (i = 0; i < count; i++) { + grib_print_values("ECCODES DEBUG set key/value pairs", &args[i], stderr); + } + } + while (more) { more = 0; for (i = 0; i < count; i++) { @@ -1839,23 +1855,22 @@ int grib_get_nearest_smaller_value(grib_handle* h, const char* name, return grib_nearest_smaller_value(act, val, nearest); } -void grib_print_values(const char* title, grib_values* values) +void grib_print_values(const char* title, grib_values* values, FILE* out) { - while(values) { - printf("%s: %s%s", title, values->name, (values->equal?"=":"!=")); + if (values) { + fprintf(out, "%s: %s=", title, values->name); switch (values->type) { case GRIB_TYPE_LONG: - printf("%ld", values->long_value); + fprintf(out, "%ld", values->long_value); break; case GRIB_TYPE_DOUBLE: - printf("%g", values->double_value); + fprintf(out, "%g", values->double_value); break; case GRIB_TYPE_STRING: - printf("%s", values->string_value); + fprintf(out, "%s", values->string_value); break; } - printf(" (type=%s)\n", grib_get_type_name(values->type)); - values = values->next; + fprintf(out, " (type=%s)\n", grib_get_type_name(values->type)); } } diff --git a/tests/unit_tests.cc b/tests/unit_tests.cc index 0fb193e41..695de023c 100644 --- a/tests/unit_tests.cc +++ b/tests/unit_tests.cc @@ -454,7 +454,7 @@ static void test_parse_keyval_string() values_required, GRIB_TYPE_UNDEFINED, values1, &count); Assert( !err ); Assert( count == 2 ); - grib_print_values("print values test: values1", values1); + grib_print_values("print values test: values1", values1, stdout); Assert( strcmp(values1[0].name, "key1")==0 ); Assert( strcmp(values1[0].string_value, "value1")==0 ); @@ -474,7 +474,7 @@ static void test_parse_keyval_string() values_required, GRIB_TYPE_LONG, values2, &count); Assert( !err ); Assert( count == 1 ); - grib_print_values("print values test: values2", values2); + grib_print_values("print values test: values2", values2, stdout); Assert( strcmp(values2[0].name, "x")==0 ); Assert( values2[0].long_value == 14 ); Assert( values2[0].equal == 1 ); @@ -485,7 +485,7 @@ static void test_parse_keyval_string() values_required, GRIB_TYPE_DOUBLE, values3, &count); Assert( !err ); Assert( count == 1 ); - grib_print_values("print values test: values3", values3); + grib_print_values("print values test: values3", values3, stdout); Assert( strcmp(values3[0].name, "mars.level")==0 ); free( (void*)values3[0].name ); }