CError -> WinError

This commit is contained in:
Victor Zverovich
2014-04-30 12:38:17 -07:00
parent c7eba007cc
commit 400812a905
3 changed files with 24 additions and 21 deletions

View File

@ -1555,6 +1555,11 @@ TEST(FormatterTest, FormatExamples) {
std::string s = writer.str(); // s == 0123456789 std::string s = writer.str(); // s == 0123456789
EXPECT_EQ("0123456789", s); 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) { TEST(FormatterTest, StrNamespace) {
@ -1653,7 +1658,7 @@ TEST(FormatterTest, FileSinkWriteError) {
std::fclose(f); 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 // The test doesn't compile on older compilers which follow C++03 and
// require an accessible copy constructor when binding a temporary to // require an accessible copy constructor when binding a temporary to

View File

@ -101,7 +101,7 @@ inline int FMT_SNPRINTF(char *buffer, size_t size, const char *format, ...) {
const char RESET_COLOR[] = "\x1b[0m"; const char RESET_COLOR[] = "\x1b[0m";
void FormatCErrorMessage( void FormatSystemErrorMessage(
fmt::Writer &out, int error_code, fmt::StringRef message) { fmt::Writer &out, int error_code, fmt::StringRef message) {
fmt::internal::Array<char, fmt::internal::INLINE_BUFFER_SIZE> buffer; fmt::internal::Array<char, fmt::internal::INLINE_BUFFER_SIZE> buffer;
buffer.resize(fmt::internal::INLINE_BUFFER_SIZE); buffer.resize(fmt::internal::INLINE_BUFFER_SIZE);
@ -122,11 +122,9 @@ void FormatCErrorMessage(
out << message << ": " << system_message; out << message << ": " << system_message;
} }
void FormatSystemErrorMessage( #ifdef _WIN32
void FormatWinErrorMessage(
fmt::Writer &out, int error_code, fmt::StringRef message) { fmt::Writer &out, int error_code, fmt::StringRef message) {
#ifndef _WIN32
FormatCErrorMessage(out, error_code, message);
#else
class String { class String {
private: private:
LPWSTR str_; LPWSTR str_;
@ -150,8 +148,8 @@ void FormatSystemErrorMessage(
} }
// Can't get error message, report error code instead. // Can't get error message, report error code instead.
out << message << ": error code = " << error_code; out << message << ": error code = " << error_code;
#endif
} }
#endif
} }
template <typename T> template <typename T>
@ -811,11 +809,13 @@ void fmt::SystemErrorSink::operator()(const fmt::Writer &w) const {
throw SystemError(message.c_str(), error_code_); 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; Writer message;
FormatCErrorMessage(message, error_code_, w.c_str()); FormatWinErrorMessage(message, error_code_, w.c_str());
throw SystemError(message.c_str(), error_code_); throw SystemError(message.c_str(), error_code_);
} }
#endif
void fmt::ANSITerminalSink::operator()( void fmt::ANSITerminalSink::operator()(
const fmt::BasicWriter<char> &w) const { const fmt::BasicWriter<char> &w) const {

View File

@ -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 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 { class SystemErrorSink {
private: private:
@ -1547,8 +1547,7 @@ class SystemErrorSink {
Formats a message and throws SystemError with the description of the form Formats a message and throws SystemError with the description of the form
"<message>: <system-message>", where <message> is the formatted message and "<message>: <system-message>", where <message> is the formatted message and
<system-message> is the system message corresponding to the error code. <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 error_code is a system error code as given by errno.
GetLastError for Windows.
*/ */
inline Formatter<SystemErrorSink> ThrowSystemError( inline Formatter<SystemErrorSink> ThrowSystemError(
int error_code, StringRef format) { 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 A sink that gets the error message corresponding to a Windows error code
error code as given by errno and throws SystemError. as given by GetLastError and throws SystemError.
*/ */
class CErrorSink { class WinErrorSink {
private: private:
int error_code_; int error_code_;
public: 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; void operator()(const Writer &w) const;
}; };
@ -1574,11 +1573,10 @@ class CErrorSink {
Formats a message and throws SystemError with the description of the form Formats a message and throws SystemError with the description of the form
"<message>: <system-message>", where <message> is the formatted message and "<message>: <system-message>", where <message> is the formatted message and
<system-message> is the system message corresponding to the error code. <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 error_code is a Windows error code as given by GetLastError.
function.
*/ */
inline Formatter<CErrorSink> ThrowCError(int error_code, StringRef format) { inline Formatter<WinErrorSink> ThrowWinError(int error_code, StringRef format) {
Formatter<CErrorSink> f(format, CErrorSink(error_code)); Formatter<WinErrorSink> f(format, WinErrorSink(error_code));
return f; return f;
} }
@ -1593,7 +1591,7 @@ class FileSink {
/** Writes the output to a file. */ /** Writes the output to a file. */
void operator()(const BasicWriter<char> &w) const { void operator()(const BasicWriter<char> &w) const {
if (std::fwrite(w.data(), w.size(), 1, file_) == 0) if (std::fwrite(w.data(), w.size(), 1, file_) == 0)
ThrowCError(errno, "cannot write to file"); ThrowSystemError(errno, "cannot write to file");
} }
}; };