FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
rstudio/src/cpp/core/system/PosixOutputCapture.cpp at master · andschar/rstudio · GitHub
andschar
/
rstudio
Public
forked from
rstudio/rstudio
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
rstudio
/
src
/
cpp
/
core
/
system
/
PosixOutputCapture.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
181 lines (152 loc) · 4.51 KB
Breadcrumbs
rstudio
/
src
/
cpp
/
core
/
system
/
PosixOutputCapture.cpp
Copy path
File metadata and controls
181 lines (152 loc) · 4.51 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* PosixOutputCapture.cpp
*
* Copyright (C) 2009-12 by RStudio, Inc.
*
* Unless you have received this program directly from RStudio pursuant
* to the terms of a commercial license agreement with RStudio, then
* this program is licensed to you under the terms of version 3 of the
* GNU Affero General Public License. This program is distributed WITHOUT
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
*
*/
#
include
<
core/system/OutputCapture.hpp
>
#
include
<
stdio.h
>
#
include
<
unistd.h
>
#
include
<
fcntl.h
>
#
include
<
iostream
>
#
include
<
core/Log.hpp
>
#
include
<
core/Error.hpp
>
#
include
<
core/BoostThread.hpp
>
#
include
<
core/BoostErrors.hpp
>
#
include
<
core/system/System.hpp
>
namespace
rstudio
{
namespace
core
{
namespace
system
{
namespace
{
void
readFromPipe
(
int
pipeFd,
const
boost::function<
void
(
const
std::string&)>& outputFunction)
{
const
int
kBufferSize
=
512
;
char
buffer[
kBufferSize
];
int
bytesRead =
0
;
while
( (bytesRead = ::
read
(pipeFd, buffer,
kBufferSize
)) >
0
)
{
std::string
output
(buffer, bytesRead);
outputFunction
(output);
}
//
log unexpected errors
if
(bytesRead == -
1
)
{
if
(errno !=
EAGAIN
&& errno !=
EINTR
)
LOG_ERROR
(
systemError
(errno,
ERROR_LOCATION
));
}
}
void
standardStreamCaptureThread
(
int
stdoutFd,
const
boost::function<
void
(
const
std::string&)>& stdoutHandler,
int stderrFd,
const
boost::function<void(
const
std::string&)>& stderrHandler)
{
try
{
while
(
true
)
{
//
create fd set
fd_set fds;
FD_ZERO
(&fds);
FD_SET
(stdoutFd, &fds);
if
(stderrFd != -
1
)
FD_SET
(stderrFd, &fds);
//
wait
int
highFd =
std::max
(stdoutFd, stderrFd);
int
result = ::
select
(highFd+
1
, &fds,
NULL
,
NULL
,
NULL
);
if
(result != -
1
)
{
if
(
FD_ISSET
(stdoutFd, &fds))
readFromPipe
(stdoutFd, stdoutHandler);
if
(stderrFd != -
1
)
{
if
(
FD_ISSET
(stderrFd, &fds))
readFromPipe
(stderrFd, stderrHandler);
}
}
else
if
(errno !=
EINTR
)
{
LOG_ERROR
(
systemError
(errno,
ERROR_LOCATION
));
}
}
}
CATCH_UNEXPECTED_EXCEPTION
}
Error
redirectToPipe
(
int
fd,
int
* pPipeReadFd)
{
//
create pipe
int
pipe[
2
];
if
(::
pipe
(pipe) == -
1
)
return
systemError
(errno,
ERROR_LOCATION
);
//
redirect
if
(::
dup2
(pipe[
1
], fd) == -
1
)
return
systemError
(errno,
ERROR_LOCATION
);
//
set read fds to non-blocking
if
(::
fcntl
(pipe[
0
],
F_SETFL
,
::fcntl
(pipe[
0
],
F_GETFL
) | O_NONBLOCK) == -1)
{
return
systemError
(errno,
ERROR_LOCATION
);
}
//
return read fds
*pPipeReadFd = pipe[
0
];
return
Success
();
}
}
//
anonymous namespace
Error
captureStandardStreams
(
const
boost::function<
void
(
const
std::string&)>& stdoutHandler,
const
boost::function<void(
const
std::string&)>& stderrHandler)
{
//
redirect stdout
int
stdoutReadPipe =
0
;
Error error =
redirectToPipe
(
STDOUT_FILENO
, &stdoutReadPipe);
if
(error)
return
error;
//
set stdout to use unbuffered io
::setvbuf
(stdout,
NULL
, _IONBF,
0
);
//
optionally oredirect stderror if handler was provided
int
stderrReadPipe = -
1
;
if
(stderrHandler)
{
error =
redirectToPipe
(
STDERR_FILENO
, &stderrReadPipe);
if
(error)
return
error;
//
set stderr to use unbuffered io
::setvbuf
(stderr,
NULL
, _IONBF,
0
);
}
//
sync c++ iostreams
std::ios::sync_with_stdio
();
//
launch the monitor thread
try
{
//
block all signals for launch of background thread (will cause it
//
to never receive signals)
core::system::SignalBlocker signalBlocker;
Error error = signalBlocker.
blockAll
();
if
(error)
LOG_ERROR
(error);
boost::thread
t
(
boost::bind
(standardStreamCaptureThread,
stdoutReadPipe,
stdoutHandler,
stderrReadPipe,
stderrHandler));
return
Success
();
}
catch
(
const
boost::thread_resource_error& e)
{
return
Error
(
boost::thread_error::ec_from_exception
(e),
ERROR_LOCATION
);
}
}
}
//
namespace system
}
//
namespace core
}
//
namespace rstudio
Back
|
FazBrowse Home
|
New Git URL