CError -> WinError
This commit is contained in:
parent
c7eba007cc
commit
400812a905
@ -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
|
||||
|
16
format.cc
16
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<char, fmt::internal::INLINE_BUFFER_SIZE> 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 <typename T>
|
||||
@ -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<char> &w) const {
|
||||
|
22
format.h
22
format.h
@ -1531,7 +1531,7 @@ inline Formatter<NullSink, wchar_t> 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
|
||||
"<message>: <system-message>", where <message> is the formatted message and
|
||||
<system-message> 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<SystemErrorSink> ThrowSystemError(
|
||||
int error_code, StringRef format) {
|
||||
@ -1557,15 +1556,15 @@ inline Formatter<SystemErrorSink> 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
|
||||
"<message>: <system-message>", where <message> is the formatted message and
|
||||
<system-message> 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<CErrorSink> ThrowCError(int error_code, StringRef format) {
|
||||
Formatter<CErrorSink> f(format, CErrorSink(error_code));
|
||||
inline Formatter<WinErrorSink> ThrowWinError(int error_code, StringRef format) {
|
||||
Formatter<WinErrorSink> f(format, WinErrorSink(error_code));
|
||||
return f;
|
||||
}
|
||||
|
||||
@ -1593,7 +1591,7 @@ class FileSink {
|
||||
/** Writes the output to a file. */
|
||||
void operator()(const BasicWriter<char> &w) const {
|
||||
if (std::fwrite(w.data(), w.size(), 1, file_) == 0)
|
||||
ThrowCError(errno, "cannot write to file");
|
||||
ThrowSystemError(errno, "cannot write to file");
|
||||
}
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user