add log manager to redirect stderr to dlog 21/184221/1 accepted/tizen/unified/20180717.063207 submit/tizen/20180717.020525
authorWoongsuk Cho <ws77.cho@samsung.com>
Mon, 16 Jul 2018 10:34:59 +0000 (19:34 +0900)
committerWoongsuk Cho <ws77.cho@samsung.com>
Mon, 16 Jul 2018 10:34:59 +0000 (19:34 +0900)
Change-Id: Ifee7e724f155897dbc92fb27e7f402e5ced6ba54

NativeLauncher/CMakeLists.txt
NativeLauncher/inc/log_manager.h [new file with mode: 0644]
NativeLauncher/inc/path_manager.h [moved from NativeLauncher/util/path_manager.h with 100% similarity]
NativeLauncher/inc/utils.h
NativeLauncher/launcher/dotnet/dotnet_launcher.cc
NativeLauncher/util/log_manager.cc [new file with mode: 0644]
NativeLauncher/util/utils.cc

index b53ec6f..42a59d1 100644 (file)
@@ -69,6 +69,7 @@ SET(${DOTNET_LAUNCHER_UTIL}_SOURCE_FILES
     util/utils.cc
     util/plugin_manager.cc
     util/path_manager.cc
+    util/log_manager.cc
 )
 ADD_LIBRARY(${DOTNET_LAUNCHER_UTIL} SHARED ${${DOTNET_LAUNCHER_UTIL}_SOURCE_FILES})
 SET_TARGET_PROPERTIES(${DOTNET_LAUNCHER_UTIL} PROPERTIES COMPILE_FLAGS "-fPIC")
diff --git a/NativeLauncher/inc/log_manager.h b/NativeLauncher/inc/log_manager.h
new file mode 100644 (file)
index 0000000..bf60651
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __LOG_MANAGER_H__
+#define __LOG_MANAGER_H__
+
+int initializeLogManager();
+int redirectFD();
+int runLoggingThread();
+
+#endif /* __LOG_MANAGER_H__ */
index e43b63a..d353f92 100644 (file)
@@ -102,9 +102,4 @@ typedef std::function<void (const std::string&, const char*)> FileReader;
  */
 void scanFilesInDir(const std::string& directory, FileReader reader, unsigned int depth);
 
-/**
- * @brief start logging thread to bypass stderr and stdout to dlog
- * @return return 0 when success, otherwise return negative value.
- */
-int runLoggingThread();
 #endif /* __UTILS_H__ */
index 2b2006c..0fb373a 100644 (file)
@@ -33,6 +33,7 @@
 #include "dotnet_launcher.h"
 #include "plugin_manager.h"
 #include "path_manager.h"
+#include "log_manager.h"
 
 #define PLUGIN_PATH "/usr/share/dotnet.tizen/lib/libdotnet_plugin.so"
 
@@ -95,10 +96,6 @@ CoreRuntime::CoreRuntime(const char* mode) :
        __initialized(false)
 {
        _DBG("Constructor called!!");
-
-       if (runLoggingThread() < 0) {
-               _ERR("Failed to create logging thread");
-       }
 }
 
 CoreRuntime::~CoreRuntime()
@@ -142,6 +139,21 @@ int CoreRuntime::initialize(bool standalone)
                return -1;
        }
 
+       if (initializeLogManager() < 0) {
+               _ERR("Failed to initnialize LogManager");
+               return -1;
+       }
+
+       if (redirectFD() < 0) {
+               _ERR("Failed to redirect FD");
+               return -1;
+       }
+
+       if (runLoggingThread() < 0) {
+               _ERR("Failed to create and run logging thread to redicrect log");
+               return -1;
+       }
+
        std::string libCoreclr(concatPath(getRuntimeDir(), "libcoreclr.so"));
 
        __coreclrLib = dlopen(libCoreclr.c_str(), RTLD_NOW | RTLD_LOCAL);
@@ -273,6 +285,13 @@ int CoreRuntime::launch(const char* appId, const char* root, const char* path, i
                return -1;
        }
 
+       // launchpad override stdout and stderr to journalctl before launch application.
+       // we have to re-override that to input pipe for logging thread.
+       if (redirectFD() < 0) {
+               _ERR("Failed to redirect FD");
+               return -1;
+       }
+
        pluginSetAppInfo(appId, path);
 
        int fd2 = open(root, O_DIRECTORY);
diff --git a/NativeLauncher/util/log_manager.cc b/NativeLauncher/util/log_manager.cc
new file mode 100644 (file)
index 0000000..84ae4c0
--- /dev/null
@@ -0,0 +1,105 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+#include <pthread.h>
+
+#include "log.h"
+#include "log_manager.h"
+
+static int __pfd[2];
+static pthread_t loggingThread;
+
+static void *stdlog(void*)
+{
+    ssize_t readSize;
+    char buf[1024];
+
+    while ((readSize = read(__pfd[0], buf, sizeof buf - 1)) > 0) {
+        if (buf[readSize - 1] == '\n') {
+            --readSize;
+        }
+
+        buf[readSize] = 0;
+
+        _LOGX("%s", buf);
+    }
+
+       close(__pfd[0]);
+       close(__pfd[1]);
+
+    return 0;
+}
+
+int initializeLogManager()
+{
+    if (setvbuf(stdout, NULL, _IOLBF, 0) < 0) {
+               _DBG("fail to make stdout line-buffered");
+               return -1;
+    }
+
+    if (setvbuf(stderr, NULL, _IONBF, 0) < 0) {
+               _DBG("make stderr unbuffered");
+               return -1;
+       }
+
+    /* create the pipe and redirect stdout and stderr */
+    if (pipe(__pfd) < 0) {
+               _DBG("fail to create pipe for logging");
+               return -1;
+    }
+
+       return 0;
+}
+
+int redirectFD()
+{
+       if (__pfd[1] < 0) {
+               _DBG("fail to create pipe for logging");
+               return -1;
+       }
+    // stdout
+    if (dup2(__pfd[1], 1) == -1) {
+               _DBG("fail to duplicate fd to stdout");
+               return -1;
+    }
+
+    // stderr
+    if (dup2(__pfd[1], 2) == -1) {
+               _DBG("fail to duplicate fd to stderr");
+               return-1;
+       }
+
+       return 0;
+}
+
+int runLoggingThread()
+{
+    /* spawn the logging thread */
+    if (pthread_create(&loggingThread, 0, stdlog, 0) != 0) {
+               _DBG("fail to create pthread");
+        return -1;
+    }
+
+    if (pthread_detach(loggingThread) != 0) {
+               _DBG("fail to detach pthread");
+               return -1;
+       }
+
+    return 0;
+}
+
index 62f7b9c..fff4cfb 100644 (file)
  * limitations under the License.
  */
 
-#include <stdio.h>
 #include <dirent.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #include <limits.h>
 #include <strings.h>
-#include <pthread.h>
 
 #include <cstdlib>
 #include <cstring>
@@ -33,9 +31,6 @@
 
 #include "utils.h"
 #include "path_manager.h"
-#include "log.h"
-
-static pthread_t loggingThread;
 
 static bool iCompare(const std::string& a, int aOffset, const std::string& b, int bOffset, int length)
 {
@@ -211,71 +206,3 @@ void scanFilesInDir(const std::string& directory, FileReader reader, unsigned in
        closedir(dir);
 }
 
-static int __pfd[2];
-
-static void *stdlog(void*)
-{
-    ssize_t readSize;
-    char buf[1024];
-
-    while ((readSize = read(__pfd[0], buf, sizeof buf - 1)) > 0) {
-        if (buf[readSize - 1] == '\n') {
-            --readSize;
-        }
-
-        buf[readSize] = 0;
-
-        _LOGX("%s", buf);
-    }
-
-       close(__pfd[0]);
-
-    return 0;
-}
-
-int runLoggingThread()
-{
-    if (setvbuf(stdout, NULL, _IOLBF, 0) < 0) {
-               _DBG("fail to make stdout line-buffered");
-               return -1;
-    }
-
-    if (setvbuf(stderr, NULL, _IONBF, 0) < 0) {
-               _DBG("make stderr unbuffered");
-               return -1;
-       }
-
-    /* create the pipe and redirect stdout and stderr */
-    if (pipe(__pfd) < 0) {
-               _DBG("fail to create pipe for logging");
-               return -1;
-    }
-
-    // stdout
-    if (dup2(__pfd[1], 1) == -1) {
-               _DBG("fail to duplicate fd to stdout");
-               return -1;
-    }
-
-    // stderr
-    if (dup2(__pfd[1], 2) == -1) {
-               _DBG("fail to duplicate fd to stderr");
-               return -1;
-       }
-
-       close(__pfd[1]);
-
-    /* spawn the logging thread */
-    if (pthread_create(&loggingThread, 0, stdlog, 0) != 0) {
-               _DBG("fail to create pthread");
-        return -1;
-    }
-
-    if (pthread_detach(loggingThread) != 0) {
-               _DBG("fail to detach pthread");
-               return -1;
-       }
-
-    return 0;
-}
-