Skip to content

Commit e181e94

Browse files
Add FMT_CUSTOM_ASSERT_FAIL (#4505)
That way one can provide ones own implementation for assert_fail, which is moved out of the detail namespace. For binary compatibility the detail version stays to call the outer version.
1 parent 43c88d0 commit e181e94

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

include/fmt/base.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,9 @@ template <typename T> constexpr auto max_of(T a, T b) -> T {
348348
return a > b ? a : b;
349349
}
350350

351+
FMT_NORETURN FMT_API void assert_fail(const char* file, int line,
352+
const char* message);
353+
351354
namespace detail {
352355
// Suppresses "unused variable" warnings with the method described in
353356
// https://herbsutter.com/2009/10/18/mailbag-shutting-up-compiler-warnings/.
@@ -388,7 +391,7 @@ FMT_NORETURN FMT_API void assert_fail(const char* file, int line,
388391
# define FMT_ASSERT(condition, message) \
389392
((condition) /* void() fails with -Winvalid-constexpr on clang 4.0.1 */ \
390393
? (void)0 \
391-
: fmt::detail::assert_fail(__FILE__, __LINE__, (message)))
394+
: ::fmt::assert_fail(__FILE__, __LINE__, (message)))
392395
#endif
393396

394397
#ifdef FMT_USE_INT128

include/fmt/format-inl.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,22 @@
3131
#endif
3232

3333
FMT_BEGIN_NAMESPACE
34-
namespace detail {
3534

35+
#ifndef FMT_CUSTOM_ASSERT_FAIL
3636
FMT_FUNC void assert_fail(const char* file, int line, const char* message) {
3737
// Use unchecked std::fprintf to avoid triggering another assertion when
3838
// writing to stderr fails.
3939
fprintf(stderr, "%s:%d: assertion failed: %s", file, line, message);
4040
abort();
4141
}
42+
#endif
43+
44+
namespace detail {
45+
46+
// For binary compatibility.
47+
FMT_FUNC void assert_fail(const char* file, int line, const char* message) {
48+
::fmt::assert_fail(file, line, message);
49+
}
4250

4351
FMT_FUNC void format_error_code(detail::buffer<char>& out, int error_code,
4452
string_view message) noexcept {

include/fmt/format.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,7 @@ template <typename T> struct iterator_traits<fmt::basic_appender<T>> {
170170
#elif FMT_USE_EXCEPTIONS
171171
# define FMT_THROW(x) throw x
172172
#else
173-
# define FMT_THROW(x) \
174-
::fmt::detail::assert_fail(__FILE__, __LINE__, (x).what())
173+
# define FMT_THROW(x) ::fmt::assert_fail(__FILE__, __LINE__, (x).what())
175174
#endif
176175

177176
// Defining FMT_REDUCE_INT_INSTANTIATIONS to 1, will reduce the number of

0 commit comments

Comments
 (0)