diff options
Diffstat (limited to 'compat')
-rw-r--r-- | compat/assert.hpp | 20 | ||||
-rw-r--r-- | compat/debug.cpp | 28 | ||||
-rw-r--r-- | compat/debug.hpp | 39 | ||||
-rw-r--r-- | compat/strerror.cpp | 2 | ||||
-rw-r--r-- | compat/strerror.hpp | 1 |
5 files changed, 63 insertions, 27 deletions
diff --git a/compat/assert.hpp b/compat/assert.hpp index bdb6b578..d72186ea 100644 --- a/compat/assert.hpp +++ b/compat/assert.hpp @@ -92,16 +92,16 @@ { \ if (a != b) [[unlikely]] \ { \ - WARN_nospace << Debug::color(Debug::Color::Magenta) \ - << "fatal:" \ - << Debug::resetColor << " " \ - << "Equality assertion failed at " \ - << __FILE__ << ":" << __LINE__; \ - WARN_nospace << #__VA_ARGS__; \ - WARN_nospace << " expected: " << a; \ - WARN_nospace << " actual: " << b; \ - fm_EMIT_ABORT(); \ - } \ + ERR_nospace << Debug::color(Debug::Color::Magenta) \ + << "fatal:" \ + << Debug::resetColor << " " \ + << "Equality assertion failed at " \ + << __FILE__ << ":" << __LINE__; \ + ERR_nospace << #__VA_ARGS__; \ + ERR_nospace << " expected: " << a; \ + ERR_nospace << " actual: " << b; \ + fm_EMIT_ABORT(); \ + } \ })(__VA_ARGS__) #ifdef __GNUG__ diff --git a/compat/debug.cpp b/compat/debug.cpp index e3efaf5c..a3c2ba25 100644 --- a/compat/debug.cpp +++ b/compat/debug.cpp @@ -1,8 +1,22 @@ #include "debug.hpp" #include "compat/strerror.hpp" +#include <cerrno> +#include <Corrade/Containers/StringView.h> + +// Error{} << "error" << colon() << "can't open file" << colon() << quoted("foo") << error_string(EINVAL); +// ===> "error: can't open file 'foo': Invalid argument" namespace floormat::detail::corrade_debug { +Debug& operator<<(Debug& dbg, Colon) +{ + auto flags = dbg.flags(); + dbg.setFlags(flags | Debug::Flag::NoSpace); + dbg << ":"; + dbg.setFlags(flags); + return dbg; +} + Debug& operator<<(Debug& dbg, ErrorString box) { auto flags = dbg.flags(); @@ -14,9 +28,7 @@ Debug& operator<<(Debug& dbg, ErrorString box) return dbg; } -template struct Quoted<StringView>; - -Debug::Flags debug1(Debug& dbg, char c) +Debug::Flags quoted_begin(Debug& dbg, char c) { auto flags = dbg.flags(); dbg << ""; @@ -26,7 +38,7 @@ Debug::Flags debug1(Debug& dbg, char c) return flags; } -Debug& debug2(Debug& dbg, Debug::Flags flags, char c) +Debug& quoted_end(Debug& dbg, Debug::Flags flags, char c) { char buf[2] { c, '\0' }; dbg << buf; @@ -34,10 +46,16 @@ Debug& debug2(Debug& dbg, Debug::Flags flags, char c) return dbg; } +template struct Quoted<StringView>; + } // namespace floormat::detail::corrade_debug namespace floormat { -floormat::detail::corrade_debug::ErrorString error_string(int error) { return { error }; } +using namespace floormat::detail::corrade_debug; + +Colon colon() { return Colon{}; } +ErrorString error_string(int error) { return { error }; } +ErrorString error_string() { return { errno }; } } // namespace floormat diff --git a/compat/debug.hpp b/compat/debug.hpp index 0009ce52..56db4b0f 100644 --- a/compat/debug.hpp +++ b/compat/debug.hpp @@ -1,31 +1,50 @@ #pragma once #include <Corrade/Utility/Debug.h> -#include <Corrade/Utility/Move.h> #include <Corrade/Containers/Containers.h> +#include <concepts> + +namespace floormat { + +template<typename T> +concept DebugPrintable = requires(Debug& dbg, const T& value) { + { dbg << value } -> std::convertible_to<Debug&>; +}; + +} // namespace floormat namespace floormat::detail::corrade_debug { -struct ErrorString final { int value; }; +// ***** colon ***** +struct Colon {}; +Debug& operator<<(Debug& dbg, Colon box); + +// ***** error string ***** +struct ErrorString { int value; }; Debug& operator<<(Debug& dbg, ErrorString box); -template<typename T> struct Quoted final { const T& value; }; -Debug::Flags debug1(Debug& dbg, char c); -Debug& debug2(Debug& dbg, Debug::Flags flags, char c); +// ***** quoted ***** +template<typename T> struct Quoted { const T& value; }; +Debug::Flags quoted_begin(Debug& dbg, char c); +Debug& quoted_end(Debug& dbg, Debug::Flags flags, char c); template<typename T> Debug& operator<<(Debug& dbg, Quoted<T> box) { - Debug::Flags flags = debug1(dbg, '\''); + Debug::Flags flags = quoted_begin(dbg, '\''); dbg << box.value; - return debug2(dbg, flags, '\''); + return quoted_end(dbg, flags, '\''); } -extern template struct Quoted<StringView>; - } // namespace floormat::detail::corrade_debug +// ***** api ***** + +// ***** functions ***** + namespace floormat { +floormat::detail::corrade_debug::Colon colon(); floormat::detail::corrade_debug::ErrorString error_string(int error); -template<typename T> floormat::detail::corrade_debug::Quoted<T> quoted(const T& value) { return { value }; } +floormat::detail::corrade_debug::ErrorString error_string(); +template<DebugPrintable T> floormat::detail::corrade_debug::Quoted<T> quoted(const T& value) { return { value }; } } // namespace floormat diff --git a/compat/strerror.cpp b/compat/strerror.cpp index 8b237153..f952cd3c 100644 --- a/compat/strerror.cpp +++ b/compat/strerror.cpp @@ -1,6 +1,7 @@ #include "strerror.hpp" #include <cerrno> #include <string.h> +#include <Corrade/Containers/StringView.h> namespace floormat { @@ -19,7 +20,6 @@ StringView get_error_string(ArrayView<char> buf, int error) if (status == 0) return { buf.data() }; #endif - return "Unknown error"_s; }; diff --git a/compat/strerror.hpp b/compat/strerror.hpp index 8bb33ec7..6b3c24eb 100644 --- a/compat/strerror.hpp +++ b/compat/strerror.hpp @@ -1,6 +1,5 @@ #pragma once #include <Corrade/Containers/ArrayView.h> -#include <Corrade/Containers/StringView.h> namespace floormat { |