5 #include "src/ostreams.h" 6 #include "src/objects.h" 7 #include "src/objects/string.h" 11 #define snprintf sprintf_s 15 #if defined(ANDROID) && !defined(V8_ANDROID_LOG_STDOUT) 17 #include <android/log.h> 23 OFStreamBase::OFStreamBase(FILE* f) : f_(f) {}
25 int OFStreamBase::sync() {
31 OFStreamBase::int_type OFStreamBase::overflow(int_type c) {
32 return (c != EOF) ? std::fputc(c, f_) : c;
36 std::streamsize OFStreamBase::xsputn(
const char* s, std::streamsize n) {
37 return static_cast<std::streamsize
>(
38 std::fwrite(s, 1, static_cast<size_t>(n), f_));
41 OFStream::OFStream(FILE* f) :
std::ostream(nullptr), buf_(f) {
46 #if defined(ANDROID) && !defined(V8_ANDROID_LOG_STDOUT) 47 AndroidLogStream::~AndroidLogStream() {
50 if (!line_buffer_.empty()) {
51 __android_log_write(ANDROID_LOG_INFO, LOG_TAG, line_buffer_.c_str());
55 std::streamsize AndroidLogStream::xsputn(
const char* s, std::streamsize n) {
56 const char*
const e = s + n;
58 const char* newline =
reinterpret_cast<const char*
>(memchr(s,
'\n', e - s));
59 size_t line_chars = (newline ? newline : e) - s;
60 line_buffer_.append(s, line_chars);
65 __android_log_write(ANDROID_LOG_INFO, LOG_TAG, line_buffer_.c_str());
76 bool IsPrint(uint16_t c) {
return 0x20 <= c && c <= 0x7E; }
77 bool IsSpace(uint16_t c) {
return (0x9 <= c && c <= 0xD) || c == 0x20; }
78 bool IsOK(uint16_t c) {
return (IsPrint(c) || IsSpace(c)) && c !=
'\\'; }
81 std::ostream& PrintUC16(std::ostream& os, uint16_t c,
bool (*pred)(uint16_t)) {
83 const char* format = pred(c) ?
"%c" : (c <= 0xFF) ?
"\\x%02x" :
"\\u%04x";
84 snprintf(buf,
sizeof(buf), format, c);
88 std::ostream& PrintUC16ForJSON(std::ostream& os, uint16_t c,
89 bool (*pred)(uint16_t)) {
92 const char* format = pred(c) ?
"%c" :
"\\u%04x";
93 snprintf(buf,
sizeof(buf), format, c);
97 std::ostream& PrintUC32(std::ostream& os, int32_t c,
bool (*pred)(uint16_t)) {
98 if (c <= String::kMaxUtf16CodeUnit) {
99 return PrintUC16(os, static_cast<uint16_t>(c), pred);
102 snprintf(buf,
sizeof(buf),
"\\u{%06x}", c);
109 std::ostream& operator<<(std::ostream& os,
const AsReversiblyEscapedUC16& c) {
110 return PrintUC16(os, c.value, IsOK);
114 std::ostream& operator<<(std::ostream& os,
const AsEscapedUC16ForJSON& c) {
115 if (c.value ==
'\n')
return os <<
"\\n";
116 if (c.value ==
'\r')
return os <<
"\\r";
117 if (c.value ==
'\t')
return os <<
"\\t";
118 if (c.value ==
'\"')
return os <<
"\\\"";
119 return PrintUC16ForJSON(os, c.value, IsOK);
123 std::ostream& operator<<(std::ostream& os,
const AsUC16& c) {
124 return PrintUC16(os, c.value, IsPrint);
128 std::ostream& operator<<(std::ostream& os,
const AsUC32& c) {
129 return PrintUC32(os, c.value, IsPrint);
132 std::ostream& operator<<(std::ostream& os,
const AsHex& hex) {
135 DCHECK_GE(
sizeof(hex.value) * 2, hex.min_width);
136 static constexpr
size_t kMaxHexLength = 3 +
sizeof(hex.value) * 2;
137 char buf[kMaxHexLength];
138 snprintf(buf, kMaxHexLength,
"%s%.*" PRIx64, hex.with_prefix ?
"0x" :
"",
139 hex.min_width, hex.value);
143 std::ostream& operator<<(std::ostream& os,
const AsHexBytes& hex) {
144 uint8_t bytes = hex.min_bytes;
145 while (bytes <
sizeof(hex.value) && (hex.value >> (bytes * 8) != 0)) ++bytes;
146 for (uint8_t b = 0; b < bytes; ++b) {
148 uint8_t printed_byte =
149 hex.byte_order == AsHexBytes::kLittleEndian ? b : bytes - b - 1;
150 os << AsHex((hex.value >> (8 * printed_byte)) & 0xFF, 2);