system/FmtError: new library

Replaces the Format*() functions in system/Error.hxx.
This commit is contained in:
Max Kellermann
2022-11-28 18:49:35 +01:00
parent 124e75c286
commit 96ae659fdf
25 changed files with 305 additions and 186 deletions

View File

@@ -32,8 +32,6 @@
#include <system_error> // IWYU pragma: export
#include <utility>
#include <stdio.h>
template<typename... Args>
static inline std::system_error
FormatSystemError(std::error_code code, const char *fmt,
@@ -47,8 +45,7 @@ FormatSystemError(std::error_code code, const char *fmt,
#ifdef _WIN32
#include <errhandlingapi.h> // for GetLastError()
#include <windef.h> // for HWND (needed by winbase.h)
#include <winbase.h> // for FormatMessageA()
#include <winerror.h>
/**
* Returns the error_category to be used to wrap WIN32 GetLastError()
@@ -85,41 +82,10 @@ MakeLastError(const char *msg) noexcept
return MakeLastError(GetLastError(), msg);
}
template<typename... Args>
static inline std::system_error
FormatLastError(DWORD code, const char *fmt, Args&&... args) noexcept
{
char buffer[512];
const auto end = buffer + sizeof(buffer);
constexpr std::size_t max_prefix = sizeof(buffer) - 128;
size_t length = snprintf(buffer, max_prefix,
fmt, std::forward<Args>(args)...);
if (length >= max_prefix)
length = max_prefix - 1;
char *p = buffer + length;
*p++ = ':';
*p++ = ' ';
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, code, 0, p, end - p, nullptr);
return MakeLastError(code, buffer);
}
template<typename... Args>
static inline std::system_error
FormatLastError(const char *fmt, Args&&... args) noexcept
{
return FormatLastError(GetLastError(), fmt,
std::forward<Args>(args)...);
}
#endif /* _WIN32 */
#include <cerrno> // IWYU pragma: export
#include <string.h>
/**
* Returns the error_category to be used to wrap errno values. The
* C++ standard does not define this well, so this code is based on
@@ -156,35 +122,6 @@ MakeErrno(const char *msg) noexcept
return MakeErrno(errno, msg);
}
template<typename... Args>
static inline std::system_error
FormatErrno(int code, const char *fmt, Args&&... args) noexcept
{
char buffer[512];
snprintf(buffer, sizeof(buffer),
fmt, std::forward<Args>(args)...);
return MakeErrno(code, buffer);
}
template<typename... Args>
static inline std::system_error
FormatErrno(const char *fmt, Args&&... args) noexcept
{
return FormatErrno(errno, fmt, std::forward<Args>(args)...);
}
template<typename... Args>
static inline std::system_error
FormatFileNotFound(const char *fmt, Args&&... args) noexcept
{
#ifdef _WIN32
return FormatLastError(ERROR_FILE_NOT_FOUND, fmt,
std::forward<Args>(args)...);
#else
return FormatErrno(ENOENT, fmt, std::forward<Args>(args)...);
#endif
}
[[gnu::pure]]
inline bool
IsErrno(const std::system_error &e, int code) noexcept

73
src/system/FmtError.cxx Normal file
View File

@@ -0,0 +1,73 @@
/*
* Copyright 2022 Max Kellermann <max.kellermann@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "FmtError.hxx"
#include <fmt/format.h>
#include <array>
std::system_error
VFmtSystemError(std::error_code code,
fmt::string_view format_str, fmt::format_args args) noexcept
{
std::array<char, 512> buffer;
auto [p, _] = fmt::vformat_to_n(buffer.begin(), buffer.size() - 1,
format_str, args);
*p = 0;
return std::system_error{code, buffer.data()};
}
#ifdef _WIN32
#include <windef.h> // for HWND (needed by winbase.h)
#include <winbase.h> // for FormatMessageA()
std::system_error
VFmtLastError(DWORD code,
fmt::string_view format_str, fmt::format_args args) noexcept
{
std::array<char, 512> buffer;
const auto end = buffer.data() + buffer.size();
constexpr std::size_t max_prefix = sizeof(buffer) - 128;
auto [p, _] = fmt::vformat_to_n(buffer.data(),
buffer.size() - max_prefix,
format_str, args);
*p++ = ':';
*p++ = ' ';
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, code, 0, p, end - p, nullptr);
return MakeLastError(code, buffer.data());
}
#endif // _WIN32

122
src/system/FmtError.hxx Normal file
View File

@@ -0,0 +1,122 @@
/*
* Copyright 2022 Max Kellermann <max.kellermann@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include "Error.hxx" // IWYU pragma: export
#include <fmt/core.h>
#if FMT_VERSION >= 80000 && FMT_VERSION < 90000
#include <fmt/format.h>
#endif
[[nodiscard]] [[gnu::pure]]
std::system_error
VFmtSystemError(std::error_code code,
fmt::string_view format_str, fmt::format_args args) noexcept;
template<typename S, typename... Args>
[[nodiscard]] [[gnu::pure]]
std::system_error
FmtSystemError(std::error_code code,
const S &format_str, Args&&... args) noexcept
{
#if FMT_VERSION >= 90000
return VFmtSystemError(code, format_str,
fmt::make_format_args(args...));
#else
return VFmtSystemError(code, fmt::to_string_view(format_str),
fmt::make_args_checked<Args...>(format_str,
args...));
#endif
}
#ifdef _WIN32
[[nodiscard]] [[gnu::pure]]
std::system_error
VFmtLastError(DWORD code,
fmt::string_view format_str, fmt::format_args args) noexcept;
template<typename S, typename... Args>
[[nodiscard]] [[gnu::pure]]
std::system_error
FmtLastError(DWORD code,
const S &format_str, Args&&... args) noexcept
{
#if FMT_VERSION >= 90000
return VFmtLastError(code, format_str,
fmt::make_format_args(args...));
#else
return VFmtLastError(code, fmt::to_string_view(format_str),
fmt::make_args_checked<Args...>(format_str,
args...));
#endif
}
template<typename S, typename... Args>
[[nodiscard]] [[gnu::pure]]
std::system_error
FmtLastError(const S &format_str, Args&&... args) noexcept
{
return FmtLastError(GetLastError(),
format_str, std::forward<Args>(args)...);
}
#endif // _WIN32
template<typename S, typename... Args>
[[nodiscard]] [[gnu::pure]]
std::system_error
FmtErrno(int code, const S &format_str, Args&&... args) noexcept
{
return FmtSystemError(std::error_code(code, ErrnoCategory()),
format_str, std::forward<Args>(args)...);
}
template<typename S, typename... Args>
[[nodiscard]] [[gnu::pure]]
std::system_error
FmtErrno(const S &format_str, Args&&... args) noexcept
{
return FmtErrno(errno, format_str, std::forward<Args>(args)...);
}
template<typename S, typename... Args>
[[nodiscard]] [[gnu::pure]]
std::system_error
FmtFileNotFound(const S &format_str, Args&&... args) noexcept
{
#ifdef _WIN32
return FmtLastError(DWORD{ERROR_FILE_NOT_FOUND},
format_str, std::forward<Args>(args)...);
#else
return FmtErrno(ENOENT, format_str, std::forward<Args>(args)...);
#endif
}

View File

@@ -1,5 +1,6 @@
system_sources = [
'EventPipe.cxx',
'FmtError.cxx',
]
if host_machine.system() == 'linux'