1 //===-- OutputRedirector.cpp -----------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===/
13 #include "OutputRedirector.h"
17 namespace lldb_vscode
{
19 Error
RedirectFd(int fd
, std::function
<void(llvm::StringRef
)> callback
) {
22 if (pipe(new_fd
) == -1) {
24 return createStringError(inconvertibleErrorCode(),
25 "Couldn't create new pipe for fd %d. %s", fd
,
29 if (dup2(new_fd
[1], fd
) == -1) {
31 return createStringError(inconvertibleErrorCode(),
32 "Couldn't override the fd %d. %s", fd
,
36 int read_fd
= new_fd
[0];
37 std::thread
t([read_fd
, callback
]() {
40 ssize_t bytes_count
= read(read_fd
, &buffer
, sizeof(buffer
));
43 if (bytes_count
== -1) {
44 if (errno
== EAGAIN
|| errno
== EINTR
)
48 callback(StringRef(buffer
, bytes_count
).str());
53 return Error::success();
56 } // namespace lldb_vscode