ostreams.h 5.65 KB
Newer Older
1 2 3 4
// 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.

5 6
#ifndef V8_UTILS_OSTREAMS_H_
#define V8_UTILS_OSTREAMS_H_
7

8 9 10
#include <cstddef>
#include <cstdio>
#include <cstring>
11
#include <ostream>
12
#include <streambuf>
13 14 15

#include "include/v8config.h"
#include "src/base/macros.h"
16
#include "src/base/platform/mutex.h"
17
#include "src/base/strings.h"
18
#include "src/common/globals.h"
19 20 21 22

namespace v8 {
namespace internal {

23
class V8_EXPORT_PRIVATE OFStreamBase : public std::streambuf {
svenpanne's avatar
svenpanne committed
24
 public:
25
  explicit OFStreamBase(FILE* f);
26
  ~OFStreamBase() override = default;
27

svenpanne's avatar
svenpanne committed
28
 protected:
29
  FILE* const f_;
30

31 32 33
  int sync() override;
  int_type overflow(int_type c) override;
  std::streamsize xsputn(const char* s, std::streamsize n) override;
34 35
};

36 37 38 39
// Output buffer and stream writing into debugger's command window.
class V8_EXPORT_PRIVATE DbgStreamBuf : public std::streambuf {
 public:
  DbgStreamBuf();
40
  ~DbgStreamBuf() override;
41 42 43 44 45 46 47 48 49 50 51

 private:
  int sync() override;
  int overflow(int c) override;

  char data_[256];
};

class DbgStdoutStream : public std::ostream {
 public:
  DbgStdoutStream();
52
  ~DbgStdoutStream() override = default;
53 54 55 56 57

 private:
  DbgStreamBuf streambuf_;
};

58
// An output stream writing to a file.
59
class V8_EXPORT_PRIVATE OFStream : public std::ostream {
60
 public:
61
  explicit OFStream(FILE* f);
62
  ~OFStream() override = default;
63 64

 private:
svenpanne's avatar
svenpanne committed
65
  OFStreamBase buf_;
66 67
};

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
#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:
85 86
  static V8_EXPORT_PRIVATE base::RecursiveMutex* GetStdoutMutex();

87
  AndroidLogStream stream_;
88
  base::RecursiveMutexGuard mutex_guard_{GetStdoutMutex()};
89 90 91 92 93
};
#else
class StdoutStream : public OFStream {
 public:
  StdoutStream() : OFStream(stdout) {}
94 95 96 97 98

 private:
  static V8_EXPORT_PRIVATE base::RecursiveMutex* GetStdoutMutex();

  base::RecursiveMutexGuard mutex_guard_{GetStdoutMutex()};
99 100
};
#endif
101

102
// Wrappers to disambiguate uint16_t and base::uc16.
103 104 105 106 107
struct AsUC16 {
  explicit AsUC16(uint16_t v) : value(v) {}
  uint16_t value;
};

108 109 110 111 112
struct AsUC32 {
  explicit AsUC32(int32_t v) : value(v) {}
  int32_t value;
};

113 114 115 116 117
struct AsReversiblyEscapedUC16 {
  explicit AsReversiblyEscapedUC16(uint16_t v) : value(v) {}
  uint16_t value;
};

danno's avatar
danno committed
118 119 120 121 122
struct AsEscapedUC16ForJSON {
  explicit AsEscapedUC16ForJSON(uint16_t v) : value(v) {}
  uint16_t value;
};

123 124 125
// 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.
126
struct AsHex {
127 128
  explicit AsHex(uint64_t v, uint8_t min_width = 1, bool with_prefix = false)
      : value(v), min_width(min_width), with_prefix(with_prefix) {}
129 130
  uint64_t value;
  uint8_t min_width;
131
  bool with_prefix;
132 133 134 135

  static AsHex Address(Address a) {
    return AsHex(a, kSystemPointerHexDigits, true);
  }
136 137 138 139 140 141 142 143 144 145 146 147 148 149
};

// 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;
150
};
151

152 153 154 155 156 157 158 159 160 161
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.
162 163 164 165
template <typename T>
auto PrintCollection(const T& collection) -> PrintIteratorRange<
    typename std::common_type<decltype(std::begin(collection)),
                              decltype(std::end(collection))>::type> {
166 167 168
  return {std::begin(collection), std::end(collection)};
}

169 170
// Writes the given character to the output escaping everything outside of
// printable/space ASCII range. Additionally escapes '\' making escaping
171
// reversible.
172
std::ostream& operator<<(std::ostream& os, const AsReversiblyEscapedUC16& c);
173

danno's avatar
danno committed
174
// Same as AsReversiblyEscapedUC16 with additional escaping of \n, \r, " and '.
175 176
V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
                                           const AsEscapedUC16ForJSON& c);
danno's avatar
danno committed
177

178 179
// Writes the given character to the output escaping everything outside
// of printable ASCII range.
180 181
std::ostream& operator<<(std::ostream& os, const AsUC16& c);

182 183 184 185
// Writes the given character to the output escaping everything outside
// of printable ASCII range.
std::ostream& operator<<(std::ostream& os, const AsUC32& c);

186 187 188
V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os, const AsHex& v);
V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
                                           const AsHexBytes& v);
189

190 191 192 193 194 195 196 197 198 199 200
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;
}

201 202
}  // namespace internal
}  // namespace v8
203

204
#endif  // V8_UTILS_OSTREAMS_H_