2014-06-27 08:42:17 +00:00
|
|
|
// Copyright 2014 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#ifndef V8_OSTREAMS_H_
|
|
|
|
#define V8_OSTREAMS_H_
|
|
|
|
|
2014-09-30 10:29:32 +00:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
|
|
|
#include <ostream> // NOLINT
|
|
|
|
#include <streambuf>
|
2014-06-27 08:42:17 +00:00
|
|
|
|
|
|
|
#include "include/v8config.h"
|
|
|
|
#include "src/base/macros.h"
|
2016-09-20 12:06:34 +00:00
|
|
|
#include "src/globals.h"
|
2014-06-27 08:42:17 +00:00
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
2018-10-03 11:51:27 +00:00
|
|
|
class V8_EXPORT_PRIVATE OFStreamBase : public std::streambuf {
|
2015-01-19 14:41:39 +00:00
|
|
|
public:
|
2014-09-30 10:29:32 +00:00
|
|
|
explicit OFStreamBase(FILE* f);
|
2018-09-19 07:10:17 +00:00
|
|
|
~OFStreamBase() override = default;
|
2014-06-27 08:42:17 +00:00
|
|
|
|
2015-01-19 14:41:39 +00:00
|
|
|
protected:
|
2014-09-30 10:29:32 +00:00
|
|
|
FILE* const f_;
|
2014-06-27 08:42:17 +00:00
|
|
|
|
2018-06-14 12:46:07 +00:00
|
|
|
int sync() override;
|
|
|
|
int_type overflow(int_type c) override;
|
|
|
|
std::streamsize xsputn(const char* s, std::streamsize n) override;
|
2014-06-27 08:42:17 +00:00
|
|
|
};
|
|
|
|
|
2019-01-28 16:50:36 +00:00
|
|
|
// Output buffer and stream writing into debugger's command window.
|
|
|
|
class V8_EXPORT_PRIVATE DbgStreamBuf : public std::streambuf {
|
|
|
|
public:
|
|
|
|
DbgStreamBuf();
|
|
|
|
~DbgStreamBuf();
|
|
|
|
|
|
|
|
private:
|
|
|
|
int sync() override;
|
|
|
|
int overflow(int c) override;
|
|
|
|
|
|
|
|
char data_[256];
|
|
|
|
};
|
|
|
|
|
|
|
|
class DbgStdoutStream : public std::ostream {
|
|
|
|
public:
|
|
|
|
DbgStdoutStream();
|
|
|
|
~DbgStdoutStream() = default;
|
|
|
|
|
|
|
|
private:
|
|
|
|
DbgStreamBuf streambuf_;
|
|
|
|
};
|
|
|
|
|
2014-06-27 08:42:17 +00:00
|
|
|
// An output stream writing to a file.
|
2016-09-20 12:06:34 +00:00
|
|
|
class V8_EXPORT_PRIVATE OFStream : public std::ostream {
|
2014-06-27 08:42:17 +00:00
|
|
|
public:
|
2014-09-30 10:29:32 +00:00
|
|
|
explicit OFStream(FILE* f);
|
2018-09-19 07:10:17 +00:00
|
|
|
~OFStream() override = default;
|
2014-06-27 08:42:17 +00:00
|
|
|
|
|
|
|
private:
|
2015-01-19 14:41:39 +00:00
|
|
|
OFStreamBase buf_;
|
2014-06-27 08:42:17 +00:00
|
|
|
};
|
|
|
|
|
2018-06-14 12:46:07 +00:00
|
|
|
#if defined(ANDROID) && !defined(V8_ANDROID_LOG_STDOUT)
|
|
|
|
class V8_EXPORT_PRIVATE AndroidLogStream : public std::streambuf {
|
|
|
|
public:
|
|
|
|
virtual ~AndroidLogStream();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
std::streamsize xsputn(const char* s, std::streamsize n) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string line_buffer_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class StdoutStream : public std::ostream {
|
|
|
|
public:
|
|
|
|
StdoutStream() : std::ostream(&stream_) {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
AndroidLogStream stream_;
|
|
|
|
};
|
|
|
|
#else
|
|
|
|
class StdoutStream : public OFStream {
|
|
|
|
public:
|
|
|
|
StdoutStream() : OFStream(stdout) {}
|
|
|
|
};
|
|
|
|
#endif
|
2014-07-07 09:57:29 +00:00
|
|
|
|
2014-08-28 07:30:58 +00:00
|
|
|
// Wrappers to disambiguate uint16_t and uc16.
|
2014-07-07 09:57:29 +00:00
|
|
|
struct AsUC16 {
|
|
|
|
explicit AsUC16(uint16_t v) : value(v) {}
|
|
|
|
uint16_t value;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-01-21 13:33:02 +00:00
|
|
|
struct AsUC32 {
|
|
|
|
explicit AsUC32(int32_t v) : value(v) {}
|
|
|
|
int32_t value;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-08-28 07:30:58 +00:00
|
|
|
struct AsReversiblyEscapedUC16 {
|
|
|
|
explicit AsReversiblyEscapedUC16(uint16_t v) : value(v) {}
|
|
|
|
uint16_t value;
|
|
|
|
};
|
|
|
|
|
2015-01-29 12:14:55 +00:00
|
|
|
struct AsEscapedUC16ForJSON {
|
|
|
|
explicit AsEscapedUC16ForJSON(uint16_t v) : value(v) {}
|
|
|
|
uint16_t value;
|
|
|
|
};
|
|
|
|
|
2017-07-24 18:03:03 +00:00
|
|
|
// Output the given value as hex, with a minimum width and optional prefix (0x).
|
|
|
|
// E.g. AsHex(23, 3, true) produces "0x017". Produces an empty string if both
|
|
|
|
// {min_width} and the value are 0.
|
2016-06-16 07:48:32 +00:00
|
|
|
struct AsHex {
|
2017-07-24 18:03:03 +00:00
|
|
|
explicit AsHex(uint64_t v, uint8_t min_width = 1, bool with_prefix = false)
|
|
|
|
: value(v), min_width(min_width), with_prefix(with_prefix) {}
|
2016-06-16 07:48:32 +00:00
|
|
|
uint64_t value;
|
|
|
|
uint8_t min_width;
|
2017-07-24 18:03:03 +00:00
|
|
|
bool with_prefix;
|
2019-02-27 12:15:11 +00:00
|
|
|
|
|
|
|
static AsHex Address(Address a) {
|
|
|
|
return AsHex(a, kSystemPointerHexDigits, true);
|
|
|
|
}
|
2017-07-24 18:03:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Output the given value as hex, separated in individual bytes.
|
|
|
|
// E.g. AsHexBytes(0x231712, 4) produces "12 17 23 00" if output as little
|
|
|
|
// endian (default), and "00 23 17 12" as big endian. Produces an empty string
|
|
|
|
// if both {min_bytes} and the value are 0.
|
|
|
|
struct AsHexBytes {
|
|
|
|
enum ByteOrder { kLittleEndian, kBigEndian };
|
|
|
|
explicit AsHexBytes(uint64_t v, uint8_t min_bytes = 1,
|
|
|
|
ByteOrder byte_order = kLittleEndian)
|
|
|
|
: value(v), min_bytes(min_bytes), byte_order(byte_order) {}
|
|
|
|
uint64_t value;
|
|
|
|
uint8_t min_bytes;
|
|
|
|
ByteOrder byte_order;
|
2016-06-16 07:48:32 +00:00
|
|
|
};
|
2014-08-28 07:30:58 +00:00
|
|
|
|
2018-06-27 10:00:29 +00:00
|
|
|
template <typename T>
|
|
|
|
struct PrintIteratorRange {
|
|
|
|
T start;
|
|
|
|
T end;
|
|
|
|
PrintIteratorRange(T start, T end) : start(start), end(end) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Print any collection which can be iterated via std::begin and std::end.
|
|
|
|
// {Iterator} is the common type of {std::begin} and {std::end} called on a
|
|
|
|
// {const T&}. This function is only instantiable if that type exists.
|
|
|
|
template <typename T, typename Iterator = typename std::common_type<
|
|
|
|
decltype(std::begin(std::declval<const T&>())),
|
|
|
|
decltype(std::end(std::declval<const T&>()))>::type>
|
|
|
|
PrintIteratorRange<Iterator> PrintCollection(const T& collection) {
|
|
|
|
return {std::begin(collection), std::end(collection)};
|
|
|
|
}
|
|
|
|
|
2014-09-09 10:33:33 +00:00
|
|
|
// Writes the given character to the output escaping everything outside of
|
|
|
|
// printable/space ASCII range. Additionally escapes '\' making escaping
|
2014-08-28 07:30:58 +00:00
|
|
|
// reversible.
|
2014-09-30 10:29:32 +00:00
|
|
|
std::ostream& operator<<(std::ostream& os, const AsReversiblyEscapedUC16& c);
|
2014-08-28 07:30:58 +00:00
|
|
|
|
2015-01-29 12:14:55 +00:00
|
|
|
// Same as AsReversiblyEscapedUC16 with additional escaping of \n, \r, " and '.
|
2016-10-14 08:55:40 +00:00
|
|
|
V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
|
|
|
|
const AsEscapedUC16ForJSON& c);
|
2015-01-29 12:14:55 +00:00
|
|
|
|
2014-08-28 07:30:58 +00:00
|
|
|
// Writes the given character to the output escaping everything outside
|
|
|
|
// of printable ASCII range.
|
2014-09-30 10:29:32 +00:00
|
|
|
std::ostream& operator<<(std::ostream& os, const AsUC16& c);
|
|
|
|
|
2016-01-21 13:33:02 +00:00
|
|
|
// Writes the given character to the output escaping everything outside
|
|
|
|
// of printable ASCII range.
|
|
|
|
std::ostream& operator<<(std::ostream& os, const AsUC32& c);
|
|
|
|
|
2017-07-24 18:03:03 +00:00
|
|
|
V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os, const AsHex& v);
|
|
|
|
V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
|
|
|
|
const AsHexBytes& v);
|
2016-06-16 07:48:32 +00:00
|
|
|
|
2018-06-27 10:00:29 +00:00
|
|
|
template <typename T>
|
|
|
|
std::ostream& operator<<(std::ostream& os, const PrintIteratorRange<T>& range) {
|
|
|
|
const char* comma = "";
|
|
|
|
os << "[";
|
|
|
|
for (T it = range.start; it != range.end; ++it, comma = ", ") {
|
|
|
|
os << comma << *it;
|
|
|
|
}
|
|
|
|
os << "]";
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2014-09-30 10:29:32 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|
2014-06-27 08:42:17 +00:00
|
|
|
|
|
|
|
#endif // V8_OSTREAMS_H_
|