mirror of
https://github.com/mhx/dwarfs.git
synced 2025-09-08 11:59:48 -04:00
feat: only report basename of file in runtime errors
This commit is contained in:
parent
51bd06e74f
commit
6e1e82e29d
@ -71,4 +71,6 @@ bool getenv_is_enabled(char const* var);
|
||||
|
||||
void setup_default_locale();
|
||||
|
||||
std::string_view basename(std::string_view path);
|
||||
|
||||
} // namespace dwarfs
|
||||
|
@ -34,6 +34,7 @@
|
||||
|
||||
#include "dwarfs/error.h"
|
||||
#include "dwarfs/terminal.h"
|
||||
#include "dwarfs/util.h"
|
||||
|
||||
namespace dwarfs {
|
||||
|
||||
@ -50,7 +51,7 @@ namespace {
|
||||
} // namespace
|
||||
|
||||
error::error(std::string const& s, char const* file, int line) noexcept
|
||||
: what_{fmt::format("{} [{}:{}]", s, file, line)}
|
||||
: what_{fmt::format("{} [{}:{}]", s, basename(file), line)}
|
||||
, file_{file}
|
||||
, line_{line} {}
|
||||
|
||||
|
@ -21,7 +21,6 @@
|
||||
|
||||
#include <cstring>
|
||||
#include <exception>
|
||||
#include <filesystem>
|
||||
#include <iterator>
|
||||
#include <stdexcept>
|
||||
|
||||
@ -250,8 +249,7 @@ void stream_logger::set_threshold(level_type threshold) {
|
||||
}
|
||||
|
||||
std::string get_logger_context(char const* path, int line) {
|
||||
auto base = ::strrchr(path, std::filesystem::path::preferred_separator);
|
||||
return fmt::format("[{0}:{1}] ", base ? base + 1 : path, line);
|
||||
return fmt::format("[{0}:{1}] ", basename(path), line);
|
||||
}
|
||||
|
||||
std::string get_current_time_string() {
|
||||
|
@ -342,4 +342,12 @@ void setup_default_locale() {
|
||||
}
|
||||
}
|
||||
|
||||
std::string_view basename(std::string_view path) {
|
||||
auto pos = path.find_last_of("/\\");
|
||||
if (pos == std::string_view::npos) {
|
||||
return path;
|
||||
}
|
||||
return path.substr(pos + 1);
|
||||
}
|
||||
|
||||
} // namespace dwarfs
|
||||
|
@ -59,10 +59,10 @@ TEST(error_test, runtime_error) {
|
||||
test_throw_runtime_error(true);
|
||||
FAIL() << "expected runtime_error to be thrown";
|
||||
} catch (const runtime_error& e) {
|
||||
EXPECT_EQ(fmt::format("my test error [{}:{}]", e.file(), e.line()),
|
||||
std::string(e.what()));
|
||||
EXPECT_EQ("error_test.cpp",
|
||||
std::filesystem::path(e.file()).filename().string());
|
||||
EXPECT_EQ(fmt::format("my test error [error_test.cpp:{}]", e.line()),
|
||||
std::string(e.what()));
|
||||
EXPECT_EQ(expected_line, e.line());
|
||||
} catch (...) {
|
||||
FAIL() << "expected runtime_error, got "
|
||||
|
Loading…
x
Reference in New Issue
Block a user