changeset 28:6122238e5855

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:30:48 +0900
parents e52df4e1bc2b
children 835c846563fb
files agent/src/fsUtil.cpp agent/src/logManager.cpp
diffstat 2 files changed, 32 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/agent/src/fsUtil.cpp	Mon Jul 07 20:21:58 2014 +0900
+++ b/agent/src/fsUtil.cpp	Wed Jul 16 17:30:48 2014 +0900
@@ -226,6 +226,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	Mon Jul 07 20:21:58 2014 +0900
+++ b/agent/src/logManager.cpp	Wed Jul 16 17:30:48 2014 +0900
@@ -1358,28 +1358,42 @@
     }
   }
 
-  /* Copy stdout */
-  result = copyFile("/proc/self/fd/1", basePath, "fd1");
-  if(unlikely(result != 0)){
-    PRINT_WARN_MSG_AND_ERRNO("Failure copy file. path:\"stdout\"", result);
+  /* 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];
 
-    /* If disk is full. */
-    if(unlikely(isRaisedDiskFull(result))){
-      return result;
+  for (int i=0; i<fdNum; i++) {
+    realpath(streamList[i], fdPath[i]);
+
+    if(unlikely( stat(fdPath[i], &fdStat[i]) != 0 )) {
+      /* Failure get file information. */
+      result = errno;
+      PRINT_WARN_MSG_AND_ERRNO("Failure get file information (stat).", result);
+    } else if( i == 1 && result == 0 && 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 {
+      result = copyFile(streamList[i], basePath, fdFile[i]);
     }
 
-  }
+    /* If catch a failure during the copy process, show warn messages. */
+    if (unlikely(result != 0)) {
+      char message[512+PATH_MAX] = {0};
+      sprintf(message, "Failure copy file. path: %s", streamList[i]);
+      PRINT_WARN_MSG_AND_ERRNO(message,result);
 
-  /* Copy stderr */
-  result = copyFile("/proc/self/fd/2", basePath, "fd2");
-  if(unlikely(result != 0)){
-    PRINT_WARN_MSG_AND_ERRNO("Failure copy file. path:\"stderr\"", result);
-
-    /* If disk is full. */
-    if(unlikely(isRaisedDiskFull(result))){
-      return result;
+      /* If disk is full. */
+      if(unlikely(isRaisedDiskFull(result))) {
+        return result;
+      }
     }
-
   }
 
   return result;