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