From 3092e5a8a5395ec514983509cdfc75f0aaf3718a Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 15 Dec 2015 22:53:43 +0100 Subject: [PATCH] system/Error: helper library for constructing std::system_error --- Makefile.am | 1 + src/system/Error.hxx | 109 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 src/system/Error.hxx diff --git a/Makefile.am b/Makefile.am index e8b79fadd..4ad1cea80 100644 --- a/Makefile.am +++ b/Makefile.am @@ -464,6 +464,7 @@ libnet_a_SOURCES = \ libsystem_a_SOURCES = \ src/system/ByteOrder.hxx \ + src/system/Error.hxx \ src/system/FatalError.cxx src/system/FatalError.hxx \ src/system/FileDescriptor.cxx src/system/FileDescriptor.hxx \ src/system/fd_util.c src/system/fd_util.h \ diff --git a/src/system/Error.hxx b/src/system/Error.hxx new file mode 100644 index 000000000..5f7f4eb59 --- /dev/null +++ b/src/system/Error.hxx @@ -0,0 +1,109 @@ +/* + * Copyright (C) 2013-2015 Max Kellermann + * + * 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. + */ + +#ifndef SYSTEM_ERROR_HXX +#define SYSTEM_ERROR_HXX + +#include "util/StringUtil.hxx" + +#include +#include + +template +static inline std::system_error +FormatSystemError(std::error_code code, const char *fmt, Args&&... args) +{ + char buffer[1024]; + snprintf(buffer, sizeof(buffer), fmt, std::forward(args)...); + return std::system_error(code, buffer); +} + +#ifdef WIN32 + +#include + +template +static inline std::system_error +FormatLastError(DWORD code, const char *fmt, Args&&... args) +{ + char buffer[512]; + const auto end = buffer + sizeof(buffer); + size_t length = snprintf(buffer, sizeof(buffer) - 128, + fmt, std::forward(args)...); + char *p = buffer + length; + *p++ = ':'; + *p++ = ' '; + + FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + nullptr, code, 0, p, end - p, nullptr); + return std::system_error(std::error_code(code, std::system_category()), + buffer); +} + +template +static inline std::system_error +FormatLastError(const char *fmt, Args&&... args) +{ + return FormatLastError(GetLastError(), fmt, + std::forward(args)...); +} + +#else + +#include +#include + +template +static inline std::system_error +FormatErrno(int code, const char *fmt, Args&&... args) +{ + char buffer[512]; + const auto end = buffer + sizeof(buffer); + size_t length = snprintf(buffer, sizeof(buffer) - 128, + fmt, std::forward(args)...); + char *p = buffer + length; + *p++ = ':'; + *p++ = ' '; + + CopyString(p, strerror(code), end - p); + return std::system_error(std::error_code(code, std::system_category()), + buffer); +} + +template +static inline std::system_error +FormatErrno(const char *fmt, Args&&... args) +{ + return FormatErrno(errno, fmt, std::forward(args)...); +} + +#endif + +#endif