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 __hasException = false;
29 static void *stdlog(void*)
34 while ((readSize = read(__pfd[0], buf, sizeof buf - 1)) > 0) {
35 if (buf[readSize - 1] == '\n') {
41 if (strstr(buf, "Unhandled Exception:") != NULL) {
42 __hasException = true;
54 int initializeLogManager()
56 if (setvbuf(stdout, NULL, _IOLBF, 0) < 0) {
57 _ERR("fail to make stdout line-buffered");
61 if (setvbuf(stderr, NULL, _IONBF, 0) < 0) {
62 _ERR("make stderr unbuffered");
66 /* create the pipe and redirect stdout and stderr */
67 if (pipe(__pfd) < 0) {
68 _ERR("fail to create pipe for logging");
78 _ERR("fail to create pipe for logging");
82 if (dup2(__pfd[1], 1) == -1) {
83 _ERR("fail to duplicate fd to stdout");
88 if (dup2(__pfd[1], 2) == -1) {
89 _ERR("fail to duplicate fd to stderr");
96 int runLoggingThread()
98 /* spawn the logging thread */
99 if (pthread_create(&loggingThread, 0, stdlog, 0) != 0) {
100 _ERR("fail to create pthread");
104 if (pthread_detach(loggingThread) != 0) {
105 _ERR("fail to detach pthread");
114 return __hasException;