|
36 | 36 | #include "llvm/Support/Errno.h"
|
37 | 37 | #include "llvm/Support/FileSystem.h"
|
38 | 38 | #include "llvm/Support/Process.h"
|
| 39 | +#include "llvm/Support/raw_ostream.h" |
39 | 40 |
|
40 | 41 | using namespace lldb;
|
41 | 42 | using namespace lldb_private;
|
@@ -247,6 +248,32 @@ uint32_t File::GetPermissions(Status &error) const {
|
247 | 248 | return file_stats.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
|
248 | 249 | }
|
249 | 250 |
|
| 251 | +NativeFile::NativeFile() = default; |
| 252 | + |
| 253 | +NativeFile::NativeFile(FILE *fh, bool transfer_ownership) |
| 254 | + : m_stream(fh), m_own_stream(transfer_ownership) { |
| 255 | +#ifdef _WIN32 |
| 256 | + // In order to properly display non ASCII characters in Windows, we need to |
| 257 | + // use Windows APIs to print to the console. This is only required if the |
| 258 | + // stream outputs to a console. |
| 259 | + int fd = _fileno(fh); |
| 260 | + is_windows_console = |
| 261 | + ::GetFileType((HANDLE)::_get_osfhandle(fd)) == FILE_TYPE_CHAR; |
| 262 | +#endif |
| 263 | +} |
| 264 | + |
| 265 | +NativeFile::NativeFile(int fd, OpenOptions options, bool transfer_ownership) |
| 266 | + : m_descriptor(fd), m_own_descriptor(transfer_ownership), |
| 267 | + m_options(options) { |
| 268 | +#ifdef _WIN32 |
| 269 | + // In order to properly display non ASCII characters in Windows, we need to |
| 270 | + // use Windows APIs to print to the console. This is only required if the |
| 271 | + // file outputs to a console. |
| 272 | + is_windows_console = |
| 273 | + ::GetFileType((HANDLE)::_get_osfhandle(fd)) == FILE_TYPE_CHAR; |
| 274 | +#endif |
| 275 | +} |
| 276 | + |
250 | 277 | bool NativeFile::IsValid() const {
|
251 | 278 | std::scoped_lock<std::mutex, std::mutex> lock(m_descriptor_mutex, m_stream_mutex);
|
252 | 279 | return DescriptorIsValidUnlocked() || StreamIsValidUnlocked();
|
@@ -625,6 +652,13 @@ Status NativeFile::Write(const void *buf, size_t &num_bytes) {
|
625 | 652 | }
|
626 | 653 |
|
627 | 654 | if (ValueGuard stream_guard = StreamIsValid()) {
|
| 655 | +#ifdef _WIN32 |
| 656 | + if (is_windows_console) { |
| 657 | + llvm::raw_fd_ostream(_fileno(m_stream), false) |
| 658 | + .write((char *)buf, num_bytes); |
| 659 | + return error; |
| 660 | + } |
| 661 | +#endif |
628 | 662 | bytes_written = ::fwrite(buf, 1, num_bytes, m_stream);
|
629 | 663 |
|
630 | 664 | if (bytes_written == 0) {
|
|
0 commit comments