diff --git a/CMakeLists.txt b/CMakeLists.txt index df0d5e30..ea8fc60b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -152,6 +152,7 @@ list( src/dwarfs/block_manager.cpp src/dwarfs/console_writer.cpp src/dwarfs/entry.cpp + src/dwarfs/error.cpp src/dwarfs/filesystem_v2.cpp src/dwarfs/filesystem_writer.cpp src/dwarfs/fstypes.cpp @@ -351,7 +352,7 @@ target_link_libraries( PkgConfig::LIBLZ4 PkgConfig::LIBLZMA) -foreach(tgt dwarfs-bin mkdwarfs dwarfsck dwarfsbench) +foreach(tgt ${BINARY_TARGETS}) target_link_libraries(${tgt} dwarfs) target_link_libraries(${tgt} -Wl,--whole-archive folly_exception_tracer_base folly_exception_tracer -Wl,--no-whole-archive) @@ -363,13 +364,8 @@ if(WITH_PYTHON) endif() target_link_libraries(mkdwarfs ${BOOST_PYTHON_LIBS} ${Python3_LIBRARIES}) -endif() -if(WITH_TESTS) - foreach(tgt dwarfs_test dwarfs_compat_test) - target_link_libraries(${tgt} dwarfs) - endforeach() - if(WITH_PYTHON) + if(WITH_TESTS) foreach(tgt dwarfs_test dwarfs_compat_test) target_link_libraries(${tgt} ${BOOST_PYTHON_LIBS} ${Python3_LIBRARIES}) endforeach() diff --git a/include/dwarfs/error.h b/include/dwarfs/error.h index 1d03a819..4e7ca7fd 100644 --- a/include/dwarfs/error.h +++ b/include/dwarfs/error.h @@ -22,34 +22,56 @@ #pragma once #include +#include +#include + +#include +#include namespace dwarfs { class error : public std::exception { public: - error(const std::string& str, int err_no) noexcept - : what_(str) - , errno_(err_no) {} + error(std::string const& s, char const* file, int line) noexcept + : what_(s) + , file_(file) + , line_(line) {} - error(const error& e) noexcept - : what_(e.what_) - , errno_(e.errno_) {} + char const* what() const noexcept override { return what_.c_str(); } - error& operator=(const error& e) noexcept { - if (&e != this) { - what_ = e.what_; - errno_ = e.errno_; - } - return *this; - } + char const* file() const { return file_; } - const char* what() const noexcept override { return what_.c_str(); } - - int get_errno() const { return errno_; } + int line() const { return line_; } private: std::string what_; - int errno_; + char const* file_; + int line_; }; +class system_error : public boost::system::system_error { + public: + system_error(char const* file, int line) noexcept; + system_error(std::string const& s, char const* file, int line) noexcept; + system_error(std::string const& s, int err, char const* file, + int line) noexcept; + system_error(int err, char const* file, int line) noexcept; + + int get_errno() const { return code().value(); } + + char const* file() const { return file_; } + + int line() const { return line_; } + + private: + char const* file_; + int line_; +}; + +#define DWARFS_THROW(cls, ...) throw cls(__VA_ARGS__, __FILE__, __LINE__) + +void dump_exceptions(); + +int safe_main(std::function fn); + } // namespace dwarfs diff --git a/include/dwarfs/logger.h b/include/dwarfs/logger.h index 6d06875a..0abbd258 100644 --- a/include/dwarfs/logger.h +++ b/include/dwarfs/logger.h @@ -36,6 +36,7 @@ #include #include +#include "dwarfs/error.h" #include "dwarfs/util.h" namespace dwarfs { @@ -302,7 +303,7 @@ template