Forgot error_string.h/cc from previous commit

This commit is contained in:
Joe Thornber 2015-01-17 10:22:57 +00:00
parent 25b4b526f4
commit 408b38a0f8
2 changed files with 55 additions and 0 deletions

39
base/error_string.cc Normal file
View File

@ -0,0 +1,39 @@
#include "base/error_string.h"
#include <errno.h>
#include <stdexcept>
#include <string.h>
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
//----------------------------------------------------------------

16
base/error_string.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef BASE_ERROR_STRING_H
#define BASE_ERROR_STRING_H
#include <string>
//----------------------------------------------------------------
namespace base {
// There are a couple of version of strerror_r kicking around, so
// we wrap it.
std::string error_string(int err);
}
//----------------------------------------------------------------
#endif