|
| 1 | +#include "StdioForwarder.h" |
| 2 | +#include <Foundation/Foundation.h> |
| 3 | +#include <TargetConditionals.h> |
| 4 | +#include <dispatch/dispatch.h> |
| 5 | +#include <os/log.h> |
| 6 | +#include <stdio.h> |
| 7 | +#include <unistd.h> |
| 8 | +#include <memory> |
| 9 | +#include <mutex> |
| 10 | +#include <string> |
| 11 | +#include "Helpers.h" |
| 12 | +#include "Runtime.h" |
| 13 | +#include "RuntimeConfig.h" |
| 14 | + |
| 15 | +namespace tns { |
| 16 | + |
| 17 | +namespace { |
| 18 | + |
| 19 | +// The CLI tells these entries apart from JS console output by this subsystem. |
| 20 | +constexpr const char* kStdioSubsystem = "org.nativescript.stdio"; |
| 21 | + |
| 22 | +// A longer line goes out in parts instead of growing the buffer without limit. |
| 23 | +constexpr size_t kMaxBufferedLineBytes = 64 * 1024; |
| 24 | + |
| 25 | +void Forward(int fd, const char* category, dispatch_queue_t queue) { |
| 26 | + int fds[2]; |
| 27 | + if (pipe(fds) != 0) { |
| 28 | + return; |
| 29 | + } |
| 30 | + if (dup2(fds[1], fd) < 0) { |
| 31 | + close(fds[0]); |
| 32 | + close(fds[1]); |
| 33 | + return; |
| 34 | + } |
| 35 | + close(fds[1]); |
| 36 | + |
| 37 | + int readEnd = fds[0]; |
| 38 | + os_log_t log = os_log_create(kStdioSubsystem, category); |
| 39 | + auto pending = std::make_shared<std::string>(); |
| 40 | + dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, readEnd, 0, queue); |
| 41 | + dispatch_source_set_event_handler(source, ^{ |
| 42 | + char buffer[4096]; |
| 43 | + ssize_t count = read(readEnd, buffer, sizeof(buffer)); |
| 44 | + if (count <= 0) { |
| 45 | + return; |
| 46 | + } |
| 47 | + pending->append(buffer, count); |
| 48 | + |
| 49 | + size_t start = 0; |
| 50 | + size_t end; |
| 51 | + while ((end = pending->find('\n', start)) != std::string::npos) { |
| 52 | + TNS_WriteToOsLog(pending->substr(start, end - start).c_str(), OS_LOG_TYPE_DEFAULT, "", log); |
| 53 | + start = end + 1; |
| 54 | + } |
| 55 | + pending->erase(0, start); |
| 56 | + |
| 57 | + if (pending->size() >= kMaxBufferedLineBytes) { |
| 58 | + TNS_WriteToOsLog(pending->c_str(), OS_LOG_TYPE_DEFAULT, "", log); |
| 59 | + pending->clear(); |
| 60 | + } |
| 61 | + }); |
| 62 | + dispatch_resume(source); |
| 63 | +} |
| 64 | + |
| 65 | +} // namespace |
| 66 | + |
| 67 | +void StartStdioForwarding() { |
| 68 | + static std::once_flag once; |
| 69 | + std::call_once(once, [] { |
| 70 | + if (!RuntimeConfig.IsDebug || RuntimeConfig.ApplicationPath.empty()) { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + id value = Runtime::GetAppConfigValue("forwardStdio"); |
| 75 | + if (value == nil || ![value boolValue] || !IsDevNull(STDOUT_FILENO)) { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + // Decided once and from stdout, which is about to stop being /dev/null. |
| 80 | + LogFormat(); |
| 81 | + |
| 82 | + // Otherwise stdout is fully buffered into a pipe, and print() only shows up |
| 83 | + // when the buffer fills. |
| 84 | + setvbuf(stdout, nullptr, _IOLBF, 0); |
| 85 | + |
| 86 | + // Serial, so that the chunks of a split line stay together. |
| 87 | + dispatch_queue_t queue = |
| 88 | + dispatch_queue_create("org.nativescript.stdio", DISPATCH_QUEUE_SERIAL); |
| 89 | + Forward(STDOUT_FILENO, "stdout", queue); |
| 90 | +#if !TARGET_OS_SIMULATOR |
| 91 | + // On the simulator NSLog reaches the unified log as well, so forwarding |
| 92 | + // stderr there would print it twice. |
| 93 | + Forward(STDERR_FILENO, "stderr", queue); |
| 94 | +#endif |
| 95 | + }); |
| 96 | +} |
| 97 | + |
| 98 | +} // namespace tns |
0 commit comments