Commit a0073e50 authored by 's avatar

Add StrError and replace posix_strerror_r calls

For now, we do not remove the declaration of posix_strerror_r,
but we might remove it in future.


git-svn-id: https://google-glog.googlecode.com/svn/trunk@139 eb4d4688-79bd-11dd-afb4-1d65580434c0
parent 0242c8e9
...@@ -1528,8 +1528,12 @@ extern GOOGLE_GLOG_DLL_DECL void SetLogger(LogSeverity level, Logger* logger); ...@@ -1528,8 +1528,12 @@ extern GOOGLE_GLOG_DLL_DECL void SetLogger(LogSeverity level, Logger* logger);
// be set to an empty string, if this function failed. This means, in most // be set to an empty string, if this function failed. This means, in most
// cases, you do not need to check the error code and you can directly // cases, you do not need to check the error code and you can directly
// use the value of "buf". It will never have an undefined value. // use the value of "buf". It will never have an undefined value.
// DEPRECATED: Use StrError(int) instead.
GOOGLE_GLOG_DLL_DECL int posix_strerror_r(int err, char *buf, size_t len); GOOGLE_GLOG_DLL_DECL int posix_strerror_r(int err, char *buf, size_t len);
// A thread-safe replacement for strerror(). Returns a string describing the
// given POSIX error code.
GOOGLE_GLOG_DLL_DECL std::string StrError(int err);
// A class for which we define operator<<, which does nothing. // A class for which we define operator<<, which does nothing.
class GOOGLE_GLOG_DLL_DECL NullStream : public LogMessage::LogStream { class GOOGLE_GLOG_DLL_DECL NullStream : public LogMessage::LogStream {
......
...@@ -450,19 +450,11 @@ static inline string Munge(const string& filename) { ...@@ -450,19 +450,11 @@ static inline string Munge(const string& filename) {
// Remove 0x prefix produced by %p. VC++ doesn't put the prefix. // Remove 0x prefix produced by %p. VC++ doesn't put the prefix.
StringReplace(&line, " 0x", " "); StringReplace(&line, " 0x", " ");
char errmsg_buf[100]; StringReplace(&line, "__SUCCESS__", StrError(0));
posix_strerror_r(0, errmsg_buf, sizeof(errmsg_buf)); StringReplace(&line, "__ENOENT__", StrError(ENOENT));
if (*errmsg_buf == '\0') { StringReplace(&line, "__EINTR__", StrError(EINTR));
// MacOSX 10.4 and FreeBSD return empty string for errno=0. StringReplace(&line, "__ENXIO__", StrError(ENXIO));
// In such case, the we need to remove an extra space. StringReplace(&line, "__ENOEXEC__", StrError(ENOEXEC));
StringReplace(&line, "__SUCCESS__ ", "");
} else {
StringReplace(&line, "__SUCCESS__", errmsg_buf);
}
StringReplace(&line, "__ENOENT__", strerror(ENOENT));
StringReplace(&line, "__EINTR__", strerror(EINTR));
StringReplace(&line, "__ENXIO__", strerror(ENXIO));
StringReplace(&line, "__ENOEXEC__", strerror(ENOEXEC));
result += line + "\n"; result += line + "\n";
} }
fclose(fp); fclose(fp);
......
...@@ -1580,9 +1580,8 @@ ErrnoLogMessage::ErrnoLogMessage(const char* file, int line, ...@@ -1580,9 +1580,8 @@ ErrnoLogMessage::ErrnoLogMessage(const char* file, int line,
ErrnoLogMessage::~ErrnoLogMessage() { ErrnoLogMessage::~ErrnoLogMessage() {
// Don't access errno directly because it may have been altered // Don't access errno directly because it may have been altered
// while streaming the message. // while streaming the message.
char buf[100]; stream() << ": " << StrError(preserved_errno()) << " ["
posix_strerror_r(preserved_errno(), buf, sizeof(buf)); << preserved_errno() << "]";
stream() << ": " << buf << " [" << preserved_errno() << "]";
} }
void FlushLogFiles(LogSeverity min_severity) { void FlushLogFiles(LogSeverity min_severity) {
...@@ -1711,13 +1710,11 @@ static bool SendEmailInternal(const char*dest, const char *subject, ...@@ -1711,13 +1710,11 @@ static bool SendEmailInternal(const char*dest, const char *subject,
bool ok = pclose(pipe) != -1; bool ok = pclose(pipe) != -1;
if ( !ok ) { if ( !ok ) {
if ( use_logging ) { if ( use_logging ) {
char buf[100]; LOG(ERROR) << "Problems sending mail to " << dest << ": "
posix_strerror_r(errno, buf, sizeof(buf)); << StrError(errno);
LOG(ERROR) << "Problems sending mail to " << dest << ": " << buf;
} else { } else {
char buf[100]; fprintf(stderr, "Problems sending mail to %s: %s\n",
posix_strerror_r(errno, buf, sizeof(buf)); dest, StrError(errno).c_str());
fprintf(stderr, "Problems sending mail to %s: %s\n", dest, buf);
} }
} }
return ok; return ok;
...@@ -1991,6 +1988,15 @@ int posix_strerror_r(int err, char *buf, size_t len) { ...@@ -1991,6 +1988,15 @@ int posix_strerror_r(int err, char *buf, size_t len) {
} }
} }
string StrError(int err) {
char buf[100];
int rc = posix_strerror_r(err, buf, sizeof(buf));
if ((rc < 0) || (buf[0] == '\000')) {
snprintf(buf, sizeof(buf), "Error number %d", err);
}
return buf;
}
LogMessageFatal::LogMessageFatal(const char* file, int line) : LogMessageFatal::LogMessageFatal(const char* file, int line) :
LogMessage(file, line, GLOG_FATAL) {} LogMessage(file, line, GLOG_FATAL) {}
......
...@@ -1058,8 +1058,9 @@ TEST(Strerror, logging) { ...@@ -1058,8 +1058,9 @@ TEST(Strerror, logging) {
CHECK_STREQ(buf, ""); CHECK_STREQ(buf, "");
CHECK_EQ(posix_strerror_r(errcode, buf, buf_size), 0); CHECK_EQ(posix_strerror_r(errcode, buf, buf_size), 0);
CHECK_STREQ(buf, msg); CHECK_STREQ(buf, msg);
free(msg);
delete[] buf; delete[] buf;
CHECK_EQ(msg, StrError(errcode));
free(msg);
} }
// Simple routines to look at the sizes of generated code for LOG(FATAL) and // Simple routines to look at the sizes of generated code for LOG(FATAL) and
......
...@@ -1532,8 +1532,12 @@ extern GOOGLE_GLOG_DLL_DECL void SetLogger(LogSeverity level, Logger* logger); ...@@ -1532,8 +1532,12 @@ extern GOOGLE_GLOG_DLL_DECL void SetLogger(LogSeverity level, Logger* logger);
// be set to an empty string, if this function failed. This means, in most // be set to an empty string, if this function failed. This means, in most
// cases, you do not need to check the error code and you can directly // cases, you do not need to check the error code and you can directly
// use the value of "buf". It will never have an undefined value. // use the value of "buf". It will never have an undefined value.
// DEPRECATED: Use StrError(int) instead.
GOOGLE_GLOG_DLL_DECL int posix_strerror_r(int err, char *buf, size_t len); GOOGLE_GLOG_DLL_DECL int posix_strerror_r(int err, char *buf, size_t len);
// A thread-safe replacement for strerror(). Returns a string describing the
// given POSIX error code.
GOOGLE_GLOG_DLL_DECL std::string StrError(int err);
// A class for which we define operator<<, which does nothing. // A class for which we define operator<<, which does nothing.
class GOOGLE_GLOG_DLL_DECL NullStream : public LogMessage::LogStream { class GOOGLE_GLOG_DLL_DECL NullStream : public LogMessage::LogStream {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment