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/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
20 #if defined(ANDROID) && !defined(V8_ANDROID_LOG_STDOUT)
21 #define LOG_TAG "v8"
22 #include <android/log.h>
23 #endif
24
25 namespace v8 {
26 namespace internal {
27
DbgStreamBuf()28 DbgStreamBuf::DbgStreamBuf() { setp(data_, data_ + sizeof(data_)); }
29
~DbgStreamBuf()30 DbgStreamBuf::~DbgStreamBuf() { sync(); }
31
overflow(int c)32 int 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
sync()52 int 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
DbgStdoutStream()68 DbgStdoutStream::DbgStdoutStream() : std::ostream(&streambuf_) {}
69
OFStreamBase(FILE * f)70 OFStreamBase::OFStreamBase(FILE* f) : f_(f) {}
71
sync()72 int OFStreamBase::sync() {
73 std::fflush(f_);
74 return 0;
75 }
76
overflow(int_type c)77 OFStreamBase::int_type OFStreamBase::overflow(int_type c) {
78 return (c != EOF) ? std::fputc(c, f_) : c;
79 }
80
xsputn(const char * s,std::streamsize n)81 std::streamsize OFStreamBase::xsputn(const char* s, std::streamsize n) {
82 return static_cast<std::streamsize>(
83 std::fwrite(s, 1, static_cast<size_t>(n), f_));
84 }
85
OFStream(FILE * f)86 OFStream::OFStream(FILE* f) : std::ostream(nullptr), buf_(f) {
87 DCHECK_NOT_NULL(f);
88 rdbuf(&buf_);
89 }
90
91 #if defined(ANDROID) && !defined(V8_ANDROID_LOG_STDOUT)
~AndroidLogStream()92 AndroidLogStream::~AndroidLogStream() {
93 // If there is anything left in the line buffer, print it now, even though it
94 // was not terminated by a newline.
95 if (!line_buffer_.empty()) {
96 __android_log_write(ANDROID_LOG_INFO, LOG_TAG, line_buffer_.c_str());
97 }
98 }
99
xsputn(const char * s,std::streamsize n)100 std::streamsize AndroidLogStream::xsputn(const char* s, std::streamsize n) {
101 const char* const e = s + n;
102 while (s < e) {
103 const char* newline = reinterpret_cast<const char*>(memchr(s, '\n', e - s));
104 size_t line_chars = (newline ? newline : e) - s;
105 line_buffer_.append(s, line_chars);
106 // Without terminating newline, keep the characters in the buffer for the
107 // next invocation.
108 if (!newline) break;
109 // Otherwise, write out the first line, then continue.
110 __android_log_write(ANDROID_LOG_INFO, LOG_TAG, line_buffer_.c_str());
111 line_buffer_.clear();
112 s = newline + 1;
113 }
114 return n;
115 }
116 #endif
117
118 DEFINE_LAZY_LEAKY_OBJECT_GETTER(base::RecursiveMutex,
119 StdoutStream::GetStdoutMutex)
120
121 namespace {
122
123 // Locale-independent predicates.
IsPrint(uint16_t c)124 bool IsPrint(uint16_t c) { return 0x20 <= c && c <= 0x7E; }
IsSpace(uint16_t c)125 bool IsSpace(uint16_t c) { return (0x9 <= c && c <= 0xD) || c == 0x20; }
IsOK(uint16_t c)126 bool IsOK(uint16_t c) { return (IsPrint(c) || IsSpace(c)) && c != '\\'; }
127
PrintUC16(std::ostream & os,uint16_t c,bool (* pred)(uint16_t))128 std::ostream& PrintUC16(std::ostream& os, uint16_t c, bool (*pred)(uint16_t)) {
129 char buf[10];
130 const char* format = pred(c) ? "%c" : (c <= 0xFF) ? "\\x%02x" : "\\u%04x";
131 snprintf(buf, sizeof(buf), format, c);
132 return os << buf;
133 }
134
PrintUC16ForJSON(std::ostream & os,uint16_t c,bool (* pred)(uint16_t))135 std::ostream& PrintUC16ForJSON(std::ostream& os, uint16_t c,
136 bool (*pred)(uint16_t)) {
137 // JSON does not allow \x99; must use \u0099.
138 char buf[10];
139 const char* format = pred(c) ? "%c" : "\\u%04x";
140 snprintf(buf, sizeof(buf), format, c);
141 return os << buf;
142 }
143
PrintUC32(std::ostream & os,int32_t c,bool (* pred)(uint16_t))144 std::ostream& PrintUC32(std::ostream& os, int32_t c, bool (*pred)(uint16_t)) {
145 if (c <= String::kMaxUtf16CodeUnit) {
146 return PrintUC16(os, static_cast<uint16_t>(c), pred);
147 }
148 char buf[13];
149 snprintf(buf, sizeof(buf), "\\u{%06x}", c);
150 return os << buf;
151 }
152
153 } // namespace
154
operator <<(std::ostream & os,const AsReversiblyEscapedUC16 & c)155 std::ostream& operator<<(std::ostream& os, const AsReversiblyEscapedUC16& c) {
156 return PrintUC16(os, c.value, IsOK);
157 }
158
operator <<(std::ostream & os,const AsEscapedUC16ForJSON & c)159 std::ostream& operator<<(std::ostream& os, const AsEscapedUC16ForJSON& c) {
160 if (c.value == '\n') return os << "\\n";
161 if (c.value == '\r') return os << "\\r";
162 if (c.value == '\t') return os << "\\t";
163 if (c.value == '\"') return os << "\\\"";
164 return PrintUC16ForJSON(os, c.value, IsOK);
165 }
166
operator <<(std::ostream & os,const AsUC16 & c)167 std::ostream& operator<<(std::ostream& os, const AsUC16& c) {
168 return PrintUC16(os, c.value, IsPrint);
169 }
170
operator <<(std::ostream & os,const AsUC32 & c)171 std::ostream& operator<<(std::ostream& os, const AsUC32& c) {
172 return PrintUC32(os, c.value, IsPrint);
173 }
174
operator <<(std::ostream & os,const AsHex & hex)175 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 snprintf(buf, kMaxHexLength, "%s%.*" PRIx64, hex.with_prefix ? "0x" : "",
182 hex.min_width, hex.value);
183 return os << buf;
184 }
185
operator <<(std::ostream & os,const AsHexBytes & hex)186 std::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