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
21namespace v8 {
22namespace internal {
23
24DbgStreamBuf::DbgStreamBuf() { setp(data_, data_ + sizeof(data_)); }
25
26DbgStreamBuf::~DbgStreamBuf() { sync(); }
27
28int 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 return 0;
46}
47
48int 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 return 0;
62}
63
64DbgStdoutStream::DbgStdoutStream() : std::ostream(&streambuf_) {}
65
66OFStreamBase::OFStreamBase(FILE* f) : f_(f) {}
67
68int OFStreamBase::sync() {
69 std::fflush(f_);
70 return 0;
71}
72
73
74OFStreamBase::int_type OFStreamBase::overflow(int_type c) {
75 return (c != EOF) ? std::fputc(c, f_) : c;
76}
77
78
79std::streamsize OFStreamBase::xsputn(const char* s, std::streamsize n) {
80 return static_cast<std::streamsize>(
81 std::fwrite(s, 1, static_cast<size_t>(n), f_));
82}
83
84OFStream::OFStream(FILE* f) : std::ostream(nullptr), buf_(f) {
85 DCHECK_NOT_NULL(f);
86 rdbuf(&buf_);
87}
88
89#if defined(ANDROID) && !defined(V8_ANDROID_LOG_STDOUT)
90AndroidLogStream::~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
98std::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
116namespace {
117
118// Locale-independent predicates.
119bool IsPrint(uint16_t c) { return 0x20 <= c && c <= 0x7E; }
120bool IsSpace(uint16_t c) { return (0x9 <= c && c <= 0xD) || c == 0x20; }
121bool IsOK(uint16_t c) { return (IsPrint(c) || IsSpace(c)) && c != '\\'; }
122
123
124std::ostream& PrintUC16(std::ostream& os, uint16_t c, bool (*pred)(uint16_t)) {
125 char buf[10];
126 const char* format = pred(c) ? "%c" : (c <= 0xFF) ? "\\x%02x" : "\\u%04x";
127 snprintf(buf, sizeof(buf), format, c);
128 return os << buf;
129}
130
131std::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 const char* format = pred(c) ? "%c" : "\\u%04x";
136 snprintf(buf, sizeof(buf), format, c);
137 return os << buf;
138}
139
140std::ostream& PrintUC32(std::ostream& os, int32_t c, bool (*pred)(uint16_t)) {
141 if (c <= String::kMaxUtf16CodeUnit) {
142 return PrintUC16(os, static_cast<uint16_t>(c), pred);
143 }
144 char buf[13];
145 snprintf(buf, sizeof(buf), "\\u{%06x}", c);
146 return os << buf;
147}
148
149} // namespace
150
151
152std::ostream& operator<<(std::ostream& os, const AsReversiblyEscapedUC16& c) {
153 return PrintUC16(os, c.value, IsOK);
154}
155
156
157std::ostream& operator<<(std::ostream& os, const AsEscapedUC16ForJSON& c) {
158 if (c.value == '\n') return os << "\\n";
159 if (c.value == '\r') return os << "\\r";
160 if (c.value == '\t') return os << "\\t";
161 if (c.value == '\"') return os << "\\\"";
162 return PrintUC16ForJSON(os, c.value, IsOK);
163}
164
165
166std::ostream& operator<<(std::ostream& os, const AsUC16& c) {
167 return PrintUC16(os, c.value, IsPrint);
168}
169
170
171std::ostream& operator<<(std::ostream& os, const AsUC32& c) {
172 return PrintUC32(os, c.value, IsPrint);
173}
174
175std::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 snprintf(buf, kMaxHexLength, "%s%.*" PRIx64, hex.with_prefix ? "0x" : "",
182 hex.min_width, hex.value);
183 return os << buf;
184}
185
186std::ostream& operator<<(std::ostream& os, const AsHexBytes& hex) {
187 uint8_t bytes = hex.min_bytes;
188 while (bytes < sizeof(hex.value) && (hex.value >> (bytes * 8) != 0)) ++bytes;
189 for (uint8_t b = 0; b < bytes; ++b) {
190 if (b) os << " ";
191 uint8_t printed_byte =
192 hex.byte_order == AsHexBytes::kLittleEndian ? b : bytes - b - 1;
193 os << AsHex((hex.value >> (8 * printed_byte)) & 0xFF, 2);
194 }
195 return os;
196}
197
198} // namespace internal
199} // namespace v8
200
201#undef snprintf
202#undef LOG_TAG
203