X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=NativeLauncher%2Futil%2Flog_manager.cc;h=72f48cda50b143524347050b5f175855ab1db4c4;hb=8863a014d0ede56d090d3c459996e61359f6bc26;hp=22683891bd02d7d4d7979fb66358e4f6a84b50be;hpb=3f59fa85cbb2a7e0f1ca58b1536fb625778924be;p=platform%2Fcore%2Fdotnet%2Flauncher.git diff --git a/NativeLauncher/util/log_manager.cc b/NativeLauncher/util/log_manager.cc index 2268389..72f48cd 100644 --- a/NativeLauncher/util/log_manager.cc +++ b/NativeLauncher/util/log_manager.cc @@ -22,84 +22,93 @@ #include "log_manager.h" static int __pfd[2]; -static pthread_t loggingThread; +static pthread_t __loggingThread; +static bool __isInit = false; static void *stdlog(void*) { - ssize_t readSize; - char buf[1024]; + ssize_t readSize; + char buf[1024]; - while ((readSize = read(__pfd[0], buf, sizeof buf - 1)) > 0) { - if (buf[readSize - 1] == '\n') { - --readSize; - } + while ((readSize = read(__pfd[0], buf, sizeof buf - 1)) > 0) { + if (buf[readSize - 1] == '\n') { + --readSize; + } - buf[readSize] = 0; + buf[readSize] = 0; - _LOGX("%s", buf); - } + _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; } -