From 5dd02e7e58dc6d9297c01ddf97706966a0745d03 Mon Sep 17 00:00:00 2001 From: Curve Date: Wed, 11 Oct 2023 18:55:08 +0200 Subject: [PATCH] refactor(check): don't throw, print to stderr --- include/rohrkabel/utils/assert.hpp | 6 ------ include/rohrkabel/utils/check.hpp | 7 +++++++ src/assert.cpp | 16 ---------------- src/check.cpp | 26 ++++++++++++++++++++++++++ 4 files changed, 33 insertions(+), 22 deletions(-) delete mode 100644 include/rohrkabel/utils/assert.hpp create mode 100644 include/rohrkabel/utils/check.hpp delete mode 100644 src/assert.cpp create mode 100644 src/check.cpp diff --git a/include/rohrkabel/utils/assert.hpp b/include/rohrkabel/utils/assert.hpp deleted file mode 100644 index 6de9a1b..0000000 --- a/include/rohrkabel/utils/assert.hpp +++ /dev/null @@ -1,6 +0,0 @@ -#pragma once - -namespace pipewire -{ - void check(bool, const char *); -} diff --git a/include/rohrkabel/utils/check.hpp b/include/rohrkabel/utils/check.hpp new file mode 100644 index 0000000..97bb8c8 --- /dev/null +++ b/include/rohrkabel/utils/check.hpp @@ -0,0 +1,7 @@ +#pragma once +#include + +namespace pipewire +{ + void check(bool, const char *, const std::source_location = std::source_location::current()); +} diff --git a/src/assert.cpp b/src/assert.cpp deleted file mode 100644 index 38c13b4..0000000 --- a/src/assert.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include "utils/assert.hpp" - -#include - -namespace pipewire -{ - void check(bool condition, const char *message) - { - if (condition) - { - return; - } - - throw std::runtime_error(message); - } -} // namespace pipewire diff --git a/src/check.cpp b/src/check.cpp new file mode 100644 index 0000000..a16293b --- /dev/null +++ b/src/check.cpp @@ -0,0 +1,26 @@ +#include "utils/check.hpp" + +#include +#include +#include + +namespace pipewire +{ +#ifdef NDEBUG + void check(bool, const char *, const std::source_location) {} +#else + void check(bool condition, const char *message, const std::source_location loc) + { + if (condition) + { + return; + } + + const auto *error = strerror(errno); // NOLINT(*-mt-unsafe) + auto [file, line, col, func] = std::make_tuple(loc.file_name(), loc.line(), loc.column(), loc.function_name()); + + std::cerr << std::format("{} ({}:{}) [{}]: check failed \"{}\", error is \"{}\"\n", file, line, col, func, + message, error); + } +#endif +} // namespace pipewire