From 9480871b2bb1c6bb96bf7aada80b5267db1d0842 Mon Sep 17 00:00:00 2001 From: Woongsuk Cho Date: Wed, 18 Mar 2020 17:02:08 +0900 Subject: [PATCH] log manager code cleanup - log redirection is handled in log manager - log redirection can be turn on / off by calling initializeLogManager() function - runLoggingThread function is merged to initializeLogManager - unhanded exception check code is removed - in corerun mode, log manager disabled --- NativeLauncher/inc/log_manager.h | 2 - NativeLauncher/launcher/dotnet/dotnet_launcher.cc | 47 +++-------- NativeLauncher/launcher/dotnet/dotnet_launcher.h | 3 +- NativeLauncher/launcher/main.cc | 9 +-- NativeLauncher/util/log_manager.cc | 97 +++++++++++------------ 5 files changed, 63 insertions(+), 95 deletions(-) diff --git a/NativeLauncher/inc/log_manager.h b/NativeLauncher/inc/log_manager.h index 32414fd..2f4bf89 100644 --- a/NativeLauncher/inc/log_manager.h +++ b/NativeLauncher/inc/log_manager.h @@ -19,7 +19,5 @@ int initializeLogManager(); int redirectFD(); -int runLoggingThread(); -int hasException(); #endif /* __LOG_MANAGER_H__ */ diff --git a/NativeLauncher/launcher/dotnet/dotnet_launcher.cc b/NativeLauncher/launcher/dotnet/dotnet_launcher.cc index aa8a8e9..9e3a077 100644 --- a/NativeLauncher/launcher/dotnet/dotnet_launcher.cc +++ b/NativeLauncher/launcher/dotnet/dotnet_launcher.cc @@ -175,12 +175,6 @@ static void onSigabrt(int signum) exit(0); } - if (hasException()) { - ret = write(STDERR_FILENO, "******************************************************\n", 55); - ret = write(STDERR_FILENO, "Unhandled exception is occured. check application code\n", 55); - ret = write(STDERR_FILENO, "******************************************************\n", 55); - } - checkOnSigabrt = true; if (sigaction(SIGABRT, &sig_abrt_old, NULL) == 0) { if (raise(signum) < 0) { @@ -303,17 +297,6 @@ CoreRuntime::CoreRuntime(const char* mode) : if (initializePluginManager(mode) < 0) { _ERR("Failed to initialize PluginManager"); } - - if (pluginHasLogControl()) { - __enableLogManager = false; - } else { - __enableLogManager = true; - } -} - -void CoreRuntime::setLogManager(bool enable) -{ - __enableLogManager = enable; } CoreRuntime::~CoreRuntime() @@ -329,7 +312,7 @@ CoreRuntime::~CoreRuntime() dispose(); } -int CoreRuntime::initialize(bool standalone) +int CoreRuntime::initialize(bool standalone, bool useDlog) { // checkInjection checks dotnet-launcher run mode // At the moment, this mechanism is used only when the Memory Profiler is started. @@ -381,21 +364,11 @@ int CoreRuntime::initialize(bool standalone) return -1; } - if (__enableLogManager) { + if (useDlog && !pluginHasLogControl()) { 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")); @@ -563,14 +536,16 @@ int CoreRuntime::launch(const char* appId, const char* root, const char* path, i return -1; } - if (__enableLogManager) { - // 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; - } + // launchpad override stdout and stderr to journalctl before launch application. + // we have to re-override that to input pipe for logging thread. + // if LogManager is not initialized, below redirectFD will return 0; + if (redirectFD() < 0) { + _ERR("Failed to redirect FD"); + return -1; + } + // VD has their own signal handler. + if (!pluginHasLogControl()) { registerSigHandler(); } diff --git a/NativeLauncher/launcher/dotnet/dotnet_launcher.h b/NativeLauncher/launcher/dotnet/dotnet_launcher.h index b7511e5..a960bcd 100644 --- a/NativeLauncher/launcher/dotnet/dotnet_launcher.h +++ b/NativeLauncher/launcher/dotnet/dotnet_launcher.h @@ -30,10 +30,9 @@ class CoreRuntime public: CoreRuntime(const char* mode); ~CoreRuntime(); - int initialize(bool standalone); + int initialize(bool standalone, bool useDlog); void dispose(); int launch(const char* appId, const char* root, const char* path, int argc, char* argv[]); - void setLogManager(bool enable); private: diff --git a/NativeLauncher/launcher/main.cc b/NativeLauncher/launcher/main.cc index ced3220..548b2e6 100644 --- a/NativeLauncher/launcher/main.cc +++ b/NativeLauncher/launcher/main.cc @@ -84,10 +84,7 @@ extern "C" int realMain(int argc, char *argv[], const char* mode) snprintf(appId, 16, "%s", "dotnet-launcher"); appRoot = baseName(argv[1]); - // diable log redirection to dlog. - runtime->setLogManager(false); - - if (runtime->initialize(true) != 0) { + if (runtime->initialize(true, false) != 0) { _ERR("Failed to initialize"); return 1; } @@ -114,7 +111,7 @@ extern "C" int realMain(int argc, char *argv[], const char* mode) } _INFO("AUL_APPID : %s", appId); - if (runtime->initialize(true) != 0) { + if (runtime->initialize(true, true) != 0) { _ERR("Failed to initialize"); return 1; } @@ -138,7 +135,7 @@ extern "C" int realMain(int argc, char *argv[], const char* mode) } Launchpad.onCreate = [&runtime]() { - if (runtime->initialize(false) != 0) { + if (runtime->initialize(false, true) != 0) { _ERR("Failed to initialized"); } else { _INFO("Success to initialized"); diff --git a/NativeLauncher/util/log_manager.cc b/NativeLauncher/util/log_manager.cc index a9436f1..6caedf0 100644 --- a/NativeLauncher/util/log_manager.cc +++ b/NativeLauncher/util/log_manager.cc @@ -22,94 +22,93 @@ #include "log_manager.h" static int __pfd[2]; -static pthread_t loggingThread; -static bool __hasException = false; - +static pthread_t __loggingThread; +static bool __isInit = false; 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; + ssize_t readSize; + char buf[1024]; - if (strstr(buf, "Unhandled Exception:") != NULL) { - __hasException = true; + while ((readSize = read(__pfd[0], buf, sizeof buf - 1)) > 0) { + if (buf[readSize - 1] == '\n') { + --readSize; } - _LOGX("%s", buf); - } + buf[readSize] = 0; + + _LOGX("%s", buf); + } close(__pfd[0]); close(__pfd[1]); - return 0; + return 0; } int initializeLogManager() { - if (setvbuf(stdout, NULL, _IOLBF, 0) < 0) { + __isInit = true; + + if (setvbuf(stdout, NULL, _IOLBF, 0) < 0) { _ERR("fail to make stdout line-buffered"); return -1; - } + } - if (setvbuf(stderr, NULL, _IONBF, 0) < 0) { + if (setvbuf(stderr, NULL, _IONBF, 0) < 0) { _ERR("make stderr unbuffered"); return -1; } - /* create the pipe and redirect stdout and stderr */ - if (pipe(__pfd) < 0) { + /* create the pipe and redirect stdout and stderr */ + if (pipe(__pfd) < 0) { _ERR("fail to create pipe for logging"); return -1; - } + } + + /* redirect stdout and stderr */ + if (redirectFD() < 0) { + return -1; + } + + /* spawn the logging thread */ + if (pthread_create(&__loggingThread, 0, stdlog, 0) != 0) { + _ERR("fail to create pthread"); + return -1; + } + + if (pthread_detach(__loggingThread) != 0) { + _ERR("fail to detach pthread"); + return -1; + } return 0; } +// launchpad override stdout and stderr to journalctl before launch application. +// So, even though fd redirection is done in initializeLogManager, we do it again before launch. +// if LogManager is not initialized, it will return 0; int redirectFD() { + if (!__isInit) return 0; + if (__pfd[1] < 0) { _ERR("fail to create pipe for logging"); return -1; } - // stdout - if (dup2(__pfd[1], 1) == -1) { + + // stdout + if (dup2(__pfd[1], 1) == -1) { _ERR("fail to duplicate fd to stdout"); return -1; - } - - // stderr - if (dup2(__pfd[1], 2) == -1) { - _ERR("fail to duplicate fd to stderr"); - return-1; } - return 0; -} - -int runLoggingThread() -{ - /* spawn the logging thread */ - if (pthread_create(&loggingThread, 0, stdlog, 0) != 0) { - _ERR("fail to create pthread"); - return -1; - } - - if (pthread_detach(loggingThread) != 0) { - _ERR("fail to detach pthread"); + // stderr + if (dup2(__pfd[1], 2) == -1) { + _ERR("fail to duplicate fd to stderr"); return -1; } - return 0; + return 0; } -int hasException() -{ - return __hasException; -} -- 2.7.4