summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
author <shinichiro.hamaji@gmail.com>2013-10-31 05:39:43 (GMT)
committer <shinichiro.hamaji@gmail.com>2013-10-31 05:39:43 (GMT)
commita0073e504be20faa7e9c3b31ba795bdcfb99c967 (patch)
treeec00bff98efd5b40bb130812a5432221e6be470a /src
parent0242c8e94d5857c313b2df0354f34003ed99f329 (diff)
downloadglog-a0073e504be20faa7e9c3b31ba795bdcfb99c967.zip
glog-a0073e504be20faa7e9c3b31ba795bdcfb99c967.tar.gz
glog-a0073e504be20faa7e9c3b31ba795bdcfb99c967.tar.bz2
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
Diffstat (limited to 'src')
-rw-r--r--src/glog/logging.h.in4
-rw-r--r--src/googletest.h18
-rw-r--r--src/logging.cc24
-rw-r--r--src/logging_unittest.cc3
-rwxr-xr-xsrc/windows/glog/logging.h4
5 files changed, 30 insertions, 23 deletions
diff --git a/src/glog/logging.h.in b/src/glog/logging.h.in
index 8f9ca98..e8e3e7a 100644
--- a/src/glog/logging.h.in
+++ b/src/glog/logging.h.in
@@ -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
// 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.
+// DEPRECATED: Use StrError(int) instead.
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.
class GOOGLE_GLOG_DLL_DECL NullStream : public LogMessage::LogStream {
diff --git a/src/googletest.h b/src/googletest.h
index 21e4f64..2525bc3 100644
--- a/src/googletest.h
+++ b/src/googletest.h
@@ -450,19 +450,11 @@ static inline string Munge(const string& filename) {
// Remove 0x prefix produced by %p. VC++ doesn't put the prefix.
StringReplace(&line, " 0x", " ");
- char errmsg_buf[100];
- posix_strerror_r(0, errmsg_buf, sizeof(errmsg_buf));
- if (*errmsg_buf == '\0') {
- // MacOSX 10.4 and FreeBSD return empty string for errno=0.
- // In such case, the we need to remove an extra space.
- 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));
+ StringReplace(&line, "__SUCCESS__", StrError(0));
+ StringReplace(&line, "__ENOENT__", StrError(ENOENT));
+ StringReplace(&line, "__EINTR__", StrError(EINTR));
+ StringReplace(&line, "__ENXIO__", StrError(ENXIO));
+ StringReplace(&line, "__ENOEXEC__", StrError(ENOEXEC));
result += line + "\n";
}
fclose(fp);
diff --git a/src/logging.cc b/src/logging.cc
index e5eae45..81d966f 100644
--- a/src/logging.cc
+++ b/src/logging.cc
@@ -1580,9 +1580,8 @@ ErrnoLogMessage::ErrnoLogMessage(const char* file, int line,
ErrnoLogMessage::~ErrnoLogMessage() {
// Don't access errno directly because it may have been altered
// while streaming the message.
- char buf[100];
- posix_strerror_r(preserved_errno(), buf, sizeof(buf));
- stream() << ": " << buf << " [" << preserved_errno() << "]";
+ stream() << ": " << StrError(preserved_errno()) << " ["
+ << preserved_errno() << "]";
}
void FlushLogFiles(LogSeverity min_severity) {
@@ -1711,13 +1710,11 @@ static bool SendEmailInternal(const char*dest, const char *subject,
bool ok = pclose(pipe) != -1;
if ( !ok ) {
if ( use_logging ) {
- char buf[100];
- posix_strerror_r(errno, buf, sizeof(buf));
- LOG(ERROR) << "Problems sending mail to " << dest << ": " << buf;
+ LOG(ERROR) << "Problems sending mail to " << dest << ": "
+ << StrError(errno);
} else {
- char buf[100];
- posix_strerror_r(errno, buf, sizeof(buf));
- fprintf(stderr, "Problems sending mail to %s: %s\n", dest, buf);
+ fprintf(stderr, "Problems sending mail to %s: %s\n",
+ dest, StrError(errno).c_str());
}
}
return ok;
@@ -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) :
LogMessage(file, line, GLOG_FATAL) {}
diff --git a/src/logging_unittest.cc b/src/logging_unittest.cc
index d7e95cf..fa6afce 100644
--- a/src/logging_unittest.cc
+++ b/src/logging_unittest.cc
@@ -1058,8 +1058,9 @@ TEST(Strerror, logging) {
CHECK_STREQ(buf, "");
CHECK_EQ(posix_strerror_r(errcode, buf, buf_size), 0);
CHECK_STREQ(buf, msg);
- free(msg);
delete[] buf;
+ CHECK_EQ(msg, StrError(errcode));
+ free(msg);
}
// Simple routines to look at the sizes of generated code for LOG(FATAL) and
diff --git a/src/windows/glog/logging.h b/src/windows/glog/logging.h
index eb4a4c5..bab8c61 100755
--- a/src/windows/glog/logging.h
+++ b/src/windows/glog/logging.h
@@ -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
// 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.
+// DEPRECATED: Use StrError(int) instead.
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.
class GOOGLE_GLOG_DLL_DECL NullStream : public LogMessage::LogStream {