Add ThrowCError to throw an exception on C library errors.

This commit is contained in:
Victor Zverovich
2014-04-30 10:18:11 -07:00
parent 2c6372d790
commit f79398699b
3 changed files with 55 additions and 13 deletions
+14 -8
View File
@@ -224,19 +224,13 @@ char *fmt::internal::StrError(
#endif
}
void fmt::internal::FormatSystemErrorMessage(
void fmt::internal::FormatCErrorMessage(
fmt::Writer &out, int error_code, fmt::StringRef message) {
#ifndef _WIN32
Array<char, INLINE_BUFFER_SIZE> buffer;
buffer.resize(INLINE_BUFFER_SIZE);
char *system_message = 0;
for (;;) {
errno = 0;
# ifdef _GNU_SOURCE
system_message = strerror_r(error_code, &buffer[0], buffer.size());
# else
strerror_r(error_code, system_message = &buffer[0], buffer.size());
# endif
system_message = StrError(error_code, &buffer[0], buffer.size());
if (errno == 0)
break;
if (errno != ERANGE) {
@@ -247,6 +241,12 @@ void fmt::internal::FormatSystemErrorMessage(
buffer.resize(buffer.size() * 2);
}
out << message << ": " << system_message;
}
void fmt::internal::FormatSystemErrorMessage(
fmt::Writer &out, int error_code, fmt::StringRef message) {
#ifndef _WIN32
FormatCErrorMessage(out, error_code, message);
#else
class String {
private:
@@ -806,6 +806,12 @@ void fmt::SystemErrorSink::operator()(const fmt::Writer &w) const {
throw SystemError(message.c_str(), error_code_);
}
void fmt::CErrorSink::operator()(const Writer &w) const {
Writer message;
internal::FormatCErrorMessage(message, error_code_, w.c_str());
throw SystemError(message.c_str(), error_code_);
}
void fmt::ANSITerminalSink::operator()(
const fmt::BasicWriter<char> &w) const {
char escape[] = "\x1b[30m";