From 65ee437b4cf72d4e4720f5d9c7c2e1d0a0d28676 Mon Sep 17 00:00:00 2001 From: Cho Woong Suk Date: Fri, 23 Feb 2018 11:45:10 +0900 Subject: [PATCH] to send stdout to dlog, move logging thread creation point Change-Id: I929a29ce4f92b547eaa74f5bdf95c5c83c202946 --- NativeLauncher/launcher/dotnet/dotnet_launcher.cc | 8 ++--- NativeLauncher/util/utils.cc | 44 ++++++++++++----------- 2 files changed, 28 insertions(+), 24 deletions(-) diff --git a/NativeLauncher/launcher/dotnet/dotnet_launcher.cc b/NativeLauncher/launcher/dotnet/dotnet_launcher.cc index 28987bb..a6077ea 100644 --- a/NativeLauncher/launcher/dotnet/dotnet_launcher.cc +++ b/NativeLauncher/launcher/dotnet/dotnet_launcher.cc @@ -143,10 +143,6 @@ CoreRuntime::~CoreRuntime() int CoreRuntime::initialize(bool standalone) { - if (runLoggingThread() < 0) { - _ERR("Failed to create logging thread"); - } - #ifdef __arm__ // libunwind library is used to unwind stack frame, but libunwind for ARM // does not support ARM vfpv3/NEON registers in DWARF format correctly. @@ -323,6 +319,10 @@ void CoreRuntime::dispose() int CoreRuntime::launch(const char* appId, const char* root, const char* path, int argc, char* argv[]) { + if (runLoggingThread() < 0) { + _ERR("Failed to create logging thread"); + } + if (path == nullptr) { _ERR("executable path is null"); return 1; diff --git a/NativeLauncher/util/utils.cc b/NativeLauncher/util/utils.cc index f5b9406..ce01795 100644 --- a/NativeLauncher/util/utils.cc +++ b/NativeLauncher/util/utils.cc @@ -33,7 +33,6 @@ #include "utils.h" #include "log.h" -static int pfd[2]; static pthread_t loggingThread; bool iCompare(const std::string& a, const std::string& b) @@ -301,49 +300,54 @@ void scanFilesInDir(const char* directory, FileReader reader, unsigned int depth static void *stdlog(void*) { + int pfd[2]; 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; - - _ERRX("%s", buf); - } - - return 0; -} - -int runLoggingThread() { // run this function to redirect your output to android log if (setvbuf(stdout, NULL, _IOLBF, 0) < 0) { _DBG("fail to make stdout line-buffered"); - return -1; + return 0; } if (setvbuf(stderr, NULL, _IONBF, 0) < 0) { _DBG("make stderr unbuffered"); - return -1; + return 0; } /* create the pipe and redirect stdout and stderr */ if (pipe(pfd) < 0) { _DBG("fail to create pipe for logging"); - return -1; + return 0; } if (dup2(pfd[1], fileno(stdout)) == -1) { _DBG("fail to duplicate fd to stdout"); - return -1; + return 0; } if (dup2(pfd[1], fileno(stderr)) == -1) { _DBG("fail to duplicate fd to stderr"); - return -1; + return 0; } + close(pfd[1]); + + while ((readSize = read(pfd[0], buf, sizeof buf - 1)) > 0) { + if (buf[readSize - 1] == '\n') { + --readSize; + } + + buf[readSize] = 0; + + _ERRX("%s", buf); + } + + close(pfd[0]); + + return 0; +} + +int runLoggingThread() { /* spawn the logging thread */ if (pthread_create(&loggingThread, 0, stdlog, 0) != 0) { _DBG("fail to create pthread"); -- 2.7.4