Add a symbolicErrno() function to Util

For a developer, it is much nicer to see "EXDEV" in a log than "18",
for instance. (Sure, we often also log strerror(), but might that
perhaps be localised? And the symbolic names are what one uses when
coding anyway.)

Change-Id: I456a8c2589147dcad87f1b4c3a20b3bd5a35d097
This commit is contained in:
Tor Lillqvist 2018-07-11 12:54:23 +03:00
parent 1c09d4a607
commit 40f4cf17c9

View file

@ -11,6 +11,7 @@
#define INCLUDED_UTIL_HPP
#include <cassert>
#include <cerrno>
#include <cstddef>
#include <atomic>
#include <functional>
@ -305,6 +306,147 @@ namespace Util
return s.length() >= t.length() && memcmp(s.c_str(), t.c_str(), t.length()) == 0;
}
/// Return the symbolic name for an errno value, or in decimal if not handled here.
inline std::string symbolicErrno(int e)
{
// Errnos from <asm-generic/errno-base.h> and <asm-generic/errno.h> on Linux.
switch (e)
{
case EPERM: return "EPERM";
case ENOENT: return "ENOENT";
case ESRCH: return "ESRCH";
case EINTR: return "EINTR";
case EIO: return "EIO";
case ENXIO: return "ENXIO";
case E2BIG: return "E2BIG";
case ENOEXEC: return "ENOEXEC";
case EBADF: return "EBADF";
case ECHILD: return "ECHILD";
case EAGAIN: return "EAGAIN";
case ENOMEM: return "ENOMEM";
case EACCES: return "EACCES";
case EFAULT: return "EFAULT";
case ENOTBLK: return "ENOTBLK";
case EBUSY: return "EBUSY";
case EEXIST: return "EEXIST";
case EXDEV: return "EXDEV";
case ENODEV: return "ENODEV";
case ENOTDIR: return "ENOTDIR";
case EISDIR: return "EISDIR";
case EINVAL: return "EINVAL";
case ENFILE: return "ENFILE";
case EMFILE: return "EMFILE";
case ENOTTY: return "ENOTTY";
case ETXTBSY: return "ETXTBSY";
case EFBIG: return "EFBIG";
case ENOSPC: return "ENOSPC";
case ESPIPE: return "ESPIPE";
case EROFS: return "EROFS";
case EMLINK: return "EMLINK";
case EPIPE: return "EPIPE";
case EDOM: return "EDOM";
case ERANGE: return "ERANGE";
case EDEADLK: return "EDEADLK";
case ENAMETOOLONG: return "ENAMETOOLONG";
case ENOLCK: return "ENOLCK";
case ENOSYS: return "ENOSYS";
case ENOTEMPTY: return "ENOTEMPTY";
case ELOOP: return "ELOOP";
case ENOMSG: return "ENOMSG";
case EIDRM: return "EIDRM";
case ECHRNG: return "ECHRNG";
case EL2NSYNC: return "EL2NSYNC";
case EL3HLT: return "EL3HLT";
case EL3RST: return "EL3RST";
case ELNRNG: return "ELNRNG";
case EUNATCH: return "EUNATCH";
case ENOCSI: return "ENOCSI";
case EL2HLT: return "EL2HLT";
case EBADE: return "EBADE";
case EBADR: return "EBADR";
case EXFULL: return "EXFULL";
case ENOANO: return "ENOANO";
case EBADRQC: return "EBADRQC";
case EBADSLT: return "EBADSLT";
case EBFONT: return "EBFONT";
case ENOSTR: return "ENOSTR";
case ENODATA: return "ENODATA";
case ETIME: return "ETIME";
case ENOSR: return "ENOSR";
case ENONET: return "ENONET";
case ENOPKG: return "ENOPKG";
case EREMOTE: return "EREMOTE";
case ENOLINK: return "ENOLINK";
case EADV: return "EADV";
case ESRMNT: return "ESRMNT";
case ECOMM: return "ECOMM";
case EPROTO: return "EPROTO";
case EMULTIHOP: return "EMULTIHOP";
case EDOTDOT: return "EDOTDOT";
case EBADMSG: return "EBADMSG";
case EOVERFLOW: return "EOVERFLOW";
case ENOTUNIQ: return "ENOTUNIQ";
case EBADFD: return "EBADFD";
case EREMCHG: return "EREMCHG";
case ELIBACC: return "ELIBACC";
case ELIBBAD: return "ELIBBAD";
case ELIBSCN: return "ELIBSCN";
case ELIBMAX: return "ELIBMAX";
case ELIBEXEC: return "ELIBEXEC";
case EILSEQ: return "EILSEQ";
case ERESTART: return "ERESTART";
case ESTRPIPE: return "ESTRPIPE";
case EUSERS: return "EUSERS";
case ENOTSOCK: return "ENOTSOCK";
case EDESTADDRREQ: return "EDESTADDRREQ";
case EMSGSIZE: return "EMSGSIZE";
case EPROTOTYPE: return "EPROTOTYPE";
case ENOPROTOOPT: return "ENOPROTOOPT";
case EPROTONOSUPPORT: return "EPROTONOSUPPORT";
case ESOCKTNOSUPPORT: return "ESOCKTNOSUPPORT";
case EOPNOTSUPP: return "EOPNOTSUPP";
case EPFNOSUPPORT: return "EPFNOSUPPORT";
case EAFNOSUPPORT: return "EAFNOSUPPORT";
case EADDRINUSE: return "EADDRINUSE";
case EADDRNOTAVAIL: return "EADDRNOTAVAIL";
case ENETDOWN: return "ENETDOWN";
case ENETUNREACH: return "ENETUNREACH";
case ENETRESET: return "ENETRESET";
case ECONNABORTED: return "ECONNABORTED";
case ECONNRESET: return "ECONNRESET";
case ENOBUFS: return "ENOBUFS";
case EISCONN: return "EISCONN";
case ENOTCONN: return "ENOTCONN";
case ESHUTDOWN: return "ESHUTDOWN";
case ETOOMANYREFS: return "ETOOMANYREFS";
case ETIMEDOUT: return "ETIMEDOUT";
case ECONNREFUSED: return "ECONNREFUSED";
case EHOSTDOWN: return "EHOSTDOWN";
case EHOSTUNREACH: return "EHOSTUNREACH";
case EALREADY: return "EALREADY";
case EINPROGRESS: return "EINPROGRESS";
case ESTALE: return "ESTALE";
case EUCLEAN: return "EUCLEAN";
case ENOTNAM: return "ENOTNAM";
case ENAVAIL: return "ENAVAIL";
case EISNAM: return "EISNAM";
case EREMOTEIO: return "EREMOTEIO";
case EDQUOT: return "EDQUOT";
case ENOMEDIUM: return "ENOMEDIUM";
case EMEDIUMTYPE: return "EMEDIUMTYPE";
case ECANCELED: return "ECANCELED";
case ENOKEY: return "ENOKEY";
case EKEYEXPIRED: return "EKEYEXPIRED";
case EKEYREVOKED: return "EKEYREVOKED";
case EKEYREJECTED: return "EKEYREJECTED";
case EOWNERDEAD: return "EOWNERDEAD";
case ENOTRECOVERABLE: return "ENOTRECOVERABLE";
case ERFKILL: return "ERFKILL";
case EHWPOISON: return "EHWPOISON";
default: return std::to_string(e);
}
}
/// Check for the URI scheme validity.
/// For now just a basic sanity check, can be extended if necessary.
bool isValidURIScheme(const std::string& scheme);