v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
ostreams.h
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
5#ifndef V8_UTILS_OSTREAMS_H_
6#define V8_UTILS_OSTREAMS_H_
7
8#include <cstddef>
9#include <cstdio>
10#include <cstring>
11#include <ostream>
12#include <streambuf>
13
14#include "src/base/macros.h"
16#include "src/common/globals.h"
17
18namespace v8 {
19namespace internal {
20
21class V8_EXPORT_PRIVATE OFStreamBase : public std::streambuf {
22 public:
23 explicit OFStreamBase(FILE* f);
24 ~OFStreamBase() override = default;
25
26 protected:
27 FILE* const f_;
28
29 int sync() override;
30 int_type overflow(int_type c) override;
31 std::streamsize xsputn(const char* s, std::streamsize n) override;
32};
33
34// Output buffer and stream writing into debugger's command window.
35class V8_EXPORT_PRIVATE DbgStreamBuf : public std::streambuf {
36 public:
38 ~DbgStreamBuf() override;
39
40 private:
41 int sync() override;
42 int overflow(int c) override;
43
44 char data_[256];
45};
46
47class DbgStdoutStream : public std::ostream {
48 public:
50 ~DbgStdoutStream() override = default;
51
52 private:
54};
55
56// An output stream writing to a file.
57class V8_EXPORT_PRIVATE OFStream : public std::ostream {
58 public:
59 explicit OFStream(FILE* f);
60 ~OFStream() override = default;
61
62 private:
64};
65
66#if defined(ANDROID) && !defined(V8_ANDROID_LOG_STDOUT)
67class V8_EXPORT_PRIVATE AndroidLogStream : public std::streambuf {
68 public:
69 virtual ~AndroidLogStream();
70
71 protected:
72 std::streamsize xsputn(const char* s, std::streamsize n) override;
73
74 private:
75 std::string line_buffer_;
76};
77
78class StdoutStream : public std::ostream {
79 public:
80 StdoutStream() : std::ostream(&stream_) {}
81
82 private:
83 friend class StderrStream;
84
85 static V8_EXPORT_PRIVATE base::RecursiveMutex* GetStdoutMutex();
86
87 AndroidLogStream stream_;
89};
90#else
101#endif
102
103class StderrStream : public OFStream {
104 public:
105 StderrStream() : OFStream(stderr) {}
106
107 private:
109};
110
111// Wrappers to disambiguate uint16_t and base::uc16.
112struct AsUC16 {
113 explicit AsUC16(uint16_t v) : value(v) {}
114 uint16_t value;
115};
116
117struct AsUC32 {
118 explicit AsUC32(int32_t v) : value(v) {}
119 int32_t value;
120};
121
123 explicit AsReversiblyEscapedUC16(uint16_t v) : value(v) {}
124 uint16_t value;
125};
126
128 explicit AsEscapedUC16ForJSON(uint16_t v) : value(v) {}
129 uint16_t value;
130};
131
132// Output the given value as hex, with a minimum width and optional prefix (0x).
133// E.g. AsHex(23, 3, true) produces "0x017". Produces an empty string if both
134// {min_width} and the value are 0.
135struct AsHex {
136 explicit AsHex(uint64_t v, uint8_t min_width = 1, bool with_prefix = false)
138 uint64_t value;
139 uint8_t min_width;
141
143 return AsHex(a, kSystemPointerHexDigits, true);
144 }
145};
146
147// Output the given value as hex, separated in individual bytes.
148// E.g. AsHexBytes(0x231712, 4) produces "12 17 23 00" if output as little
149// endian (default), and "00 23 17 12" as big endian. Produces an empty string
150// if both {min_bytes} and the value are 0.
153 explicit AsHexBytes(uint64_t v, uint8_t min_bytes = 1,
155 : value(v), min_bytes(min_bytes), byte_order(byte_order) {}
156 uint64_t value;
157 uint8_t min_bytes;
159};
160
161template <typename T>
162 requires requires(T t, std::ostream& os) { os << *t; }
166 const char* separator = ", ";
167 const char* startBracket = "[";
168 const char* endBracket = "]";
169
172 startBracket = "";
173 endBracket = "";
174 return *this;
175 }
176 PrintIteratorRange& WithSeparator(const char* new_separator) {
177 this->separator = new_separator;
178 return *this;
179 }
180};
181
182// Print any collection which can be iterated via std::begin and std::end.
183// {Iterator} is the common type of {std::begin} and {std::end} called on a
184// {const T&}. This function is only instantiable if that type exists.
185template <typename T>
186auto PrintCollection(const T& collection) -> PrintIteratorRange<
187 typename std::common_type<decltype(std::begin(collection)),
188 decltype(std::end(collection))>::type> {
189 return {std::begin(collection), std::end(collection)};
190}
191
192// Writes the given character to the output escaping everything outside of
193// printable/space ASCII range. Additionally escapes '\' making escaping
194// reversible.
195std::ostream& operator<<(std::ostream& os, const AsReversiblyEscapedUC16& c);
196
197// Same as AsReversiblyEscapedUC16 with additional escaping of \n, \r, " and '.
198V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
199 const AsEscapedUC16ForJSON& c);
200
201// Writes the given character to the output escaping everything outside
202// of printable ASCII range.
203std::ostream& operator<<(std::ostream& os, const AsUC16& c);
204
205// Writes the given character to the output escaping everything outside
206// of printable ASCII range.
207std::ostream& operator<<(std::ostream& os, const AsUC32& c);
208
209V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os, const AsHex& v);
210V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
211 const AsHexBytes& v);
212
213template <typename T>
214std::ostream& operator<<(std::ostream& os, const PrintIteratorRange<T>& range) {
215 const char* separator = "";
216 os << range.startBracket;
217 for (T it = range.start; it != range.end; ++it, separator = range.separator) {
218 os << separator << *it;
219 }
220 os << range.endBracket;
221 return os;
222}
223
224} // namespace internal
225} // namespace v8
226
227#endif // V8_UTILS_OSTREAMS_H_
uint8_t data_[MAX_STACK_LENGTH]
~DbgStdoutStream() override=default
~OFStreamBase() override=default
~OFStream() override=default
OFStreamBase buf_
Definition ostreams.h:63
base::RecursiveMutexGuard mutex_guard_
Definition ostreams.h:108
base::RecursiveMutexGuard mutex_guard_
Definition ostreams.h:99
friend class StderrStream
Definition ostreams.h:96
static V8_EXPORT_PRIVATE base::RecursiveMutex * GetStdoutMutex()
BalanceOverflow overflow
STL namespace.
LockGuard< RecursiveMutex > RecursiveMutexGuard
Definition mutex.h:220
constexpr int kSystemPointerHexDigits
Definition globals.h:411
std::ostream & operator<<(std::ostream &os, AtomicMemoryOrder order)
auto PrintCollection(const T &collection) -> PrintIteratorRange< typename std::common_type< decltype(std::begin(collection)), decltype(std::end(collection))>::type >
Definition ostreams.h:186
#define V8_EXPORT_PRIVATE
Definition macros.h:460
AsHexBytes(uint64_t v, uint8_t min_bytes=1, ByteOrder byte_order=kLittleEndian)
Definition ostreams.h:153
AsHex(uint64_t v, uint8_t min_width=1, bool with_prefix=false)
Definition ostreams.h:136
static AsHex Address(Address a)
Definition ostreams.h:142
AsUC16(uint16_t v)
Definition ostreams.h:113
AsUC32(int32_t v)
Definition ostreams.h:118
PrintIteratorRange & WithoutBrackets()
Definition ostreams.h:171
PrintIteratorRange & WithSeparator(const char *new_separator)
Definition ostreams.h:176
PrintIteratorRange(T start, T end)
Definition ostreams.h:170