diff --git a/doc/api.rst b/doc/api.rst index 24b5d633..75dc03ac 100644 --- a/doc/api.rst +++ b/doc/api.rst @@ -197,6 +197,8 @@ System errors .. doxygenclass:: fmt::SystemError :members: +.. doxygenfunction:: fmt::format_system_error + .. doxygenclass:: fmt::WindowsError :members: diff --git a/fmt/format.cc b/fmt/format.cc index ae5d1103..886cb9b6 100644 --- a/fmt/format.cc +++ b/fmt/format.cc @@ -359,6 +359,13 @@ class CharConverter : public ArgVisitor { namespace internal { +// This method is used to preserve binary compatibility with fmt 3.0. +// It can be removed in 4.0. +FMT_FUNC void format_system_error( + Writer &out, int error_code, StringRef message) FMT_NOEXCEPT { + fmt::format_system_error(out, error_code, message); +} + template class PrintfArgFormatter : public ArgFormatterBase, Char> { @@ -434,7 +441,7 @@ FMT_FUNC void fmt::SystemError::init( int err_code, CStringRef format_str, ArgList args) { error_code_ = err_code; MemoryWriter w; - internal::format_system_error(w, err_code, format(format_str, args)); + format_system_error(w, err_code, format(format_str, args)); std::runtime_error &base = *this; base = std::runtime_error(w.str()); } @@ -592,12 +599,12 @@ FMT_FUNC void fmt::internal::format_windows_error( #endif // FMT_USE_WINDOWS_H -FMT_FUNC void fmt::internal::format_system_error( +FMT_FUNC void fmt::format_system_error( fmt::Writer &out, int error_code, fmt::StringRef message) FMT_NOEXCEPT { FMT_TRY { - MemoryBuffer buffer; - buffer.resize(INLINE_BUFFER_SIZE); + internal::MemoryBuffer buffer; + buffer.resize(internal::INLINE_BUFFER_SIZE); for (;;) { char *system_message = &buffer[0]; int result = safe_strerror(error_code, system_message, buffer.size()); @@ -854,7 +861,7 @@ void fmt::internal::PrintfFormatter::format( FMT_FUNC void fmt::report_system_error( int error_code, fmt::StringRef message) FMT_NOEXCEPT { // 'fmt::' is for bcc32. - fmt::report_error(internal::format_system_error, error_code, message); + fmt::report_error(format_system_error, error_code, message); } #if FMT_USE_WINDOWS_H diff --git a/fmt/format.h b/fmt/format.h index 7d38abf9..08623343 100644 --- a/fmt/format.h +++ b/fmt/format.h @@ -989,9 +989,6 @@ FMT_API void format_windows_error(fmt::Writer &out, int error_code, fmt::StringRef message) FMT_NOEXCEPT; #endif -FMT_API void format_system_error(fmt::Writer &out, int error_code, - fmt::StringRef message) FMT_NOEXCEPT; - // A formatting argument value. struct Value { template @@ -2246,17 +2243,10 @@ class SystemError : public internal::RuntimeError { public: /** \rst - Constructs a :class:`fmt::SystemError` object with the description - of the form - - .. parsed-literal:: - **: ** - - where ** is the formatted message and ** is - the system message corresponding to the error code. - *error_code* is a system error code as given by ``errno``. - If *error_code* is not a valid error code such as -1, the system message - may look like "Unknown error -1" and is platform-dependent. + Constructs a :class:`fmt::SystemError` object with a description + formatted with `fmt::format_system_error`. *message* and additional + arguments passed into the constructor are formatted similarly to + `fmt::format`. **Example**:: @@ -2277,6 +2267,25 @@ class SystemError : public internal::RuntimeError { int error_code() const { return error_code_; } }; +/** + \rst + Formats an error returned by an operating system or a language runtime, + for example a file opening error, and writes it to *out* in the following + form: + + .. parsed-literal:: + **: ** + + where ** is the passed message and ** is + the system message corresponding to the error code. + *error_code* is a system error code as given by ``errno``. + If *error_code* is not a valid error code such as -1, the system message + may look like "Unknown error -1" and is platform-dependent. + \endrst + */ +FMT_API void format_system_error(fmt::Writer &out, int error_code, + fmt::StringRef message) FMT_NOEXCEPT; + /** \rst This template provides operations for formatting and writing data into diff --git a/test/gtest-extra-test.cc b/test/gtest-extra-test.cc index 8f681c2c..6a8c5676 100644 --- a/test/gtest-extra-test.cc +++ b/test/gtest-extra-test.cc @@ -320,7 +320,7 @@ TEST(StreamingAssertionsTest, EXPECT_WRITE) { TEST(UtilTest, FormatSystemError) { fmt::MemoryWriter out; - fmt::internal::format_system_error(out, EDOM, "test message"); + fmt::format_system_error(out, EDOM, "test message"); EXPECT_EQ(out.str(), format_system_error(EDOM, "test message")); } diff --git a/test/gtest-extra.cc b/test/gtest-extra.cc index f7c29630..7640d154 100644 --- a/test/gtest-extra.cc +++ b/test/gtest-extra.cc @@ -105,6 +105,6 @@ std::string read(File &f, std::size_t count) { std::string format_system_error(int error_code, fmt::StringRef message) { fmt::MemoryWriter out; - fmt::internal::format_system_error(out, error_code, message); + fmt::format_system_error(out, error_code, message); return out.str(); } diff --git a/test/util-test.cc b/test/util-test.cc index 2134d095..28bb77b1 100644 --- a/test/util-test.cc +++ b/test/util-test.cc @@ -834,10 +834,10 @@ void check_throw_error(int error_code, FormatErrorMessage format) { TEST(UtilTest, FormatSystemError) { fmt::MemoryWriter message; - fmt::internal::format_system_error(message, EDOM, "test"); + fmt::format_system_error(message, EDOM, "test"); EXPECT_EQ(fmt::format("test: {}", get_system_error(EDOM)), message.str()); message.clear(); - fmt::internal::format_system_error( + fmt::format_system_error( message, EDOM, fmt::StringRef(0, std::numeric_limits::max())); EXPECT_EQ(fmt::format("error {}", EDOM), message.str()); } @@ -846,12 +846,12 @@ TEST(UtilTest, SystemError) { fmt::SystemError e(EDOM, "test"); EXPECT_EQ(fmt::format("test: {}", get_system_error(EDOM)), e.what()); EXPECT_EQ(EDOM, e.error_code()); - check_throw_error(EDOM, fmt::internal::format_system_error); + check_throw_error(EDOM, fmt::format_system_error); } TEST(UtilTest, ReportSystemError) { fmt::MemoryWriter out; - fmt::internal::format_system_error(out, EDOM, "test error"); + fmt::format_system_error(out, EDOM, "test error"); out << '\n'; EXPECT_WRITE(stderr, fmt::report_system_error(EDOM, "test error"), out.str()); }