From 53b4c31afbb7e0db283e2ad7ef82ae2f795d3e7f Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Wed, 30 Apr 2014 15:00:41 -0700 Subject: [PATCH] Move Format*ErrorMessage to internal namespace. --- format.cc | 103 +++++++++++++++++++++++++++--------------------------- format.h | 8 +++++ 2 files changed, 59 insertions(+), 52 deletions(-) diff --git a/format.cc b/format.cc index 288515aa..2b45fcc8 100644 --- a/format.cc +++ b/format.cc @@ -100,56 +100,6 @@ inline int FMT_SNPRINTF(char *buffer, size_t size, const char *format, ...) { #endif // _MSC_VER const char RESET_COLOR[] = "\x1b[0m"; - -void FormatSystemErrorMessage( - fmt::Writer &out, int error_code, fmt::StringRef message) { - fmt::internal::Array 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(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 @@ -277,6 +227,55 @@ int fmt::internal::StrError( return result; } +void fmt::internal::FormatSystemErrorMessage( + fmt::Writer &out, int error_code, fmt::StringRef message) { + Array 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(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 // content area. template @@ -805,14 +804,14 @@ void fmt::BasicWriter::FormatParser::Format( void fmt::SystemErrorSink::operator()(const fmt::Writer &w) const { Writer message; - FormatSystemErrorMessage(message, error_code_, w.c_str()); + internal::FormatSystemErrorMessage(message, error_code_, w.c_str()); throw SystemError(message.c_str(), error_code_); } #ifdef _WIN32 void fmt::WinErrorSink::operator()(const Writer &w) const { Writer message; - FormatWinErrorMessage(message, error_code_, w.c_str()); + internal::FormatWinErrorMessage(message, error_code_, w.c_str()); throw SystemError(message.c_str(), error_code_); } #endif diff --git a/format.h b/format.h index 778aa769..07066392 100644 --- a/format.h +++ b/format.h @@ -500,6 +500,14 @@ class UTF16ToUTF8 { // Buffer should be at least of size 1. 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 /**