Tools: Error messages

This commit is contained in:
Shahram Najm 2023-08-08 19:49:28 +01:00
parent 4aec267301
commit 7e882a34c4
3 changed files with 27 additions and 27 deletions

View File

@ -118,7 +118,7 @@ static void new_keys_list()
grib_context* c = grib_context_get_default(); grib_context* c = grib_context_get_default();
keys_list = (grib_string_list*)grib_context_malloc_clear(c, sizeof(grib_string_list)); keys_list = (grib_string_list*)grib_context_malloc_clear(c, sizeof(grib_string_list));
if (!keys_list) { if (!keys_list) {
fprintf(stderr, "Failed to allocate memory for keys list"); fprintf(stderr, "%s: Failed to allocate memory for keys list", tool_name);
exit(1); exit(1);
} }
} }
@ -281,11 +281,11 @@ int grib_tool_init(grib_runtime_options* options)
headerMode = 0; headerMode = 0;
if (grib_options_on("H") && grib_options_on("c:")) { if (grib_options_on("H") && grib_options_on("c:")) {
fprintf(stderr, "Error: -H and -c options are incompatible. Choose one of the two please.\n"); fprintf(stderr, "%s: -H and -c options are incompatible. Choose one of the two please.\n",tool_name);
exit(1); exit(1);
} }
if (grib_options_on("a") && !grib_options_on("c:")) { if (grib_options_on("a") && !grib_options_on("c:")) {
fprintf(stderr, "Error: -a option requires -c option. Please define a list of keys with the -c option.\n"); fprintf(stderr, "%s: -a option requires -c option. Please define a list of keys with the -c option.\n",tool_name);
exit(1); exit(1);
} }
@ -314,8 +314,8 @@ int grib_tool_init(grib_runtime_options* options)
options->idx = grib_fieldset_new_from_files(context, filename, options->idx = grib_fieldset_new_from_files(context, filename,
nfiles, 0, 0, 0, orderby, &ret); nfiles, 0, 0, 0, orderby, &ret);
if (ret) { if (ret) {
fprintf(stderr, "unable to create index for input file %s (%s)", fprintf(stderr, "%s: Unable to create index for input file %s (%s)",
options->infile_extra->name, grib_get_error_message(ret)); tool_name, options->infile_extra->name, grib_get_error_message(ret));
exit(ret); exit(ret);
} }
} }

View File

@ -307,7 +307,7 @@ static void bufr_dump_descriptors(grib_handle* h)
GRIB_CHECK_NOLINE(grib_get_size(h, the_key, &size_desc), 0); GRIB_CHECK_NOLINE(grib_get_size(h, the_key, &size_desc), 0);
array_descriptors = (long*)malloc(size_desc * sizeof(long)); array_descriptors = (long*)malloc(size_desc * sizeof(long));
if (!array_descriptors) { if (!array_descriptors) {
fprintf(stderr, "%s: Memory allocation error", the_key); fprintf(stderr, "%s: Memory allocation error. Key %s\n", tool_name, the_key);
exit(GRIB_OUT_OF_MEMORY); exit(GRIB_OUT_OF_MEMORY);
} }
GRIB_CHECK_NOLINE(grib_get_long_array(h, the_key, array_descriptors, &size_desc), 0); GRIB_CHECK_NOLINE(grib_get_long_array(h, the_key, array_descriptors, &size_desc), 0);
@ -325,7 +325,7 @@ static void bufr_dump_descriptors(grib_handle* h)
GRIB_CHECK_NOLINE(grib_get_size(h, the_key, &size_abbrevs), 0); GRIB_CHECK_NOLINE(grib_get_size(h, the_key, &size_abbrevs), 0);
array_abbrevs = (char**)malloc(size_abbrevs * sizeof(char*)); array_abbrevs = (char**)malloc(size_abbrevs * sizeof(char*));
if (!array_abbrevs) { if (!array_abbrevs) {
fprintf(stderr, "%s: Memory allocation error", the_key); fprintf(stderr, "%s: Memory allocation error. Key %s\n", tool_name, the_key);
exit(GRIB_OUT_OF_MEMORY); exit(GRIB_OUT_OF_MEMORY);
} }
GRIB_CHECK_NOLINE(grib_get_string_array(h, the_key, array_abbrevs, &size_abbrevs), 0); GRIB_CHECK_NOLINE(grib_get_string_array(h, the_key, array_abbrevs, &size_abbrevs), 0);
@ -335,7 +335,7 @@ static void bufr_dump_descriptors(grib_handle* h)
GRIB_CHECK_NOLINE(grib_get_size(h, the_key, &size_names), 0); GRIB_CHECK_NOLINE(grib_get_size(h, the_key, &size_names), 0);
array_names = (char**)malloc(size_names * sizeof(char*)); array_names = (char**)malloc(size_names * sizeof(char*));
if (!array_names) { if (!array_names) {
fprintf(stderr, "%s: Memory allocation error", the_key); fprintf(stderr, "%s: Memory allocation error. Key %s\n", tool_name, the_key);
exit(GRIB_OUT_OF_MEMORY); exit(GRIB_OUT_OF_MEMORY);
} }
GRIB_CHECK_NOLINE(grib_get_string_array(h, the_key, array_names, &size_names), 0); GRIB_CHECK_NOLINE(grib_get_string_array(h, the_key, array_names, &size_names), 0);
@ -345,7 +345,7 @@ static void bufr_dump_descriptors(grib_handle* h)
GRIB_CHECK_NOLINE(grib_get_size(h, the_key, &size_units), 0); GRIB_CHECK_NOLINE(grib_get_size(h, the_key, &size_units), 0);
array_units = (char**)malloc(size_units * sizeof(char*)); array_units = (char**)malloc(size_units * sizeof(char*));
if (!array_units) { if (!array_units) {
fprintf(stderr, "%s: Memory allocation error", the_key); fprintf(stderr, "%s: Memory allocation error. Key %s\n", tool_name, the_key);
exit(GRIB_OUT_OF_MEMORY); exit(GRIB_OUT_OF_MEMORY);
} }
GRIB_CHECK_NOLINE(grib_get_string_array(h, the_key, array_units, &size_units), 0); GRIB_CHECK_NOLINE(grib_get_string_array(h, the_key, array_units, &size_units), 0);
@ -423,7 +423,7 @@ int grib_tool_new_handle_action(grib_runtime_options* options, grib_handle* h)
char* str = grib_options_get_option("S:"); char* str = grib_options_get_option("S:");
err = grib_get_long(h, "numberOfSubsets", &numberOfSubsets); err = grib_get_long(h, "numberOfSubsets", &numberOfSubsets);
if (err) { if (err) {
fprintf(stderr, "ERROR: Failed to get numberOfSubsets.\n"); fprintf(stderr, "%s: Failed to get numberOfSubsets.\n", tool_name);
exit(1); exit(1);
} }
@ -451,7 +451,7 @@ int grib_tool_new_handle_action(grib_runtime_options* options, grib_handle* h)
} }
} }
else { else {
fprintf(stderr, "ERROR: -S option: Please specify a subset number > 0 and < %ld\n", numberOfSubsets + 1); fprintf(stderr, "%s: -S option: Please specify a subset number > 0 and < %ld\n", tool_name, numberOfSubsets + 1);
exit(1); exit(1);
} }
} }
@ -470,8 +470,8 @@ int grib_tool_new_handle_action(grib_runtime_options* options, grib_handle* h)
err = grib_set_long(h, "unpack", 2); err = grib_set_long(h, "unpack", 2);
if (err) { if (err) {
if (options->fail) { if (options->fail) {
fprintf(stderr, "ERROR: unable to unpack data section: %s (message=%d)\n", fprintf(stderr, "%s: Unable to unpack data section: %s (message=%d)\n",
grib_get_error_message(err), options->handle_count); tool_name, grib_get_error_message(err), options->handle_count);
exit(err); exit(err);
} }
else { else {
@ -489,8 +489,8 @@ int grib_tool_new_handle_action(grib_runtime_options* options, grib_handle* h)
err = grib_set_long(h, "unpack", 1); err = grib_set_long(h, "unpack", 1);
if (err) { if (err) {
if (options->fail) { if (options->fail) {
fprintf(stderr, "ERROR: unable to unpack data section: %s (message=%d)\n", fprintf(stderr, "%s: Unable to unpack data section: %s (message=%d)\n",
grib_get_error_message(err), options->handle_count); tool_name, grib_get_error_message(err), options->handle_count);
exit(err); exit(err);
} }
else { else {
@ -505,8 +505,8 @@ int grib_tool_new_handle_action(grib_runtime_options* options, grib_handle* h)
err = grib_set_long(h, "unpack", 1); err = grib_set_long(h, "unpack", 1);
if (err) { if (err) {
if (options->fail) { if (options->fail) {
fprintf(stderr, "ERROR: unable to unpack data section: %s (message=%d)\n", fprintf(stderr, "%s: Unable to unpack data section: %s (message=%d)\n",
grib_get_error_message(err), options->handle_count); tool_name, grib_get_error_message(err), options->handle_count);
exit(err); exit(err);
} }
else { else {
@ -519,7 +519,7 @@ int grib_tool_new_handle_action(grib_runtime_options* options, grib_handle* h)
grib_dump_content(h, stdout, options->dump_mode, options->dump_flags, 0); grib_dump_content(h, stdout, options->dump_mode, options->dump_flags, 0);
break; break;
default: default:
printf("Unknown JSON option %s\n", json_option); fprintf(stderr, "%s: Unknown JSON option %s\n", tool_name, json_option);
exit(1); exit(1);
} }
if (!strcmp(options->dump_mode, "default")) { if (!strcmp(options->dump_mode, "default")) {
@ -546,7 +546,7 @@ int grib_tool_new_handle_action(grib_runtime_options* options, grib_handle* h)
err = grib_set_long(h, "unpack", 1); err = grib_set_long(h, "unpack", 1);
if (err) { if (err) {
if (options->fail) { if (options->fail) {
fprintf(stderr, "ERROR: unable to unpack data section: %s\n", grib_get_error_message(err)); fprintf(stderr, "%s: unable to unpack data section: %s\n", tool_name, grib_get_error_message(err));
exit(err); exit(err);
} }
else { else {

View File

@ -22,6 +22,7 @@ static int verbose = 0;
static const char* OUTPUT_FILENAME_DEFAULT = "split_rdbSubtype.undef.bufr"; static const char* OUTPUT_FILENAME_DEFAULT = "split_rdbSubtype.undef.bufr";
static const char* OUTPUT_FILENAME_SUBTYPE = "split_rdbSubtype.%ld.bufr"; static const char* OUTPUT_FILENAME_SUBTYPE = "split_rdbSubtype.%ld.bufr";
const char* tool_name = "bufr_split_by_rdbSubtype";
static void usage(const char* prog) static void usage(const char* prog)
{ {
@ -56,7 +57,7 @@ static int decode_rdbSubtype(const void* msg, long* rdbSubtype)
edition = (long)grib_decode_unsigned_long(message, &pos_edition, nbits_edition); edition = (long)grib_decode_unsigned_long(message, &pos_edition, nbits_edition);
if (edition != 2 && edition != 3 && edition != 4) { if (edition != 2 && edition != 3 && edition != 4) {
fprintf(stderr, "ERROR: Unsupported BUFR edition: %ld", edition); fprintf(stderr, "%s: Unsupported BUFR edition: %ld", tool_name, edition);
return GRIB_DECODING_ERROR; return GRIB_DECODING_ERROR;
} }
section1Length = (long)grib_decode_unsigned_long(message, &pos_section1Length, nbits_section1Length); section1Length = (long)grib_decode_unsigned_long(message, &pos_section1Length, nbits_section1Length);
@ -73,7 +74,7 @@ static int decode_rdbSubtype(const void* msg, long* rdbSubtype)
section1Flags = (long)grib_decode_unsigned_long(message, &pos_section1Flags, nbits_section1Flags); section1Flags = (long)grib_decode_unsigned_long(message, &pos_section1Flags, nbits_section1Flags);
if (section1Flags != 0 && section1Flags != 128) { if (section1Flags != 0 && section1Flags != 128) {
fprintf(stderr, "ERROR: Invalid BUFR section1 flags: %ld", section1Flags); fprintf(stderr, "%s: Invalid BUFR section1 flags: %ld\n", tool_name, section1Flags);
return GRIB_DECODING_ERROR; return GRIB_DECODING_ERROR;
} }
ecmwfLocalSectionPresent = (bufrHeaderCentre == 98 && section1Flags != 0); ecmwfLocalSectionPresent = (bufrHeaderCentre == 98 && section1Flags != 0);
@ -121,7 +122,7 @@ static int split_file_by_subtype(FILE* in, const char* filename, unsigned long*
long rdbSubtype = 0; long rdbSubtype = 0;
int status = decode_rdbSubtype(mesg, &rdbSubtype); int status = decode_rdbSubtype(mesg, &rdbSubtype);
if (status != GRIB_SUCCESS) { if (status != GRIB_SUCCESS) {
fprintf(stderr, "ERROR: Failed to decode rdbSubtype from message %lu\n", *count); fprintf(stderr, "%s: Failed to decode rdbSubtype from message %lu\n", tool_name, *count);
return status; return status;
} }
@ -135,12 +136,12 @@ static int split_file_by_subtype(FILE* in, const char* filename, unsigned long*
} }
out = fopen(ofilename, "ab"); out = fopen(ofilename, "ab");
if (!out) { if (!out) {
fprintf(stderr, "ERROR: Failed to open output file '%s'\n", ofilename); fprintf(stderr, "%s: Failed to open output file '%s'\n", tool_name, ofilename);
perror(ofilename); perror(ofilename);
return GRIB_IO_PROBLEM; return GRIB_IO_PROBLEM;
} }
if (fwrite(mesg, 1, size, out) != size) { if (fwrite(mesg, 1, size, out) != size) {
fprintf(stderr, "ERROR: Failed to append to file '%s'\n", ofilename); fprintf(stderr, "%s: Failed to append to file '%s'\n", tool_name, ofilename);
perror(ofilename); perror(ofilename);
fclose(out); fclose(out);
return GRIB_IO_PROBLEM; return GRIB_IO_PROBLEM;
@ -178,7 +179,7 @@ int main(int argc, char* argv[])
filename = argv[i]; filename = argv[i];
if (path_is_directory(filename)) { if (path_is_directory(filename)) {
fprintf(stderr, "ERROR: %s: Is a directory\n", filename); fprintf(stderr, "%s: %s: Is a directory\n", tool_name, filename);
return 1; return 1;
} }
infh = fopen(filename, "rb"); infh = fopen(filename, "rb");
@ -190,8 +191,7 @@ int main(int argc, char* argv[])
count = 0; count = 0;
err = split_file_by_subtype(infh, filename, &count); err = split_file_by_subtype(infh, filename, &count);
if (err) { if (err) {
fprintf(stderr, "ERROR: Failed to split BUFR file %s", filename); fprintf(stderr, "%s: Failed to split BUFR file %s\n", tool_name, filename);
fprintf(stderr, "\n");
status = 1; status = 1;
} }
else { else {