changeset 42:c80ffa6f3759

Bug 1861: Improve the messaging about gathering stdout/stderr. reviewed-by: yasuenag
author KUBOTA Yuji <kubota.yuji@lab.ntt.co.jp>
date Wed, 16 Jul 2014 17:43:58 +0900
parents db8eea95ac52
children d29334a29aa1
files agent/ChangeLog agent/src/fsUtil.cpp agent/src/logManager.cpp
diffstat 3 files changed, 38 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/agent/ChangeLog	Fri Jun 20 12:10:55 2014 +0900
+++ b/agent/ChangeLog	Wed Jul 16 17:43:58 2014 +0900
@@ -1,3 +1,7 @@
+2014-07-16	KUBOTA Yuji  <kubota.yuji@lab.ntt.co.jp>
+
+	* Bug 1861: Improve the messaging about gathering stdout/stderr.
+
 2014-06-20	KUBOTA Yuji  <kubota.yuji@lab.ntt.co.jp>
 
 	* Bug 1849: HeapStats agent should collect systemd-journald information
--- a/agent/src/fsUtil.cpp	Fri Jun 20 12:10:55 2014 +0900
+++ b/agent/src/fsUtil.cpp	Wed Jul 16 17:43:58 2014 +0900
@@ -207,6 +207,7 @@
 
   if((st.st_mode & S_IFMT) != S_IFREG){
     /* File isn't regular file. This route is not error. */
+    PRINT_DEBUG_MSG_HEADER << "Couldn't copy file. Not a regular file: " << path << NEWLINE;
     return false;
   }
 
--- a/agent/src/logManager.cpp	Fri Jun 20 12:10:55 2014 +0900
+++ b/agent/src/logManager.cpp	Wed Jul 16 17:43:58 2014 +0900
@@ -1139,14 +1139,40 @@
     }
   }
 
-  /* Copy stdout. */
-  if (unlikely(!copyFile("/proc/self/fd/1", basePath, "fd1"))) {
-    PRINT_WARN_MSG("Failure copy standard output.");
-  }
+  /* Copy file descriptors, i.e. stdout and stderr, as avoid double work. */
+  const int fdNum = 2;
+  const char streamList[fdNum][255] = {
+        /* Standard streams */
+        "/proc/self/fd/1", "/proc/self/fd/2",
+  };
+
+  const char fdFile[fdNum][10] = {"fd1", "fd2"};
+  char fdPath[fdNum][PATH_MAX];
+  struct stat fdStat[fdNum];
+  bool flagCopyStreams = true;
+
+  for (int i=0; i<fdNum; i++) {
+    realpath(streamList[i], fdPath[i]);
 
-  /* Copy stderr. */
-  if (unlikely(!copyFile("/proc/self/fd/2", basePath, "fd2"))) {
-    PRINT_WARN_MSG("Failure copy standard error.");
+    if(unlikely( stat(fdPath[i], &fdStat[i]) != 0 )) {
+      /* Failure get file information. */
+      PRINT_WARN_MSG("Failure get file information (stat).");
+      flagCopyStreams = false;
+    } else if( i == 1 && flagCopyStreams && fdStat[0].st_ino == fdStat[1].st_ino ) {
+      /* If stdout and stderr are redirected to same file, no need to copy the file again. */
+      break;
+    } else {
+      flagCopyStreams = copyFile(streamList[i], basePath, fdFile[i]);
+    }
+
+    /* If catch a failure during the copy process, show warn messages. */
+    if (unlikely(!flagCopyStreams)) {
+      if (i==0) {
+        PRINT_WARN_MSG("Failure copy standard output.");
+      } else {
+        PRINT_WARN_MSG("Failure copy standard error.");
+      }
+    }
   }
 
 }