mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 13:47:35 +00:00
AK: Add support for "debug only" formatters
These are formatters that can only be used with debug print functions, such as dbgln(). Currently this is limited to Formatter<ErrorOr<T>>. With this you can still debug log ErrorOr values (good for debugging), but trying to use them in any String::formatted() call will fail (which prevents .to_string() errors with the new failable strings being ignored). You make a formatter debug only by adding a constexpr method like: static constexpr bool is_debug_only() { return true; }
This commit is contained in:
parent
1388375310
commit
9a120d7243
10 changed files with 36 additions and 17 deletions
33
AK/Format.h
33
AK/Format.h
|
@ -33,12 +33,26 @@ struct Formatter {
|
|||
using __no_formatter_defined = void;
|
||||
};
|
||||
|
||||
enum AllowDebugOnlyFormatters {
|
||||
No,
|
||||
Yes
|
||||
};
|
||||
|
||||
template<typename T, typename = void>
|
||||
inline constexpr bool HasFormatter = true;
|
||||
|
||||
template<typename T>
|
||||
inline constexpr bool HasFormatter<T, typename Formatter<T>::__no_formatter_defined> = false;
|
||||
|
||||
template<typename Formatter>
|
||||
inline constexpr bool is_debug_only_formatter()
|
||||
{
|
||||
constexpr bool has_is_debug_only = requires(Formatter const& formatter) { formatter.is_debug_only(); };
|
||||
if constexpr (has_is_debug_only)
|
||||
return Formatter::is_debug_only();
|
||||
return false;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
concept Formattable = HasFormatter<T>;
|
||||
|
||||
|
@ -270,7 +284,7 @@ ErrorOr<void> __format_value(TypeErasedFormatParams& params, FormatBuilder& buil
|
|||
return formatter.format(builder, *static_cast<T const*>(value));
|
||||
}
|
||||
|
||||
template<typename... Parameters>
|
||||
template<AllowDebugOnlyFormatters allow_debug_formatters, typename... Parameters>
|
||||
class VariadicFormatParams : public TypeErasedFormatParams {
|
||||
public:
|
||||
static_assert(sizeof...(Parameters) <= max_format_arguments);
|
||||
|
@ -278,6 +292,9 @@ public:
|
|||
explicit VariadicFormatParams(Parameters const&... parameters)
|
||||
: m_data({ TypeErasedParameter { ¶meters, TypeErasedParameter::get_type<Parameters>(), __format_value<Parameters> }... })
|
||||
{
|
||||
constexpr bool any_debug_formatters = (is_debug_only_formatter<Formatter<Parameters>>() || ...);
|
||||
static_assert(!any_debug_formatters || allow_debug_formatters == AllowDebugOnlyFormatters::Yes,
|
||||
"You are attempting to use a debug-only formatter outside of a debug log! Maybe one of your format values is an ErrorOr<T>?");
|
||||
this->set_parameters(m_data);
|
||||
}
|
||||
|
||||
|
@ -574,14 +591,14 @@ void vout(FILE*, StringView fmtstr, TypeErasedFormatParams&, bool newline = fals
|
|||
template<typename... Parameters>
|
||||
void out(FILE* file, CheckedFormatString<Parameters...>&& fmtstr, Parameters const&... parameters)
|
||||
{
|
||||
VariadicFormatParams variadic_format_params { parameters... };
|
||||
VariadicFormatParams<AllowDebugOnlyFormatters::Yes, Parameters...> variadic_format_params { parameters... };
|
||||
vout(file, fmtstr.view(), variadic_format_params);
|
||||
}
|
||||
|
||||
template<typename... Parameters>
|
||||
void outln(FILE* file, CheckedFormatString<Parameters...>&& fmtstr, Parameters const&... parameters)
|
||||
{
|
||||
VariadicFormatParams variadic_format_params { parameters... };
|
||||
VariadicFormatParams<AllowDebugOnlyFormatters::Yes, Parameters...> variadic_format_params { parameters... };
|
||||
vout(file, fmtstr.view(), variadic_format_params, true);
|
||||
}
|
||||
|
||||
|
@ -625,7 +642,7 @@ void vdbgln(StringView fmtstr, TypeErasedFormatParams&);
|
|||
template<typename... Parameters>
|
||||
void dbgln(CheckedFormatString<Parameters...>&& fmtstr, Parameters const&... parameters)
|
||||
{
|
||||
VariadicFormatParams variadic_format_params { parameters... };
|
||||
VariadicFormatParams<AllowDebugOnlyFormatters::Yes, Parameters...> variadic_format_params { parameters... };
|
||||
vdbgln(fmtstr.view(), variadic_format_params);
|
||||
}
|
||||
|
||||
|
@ -639,7 +656,7 @@ void vdmesgln(StringView fmtstr, TypeErasedFormatParams&);
|
|||
template<typename... Parameters>
|
||||
void dmesgln(CheckedFormatString<Parameters...>&& fmt, Parameters const&... parameters)
|
||||
{
|
||||
VariadicFormatParams variadic_format_params { parameters... };
|
||||
VariadicFormatParams<AllowDebugOnlyFormatters::Yes, Parameters...> variadic_format_params { parameters... };
|
||||
vdmesgln(fmt.view(), variadic_format_params);
|
||||
}
|
||||
|
||||
|
@ -650,7 +667,7 @@ void v_critical_dmesgln(StringView fmtstr, TypeErasedFormatParams&);
|
|||
template<typename... Parameters>
|
||||
void critical_dmesgln(CheckedFormatString<Parameters...>&& fmt, Parameters const&... parameters)
|
||||
{
|
||||
VariadicFormatParams variadic_format_params { parameters... };
|
||||
VariadicFormatParams<AllowDebugOnlyFormatters::Yes, Parameters...> variadic_format_params { parameters... };
|
||||
v_critical_dmesgln(fmt.view(), variadic_format_params);
|
||||
}
|
||||
#endif
|
||||
|
@ -695,7 +712,7 @@ struct Formatter<FormatString> : Formatter<StringView> {
|
|||
template<typename... Parameters>
|
||||
ErrorOr<void> format(FormatBuilder& builder, StringView fmtstr, Parameters const&... parameters)
|
||||
{
|
||||
VariadicFormatParams variadic_format_params { parameters... };
|
||||
VariadicFormatParams<AllowDebugOnlyFormatters::No, Parameters...> variadic_format_params { parameters... };
|
||||
return vformat(builder, fmtstr, variadic_format_params);
|
||||
}
|
||||
ErrorOr<void> vformat(FormatBuilder& builder, StringView fmtstr, TypeErasedFormatParams& params);
|
||||
|
@ -722,6 +739,8 @@ struct Formatter<Error> : Formatter<FormatString> {
|
|||
|
||||
template<typename T, typename ErrorType>
|
||||
struct Formatter<ErrorOr<T, ErrorType>> : Formatter<FormatString> {
|
||||
static constexpr bool is_debug_only() { return true; }
|
||||
|
||||
ErrorOr<void> format(FormatBuilder& builder, ErrorOr<T, ErrorType> const& error_or)
|
||||
{
|
||||
if (error_or.is_error())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue