From 9c8fd29ec298e7ef8694da33e9db7ca4edea08d7 Mon Sep 17 00:00:00 2001 From: Marcus Holland-Moritz Date: Sat, 27 Jul 2024 23:17:45 +0200 Subject: [PATCH] refactor: provide exception_str wrapper --- include/dwarfs/util.h | 4 ++++ src/dwarfs/error.cpp | 6 ++---- src/dwarfs/file_scanner.cpp | 5 +++-- src/dwarfs/filesystem_extractor.cpp | 8 ++++---- src/dwarfs/filesystem_v2.cpp | 4 ++-- src/dwarfs/inode_manager.cpp | 5 ++--- src/dwarfs/internal/inode_reader_v2.cpp | 4 ++-- src/dwarfs/internal/worker_group.cpp | 2 +- src/dwarfs/safe_main.cpp | 4 +--- src/dwarfs/scanner.cpp | 7 +++---- src/dwarfs/util.cpp | 11 ++++++++++- src/dwarfs_main.cpp | 10 +++++----- src/dwarfsbench_main.cpp | 10 ++++------ src/dwarfsck_main.cpp | 3 +-- src/dwarfsextract_main.cpp | 2 +- src/mkdwarfs_main.cpp | 2 +- 16 files changed, 46 insertions(+), 41 deletions(-) diff --git a/include/dwarfs/util.h b/include/dwarfs/util.h index 9c3182d9..4c809cbd 100644 --- a/include/dwarfs/util.h +++ b/include/dwarfs/util.h @@ -23,6 +23,7 @@ #include #include +#include #include #include #include @@ -78,4 +79,7 @@ std::string_view basename(std::string_view path); void ensure_binary_mode(std::ostream& os); +std::string exception_str(std::exception const& e); +std::string exception_str(std::exception_ptr const& e); + } // namespace dwarfs diff --git a/src/dwarfs/error.cpp b/src/dwarfs/error.cpp index 7a7a8b0d..eba1914a 100644 --- a/src/dwarfs/error.cpp +++ b/src/dwarfs/error.cpp @@ -26,8 +26,6 @@ #include -#include - #ifdef DWARFS_USE_EXCEPTION_TRACER #include #endif @@ -86,8 +84,8 @@ void dump_exceptions() { void handle_nothrow(char const* expr, char const* file, int line) { std::cerr << "Expression `" << expr << "` threw `" - << folly::exceptionStr(std::current_exception()) << "` in " << file - << "(" << line << ")\n"; + << exception_str(std::current_exception()) << "` in " << file << "(" + << line << ")\n"; do_terminate(); } diff --git a/src/dwarfs/file_scanner.cpp b/src/dwarfs/file_scanner.cpp index b015abbd..f6d30faa 100644 --- a/src/dwarfs/file_scanner.cpp +++ b/src/dwarfs/file_scanner.cpp @@ -45,6 +45,7 @@ #include #include #include +#include template class fmt::formatter> { @@ -289,7 +290,7 @@ void file_scanner_::scan_dedupe(file* p) { cs.finalize(&start_hash); } catch (...) { LOG_ERROR << "failed to map file " << p->path_as_string() << ": " - << folly::exceptionStr(std::current_exception()) + << exception_str(std::current_exception()) << ", creating empty file"; ++prog_.errors; p->set_invalid(); @@ -422,7 +423,7 @@ void file_scanner_::hash_file(file* p) { mm = os_.map_file(p->fs_path(), size); } catch (...) { LOG_ERROR << "failed to map file " << p->path_as_string() << ": " - << folly::exceptionStr(std::current_exception()) + << exception_str(std::current_exception()) << ", creating empty file"; ++prog_.errors; p->set_invalid(); diff --git a/src/dwarfs/filesystem_extractor.cpp b/src/dwarfs/filesystem_extractor.cpp index 464a97bf..109ebb20 100644 --- a/src/dwarfs/filesystem_extractor.cpp +++ b/src/dwarfs/filesystem_extractor.cpp @@ -300,14 +300,14 @@ bool filesystem_extractor_::extract( } sem.post(bs); } catch (archive_error const& e) { - LOG_ERROR << folly::exceptionStr(e); + LOG_ERROR << exception_str(e); ++hard_error; } catch (...) { if (opts.continue_on_error) { - LOG_WARN << folly::exceptionStr(std::current_exception()); + LOG_WARN << exception_str(std::current_exception()); ++soft_error; } else { - LOG_ERROR << folly::exceptionStr(std::current_exception()); + LOG_ERROR << exception_str(std::current_exception()); ++hard_error; } archive_entry_free(ae); @@ -328,7 +328,7 @@ bool filesystem_extractor_::extract( try { check_result(::archive_write_header(a_, ae)); } catch (...) { - LOG_ERROR << folly::exceptionStr(std::current_exception()); + LOG_ERROR << exception_str(std::current_exception()); hard_error = true; } }); diff --git a/src/dwarfs/filesystem_v2.cpp b/src/dwarfs/filesystem_v2.cpp index 7b9b8496..fe4ac11a 100644 --- a/src/dwarfs/filesystem_v2.cpp +++ b/src/dwarfs/filesystem_v2.cpp @@ -885,7 +885,7 @@ int filesystem_::check(filesystem_check_level level, } } } catch (std::exception const& e) { - LOG_ERROR << folly::exceptionStr(e); + LOG_ERROR << exception_str(e); ++errors; } } @@ -894,7 +894,7 @@ int filesystem_::check(filesystem_check_level level, try { meta_.check_consistency(); } catch (std::exception const& e) { - LOG_ERROR << folly::exceptionStr(e); + LOG_ERROR << exception_str(e); ++errors; } } diff --git a/src/dwarfs/inode_manager.cpp b/src/dwarfs/inode_manager.cpp index 3f2c9461..d7c2e8c9 100644 --- a/src/dwarfs/inode_manager.cpp +++ b/src/dwarfs/inode_manager.cpp @@ -38,7 +38,6 @@ #include #include -#include #include #include @@ -60,6 +59,7 @@ #include #include #include +#include #include @@ -737,8 +737,7 @@ void inode_manager_::try_scan_invalid(internal::worker_group& wg, for (auto const& [fp, ep] : errors) { LOG_ERROR << "failed to map file \"" << fp->path_as_string() - << "\": " << folly::exceptionStr(ep) - << ", creating empty inode"; + << "\": " << exception_str(ep) << ", creating empty inode"; ++prog_.errors; } } diff --git a/src/dwarfs/internal/inode_reader_v2.cpp b/src/dwarfs/internal/inode_reader_v2.cpp index 5b458558..1285752d 100644 --- a/src/dwarfs/internal/inode_reader_v2.cpp +++ b/src/dwarfs/internal/inode_reader_v2.cpp @@ -28,7 +28,6 @@ #include #include -#include #include #include #include @@ -41,6 +40,7 @@ #include #include #include +#include namespace dwarfs::internal { @@ -345,7 +345,7 @@ inode_reader_::read_internal(uint32_t inode, size_t size, } return num_read; } catch (...) { - LOG_ERROR << folly::exceptionStr(std::current_exception()); + LOG_ERROR << exception_str(std::current_exception()); } return -EIO; diff --git a/src/dwarfs/internal/worker_group.cpp b/src/dwarfs/internal/worker_group.cpp index b10d6ab6..04e7cd12 100644 --- a/src/dwarfs/internal/worker_group.cpp +++ b/src/dwarfs/internal/worker_group.cpp @@ -301,7 +301,7 @@ class basic_worker_group final : public worker_group::impl, private Policy { std::visit([](auto&& j) { j(); }, job); } catch (...) { LOG_FATAL << "exception thrown in worker thread: " - << folly::exceptionStr(std::current_exception()); + << exception_str(std::current_exception()); } #ifdef _WIN32 if (is_background) { diff --git a/src/dwarfs/safe_main.cpp b/src/dwarfs/safe_main.cpp index 2953e603..55f070a0 100644 --- a/src/dwarfs/safe_main.cpp +++ b/src/dwarfs/safe_main.cpp @@ -23,7 +23,6 @@ #include #include -#include #include #include @@ -43,8 +42,7 @@ int safe_main(std::function fn) { return fn(); } catch (...) { - std::cerr << "ERROR: " << folly::exceptionStr(std::current_exception()) - << "\n"; + std::cerr << "ERROR: " << exception_str(std::current_exception()) << "\n"; dump_exceptions(); } return 1; diff --git a/src/dwarfs/scanner.cpp b/src/dwarfs/scanner.cpp index 0cc7cc7b..7144834f 100644 --- a/src/dwarfs/scanner.cpp +++ b/src/dwarfs/scanner.cpp @@ -34,7 +34,6 @@ #include #include -#include #include #include @@ -437,7 +436,7 @@ scanner_::add_entry(std::filesystem::path const& name, return pe; } catch (const std::system_error& e) { - LOG_ERROR << "error reading entry: " << folly::exceptionStr(e); + LOG_ERROR << "error reading entry: " << exception_str(e); prog.errors++; } @@ -514,7 +513,7 @@ scanner_::scan_tree(std::filesystem::path const& path, prog.dirs_scanned++; } catch (const std::system_error& e) { LOG_ERROR << "cannot read directory `" << ppath - << "`: " << folly::exceptionStr(e); + << "`: " << exception_str(e); prog.errors++; } } @@ -801,7 +800,7 @@ void scanner_::scan( } else { for (auto& [fp, e] : errors) { LOG_ERROR << "failed to map file " << fp->path_as_string() - << ": " << folly::exceptionStr(e) + << ": " << exception_str(e) << ", creating empty inode"; ++prog.errors; } diff --git a/src/dwarfs/util.cpp b/src/dwarfs/util.cpp index a231fc58..04e98576 100644 --- a/src/dwarfs/util.cpp +++ b/src/dwarfs/util.cpp @@ -36,6 +36,7 @@ #include #include +#include #include #include #include @@ -192,7 +193,7 @@ file_off_t parse_image_offset(std::string const& str) { auto ce = folly::makeConversionError(off.error(), str); DWARFS_THROW(runtime_error, fmt::format("failed to parse image offset: {} ({})", str, - folly::exceptionStr(ce))); + exception_str(ce))); } if (off.value() < 0) { @@ -377,4 +378,12 @@ void ensure_binary_mode(std::ostream& os [[maybe_unused]]) { #endif } +std::string exception_str(std::exception const& e) { + return folly::exceptionStr(e).toStdString(); +} + +std::string exception_str(std::exception_ptr const& e) { + return folly::exceptionStr(e).toStdString(); +} + } // namespace dwarfs diff --git a/src/dwarfs_main.cpp b/src/dwarfs_main.cpp index 1d75b8ea..1636982b 100644 --- a/src/dwarfs_main.cpp +++ b/src/dwarfs_main.cpp @@ -263,10 +263,10 @@ auto checked_call(LogProxy& log_, T&& f) -> decltype(std::forward(f)()) { try { return std::forward(f)(); } catch (dwarfs::system_error const& e) { - LOG_ERROR << folly::exceptionStr(e); + LOG_ERROR << exception_str(e); return e.get_errno(); } catch (std::exception const& e) { - LOG_ERROR << folly::exceptionStr(e); + LOG_ERROR << exception_str(e); return EIO; } } @@ -1580,10 +1580,10 @@ int dwarfs_main(int argc, sys_char** argv, iolayer const& iol) { } } } catch (std::filesystem::filesystem_error const& e) { - iol.err << folly::exceptionStr(e) << "\n"; + iol.err << exception_str(e) << "\n"; return 1; } catch (std::exception const& e) { - iol.err << "error: " << folly::exceptionStr(e) << "\n"; + iol.err << "error: " << exception_str(e) << "\n"; return 1; } @@ -1620,7 +1620,7 @@ int dwarfs_main(int argc, sys_char** argv, iolayer const& iol) { load_filesystem(userdata); } } catch (std::exception const& e) { - LOG_ERROR << "error initializing file system: " << folly::exceptionStr(e); + LOG_ERROR << "error initializing file system: " << exception_str(e); return 1; } diff --git a/src/dwarfsbench_main.cpp b/src/dwarfsbench_main.cpp index cee050e8..08dce3a7 100644 --- a/src/dwarfsbench_main.cpp +++ b/src/dwarfsbench_main.cpp @@ -23,8 +23,6 @@ #include -#include - #include #include #include @@ -118,10 +116,10 @@ int dwarfsbench_main(int argc, sys_char** argv, iolayer const& iol) { fs.read(fh, buf.data(), buf.size()); } } catch (std::exception const& e) { - iol.err << "error: " << folly::exceptionStr(e) << "\n"; + iol.err << "error: " << exception_str(e) << "\n"; } catch (...) { - iol.err << "error: " - << folly::exceptionStr(std::current_exception()) << "\n"; + iol.err << "error: " << exception_str(std::current_exception()) + << "\n"; dump_exceptions(); } }); @@ -130,7 +128,7 @@ int dwarfsbench_main(int argc, sys_char** argv, iolayer const& iol) { pool.wait(); } catch (std::exception const& e) { - iol.err << "error: " << folly::exceptionStr(e) << "\n"; + iol.err << "error: " << exception_str(e) << "\n"; return 1; } diff --git a/src/dwarfsck_main.cpp b/src/dwarfsck_main.cpp index 3f9da523..e724fb99 100644 --- a/src/dwarfsck_main.cpp +++ b/src/dwarfsck_main.cpp @@ -34,7 +34,6 @@ #include #endif -#include #include #include @@ -350,7 +349,7 @@ int dwarfsck_main(int argc, sys_char** argv, iolayer const& iol) { } } } catch (std::exception const& e) { - iol.err << folly::exceptionStr(e) << "\n"; + iol.err << exception_str(e) << "\n"; return 1; } diff --git a/src/dwarfsextract_main.cpp b/src/dwarfsextract_main.cpp index 23b36247..79adb581 100644 --- a/src/dwarfsextract_main.cpp +++ b/src/dwarfsextract_main.cpp @@ -227,7 +227,7 @@ int dwarfsextract_main(int argc, sys_char** argv, iolayer const& iol) { perfmon->summarize(iol.err); } } catch (std::exception const& e) { - iol.err << folly::exceptionStr(e) << "\n"; + iol.err << exception_str(e) << "\n"; return 1; } diff --git a/src/mkdwarfs_main.cpp b/src/mkdwarfs_main.cpp index fecb96ae..e3ab81b4 100644 --- a/src/mkdwarfs_main.cpp +++ b/src/mkdwarfs_main.cpp @@ -1356,7 +1356,7 @@ int mkdwarfs_main(int argc, sys_char** argv, iolayer const& iol) { options.inode.categorizer_mgr.reset(); } } catch (std::exception const& e) { - LOG_ERROR << folly::exceptionStr(e); + LOG_ERROR << exception_str(e); return 1; }