diff --git a/format-test.cc b/format-test.cc index c5be9030..a1f55496 100644 --- a/format-test.cc +++ b/format-test.cc @@ -1555,6 +1555,11 @@ TEST(FormatterTest, FormatExamples) { std::string s = writer.str(); // s == 0123456789 EXPECT_EQ("0123456789", s); } + + const char *filename = "nonexistent"; + FILE *f = fopen(filename, "r"); + if (!f) + fmt::ThrowSystemError(errno, "Cannot open file '{}'") << filename; } TEST(FormatterTest, StrNamespace) { @@ -1653,7 +1658,7 @@ TEST(FormatterTest, FileSinkWriteError) { std::fclose(f); } -// TODO: test SystemErrorSink, ThrowSystemError, CErrorSink, ThrowCError. +// TODO: test SystemErrorSink, ThrowSystemError, CErrorSink, ThrowWinError. // The test doesn't compile on older compilers which follow C++03 and // require an accessible copy constructor when binding a temporary to diff --git a/format.cc b/format.cc index 3dc7a083..288515aa 100644 --- a/format.cc +++ b/format.cc @@ -101,7 +101,7 @@ inline int FMT_SNPRINTF(char *buffer, size_t size, const char *format, ...) { const char RESET_COLOR[] = "\x1b[0m"; -void FormatCErrorMessage( +void FormatSystemErrorMessage( fmt::Writer &out, int error_code, fmt::StringRef message) { fmt::internal::Array buffer; buffer.resize(fmt::internal::INLINE_BUFFER_SIZE); @@ -122,11 +122,9 @@ void FormatCErrorMessage( out << message << ": " << system_message; } -void FormatSystemErrorMessage( +#ifdef _WIN32 +void FormatWinErrorMessage( fmt::Writer &out, int error_code, fmt::StringRef message) { -#ifndef _WIN32 - FormatCErrorMessage(out, error_code, message); -#else class String { private: LPWSTR str_; @@ -150,8 +148,8 @@ void FormatSystemErrorMessage( } // Can't get error message, report error code instead. out << message << ": error code = " << error_code; -#endif } +#endif } template @@ -811,11 +809,13 @@ void fmt::SystemErrorSink::operator()(const fmt::Writer &w) const { throw SystemError(message.c_str(), error_code_); } -void fmt::CErrorSink::operator()(const Writer &w) const { +#ifdef _WIN32 +void fmt::WinErrorSink::operator()(const Writer &w) const { Writer message; - FormatCErrorMessage(message, error_code_, w.c_str()); + FormatWinErrorMessage(message, error_code_, w.c_str()); throw SystemError(message.c_str(), error_code_); } +#endif void fmt::ANSITerminalSink::operator()( const fmt::BasicWriter &w) const { diff --git a/format.h b/format.h index a8f9e62f..778aa769 100644 --- a/format.h +++ b/format.h @@ -1531,7 +1531,7 @@ inline Formatter Format(WStringRef format) { /** A sink that gets the error message corresponding to a system error code - and throws SystemError. + as given by errno and throws SystemError. */ class SystemErrorSink { private: @@ -1547,8 +1547,7 @@ class SystemErrorSink { Formats a message and throws SystemError with the description of the form ": ", 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 for POSIX and - GetLastError for Windows. + error_code is a system error code as given by errno. */ inline Formatter ThrowSystemError( int error_code, StringRef format) { @@ -1557,15 +1556,15 @@ inline Formatter ThrowSystemError( } /** - A sink that gets the error message corresponding to a standard C library - error code as given by errno and throws SystemError. + A sink that gets the error message corresponding to a Windows error code + as given by GetLastError and throws SystemError. */ -class CErrorSink { +class WinErrorSink { private: int error_code_; public: - explicit CErrorSink(int error_code) : error_code_(error_code) {} + explicit WinErrorSink(int error_code) : error_code_(error_code) {} void operator()(const Writer &w) const; }; @@ -1574,11 +1573,10 @@ class CErrorSink { Formats a message and throws SystemError with the description of the form ": ", where is the formatted message and is the system message corresponding to the error code. - error_code is an error code as given by errno after calling a C library - function. + error_code is a Windows error code as given by GetLastError. */ -inline Formatter ThrowCError(int error_code, StringRef format) { - Formatter f(format, CErrorSink(error_code)); +inline Formatter ThrowWinError(int error_code, StringRef format) { + Formatter f(format, WinErrorSink(error_code)); return f; } @@ -1593,7 +1591,7 @@ class FileSink { /** Writes the output to a file. */ void operator()(const BasicWriter &w) const { if (std::fwrite(w.data(), w.size(), 1, file_) == 0) - ThrowCError(errno, "cannot write to file"); + ThrowSystemError(errno, "cannot write to file"); } };