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