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 : #if _MSC_VER < 1900
11 : #define snprintf sprintf_s
12 : #endif
13 : #endif
14 :
15 : #if defined(ANDROID) && !defined(V8_ANDROID_LOG_STDOUT)
16 : #define LOG_TAG "v8"
17 : #include <android/log.h> // NOLINT
18 : #endif
19 :
20 : namespace v8 {
21 : namespace internal {
22 :
23 73461 : OFStreamBase::OFStreamBase(FILE* f) : f_(f) {}
24 :
25 49412 : int OFStreamBase::sync() {
26 49412 : std::fflush(f_);
27 49412 : return 0;
28 : }
29 :
30 :
31 53038 : OFStreamBase::int_type OFStreamBase::overflow(int_type c) {
32 53038 : return (c != EOF) ? std::fputc(c, f_) : c;
33 : }
34 :
35 :
36 17752959 : std::streamsize OFStreamBase::xsputn(const char* s, std::streamsize n) {
37 : return static_cast<std::streamsize>(
38 17752959 : std::fwrite(s, 1, static_cast<size_t>(n), f_));
39 : }
40 :
41 145194 : OFStream::OFStream(FILE* f) : std::ostream(nullptr), buf_(f) {
42 : DCHECK_NOT_NULL(f);
43 73461 : rdbuf(&buf_);
44 73461 : }
45 :
46 : #if defined(ANDROID) && !defined(V8_ANDROID_LOG_STDOUT)
47 : AndroidLogStream::~AndroidLogStream() {
48 : // If there is anything left in the line buffer, print it now, even though it
49 : // was not terminated by a newline.
50 : if (!line_buffer_.empty()) {
51 : __android_log_write(ANDROID_LOG_INFO, LOG_TAG, line_buffer_.c_str());
52 : }
53 : }
54 :
55 : std::streamsize AndroidLogStream::xsputn(const char* s, std::streamsize n) {
56 : const char* const e = s + n;
57 : while (s < e) {
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);
61 : // Without terminating newline, keep the characters in the buffer for the
62 : // next invocation.
63 : if (!newline) break;
64 : // Otherwise, write out the first line, then continue.
65 : __android_log_write(ANDROID_LOG_INFO, LOG_TAG, line_buffer_.c_str());
66 : line_buffer_.clear();
67 : s = newline + 1;
68 : }
69 : return n;
70 : }
71 : #endif
72 :
73 : namespace {
74 :
75 : // Locale-independent predicates.
76 53627 : bool IsPrint(uint16_t c) { return 0x20 <= c && c <= 0x7E; }
77 0 : bool IsSpace(uint16_t c) { return (0x9 <= c && c <= 0xD) || c == 0x20; }
78 91008 : bool IsOK(uint16_t c) { return (IsPrint(c) || IsSpace(c)) && c != '\\'; }
79 :
80 :
81 8123 : std::ostream& PrintUC16(std::ostream& os, uint16_t c, bool (*pred)(uint16_t)) {
82 : char buf[10];
83 8123 : const char* format = pred(c) ? "%c" : (c <= 0xFF) ? "\\x%02x" : "\\u%04x";
84 : snprintf(buf, sizeof(buf), format, c);
85 8123 : return os << buf;
86 : }
87 :
88 45504 : std::ostream& PrintUC16ForJSON(std::ostream& os, uint16_t c,
89 : bool (*pred)(uint16_t)) {
90 : // JSON does not allow \x99; must use \u0099.
91 : char buf[10];
92 45504 : const char* format = pred(c) ? "%c" : "\\u%04x";
93 : snprintf(buf, sizeof(buf), format, c);
94 45504 : return os << buf;
95 : }
96 :
97 505 : std::ostream& PrintUC32(std::ostream& os, int32_t c, bool (*pred)(uint16_t)) {
98 505 : if (c <= String::kMaxUtf16CodeUnit) {
99 485 : return PrintUC16(os, static_cast<uint16_t>(c), pred);
100 : }
101 : char buf[13];
102 : snprintf(buf, sizeof(buf), "\\u{%06x}", c);
103 20 : return os << buf;
104 : }
105 :
106 : } // namespace
107 :
108 :
109 0 : std::ostream& operator<<(std::ostream& os, const AsReversiblyEscapedUC16& c) {
110 0 : return PrintUC16(os, c.value, IsOK);
111 : }
112 :
113 :
114 45701 : std::ostream& operator<<(std::ostream& os, const AsEscapedUC16ForJSON& c) {
115 45701 : if (c.value == '\n') return os << "\\n";
116 45504 : if (c.value == '\r') return os << "\\r";
117 45504 : if (c.value == '\t') return os << "\\t";
118 45504 : if (c.value == '\"') return os << "\\\"";
119 45504 : return PrintUC16ForJSON(os, c.value, IsOK);
120 : }
121 :
122 :
123 7638 : std::ostream& operator<<(std::ostream& os, const AsUC16& c) {
124 7638 : return PrintUC16(os, c.value, IsPrint);
125 : }
126 :
127 :
128 505 : std::ostream& operator<<(std::ostream& os, const AsUC32& c) {
129 505 : return PrintUC32(os, c.value, IsPrint);
130 : }
131 :
132 181612 : std::ostream& operator<<(std::ostream& os, const AsHex& hex) {
133 : // Each byte uses up to two characters. Plus two characters for the prefix,
134 : // plus null terminator.
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 181612 : hex.min_width, hex.value);
140 181612 : return os << buf;
141 : }
142 :
143 18 : std::ostream& operator<<(std::ostream& os, const AsHexBytes& hex) {
144 18 : uint8_t bytes = hex.min_bytes;
145 18 : while (bytes < sizeof(hex.value) && (hex.value >> (bytes * 8) != 0)) ++bytes;
146 42 : for (uint8_t b = 0; b < bytes; ++b) {
147 42 : if (b) os << " ";
148 : uint8_t printed_byte =
149 42 : hex.byte_order == AsHexBytes::kLittleEndian ? b : bytes - b - 1;
150 84 : os << AsHex((hex.value >> (8 * printed_byte)) & 0xFF, 2);
151 : }
152 18 : return os;
153 : }
154 :
155 : } // namespace internal
156 183867 : } // namespace v8
157 :
158 : #undef snprintf
159 : #undef LOG_TAG
|