From: Cho Woong Suk Date: Tue, 28 Nov 2017 05:04:34 +0000 (+0900) Subject: redirect stdout and stderr to dlog X-Git-Tag: accepted/tizen/unified/20171130.063237^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=b9e2f8612cc34f8b34290ebb7e3c628664b8026f;p=platform%2Fcore%2Fdotnet%2Flauncher.git redirect stdout and stderr to dlog Change-Id: I4c5fe9f676e5fa3f5f8009eac910976e933acffd --- diff --git a/NativeLauncher/inc/utils.h b/NativeLauncher/inc/utils.h index 034b7f3..ac3816e 100644 --- a/NativeLauncher/inc/utils.h +++ b/NativeLauncher/inc/utils.h @@ -43,4 +43,6 @@ std::string joinStrings(const std::vector& strings, const char* con typedef std::function FileReader; void scanFilesInDir(const char* directory, FileReader reader, unsigned int depth); + +int runLoggingThread(); #endif /* __UTILS_H__ */ diff --git a/NativeLauncher/launcher/dotnet/dotnet_launcher.cc b/NativeLauncher/launcher/dotnet/dotnet_launcher.cc index db93b4f..f19e5af 100644 --- a/NativeLauncher/launcher/dotnet/dotnet_launcher.cc +++ b/NativeLauncher/launcher/dotnet/dotnet_launcher.cc @@ -335,6 +335,10 @@ int CoreRuntime::launch(const char* appId, const char* root, const char* path, i if (pluginBeforeExecute) pluginBeforeExecute(); + if (runLoggingThread() < 0) { + _ERR("Failed to create logging thread"); + } + #ifdef USE_MANAGED_LAUNCHER runManagedLauncher(appId, probePath.c_str(), tpa.c_str()); diff --git a/NativeLauncher/util/utils.cc b/NativeLauncher/util/utils.cc index 92440f1..d4142eb 100644 --- a/NativeLauncher/util/utils.cc +++ b/NativeLauncher/util/utils.cc @@ -14,12 +14,13 @@ * limitations under the License. */ - +#include #include #include #include #include #include +#include #include #include @@ -30,6 +31,10 @@ #include #include "utils.h" +#include "log.h" + +static int pfd[2]; +static pthread_t loggingThread; bool iCompare(const std::string& a, const std::string& b) { @@ -292,4 +297,64 @@ void scanFilesInDir(const char* directory, FileReader reader, unsigned int depth scanFilesInDir(d.c_str(), reader, depth-1); closedir(dir); -} \ No newline at end of file +} + +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; + + _INFO("%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; + } + + 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; + } + + if (dup2(pfd[1], fileno(stdout)) == -1) { + _DBG("fail to duplicate fd to stdout"); + return -1; + } + + if (dup2(pfd[1], fileno(stderr)) == -1) { + _DBG("fail to duplicate fd to stderr"); + return -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; +} +