Commit e995206e authored by Lukas Weber's avatar Lukas Weber

add filename to h5 exceptions

parent 21631892
...@@ -35,11 +35,11 @@ static herr_t H5Ewalk_cb(unsigned int n, const H5E_error2_t *err_desc, void *cli ...@@ -35,11 +35,11 @@ static herr_t H5Ewalk_cb(unsigned int n, const H5E_error2_t *err_desc, void *cli
return 0; return 0;
} }
iodump_exception::iodump_exception(const std::string &message) { iodump_exception::iodump_exception(const std::string &filename, const std::string &message) {
std::stringstream s; std::stringstream s;
H5Ewalk(H5E_DEFAULT, H5E_WALK_DOWNWARD, H5Ewalk_cb, &s); H5Ewalk(H5E_DEFAULT, H5E_WALK_DOWNWARD, H5Ewalk_cb, &s);
s << "Error triggered: " << message; s << "File " << filename << " Error triggered: " << message;
message_ = s.str(); message_ = s.str();
} }
...@@ -47,12 +47,13 @@ const char *iodump_exception::what() const noexcept { ...@@ -47,12 +47,13 @@ const char *iodump_exception::what() const noexcept {
return message_.c_str(); return message_.c_str();
} }
iodump::group::group(hid_t parent, const std::string &path) { iodump::group::group(hid_t parent, const std::string &filename, const std::string &path)
: filename_{filename} {
group_ = H5Gopen(parent, path.c_str(), H5P_DEFAULT); group_ = H5Gopen(parent, path.c_str(), H5P_DEFAULT);
if(group_ < 0) { if(group_ < 0) {
group_ = H5Gcreate2(parent, path.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); group_ = H5Gcreate2(parent, path.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
if(group_ < 0) { if(group_ < 0) {
throw iodump_exception{"H5Gcreate"}; throw iodump_exception{filename_, "H5Gcreate"};
} }
} }
} }
...@@ -63,40 +64,38 @@ iodump::group::~group() { ...@@ -63,40 +64,38 @@ iodump::group::~group() {
} }
herr_t status = H5Gclose(group_); herr_t status = H5Gclose(group_);
if(status < 0) { if(status < 0) {
std::cerr << iodump_exception{"H5Gclose"}.what(); std::cerr << iodump_exception{filename_, "H5Gclose"}.what();
std::cerr << group_ << " …something went wrong in the destructor.\n"; std::cerr << group_ << " …something went wrong in the destructor.\n";
assert(false); assert(false);
} }
} }
iodump::group::iterator iodump::group::begin() const { iodump::group::iterator iodump::group::begin() const {
return iodump::group::iterator{group_, 0}; return iodump::group::iterator{group_, filename_, 0};
} }
iodump::group::iterator iodump::group::end() const { iodump::group::iterator iodump::group::end() const {
H5G_info_t info{}; H5G_info_t info{};
herr_t status = H5Gget_info(group_, &info); herr_t status = H5Gget_info(group_, &info);
if(status < 0) { if(status < 0) {
throw iodump_exception{"H5Gget_info"}; throw iodump_exception{filename_, "H5Gget_info"};
} }
return iodump::group::iterator{group_, info.nlinks}; return iodump::group::iterator{group_, filename_, info.nlinks};
} }
iodump::group::iterator::iterator(hid_t group, uint64_t idx) : group_(group), idx_(idx) {}
std::string iodump::group::iterator::operator*() { std::string iodump::group::iterator::operator*() {
ssize_t name_size = ssize_t name_size =
H5Lget_name_by_idx(group_, ".", H5_INDEX_NAME, H5_ITER_INC, idx_, nullptr, 0, H5P_DEFAULT); H5Lget_name_by_idx(group_, ".", H5_INDEX_NAME, H5_ITER_INC, idx_, nullptr, 0, H5P_DEFAULT);
if(name_size < 0) { if(name_size < 0) {
throw iodump_exception{"H5Lget_name_by_idx"}; throw iodump_exception{filename_, "H5Lget_name_by_idx"};
} }
std::vector<char> buf(name_size + 1); std::vector<char> buf(name_size + 1);
name_size = H5Lget_name_by_idx(group_, ".", H5_INDEX_NAME, H5_ITER_INC, idx_, buf.data(), name_size = H5Lget_name_by_idx(group_, ".", H5_INDEX_NAME, H5_ITER_INC, idx_, buf.data(),
buf.size(), H5P_DEFAULT); buf.size(), H5P_DEFAULT);
if(name_size < 0) { if(name_size < 0) {
throw iodump_exception{"H5Lget_name_by_idx"}; throw iodump_exception{filename_, "H5Lget_name_by_idx"};
} }
return std::string(buf.data()); return std::string(buf.data());
...@@ -116,7 +115,7 @@ iodump iodump::create(const std::string &filename) { ...@@ -116,7 +115,7 @@ iodump iodump::create(const std::string &filename) {
hid_t file = H5Fcreate(filename.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); hid_t file = H5Fcreate(filename.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
if(file < 0) { if(file < 0) {
throw iodump_exception{"H5Fcreate"}; throw iodump_exception{filename, "H5Fcreate"};
} }
return iodump{filename, file}; return iodump{filename, file};
...@@ -126,7 +125,7 @@ iodump iodump::open_readonly(const std::string &filename) { ...@@ -126,7 +125,7 @@ iodump iodump::open_readonly(const std::string &filename) {
H5Eset_auto(H5E_DEFAULT, nullptr, nullptr); H5Eset_auto(H5E_DEFAULT, nullptr, nullptr);
hid_t file = H5Fopen(filename.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT); hid_t file = H5Fopen(filename.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
if(file < 0) { if(file < 0) {
throw iodump_exception{"H5Fopen"}; throw iodump_exception{filename, "H5Fopen"};
} }
return iodump{filename, file}; return iodump{filename, file};
} }
...@@ -139,7 +138,7 @@ iodump iodump::open_readwrite(const std::string &filename) { ...@@ -139,7 +138,7 @@ iodump iodump::open_readwrite(const std::string &filename) {
hid_t file = H5Fopen(filename.c_str(), H5F_ACC_RDWR, H5P_DEFAULT); hid_t file = H5Fopen(filename.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
if(file < 0) { if(file < 0) {
throw iodump_exception{"H5Fopen"}; throw iodump_exception{filename, "H5Fopen"};
} }
return iodump{filename, file}; return iodump{filename, file};
} }
...@@ -147,7 +146,7 @@ iodump iodump::open_readwrite(const std::string &filename) { ...@@ -147,7 +146,7 @@ iodump iodump::open_readwrite(const std::string &filename) {
iodump::iodump(std::string filename, hid_t h5_file) iodump::iodump(std::string filename, hid_t h5_file)
: filename_{std::move(filename)}, h5_file_{h5_file} { : filename_{std::move(filename)}, h5_file_{h5_file} {
if(compression_filter_ != 0 && !filter_available(compression_filter_)) { if(compression_filter_ != 0 && !filter_available(compression_filter_)) {
throw iodump_exception{"H5Filter not available."}; throw iodump_exception{filename_, "H5Filter not available."};
} }
} }
...@@ -156,7 +155,7 @@ iodump::~iodump() { ...@@ -156,7 +155,7 @@ iodump::~iodump() {
} }
iodump::group iodump::get_root() { iodump::group iodump::get_root() {
return group{h5_file_, "/"}; return group{h5_file_, filename_, "/"};
} }
iodump::h5_handle iodump::group::create_dataset(const std::string &name, hid_t datatype, iodump::h5_handle iodump::group::create_dataset(const std::string &name, hid_t datatype,
...@@ -172,7 +171,7 @@ iodump::h5_handle iodump::group::create_dataset(const std::string &name, hid_t d ...@@ -172,7 +171,7 @@ iodump::h5_handle iodump::group::create_dataset(const std::string &name, hid_t d
hssize_t oldsize = H5Sget_simple_extent_npoints(*dataspace); hssize_t oldsize = H5Sget_simple_extent_npoints(*dataspace);
if(oldsize < 0) { if(oldsize < 0) {
throw iodump_exception{"H5Sget_simple_extent_npoints"}; throw iodump_exception{filename_, "H5Sget_simple_extent_npoints"};
} }
if(static_cast<hsize_t>(oldsize) != size) { if(static_cast<hsize_t>(oldsize) != size) {
throw std::runtime_error{ throw std::runtime_error{
...@@ -195,13 +194,13 @@ iodump::h5_handle iodump::group::create_dataset(const std::string &name, hid_t d ...@@ -195,13 +194,13 @@ iodump::h5_handle iodump::group::create_dataset(const std::string &name, hid_t d
if(chunk_size > 1) { // do not use compression on small datasets if(chunk_size > 1) { // do not use compression on small datasets
status = H5Pset_chunk(*plist, 1, &chunk_size); status = H5Pset_chunk(*plist, 1, &chunk_size);
if(status < 0) { if(status < 0) {
throw iodump_exception{"H5Pset_chunk"}; throw iodump_exception{filename_, "H5Pset_chunk"};
} }
if(compression_filter == H5Z_FILTER_DEFLATE) { if(compression_filter == H5Z_FILTER_DEFLATE) {
status = H5Pset_deflate(*plist, 6); status = H5Pset_deflate(*plist, 6);
if(status < 0) { if(status < 0) {
throw iodump_exception{"H5Pset_deflate"}; throw iodump_exception{filename_, "H5Pset_deflate"};
} }
} }
} }
...@@ -213,7 +212,7 @@ iodump::h5_handle iodump::group::create_dataset(const std::string &name, hid_t d ...@@ -213,7 +212,7 @@ iodump::h5_handle iodump::group::create_dataset(const std::string &name, hid_t d
} }
iodump::group iodump::group::open_group(const std::string &path) const { iodump::group iodump::group::open_group(const std::string &path) const {
return group{group_, path}; return group{group_, filename_, path};
} }
size_t iodump::group::get_extent(const std::string &name) const { size_t iodump::group::get_extent(const std::string &name) const {
...@@ -222,7 +221,7 @@ size_t iodump::group::get_extent(const std::string &name) const { ...@@ -222,7 +221,7 @@ size_t iodump::group::get_extent(const std::string &name) const {
int size = H5Sget_simple_extent_npoints(*dataspace); // rank > 1 will get flattened when loaded. int size = H5Sget_simple_extent_npoints(*dataspace); // rank > 1 will get flattened when loaded.
if(size < 0) { if(size < 0) {
throw iodump_exception{"H5Sget_simple_extent_npoints"}; throw iodump_exception{filename_, "H5Sget_simple_extent_npoints"};
} }
return size; return size;
...@@ -235,7 +234,7 @@ bool iodump::group::exists(const std::string &path) const { ...@@ -235,7 +234,7 @@ bool iodump::group::exists(const std::string &path) const {
} }
if(exists < 0) { if(exists < 0) {
throw iodump_exception{"H5Lexists"}; throw iodump_exception{filename_, "H5Lexists"};
} }
return true; return true;
...@@ -244,7 +243,7 @@ bool iodump::group::exists(const std::string &path) const { ...@@ -244,7 +243,7 @@ bool iodump::group::exists(const std::string &path) const {
iodump::h5_handle::h5_handle(hid_t handle, herr_t (*closer)(hid_t)) iodump::h5_handle::h5_handle(hid_t handle, herr_t (*closer)(hid_t))
: closer_{closer}, handle_{handle} { : closer_{closer}, handle_{handle} {
if(handle < 0) { if(handle < 0) {
throw iodump_exception{"h5_handle"}; throw iodump_exception{"{undef}", "h5_handle"};
} }
} }
...@@ -258,7 +257,7 @@ iodump::h5_handle::~h5_handle() { ...@@ -258,7 +257,7 @@ iodump::h5_handle::~h5_handle() {
} }
herr_t status = closer_(handle_); herr_t status = closer_(handle_);
if(status < 0) { if(status < 0) {
std::cerr << iodump_exception{"~h5_handle"}.what() << "\n"; std::cerr << iodump_exception{"{undef}", "~h5_handle"}.what() << "\n";
std::abort(); std::abort();
} }
} }
......
...@@ -13,7 +13,7 @@ private: ...@@ -13,7 +13,7 @@ private:
std::string message_; std::string message_;
public: public:
iodump_exception(const std::string &msg); iodump_exception(const std::string &filename, const std::string &msg);
const char *what() const noexcept override; const char *what() const noexcept override;
}; };
...@@ -55,16 +55,16 @@ public: ...@@ -55,16 +55,16 @@ public:
class group { class group {
public: public:
struct iterator { struct iterator {
iterator(hid_t group, uint64_t idx);
std::string operator*(); std::string operator*();
iterator operator++(); iterator operator++();
bool operator!=(const iterator &b); bool operator!=(const iterator &b);
const hid_t group_; const hid_t group_;
const std::string &filename_;
uint64_t idx_; uint64_t idx_;
}; };
group(hid_t parent, const std::string &path); group(hid_t parent, const std::string &filename, const std::string &path);
group(const group &) = group(const group &) =
delete; // did you know this is a thing? Very handy in preventing errors. delete; // did you know this is a thing? Very handy in preventing errors.
~group(); ~group();
...@@ -95,6 +95,7 @@ public: ...@@ -95,6 +95,7 @@ public:
const std::string &path) const; // checks whether an object in the dump file exists const std::string &path) const; // checks whether an object in the dump file exists
private: private:
hid_t group_; hid_t group_;
const std::string &filename_;
// chunk_size == 0 means contiguous storage // chunk_size == 0 means contiguous storage
// if the dataset already exists, we try to overwrite it. However it must have the same // if the dataset already exists, we try to overwrite it. However it must have the same
...@@ -172,7 +173,7 @@ void iodump::group::write(const std::string &name, const std::vector<T> &data) c ...@@ -172,7 +173,7 @@ void iodump::group::write(const std::string &name, const std::vector<T> &data) c
herr_t status = herr_t status =
H5Dwrite(*dataset, h5_datatype<T>(), H5S_ALL, H5S_ALL, H5P_DEFAULT, data.data()); H5Dwrite(*dataset, h5_datatype<T>(), H5S_ALL, H5S_ALL, H5P_DEFAULT, data.data());
if(status < 0) if(status < 0)
throw iodump_exception{"H5Dwrite"}; throw iodump_exception{filename_, "H5Dwrite"};
} }
template<class T> template<class T>
...@@ -205,14 +206,14 @@ void iodump::group::insert_back(const std::string &name, const std::vector<T> &d ...@@ -205,14 +206,14 @@ void iodump::group::insert_back(const std::string &name, const std::vector<T> &d
size = H5Sget_simple_extent_npoints(*dataspace); size = H5Sget_simple_extent_npoints(*dataspace);
if(size < 0) { if(size < 0) {
throw iodump_exception{"H5Sget_simple_extent_npoints"}; throw iodump_exception{filename_, "H5Sget_simple_extent_npoints"};
} }
if(data.size() > 0) { if(data.size() > 0) {
hsize_t new_size = size + data.size(); hsize_t new_size = size + data.size();
status = H5Dset_extent(*dataset, &new_size); status = H5Dset_extent(*dataset, &new_size);
if(status < 0) { if(status < 0) {
throw iodump_exception{"H5Pset_extent"}; throw iodump_exception{filename_, "H5Pset_extent"};
} }
} }
} // because it will be reopened after this } // because it will be reopened after this
...@@ -224,11 +225,11 @@ void iodump::group::insert_back(const std::string &name, const std::vector<T> &d ...@@ -224,11 +225,11 @@ void iodump::group::insert_back(const std::string &name, const std::vector<T> &d
hsize_t extent = data.size(); hsize_t extent = data.size();
status = H5Sselect_hyperslab(*dataspace, H5S_SELECT_SET, &pos, nullptr, &extent, nullptr); status = H5Sselect_hyperslab(*dataspace, H5S_SELECT_SET, &pos, nullptr, &extent, nullptr);
if(status < 0) if(status < 0)
throw iodump_exception{"H5Sselect_hyperslap"}; throw iodump_exception{filename_, "H5Sselect_hyperslap"};
status = H5Dwrite(*dataset, h5_datatype<T>(), *memspace, *dataspace, H5P_DEFAULT, data.data()); status = H5Dwrite(*dataset, h5_datatype<T>(), *memspace, *dataspace, H5P_DEFAULT, data.data());
if(status < 0) if(status < 0)
throw iodump_exception{"H5Dwrite"}; throw iodump_exception{filename_, "H5Dwrite"};
} }
template<class T> template<class T>
...@@ -238,7 +239,7 @@ void iodump::group::read(const std::string &name, std::vector<T> &data) const { ...@@ -238,7 +239,7 @@ void iodump::group::read(const std::string &name, std::vector<T> &data) const {
int size = H5Sget_simple_extent_npoints(*dataspace); // rank > 1 will get flattened when loaded. int size = H5Sget_simple_extent_npoints(*dataspace); // rank > 1 will get flattened when loaded.
if(size < 0) if(size < 0)
throw iodump_exception{"H5Sget_simple_extent_npoints"}; throw iodump_exception{filename_, "H5Sget_simple_extent_npoints"};
data.resize(size); data.resize(size);
if(size == 0) { // handle empty dataset correctly if(size == 0) { // handle empty dataset correctly
...@@ -248,7 +249,7 @@ void iodump::group::read(const std::string &name, std::vector<T> &data) const { ...@@ -248,7 +249,7 @@ void iodump::group::read(const std::string &name, std::vector<T> &data) const {
herr_t status = herr_t status =
H5Dread(*dataset, h5_datatype<T>(), H5S_ALL, H5P_DEFAULT, H5P_DEFAULT, data.data()); H5Dread(*dataset, h5_datatype<T>(), H5S_ALL, H5P_DEFAULT, H5P_DEFAULT, data.data());
if(status < 0) if(status < 0)
throw iodump_exception{"H5Dread"}; throw iodump_exception{filename_, "H5Dread"};
} }
template<> template<>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment