blob: d58357c3a619cbf6f6a9fa3e18444bc8e5ef114e [file] [log] [blame]
Kaido Kertf309f9a2021-04-30 12:09:15 -07001// Copyright 2014 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "src/utils/ostreams.h"
6
7#include <cinttypes>
8
9#include "src/base/lazy-instance.h"
10#include "src/objects/objects.h"
11#include "src/objects/string.h"
12
13#if V8_OS_WIN
14#include <windows.h>
15#if _MSC_VER < 1900
16#define snprintf sprintf_s
17#endif
18#endif
19
Kaido Kertf309f9a2021-04-30 12:09:15 -070020#if defined(ANDROID) && !defined(V8_ANDROID_LOG_STDOUT)
21#define LOG_TAG "v8"
22#include <android/log.h> // NOLINT
23#endif
24
25namespace v8 {
26namespace internal {
27
28DbgStreamBuf::DbgStreamBuf() { setp(data_, data_ + sizeof(data_)); }
29
30DbgStreamBuf::~DbgStreamBuf() { sync(); }
31
32int DbgStreamBuf::overflow(int c) {
33#if V8_OS_WIN
34 if (!IsDebuggerPresent()) {
35 return 0;
36 }
37
38 sync();
39
40 if (c != EOF) {
41 if (pbase() == epptr()) {
42 auto as_char = static_cast<char>(c);
43 OutputDebugStringA(&as_char);
44 } else {
45 sputc(static_cast<char>(c));
46 }
47 }
48#endif
49 return 0;
50}
51
52int DbgStreamBuf::sync() {
53#if V8_OS_WIN
54 if (!IsDebuggerPresent()) {
55 return 0;
56 }
57
58 if (pbase() != pptr()) {
59 OutputDebugStringA(std::string(pbase(), static_cast<std::string::size_type>(
60 pptr() - pbase()))
61 .c_str());
62 setp(pbase(), epptr());
63 }
64#endif
65 return 0;
66}
67
68DbgStdoutStream::DbgStdoutStream() : std::ostream(&streambuf_) {}
69
70#if !defined(V8_OS_STARBOARD)
71OFStreamBase::OFStreamBase(FILE* f) : f_(f) {}
72
73int OFStreamBase::sync() {
74 std::fflush(f_);
75 return 0;
76}
77
78OFStreamBase::int_type OFStreamBase::overflow(int_type c) {
79 return (c != EOF) ? std::fputc(c, f_) : c;
80}
81
82std::streamsize OFStreamBase::xsputn(const char* s, std::streamsize n) {
83 return static_cast<std::streamsize>(
84 std::fwrite(s, 1, static_cast<size_t>(n), f_));
85}
86
87OFStream::OFStream(FILE* f) : std::ostream(nullptr), buf_(f) {
88 DCHECK_NOT_NULL(f);
89 rdbuf(&buf_);
90}
91#else
92OFStream::OFStream(FILE* f) : std::ostream(nullptr) {}
93#endif
94
95#if defined(ANDROID) && !defined(V8_ANDROID_LOG_STDOUT)
96AndroidLogStream::~AndroidLogStream() {
97 // If there is anything left in the line buffer, print it now, even though it
98 // was not terminated by a newline.
99 if (!line_buffer_.empty()) {
100 __android_log_write(ANDROID_LOG_INFO, LOG_TAG, line_buffer_.c_str());
101 }
102}
103
104std::streamsize AndroidLogStream::xsputn(const char* s, std::streamsize n) {
105 const char* const e = s + n;
106 while (s < e) {
107 const char* newline = reinterpret_cast<const char*>(memchr(s, '\n', e - s));
108 size_t line_chars = (newline ? newline : e) - s;
109 line_buffer_.append(s, line_chars);
110 // Without terminating newline, keep the characters in the buffer for the
111 // next invocation.
112 if (!newline) break;
113 // Otherwise, write out the first line, then continue.
114 __android_log_write(ANDROID_LOG_INFO, LOG_TAG, line_buffer_.c_str());
115 line_buffer_.clear();
116 s = newline + 1;
117 }
118 return n;
119}
120#endif
121
122DEFINE_LAZY_LEAKY_OBJECT_GETTER(base::RecursiveMutex,
123 StdoutStream::GetStdoutMutex)
124
125namespace {
126
127// Locale-independent predicates.
128bool IsPrint(uint16_t c) { return 0x20 <= c && c <= 0x7E; }
129bool IsSpace(uint16_t c) { return (0x9 <= c && c <= 0xD) || c == 0x20; }
130bool IsOK(uint16_t c) { return (IsPrint(c) || IsSpace(c)) && c != '\\'; }
131
132std::ostream& PrintUC16(std::ostream& os, uint16_t c, bool (*pred)(uint16_t)) {
133 char buf[10];
134 const char* format = pred(c) ? "%c" : (c <= 0xFF) ? "\\x%02x" : "\\u%04x";
135 snprintf(buf, sizeof(buf), format, c);
136 return os << buf;
137}
138
139std::ostream& PrintUC16ForJSON(std::ostream& os, uint16_t c,
140 bool (*pred)(uint16_t)) {
141 // JSON does not allow \x99; must use \u0099.
142 char buf[10];
143 const char* format = pred(c) ? "%c" : "\\u%04x";
144 snprintf(buf, sizeof(buf), format, c);
145 return os << buf;
146}
147
148std::ostream& PrintUC32(std::ostream& os, int32_t c, bool (*pred)(uint16_t)) {
149 if (c <= String::kMaxUtf16CodeUnit) {
150 return PrintUC16(os, static_cast<uint16_t>(c), pred);
151 }
152 char buf[13];
153 snprintf(buf, sizeof(buf), "\\u{%06x}", c);
154 return os << buf;
155}
156
157} // namespace
158
159std::ostream& operator<<(std::ostream& os, const AsReversiblyEscapedUC16& c) {
160 return PrintUC16(os, c.value, IsOK);
161}
162
163std::ostream& operator<<(std::ostream& os, const AsEscapedUC16ForJSON& c) {
164 if (c.value == '\n') return os << "\\n";
165 if (c.value == '\r') return os << "\\r";
166 if (c.value == '\t') return os << "\\t";
167 if (c.value == '\"') return os << "\\\"";
168 return PrintUC16ForJSON(os, c.value, IsOK);
169}
170
171std::ostream& operator<<(std::ostream& os, const AsUC16& c) {
172 return PrintUC16(os, c.value, IsPrint);
173}
174
175std::ostream& operator<<(std::ostream& os, const AsUC32& c) {
176 return PrintUC32(os, c.value, IsPrint);
177}
178
179std::ostream& operator<<(std::ostream& os, const AsHex& hex) {
180 // Each byte uses up to two characters. Plus two characters for the prefix,
181 // plus null terminator.
182 DCHECK_GE(sizeof(hex.value) * 2, hex.min_width);
183 static constexpr size_t kMaxHexLength = 3 + sizeof(hex.value) * 2;
184 char buf[kMaxHexLength];
185 snprintf(buf, kMaxHexLength, "%s%.*" PRIx64, hex.with_prefix ? "0x" : "",
186 hex.min_width, hex.value);
187 return os << buf;
188}
189
190std::ostream& operator<<(std::ostream& os, const AsHexBytes& hex) {
191 uint8_t bytes = hex.min_bytes;
192 while (bytes < sizeof(hex.value) && (hex.value >> (bytes * 8) != 0)) ++bytes;
193 for (uint8_t b = 0; b < bytes; ++b) {
194 if (b) os << " ";
195 uint8_t printed_byte =
196 hex.byte_order == AsHexBytes::kLittleEndian ? b : bytes - b - 1;
197 os << AsHex((hex.value >> (8 * printed_byte)) & 0xFF, 2);
198 }
199 return os;
200}
201
202} // namespace internal
203} // namespace v8
204
205#undef snprintf
206#undef LOG_TAG