Created
January 28, 2015 20:14
-
-
Save ttldtor/de4dd8219eea76a5a27b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std.c.string; | |
import std.conv; | |
string formatError(int err, string prefix = "") { | |
version(Posix) { | |
char[512] buf; | |
const(char)* cs; | |
version (linux) { | |
cs = strerror_r(err, buf.ptr, buf.length); | |
} | |
else version (OSX) { | |
auto errs = strerror_r(err, buf.ptr, buf.length); | |
if (errs == 0) { | |
cs = buf.ptr; | |
} | |
else { | |
return prefix ~ to!string(err); | |
} | |
} | |
else version (FreeBSD) { | |
auto errs = strerror_r(err, buf.ptr, buf.length); | |
if (errs == 0) { | |
cs = buf.ptr; | |
} | |
else { | |
return prefix ~ to!string(err); | |
} | |
} | |
else { | |
static assert(0); | |
} | |
auto len = strlen(cs); | |
if(cs[len - 1] == '\n') { | |
len--; | |
} | |
if(cs[len - 1] == '\r') { | |
len--; | |
} | |
return cs[0 .. len].idup; | |
} | |
else version(Windows) { | |
import std.windows.syserror; | |
try { | |
return prefix ~ sysErrorString(err); | |
} catch (Exception e) { | |
return prefix ~ "error " ~ to!string(err); | |
} | |
} | |
else { | |
return prefix ~ to!string(err); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment