log: restore stdout when set NULL. 67/170767/3
authorBoram Park <boram1288.park@samsung.com>
Wed, 21 Feb 2018 07:09:04 +0000 (16:09 +0900)
committerSooChan Lim <sc1.lim@samsung.com>
Thu, 22 Feb 2018 05:59:21 +0000 (05:59 +0000)
Change-Id: I25934b33f8468e55fdd063068f868fafab81f948

common/tdm_log.c

index 3ddcda12bce0822f462f1bdaa23762c64f82dbb5..1befbfb8c462e32c46956d70f87fa1b0bd68a142 100644 (file)
@@ -69,6 +69,8 @@ static pthread_mutex_t log_lock;
 
 unsigned int tdm_log_debug_level = TDM_LOG_LEVEL_INFO;
 
+static int stdout_fd = -1;
+
 EXTERN void
 tdm_log_enable_color(unsigned int enable)
 {
@@ -105,26 +107,40 @@ tdm_log_set_assert_level(int level)
 EXTERN void
 tdm_log_set_path(const char *path)
 {
-       char fd_name[TDM_PATH_LEN];
-       int  log_fd = -1;
-       FILE *log_fl;
+       if (!path) {
+               if (stdout_fd != -1) {
+                       fflush(stdout);
+                       close(STDOUT_FILENO);
+                       dup2(stdout_fd, STDOUT_FILENO);
+                       close(stdout_fd);
+                       stdout_fd = -1;
+               }
+       } else {
+               char fd_name[TDM_PATH_LEN];
+               int  log_fd = -1;
+               FILE *log_fl;
 
-       snprintf(fd_name, TDM_PATH_LEN, "%s", path);
+               snprintf(fd_name, TDM_PATH_LEN, "%s", path);
 
-       log_fl = fopen(fd_name, "a");
-       if (!log_fl) {
-               TDM_ERR("failed: open file(%s)\n", fd_name);
-               return;
-       }
+               log_fl = fopen(fd_name, "a");
+               if (!log_fl) {
+                       TDM_ERR("failed: open file(%s)\n", fd_name);
+                       return;
+               }
 
-       fflush(stdout);
-       close(STDOUT_FILENO);
+               if (stdout_fd == -1) {
+                       fflush(stdout);
+                       stdout_fd = dup(STDOUT_FILENO);
+                       TDM_RETURN_IF_FAIL(stdout_fd != -1);
+               }
 
-       setvbuf(log_fl, NULL, _IOLBF, 512);
-       log_fd = fileno(log_fl);
+               setvbuf(log_fl, NULL, _IOLBF, 512);
+               log_fd = fileno(log_fl);
 
-       dup2(log_fd, STDOUT_FILENO);
-       fclose(log_fl);
+               close(STDOUT_FILENO);
+               dup2(log_fd, STDOUT_FILENO);
+               fclose(log_fl);
+       }
 }
 
 EXTERN void