diff options
author | Taku Kudo <taku@google.com> | 2018-06-20 04:37:28 +0300 |
---|---|---|
committer | Taku Kudo <taku@google.com> | 2018-06-20 04:37:28 +0300 |
commit | 23959e3c94d543e18b2c119b990a4b47043724e8 (patch) | |
tree | bbd88ed9dfe205f0a9bd22e0a8fa5b04b4089c58 /src/util.cc | |
parent | 0fd7f6b33188434c63b164cd46e6c831419065d3 (diff) |
Fixes the usage of strerror_r
Diffstat (limited to 'src/util.cc')
-rw-r--r-- | src/util.cc | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/src/util.cc b/src/util.cc index cbf0d1a..916c52c 100644 --- a/src/util.cc +++ b/src/util.cc @@ -263,4 +263,23 @@ bool OutputBuffer::WriteLine(absl::string_view text) { return Write(text) && Write("\n"); } } // namespace io + +namespace util { + +std::string StrError(int errnum) { + constexpr int kStrErrorSize = 1024; + char buffer[kStrErrorSize]; + char *str = nullptr; +#if defined(__GLIBC__) && defined(_GNU_SOURCE) + str = strerror_r(errnum, buffer, kStrErrorSize - 1); +#else + strerror_r(errnum, buffer, kStrErrorSize - 1); + str = buffer; +#endif + std::ostringstream os; + os << str << " Error #" << errnum; + return os.str(); +} + +} // namespace util } // namespace sentencepiece |