Move Format*ErrorMessage to internal namespace.

This commit is contained in:
Victor Zverovich 2014-04-30 15:00:41 -07:00
parent f3a86e83fe
commit 53b4c31afb
2 changed files with 59 additions and 52 deletions

103
format.cc
View File

@ -100,56 +100,6 @@ inline int FMT_SNPRINTF(char *buffer, size_t size, const char *format, ...) {
#endif // _MSC_VER #endif // _MSC_VER
const char RESET_COLOR[] = "\x1b[0m"; const char RESET_COLOR[] = "\x1b[0m";
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);
char *system_message = 0;
for (;;) {
system_message = &buffer[0];
int result = fmt::internal::StrError(
error_code, system_message, buffer.size());
if (result == 0)
break;
if (result != ERANGE) {
// Can't get error message, report error code instead.
out << message << ": error code = " << error_code;
return;
}
buffer.resize(buffer.size() * 2);
}
out << message << ": " << system_message;
}
#ifdef _WIN32
void FormatWinErrorMessage(
fmt::Writer &out, int error_code, fmt::StringRef message) {
class String {
private:
LPWSTR str_;
public:
String() : str_() {}
~String() { LocalFree(str_); }
LPWSTR *ptr() { return &str_; }
LPCWSTR c_str() const { return str_; }
};
String system_message;
if (FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0,
error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPWSTR>(system_message.ptr()), 0, 0)) {
fmt::internal::UTF16ToUTF8 utf8_message;
if (!utf8_message.Convert(system_message.c_str())) {
out << message << ": " << fmt::c_str(utf8_message);
return;
}
}
// Can't get error message, report error code instead.
out << message << ": error code = " << error_code;
}
#endif
} }
template <typename T> template <typename T>
@ -277,6 +227,55 @@ int fmt::internal::StrError(
return result; return result;
} }
void fmt::internal::FormatSystemErrorMessage(
fmt::Writer &out, int error_code, fmt::StringRef message) {
Array<char, INLINE_BUFFER_SIZE> buffer;
buffer.resize(INLINE_BUFFER_SIZE);
char *system_message = 0;
for (;;) {
system_message = &buffer[0];
int result = StrError(error_code, system_message, buffer.size());
if (result == 0)
break;
if (result != ERANGE) {
// Can't get error message, report error code instead.
out << message << ": error code = " << error_code;
return;
}
buffer.resize(buffer.size() * 2);
}
out << message << ": " << system_message;
}
#ifdef _WIN32
void fmt::internal::FormatWinErrorMessage(
fmt::Writer &out, int error_code, fmt::StringRef message) {
class String {
private:
LPWSTR str_;
public:
String() : str_() {}
~String() { LocalFree(str_); }
LPWSTR *ptr() { return &str_; }
LPCWSTR c_str() const { return str_; }
};
String system_message;
if (FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0,
error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPWSTR>(system_message.ptr()), 0, 0)) {
UTF16ToUTF8 utf8_message;
if (!utf8_message.Convert(system_message.c_str())) {
out << message << ": " << c_str(utf8_message);
return;
}
}
// Can't get error message, report error code instead.
out << message << ": error code = " << error_code;
}
#endif
// Fills the padding around the content and returns the pointer to the // Fills the padding around the content and returns the pointer to the
// content area. // content area.
template <typename Char> template <typename Char>
@ -805,14 +804,14 @@ void fmt::BasicWriter<Char>::FormatParser::Format(
void fmt::SystemErrorSink::operator()(const fmt::Writer &w) const { void fmt::SystemErrorSink::operator()(const fmt::Writer &w) const {
Writer message; Writer message;
FormatSystemErrorMessage(message, error_code_, w.c_str()); internal::FormatSystemErrorMessage(message, error_code_, w.c_str());
throw SystemError(message.c_str(), error_code_); throw SystemError(message.c_str(), error_code_);
} }
#ifdef _WIN32 #ifdef _WIN32
void fmt::WinErrorSink::operator()(const Writer &w) const { void fmt::WinErrorSink::operator()(const Writer &w) const {
Writer message; Writer message;
FormatWinErrorMessage(message, error_code_, w.c_str()); internal::FormatWinErrorMessage(message, error_code_, w.c_str());
throw SystemError(message.c_str(), error_code_); throw SystemError(message.c_str(), error_code_);
} }
#endif #endif

View File

@ -500,6 +500,14 @@ class UTF16ToUTF8 {
// Buffer should be at least of size 1. // Buffer should be at least of size 1.
int StrError(int error_code, char *&buffer, std::size_t buffer_size); int StrError(int error_code, char *&buffer, std::size_t buffer_size);
void FormatSystemErrorMessage(
fmt::Writer &out, int error_code, fmt::StringRef message);
#ifdef _WIN32
void FormatWinErrorMessage(
fmt::Writer &out, int error_code, fmt::StringRef message);
#endif
} // namespace internal } // namespace internal
/** /**