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;
27 static void *stdlog(void*)
32 while ((readSize = read(__pfd[0], buf, sizeof buf - 1)) > 0) {
33 if (buf[readSize - 1] == '\n') {
48 int initializeLogManager()
50 if (setvbuf(stdout, NULL, _IOLBF, 0) < 0) {
51 _ERR("fail to make stdout line-buffered");
55 if (setvbuf(stderr, NULL, _IONBF, 0) < 0) {
56 _ERR("make stderr unbuffered");
60 /* create the pipe and redirect stdout and stderr */
61 if (pipe(__pfd) < 0) {
62 _ERR("fail to create pipe for logging");
72 _ERR("fail to create pipe for logging");
76 if (dup2(__pfd[1], 1) == -1) {
77 _ERR("fail to duplicate fd to stdout");
82 if (dup2(__pfd[1], 2) == -1) {
83 _ERR("fail to duplicate fd to stderr");
90 int runLoggingThread()
92 /* spawn the logging thread */
93 if (pthread_create(&loggingThread, 0, stdlog, 0) != 0) {
94 _ERR("fail to create pthread");
98 if (pthread_detach(loggingThread) != 0) {
99 _ERR("fail to detach pthread");