system/FmtError: move to lib/fmt/

This commit is contained in:
Max Kellermann
2022-11-28 23:05:15 +01:00
parent 7d8c076e06
commit 66029c405f
23 changed files with 31 additions and 23 deletions

View File

@@ -0,0 +1,69 @@
/*
* 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 "SystemError.hxx"
#include "ToBuffer.hxx"
#include <array>
std::system_error
VFmtSystemError(std::error_code code,
fmt::string_view format_str, fmt::format_args args) noexcept
{
const auto msg = VFmtBuffer<512>(format_str, args);
return std::system_error{code, msg};
}
#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/lib/fmt/SystemError.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 "system/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

@@ -8,6 +8,14 @@ if compiler.get_id() == 'clang' and compiler.version().version_compare('<15')
)
endif
fmt_dep = declare_dependency(
fmt = static_library(
'fmt',
'SystemError.cxx',
include_directories: inc,
dependencies: libfmt,
)
fmt_dep = declare_dependency(
link_with: fmt,
dependencies: libfmt,
)