I’m trying to customise the error messages reported by my software when HDF5 encounters a problem. I’m having difficulty with reporting both the name of the source file and the type of error encountered.
The code at the end of this post (based on section 9.4 here) provides a minimal example of the issue.
If I run the code as it is below the file names in the error stack are reported fine e.g.
FileName: ../../../src/H5F.c
FileName: ../../../src/H5Fint.c
FileName: ../../../src/H5FD.c
FileName: ../../../src/H5FDsec2.c
However if I uncomment the line that calls H5Eget_msg()
to try and retrieve the type of error too, the file names are no longer correctly handled and I get something like this (it changes on each run):
FileName: �p3ȊU
FileName: `p3ȊU
FileName: `l3ȊU
FileName: q3ȊU
Does H5Eget_msg()
modify err_desc
in someway the I’m not appreciating? Many thanks for any pointers.
#include "hdf5.h"
#define H5FILE_NAME "SDS.h5"
#define MSG_SIZE 32
herr_t custom_print_cb(unsigned n, const H5E_error2_t *err_desc, void* client_data)
{
/* uncomment these two lines and file names break */
//char maj[MSG_SIZE];
//ssize_t len = H5Eget_msg(err_desc->maj_num, NULL, maj, MSG_SIZE);
printf("FileName: %s\n", err_desc->file_name);
return 0;
}
herr_t _rhdf5PrintErrorR( hid_t estack_id, void * stream) {
ssize_t s = H5Eget_num(estack_id);
void *client_data;
if ( s > 0) {
herr_t eee = H5Ewalk2(estack_id, H5E_WALK_DOWNWARD, &custom_print_cb, client_data);
}
return 0;
}
int main (void) {
hid_t file;
void *err_func_data;
H5E_auto_t myfct = &_rhdf5PrintErrorR;
H5Eset_auto (H5E_DEFAULT, myfct, &err_func_data);
/* Open the file. It should fail */
file = H5Fopen(H5FILE_NAME, H5F_ACC_RDONLY, H5P_DEFAULT);
if(file < 0) {
return 1;
} else {
return 0;
}
}