Add comments for added function in utils.h
[platform/core/dotnet/launcher.git] / NativeLauncher / util / log_manager.cc
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <stdio.h>
18 #include <unistd.h>
19 #include <pthread.h>
20
21 #include "log.h"
22 #include "log_manager.h"
23
24 static int __pfd[2];
25 static pthread_t loggingThread;
26 static bool __hasException = false;
27
28
29 static void *stdlog(void*)
30 {
31     ssize_t readSize;
32     char buf[1024];
33
34     while ((readSize = read(__pfd[0], buf, sizeof buf - 1)) > 0) {
35         if (buf[readSize - 1] == '\n') {
36             --readSize;
37         }
38
39         buf[readSize] = 0;
40
41                 if (strstr(buf, "Unhandled Exception:") != NULL) {
42                         __hasException = true;
43                 }
44
45         _LOGX("%s", buf);
46     }
47
48         close(__pfd[0]);
49         close(__pfd[1]);
50
51     return 0;
52 }
53
54 int initializeLogManager()
55 {
56     if (setvbuf(stdout, NULL, _IOLBF, 0) < 0) {
57                 _ERR("fail to make stdout line-buffered");
58                 return -1;
59     }
60
61     if (setvbuf(stderr, NULL, _IONBF, 0) < 0) {
62                 _ERR("make stderr unbuffered");
63                 return -1;
64         }
65
66     /* create the pipe and redirect stdout and stderr */
67     if (pipe(__pfd) < 0) {
68                 _ERR("fail to create pipe for logging");
69                 return -1;
70     }
71
72         return 0;
73 }
74
75 int redirectFD()
76 {
77         if (__pfd[1] < 0) {
78                 _ERR("fail to create pipe for logging");
79                 return -1;
80         }
81     // stdout
82     if (dup2(__pfd[1], 1) == -1) {
83                 _ERR("fail to duplicate fd to stdout");
84                 return -1;
85     }
86
87     // stderr
88     if (dup2(__pfd[1], 2) == -1) {
89                 _ERR("fail to duplicate fd to stderr");
90                 return-1;
91         }
92
93         return 0;
94 }
95
96 int runLoggingThread()
97 {
98     /* spawn the logging thread */
99     if (pthread_create(&loggingThread, 0, stdlog, 0) != 0) {
100                 _ERR("fail to create pthread");
101         return -1;
102     }
103
104     if (pthread_detach(loggingThread) != 0) {
105                 _ERR("fail to detach pthread");
106                 return -1;
107         }
108
109     return 0;
110 }
111
112 int hasException()
113 {
114         return __hasException;
115 }