From 408b38a0f8d1da9ec7cb8f0d62ac2bc3e63ba123 Mon Sep 17 00:00:00 2001 From: Joe Thornber Date: Sat, 17 Jan 2015 10:22:57 +0000 Subject: [PATCH] Forgot error_string.h/cc from previous commit --- base/error_string.cc | 39 +++++++++++++++++++++++++++++++++++++++ base/error_string.h | 16 ++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 base/error_string.cc create mode 100644 base/error_string.h diff --git a/base/error_string.cc b/base/error_string.cc new file mode 100644 index 0000000..6cb02c4 --- /dev/null +++ b/base/error_string.cc @@ -0,0 +1,39 @@ +#include "base/error_string.h" + +#include +#include + +#include + +using namespace std; + +//---------------------------------------------------------------- + +#ifdef STRERROR_R_CHAR_P + +string base::error_string(int err) +{ + char *ptr; + char buffer[128]; + + ptr = strerror_r(errno, buffer, sizeof(buffer)); + return string(ptr); +} + +#else + +string base::error_string(int err) +{ + int r; + char buffer[128]; + + r = strerror_r(errno, buffer, sizeof(buffer)); + if (r) + throw runtime_error("strerror_r failed"); + + return string(buffer); +} + +#endif + +//---------------------------------------------------------------- diff --git a/base/error_string.h b/base/error_string.h new file mode 100644 index 0000000..dd7549a --- /dev/null +++ b/base/error_string.h @@ -0,0 +1,16 @@ +#ifndef BASE_ERROR_STRING_H +#define BASE_ERROR_STRING_H + +#include + +//---------------------------------------------------------------- + +namespace base { + // There are a couple of version of strerror_r kicking around, so + // we wrap it. + std::string error_string(int err); +} + +//---------------------------------------------------------------- + +#endif