2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #include "log_manager.h"
25 static pthread_t __loggingThread;
26 static bool __isInit = false;
28 static void *stdlog(void*)
33 while ((readSize = read(__pfd[0], buf, sizeof buf - 1)) > 0) {
34 if (buf[readSize - 1] == '\n') {
49 int initializeLogManager()
53 if (setvbuf(stdout, NULL, _IOLBF, 0) < 0) {
54 _ERR("fail to make stdout line-buffered");
58 if (setvbuf(stderr, NULL, _IONBF, 0) < 0) {
59 _ERR("make stderr unbuffered");
63 /* create the pipe and redirect stdout and stderr */
64 if (pipe(__pfd) < 0) {
65 _ERR("fail to create pipe for logging");
69 /* redirect stdout and stderr */
70 if (redirectFD() < 0) {
74 /* spawn the logging thread */
75 if (pthread_create(&__loggingThread, 0, stdlog, 0) != 0) {
76 _ERR("fail to create pthread");
80 if (pthread_detach(__loggingThread) != 0) {
81 _ERR("fail to detach pthread");
88 // launchpad override stdout and stderr to journalctl before launch application.
89 // So, even though fd redirection is done in initializeLogManager, we do it again before launch.
90 // if LogManager is not initialized, it will return 0;
97 _ERR("fail to create pipe for logging");
102 if (dup2(__pfd[1], 1) == -1) {
103 _ERR("fail to duplicate fd to stdout");
108 if (dup2(__pfd[1], 2) == -1) {
109 _ERR("fail to duplicate fd to stderr");