Debugging: Output list of key/value pairs being set

This commit is contained in:
Shahram Najm 2024-01-12 13:38:57 +00:00
parent 9d556f9d43
commit 27ef7b9062
3 changed files with 27 additions and 12 deletions

View File

@ -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_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_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); 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_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 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); int codes_copy_key(grib_handle* h1, grib_handle* h2, const char* key, int type);

View File

@ -131,6 +131,11 @@ int grib_set_long(grib_handle* h, const char* name, long val)
return ret; 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; 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; 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; 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++) for (i = 0; i < count; i++)
args[i].error = GRIB_NOT_FOUND; 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) { while (more) {
more = 0; more = 0;
for (i = 0; i < count; i++) { 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); 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) { if (values) {
printf("%s: %s%s", title, values->name, (values->equal?"=":"!=")); fprintf(out, "%s: %s=", title, values->name);
switch (values->type) { switch (values->type) {
case GRIB_TYPE_LONG: case GRIB_TYPE_LONG:
printf("%ld", values->long_value); fprintf(out, "%ld", values->long_value);
break; break;
case GRIB_TYPE_DOUBLE: case GRIB_TYPE_DOUBLE:
printf("%g", values->double_value); fprintf(out, "%g", values->double_value);
break; break;
case GRIB_TYPE_STRING: case GRIB_TYPE_STRING:
printf("%s", values->string_value); fprintf(out, "%s", values->string_value);
break; break;
} }
printf(" (type=%s)\n", grib_get_type_name(values->type)); fprintf(out, " (type=%s)\n", grib_get_type_name(values->type));
values = values->next;
} }
} }

View File

@ -454,7 +454,7 @@ static void test_parse_keyval_string()
values_required, GRIB_TYPE_UNDEFINED, values1, &count); values_required, GRIB_TYPE_UNDEFINED, values1, &count);
Assert( !err ); Assert( !err );
Assert( count == 2 ); 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].name, "key1")==0 );
Assert( strcmp(values1[0].string_value, "value1")==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); values_required, GRIB_TYPE_LONG, values2, &count);
Assert( !err ); Assert( !err );
Assert( count == 1 ); 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( strcmp(values2[0].name, "x")==0 );
Assert( values2[0].long_value == 14 ); Assert( values2[0].long_value == 14 );
Assert( values2[0].equal == 1 ); Assert( values2[0].equal == 1 );
@ -485,7 +485,7 @@ static void test_parse_keyval_string()
values_required, GRIB_TYPE_DOUBLE, values3, &count); values_required, GRIB_TYPE_DOUBLE, values3, &count);
Assert( !err ); Assert( !err );
Assert( count == 1 ); 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 ); Assert( strcmp(values3[0].name, "mars.level")==0 );
free( (void*)values3[0].name ); free( (void*)values3[0].name );
} }