/*------------------------------------------------------------------------- * Function: error_stack * * Purpose: Manipulates current error stack. * * Return: Success: 0 * * Failure: -1 * * Programmer: Raymond Lu * July 14, 2003 * *------------------------------------------------------------------------- */ static herr_t error_stack(void) { int err_num; const char *FUNC_error_stack = "error_stack"; if((err_num = H5Eget_num(H5E_DEFAULT)) < 0) TEST_ERROR; if(err_num) TEST_ERROR; if((ERR_STACK = H5Eget_current_stack()) < 0) TEST_ERROR; /* Make it push error, force this function to fail */ if((err_num = H5Eget_num(ERR_STACK)) == 0) { H5Epush(ERR_STACK, __FILE__, FUNC_error_stack, __LINE__, ERR_CLS, ERR_MAJ_API, ERR_MIN_GETNUM, "Get number test failed, returned %d", err_num); goto error; } /* end if */ /* In case program falls through here, close the stack and let it fail. */ if(H5Eclose_stack(ERR_STACK) < 0) TEST_ERROR; return -1; error: return -1; } /* end error_stack() */
Exception::Exception() noexcept { // FIXME: is this the best approach? Is it thread-safe? hid_t stack{H5Eget_current_stack()}; // Pop all but inner-most frame, i.e., the root cause. H5Epop(stack, H5Eget_num(stack) - 1); // Get inner-most error message. H5Ewalk2(stack, H5E_WALK_DOWNWARD, setError, what_); // Free stack. H5Eclose_stack(stack); }
/* * Class: hdf_hdf5lib_H5 * Method: H5Eget_current_stack * Signature: ()J */ JNIEXPORT jlong JNICALL Java_hdf_hdf5lib_H5_H5Eget_1current_1stack (JNIEnv *env, jclass cls) { hid_t ret_val = -1; ret_val = H5Eget_current_stack(); if (ret_val < 0) h5libraryError(env); return (jlong)ret_val; } /* end Java_hdf_hdf5lib_H5_H5Eget_1current_1stack */
/* * h5libraryError() determines the HDF-5 major error code * and creates and throws the appropriate sub-class of * HDF5LibraryException(). This routine should be called * whenever a call to the HDF-5 library fails, i.e., when * the return is -1. * * Note: This routine never returns from the 'throw', * and the Java native method immediately raises the * exception. */ jboolean h5libraryError (JNIEnv *env) { char *args[2]; const char *exception = NULL; char *msg_str = NULL; int num_errs = 0; hid_t min_num; hid_t maj_num; ssize_t msg_size = 0; H5E_type_t error_msg_type; jstring str = NULL; hid_t stk_id = -1; H5E_num_t exceptionNumbers; exceptionNumbers.maj_num = 0; exceptionNumbers.min_num = 0; /* Save current stack contents for future use */ stk_id = H5Eget_current_stack(); /* This will clear current stack */ if(stk_id >= 0) H5Ewalk2(stk_id, H5E_WALK_DOWNWARD, walk_error_callback, &exceptionNumbers); maj_num = exceptionNumbers.maj_num; min_num = exceptionNumbers.min_num; exception = defineHDF5LibraryException(maj_num); /* get the length of the name */ msg_size = H5Eget_msg(min_num, NULL, NULL, 0); if(msg_size > 0) { msg_size++; /* add extra space for the null terminator */ msg_str = (char*)HDcalloc((size_t)msg_size, sizeof(char)); if(msg_str) { msg_size = H5Eget_msg(min_num, &error_msg_type, (char *)msg_str, (size_t)msg_size); str = ENVPTR->NewStringUTF(ENVPAR msg_str); HDfree(msg_str); } /* end if */ } /* end if */ else str = NULL; if(stk_id >= 0) H5Eset_current_stack(stk_id); args[0] = (char *)str; args[1] = 0; THROWEXCEPTION(exception, args); } /* end h5libraryError() */
/*------------------------------------------------------------------------- * Function: test_copy * * Purpose: Test copyinging an error stack * * Return: Success: 0 * Failure: -1 * * Programmer: Allen Byrne * February 18, 2010 * *------------------------------------------------------------------------- */ static herr_t test_copy(void) { const char *err_func = "test_copy"; /* Function name for pushing error */ const char *err_msg = "Error message"; /* Error message for pushing error */ int err_num; /* Number of errors on stack */ hid_t estack_id; /* Error stack ID */ herr_t ret; /* Generic return value */ /* Push an error with a long description */ if(H5Epush(H5E_DEFAULT, __FILE__, err_func, __LINE__, ERR_CLS, ERR_MAJ_TEST, ERR_MIN_SUBROUTINE, err_msg) < 0) TEST_ERROR; /* Check the number of errors on stack */ err_num = H5Eget_num(H5E_DEFAULT); if(err_num != 1) TEST_ERROR /* Copy error stack, which clears the original */ if((estack_id = H5Eget_current_stack()) < 0) TEST_ERROR /* Check the number of errors on stack copy */ err_num = H5Eget_num(estack_id); if(err_num != 1) TEST_ERROR /* Check the number of errors on original stack */ err_num = H5Eget_num(H5E_DEFAULT); if(err_num != 0) TEST_ERROR /* Put the stack copy as the default. It closes the stack copy, too. */ if(H5Eset_current_stack(estack_id) < 0) TEST_ERROR /* Check the number of errors on default stack */ err_num = H5Eget_num(H5E_DEFAULT); if(err_num != 1) TEST_ERROR /* Try to close error stack copy. Should fail because * the current H5Eset_current_stack closes the stack to be set.*/ H5E_BEGIN_TRY { ret = H5Eclose_stack(estack_id); } H5E_END_TRY if(ret >= 0) TEST_ERROR return(0); error: return(-1); } /* end test_copy() */
void hdfFileSaver::saveNow(void) { QDir prnt; herr_t status; hsize_t maxdims[1]; hsize_t d_dims[1]; int rank = 1; hsize_t datasetlength; size_t memdims[1]; hsize_t count[1]; /* size of subset in the file */ hsize_t offset[1]; /* subset offset in the file */ hsize_t mcount[1]; hsize_t moffset[1]; hid_t prop; if (events==0) return; if (access(fname.toStdString().c_str(), F_OK) ==-1) { hfile = H5Fcreate( fname.toStdString().c_str(), H5F_ACC_EXCL , H5P_DEFAULT, H5P_DEFAULT ) ; topgroup = H5Gcreate( hfile, "iqdata_raw", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); hid_t aid3 = H5Screate(H5S_SCALAR); hid_t atype = H5Tcopy(H5T_C_S1); H5Tset_size(atype, 4); hid_t attr3 = H5Acreate1(topgroup, "type", atype, aid3, H5P_DEFAULT); status = H5Awrite(attr3, atype,"dict"); H5Aclose(attr3); H5Tclose(atype); H5Sclose(aid3); } else { //printf("File already exists, will append\n"); hfile = H5Fopen( fname.toStdString().c_str(), H5F_ACC_RDWR, H5P_DEFAULT ); topgroup = H5Gopen(hfile, "iqdata_raw", H5P_DEFAULT); //fprintf(stderr,"Bad Hdf file already existant, cannot open\n"); } if (hfile <=0 || topgroup <=0) { fprintf(stderr,"Bad Hdf file, cannot open\n"); return; } if (true) { //QHash<int,QHash<QString,QList<float> > > events; QList<int> keys1=(*events).keys(); for (int i=0;i<keys1.length();i++) { int chan = keys1[i]; if ((*events)[chan]["bin"].length() > 0) { int bin = (int)((*events)[chan]["bin"][0]); QString dirname = QString("keyint_%1").arg(chan); //turn off errors when we query the group, using open hid_t error_stack = H5Eget_current_stack(); H5E_auto2_t oldfunc; void *old_client_data; H5Eget_auto(error_stack, &oldfunc, &old_client_data); H5Eset_auto(error_stack, NULL, NULL); channelgroup = H5Gopen( topgroup, dirname.toStdString().c_str(), H5P_DEFAULT); //turn errors back on. H5Eset_auto(error_stack, oldfunc, old_client_data); if (channelgroup<0) { channelgroup = H5Gcreate( topgroup, dirname.toStdString().c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); hid_t aid3 = H5Screate(H5S_SCALAR); hid_t atype = H5Tcopy(H5T_C_S1); H5Tset_size(atype, 4); hid_t attr3 = H5Acreate1(channelgroup, "type", atype, aid3, H5P_DEFAULT); status = H5Awrite(attr3, atype,"dict"); H5Aclose(attr3); H5Tclose(atype); H5Sclose(aid3); } if (channelgroup!=0) { QList<QString> keys2=(*events)[keys1[i]].keys(); for (int i2=0;i2<keys2.length();i2++) { QString dname = QString("keystr_%1").arg(keys2[i2]); datasetlength = (*events)[keys1[i]][keys2[i2]].length(); if (datasetlength>0) { //Try to open dataset if it exists //turn off errors when we query the group, using open hid_t error_stack = H5Eget_current_stack(); H5E_auto2_t oldfunc; void *old_client_data; H5Eget_auto(error_stack, &oldfunc, &old_client_data); H5Eset_auto(error_stack, NULL, NULL); //query or open dataset curdataset = H5Dopen(channelgroup,dname.toStdString().c_str(),H5P_DEFAULT); //turn errors back on. H5Eset_auto(error_stack, oldfunc, old_client_data); //if cannot open dataset, create it, and make it chunked. if (curdataset<=0) { //set up size info, chunks etc... maxdims[0]=H5S_UNLIMITED; rank = 1; d_dims[0]=datasetlength; curdataspace= H5Screate_simple(rank, d_dims,maxdims); prop = H5Pcreate(H5P_DATASET_CREATE); status = H5Pset_chunk(prop, rank, d_dims); if (status) trap(); curdataset = H5Dcreate( channelgroup, dname.toStdString().c_str(), H5T_NATIVE_FLOAT, curdataspace, H5P_DEFAULT, prop, H5P_DEFAULT); hid_t aid3 = H5Screate(H5S_SCALAR); hid_t atype = H5Tcopy(H5T_C_S1); H5Tset_size(atype, 5); hid_t attr3 = H5Acreate1(curdataset, "type", atype, aid3, H5P_DEFAULT); status = H5Awrite(attr3, atype,"array"); H5Aclose(attr3); H5Tclose(atype); H5Sclose(aid3); H5Pclose(prop); }//if (curdataset<=0) else { //get dataspace from exant dataset curdataspace = H5Dget_space(curdataset); H5Sget_simple_extent_dims(curdataspace, d_dims, maxdims); H5Sclose(curdataspace); //if dataset already exist, extend its size d_dims[0] = d_dims[0] + datasetlength; status = H5Dset_extent(curdataset, d_dims); if (status) trap(); curdataspace = H5Dget_space(curdataset); H5Sget_simple_extent_dims(curdataspace, d_dims, maxdims); }//else if (curdataset>0) { // // Set up stride, offset, count block in dastasets... // // stride[0]=1;//hop size between floats in dataset... //block[0]=1;//get 1 slab count[0]=datasetlength;//size of hyperslab or chunk of data offset[0]=d_dims[0] - datasetlength;//offset from beginning of file. status = H5Sselect_hyperslab( curdataspace, H5S_SELECT_SET, offset, NULL, count, NULL); if (status) trap(); rank = 1; memdims[0]=datasetlength; memdataspace= H5Screate_simple(rank, (const hsize_t*)memdims,NULL); mcount[0] = datasetlength; moffset[0] = 0; status = H5Sselect_hyperslab( memdataspace, H5S_SELECT_SET, moffset, NULL, mcount, NULL); if (status) trap(); for (int mmm=0;mmm<datasetlength;mmm++) this->rawdata[mmm]=(*events)[keys1[i]][keys2[i2]][mmm]; status = H5Dwrite( curdataset, H5T_NATIVE_FLOAT, memdataspace, curdataspace, H5P_DEFAULT, this->rawdata); if (status) trap(); H5Sclose(memdataspace); H5Sclose(curdataspace); H5Dclose(curdataset); } //if (curdataset>0) } }//for (int i2=0;i2<keys2.length();i2++) status = H5Gclose(channelgroup); }//if (channelgroup!=0) }//if ((*events)[chan]["bin"].length() > 0) }//for (int i=0;i<keys1.length();i++) }//if (true) status = H5Gclose(topgroup); status = H5Fclose(hfile); }
/*------------------------------------------------------------------------- * Function: test_error * * Purpose: Test error API functions * * Return: Success: 0 * * Failure: -1 * * Programmer: Raymond Lu * July 10, 2003 * *------------------------------------------------------------------------- */ static herr_t test_error(hid_t file) { hid_t dataset, space; hid_t estack_id; hsize_t dims[2]; const char *FUNC_test_error = "test_error"; H5E_auto2_t old_func; void *old_data; HDfprintf(stderr, "\nTesting error API based on data I/O\n"); /* Create the data space */ dims[0] = DIM0; dims[1] = DIM1; if ((space = H5Screate_simple(2, dims, NULL))<0) TEST_ERROR; /* Test H5E_BEGIN_TRY */ H5E_BEGIN_TRY { dataset = H5Dcreate2(FAKE_ID, DSET_NAME, H5T_STD_I32BE, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); } H5E_END_TRY; /* Create the dataset */ if((dataset = H5Dcreate2(file, DSET_NAME, H5T_STD_I32BE, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) { H5Epush(H5E_DEFAULT, __FILE__, FUNC_test_error, __LINE__, ERR_CLS, ERR_MAJ_IO, ERR_MIN_CREATE, "H5Dcreate2 failed"); goto error; } /* end if */ /* Test enabling and disabling default printing */ if(H5Eget_auto2(H5E_DEFAULT, &old_func, &old_data) < 0) TEST_ERROR; if(old_data != NULL) TEST_ERROR; #ifdef H5_USE_16_API if (old_func != (H5E_auto_t)H5Eprint) TEST_ERROR; #else /* H5_USE_16_API */ if (old_func != (H5E_auto2_t)H5Eprint2) TEST_ERROR; #endif /* H5_USE_16_API */ if(H5Eset_auto2(H5E_DEFAULT, NULL, NULL) < 0) TEST_ERROR; /* Make H5Dwrite fail, verify default print is disabled */ if(H5Dwrite(FAKE_ID, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, ipoints2) >= 0) { H5Epush(H5E_DEFAULT, __FILE__, FUNC_test_error, __LINE__, ERR_CLS, ERR_MAJ_IO, ERR_MIN_WRITE, "H5Dwrite shouldn't succeed"); goto error; } /* end if */ if(H5Eset_auto2(H5E_DEFAULT, old_func, old_data) < 0) TEST_ERROR; /* Test saving and restoring the current error stack */ if(H5Dwrite(FAKE_ID, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, ipoints2) < 0) { H5Epush(H5E_DEFAULT, __FILE__, FUNC_test_error, __LINE__, ERR_CLS, ERR_MAJ_IO, ERR_MIN_WRITE, "H5Dwrite failed as supposed to"); estack_id = H5Eget_current_stack(); H5Dclose(dataset); H5Sclose(space); H5Eset_current_stack(estack_id); goto error; } /* end if */ /* In case program comes to this point, close dataset */ if(H5Dclose(dataset) < 0) TEST_ERROR; TEST_ERROR; error: return -1; } /* end test_error() */
/*------------------------------------------------------------------------- * Function: main * * Purpose: Test error API. * * Programmer: Raymond Lu * July 10, 2003 * *------------------------------------------------------------------------- */ int main(void) { hid_t file, fapl; hid_t estack_id; char filename[1024]; const char *FUNC_main = "main"; HDfprintf(stderr, " This program tests the Error API. There're supposed to be some error messages\n"); /* Initialize errors */ if(init_error() < 0) TEST_ERROR; fapl = h5_fileaccess(); h5_fixname(FILENAME[0], fapl, filename, sizeof filename); if((file = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0) TEST_ERROR; /* Test error stack */ if(error_stack() < 0) { /* Push an error onto error stack */ if(H5Epush(ERR_STACK, __FILE__, FUNC_main, __LINE__, ERR_CLS, ERR_MAJ_TEST, ERR_MIN_ERRSTACK, "Error stack test failed") < 0) TEST_ERROR; /* Delete an error from the top of error stack */ H5Epop(ERR_STACK, 1); /* Make sure we can use other class's major or minor errors. */ H5Epush(ERR_STACK, __FILE__, FUNC_main, __LINE__, ERR_CLS2, ERR_MAJ_TEST, ERR_MIN_ERRSTACK, "Error stack test failed"); /* Print out the errors on stack */ dump_error(ERR_STACK); /* Empty error stack */ H5Eclear2(ERR_STACK); /* Close error stack */ H5Eclose_stack(ERR_STACK); } /* end if */ /* Test error API */ if(test_error(file) < 0) { H5Epush(H5E_DEFAULT, __FILE__, FUNC_main, __LINE__, ERR_CLS, ERR_MAJ_TEST, ERR_MIN_SUBROUTINE, "Error test failed, %s", "it's wrong"); estack_id = H5Eget_current_stack(); H5Eprint2(estack_id, stderr); H5Eclose_stack(estack_id); } /* end if */ /* Test pushing a very long error description */ if(test_long_desc() < 0) TEST_ERROR; /* Test creating a new error stack */ if(test_create() < 0) TEST_ERROR; /* Test copying a new error stack */ if(test_copy() < 0) TEST_ERROR; if(H5Fclose(file) < 0) TEST_ERROR; /* Close error information */ if(close_error() < 0) TEST_ERROR; /* Test error message during data reading when filter isn't registered * Use default FAPL to avoid some VFD drivers by the check-vfd test because * the test file was pre-generated. */ h5_fixname(DATAFILE, H5P_DEFAULT, filename, sizeof filename); if(test_filter_error(filename) < 0) TEST_ERROR; h5_clean_files(FILENAME, fapl); HDfprintf(stderr, "\nAll error API tests passed.\n"); return 0; error: HDfprintf(stderr, "\n***** ERROR TEST FAILED (real problem)! *****\n"); return 1; }
/*--------------------------------------------------------------------------*/ void HDF5ErrorCleanup() { H5Eclear(H5Eget_current_stack()); }