ClickHouse/base/base/errnoToString.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

30 lines
753 B
C++
Raw Normal View History

#include "errnoToString.h"
#include <fmt/format.h>
2022-08-20 15:09:20 +00:00
std::string errnoToString(int the_errno)
{
const size_t buf_size = 128;
char buf[buf_size];
2022-08-20 15:09:20 +00:00
#ifndef _GNU_SOURCE
int rc = strerror_r(the_errno, buf, buf_size);
#ifdef OS_DARWIN
if (rc != 0 && rc != EINVAL)
#else
if (rc != 0)
#endif
{
2022-08-20 15:09:20 +00:00
std::string tmp = std::to_string(the_errno);
const char * code_str = tmp.c_str();
const char * unknown_message = "Unknown error ";
strcpy(buf, unknown_message);
strcpy(buf + strlen(unknown_message), code_str);
}
return fmt::format("errno: {}, strerror: {}", the_errno, buf);
#else
return fmt::format("errno: {}, strerror: {}", the_errno, strerror_r(the_errno, buf, sizeof(buf)));
#endif
}