From c844067959c0f4a7e7a39f6ad1a26346cbe98c1b Mon Sep 17 00:00:00 2001 From: Woongsuk Cho Date: Mon, 16 Jul 2018 19:34:59 +0900 Subject: [PATCH] add log manager to redirect stderr to dlog Change-Id: Ifee7e724f155897dbc92fb27e7f402e5ced6ba54 --- NativeLauncher/CMakeLists.txt | 1 + NativeLauncher/inc/log_manager.h | 24 +++++ NativeLauncher/{util => inc}/path_manager.h | 0 NativeLauncher/inc/utils.h | 5 -- NativeLauncher/launcher/dotnet/dotnet_launcher.cc | 27 +++++- NativeLauncher/util/log_manager.cc | 105 ++++++++++++++++++++++ NativeLauncher/util/utils.cc | 73 --------------- 7 files changed, 153 insertions(+), 82 deletions(-) create mode 100644 NativeLauncher/inc/log_manager.h rename NativeLauncher/{util => inc}/path_manager.h (100%) create mode 100644 NativeLauncher/util/log_manager.cc diff --git a/NativeLauncher/CMakeLists.txt b/NativeLauncher/CMakeLists.txt index b53ec6f..42a59d1 100644 --- a/NativeLauncher/CMakeLists.txt +++ b/NativeLauncher/CMakeLists.txt @@ -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 index 0000000..bf60651 --- /dev/null +++ b/NativeLauncher/inc/log_manager.h @@ -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__ */ diff --git a/NativeLauncher/util/path_manager.h b/NativeLauncher/inc/path_manager.h similarity index 100% rename from NativeLauncher/util/path_manager.h rename to NativeLauncher/inc/path_manager.h diff --git a/NativeLauncher/inc/utils.h b/NativeLauncher/inc/utils.h index e43b63a..d353f92 100644 --- a/NativeLauncher/inc/utils.h +++ b/NativeLauncher/inc/utils.h @@ -102,9 +102,4 @@ typedef std::function 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__ */ diff --git a/NativeLauncher/launcher/dotnet/dotnet_launcher.cc b/NativeLauncher/launcher/dotnet/dotnet_launcher.cc index 2b2006c..0fb373a 100644 --- a/NativeLauncher/launcher/dotnet/dotnet_launcher.cc +++ b/NativeLauncher/launcher/dotnet/dotnet_launcher.cc @@ -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 index 0000000..84ae4c0 --- /dev/null +++ b/NativeLauncher/util/log_manager.cc @@ -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 +#include +#include + +#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; +} + diff --git a/NativeLauncher/util/utils.cc b/NativeLauncher/util/utils.cc index 62f7b9c..fff4cfb 100644 --- a/NativeLauncher/util/utils.cc +++ b/NativeLauncher/util/utils.cc @@ -14,13 +14,11 @@ * limitations under the License. */ -#include #include #include #include #include #include -#include #include #include @@ -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; -} - -- 2.7.4