ostreams.cc 5.53 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
#include "src/utils/ostreams.h"
6 7 8

#include <cinttypes>

9
#include "src/base/lazy-instance.h"
10
#include "src/objects/objects.h"
11
#include "src/objects/string.h"
12

13
#if V8_OS_WIN
14
#include <windows.h>
15
#if _MSC_VER < 1900
16 17
#define snprintf sprintf_s
#endif
18
#endif
19

20 21 22 23 24
#if defined(ANDROID) && !defined(V8_ANDROID_LOG_STDOUT)
#define LOG_TAG "v8"
#include <android/log.h>  // NOLINT
#endif

25 26 27
namespace v8 {
namespace internal {

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
DbgStreamBuf::DbgStreamBuf() { setp(data_, data_ + sizeof(data_)); }

DbgStreamBuf::~DbgStreamBuf() { sync(); }

int DbgStreamBuf::overflow(int c) {
#if V8_OS_WIN
  if (!IsDebuggerPresent()) {
    return 0;
  }

  sync();

  if (c != EOF) {
    if (pbase() == epptr()) {
      auto as_char = static_cast<char>(c);
      OutputDebugStringA(&as_char);
    } else {
      sputc(static_cast<char>(c));
    }
  }
#endif
  return 0;
}

int DbgStreamBuf::sync() {
#if V8_OS_WIN
  if (!IsDebuggerPresent()) {
    return 0;
  }

  if (pbase() != pptr()) {
59 60 61
    OutputDebugStringA(std::string(pbase(), static_cast<std::string::size_type>(
                                                pptr() - pbase()))
                           .c_str());
62 63 64 65 66 67 68 69
    setp(pbase(), epptr());
  }
#endif
  return 0;
}

DbgStdoutStream::DbgStdoutStream() : std::ostream(&streambuf_) {}

70
OFStreamBase::OFStreamBase(FILE* f) : f_(f) {}
71

svenpanne's avatar
svenpanne committed
72
int OFStreamBase::sync() {
73 74 75
  std::fflush(f_);
  return 0;
}
76

77 78
OFStreamBase::int_type OFStreamBase::overflow(int_type c) {
  return (c != EOF) ? std::fputc(c, f_) : c;
79 80
}

svenpanne's avatar
svenpanne committed
81 82 83 84 85 86
std::streamsize OFStreamBase::xsputn(const char* s, std::streamsize n) {
  return static_cast<std::streamsize>(
      std::fwrite(s, 1, static_cast<size_t>(n), f_));
}

OFStream::OFStream(FILE* f) : std::ostream(nullptr), buf_(f) {
87
  DCHECK_NOT_NULL(f);
svenpanne's avatar
svenpanne committed
88
  rdbuf(&buf_);
89
}
90

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
#if defined(ANDROID) && !defined(V8_ANDROID_LOG_STDOUT)
AndroidLogStream::~AndroidLogStream() {
  // If there is anything left in the line buffer, print it now, even though it
  // was not terminated by a newline.
  if (!line_buffer_.empty()) {
    __android_log_write(ANDROID_LOG_INFO, LOG_TAG, line_buffer_.c_str());
  }
}

std::streamsize AndroidLogStream::xsputn(const char* s, std::streamsize n) {
  const char* const e = s + n;
  while (s < e) {
    const char* newline = reinterpret_cast<const char*>(memchr(s, '\n', e - s));
    size_t line_chars = (newline ? newline : e) - s;
    line_buffer_.append(s, line_chars);
    // Without terminating newline, keep the characters in the buffer for the
    // next invocation.
    if (!newline) break;
    // Otherwise, write out the first line, then continue.
    __android_log_write(ANDROID_LOG_INFO, LOG_TAG, line_buffer_.c_str());
    line_buffer_.clear();
    s = newline + 1;
  }
  return n;
}
#endif
117

118 119 120
DEFINE_LAZY_LEAKY_OBJECT_GETTER(base::RecursiveMutex,
                                StdoutStream::GetStdoutMutex)

121
namespace {
122

123
// Locale-independent predicates.
124 125
bool IsPrint(uint16_t c) { return 0x20 <= c && c <= 0x7E; }
bool IsSpace(uint16_t c) { return (0x9 <= c && c <= 0xD) || c == 0x20; }
126
bool IsOK(uint16_t c) { return (IsPrint(c) || IsSpace(c)) && c != '\\'; }
127

128
std::ostream& PrintUC16(std::ostream& os, uint16_t c, bool (*pred)(uint16_t)) {
129
  char buf[10];
130
  const char* format = pred(c) ? "%c" : (c <= 0xFF) ? "\\x%02x" : "\\u%04x";
131
  snprintf(buf, sizeof(buf), format, c);
132 133 134
  return os << buf;
}

135 136 137 138 139 140 141 142
std::ostream& PrintUC16ForJSON(std::ostream& os, uint16_t c,
                               bool (*pred)(uint16_t)) {
  // JSON does not allow \x99; must use \u0099.
  char buf[10];
  const char* format = pred(c) ? "%c" : "\\u%04x";
  snprintf(buf, sizeof(buf), format, c);
  return os << buf;
}
143 144 145 146 147 148 149 150 151 152

std::ostream& PrintUC32(std::ostream& os, int32_t c, bool (*pred)(uint16_t)) {
  if (c <= String::kMaxUtf16CodeUnit) {
    return PrintUC16(os, static_cast<uint16_t>(c), pred);
  }
  char buf[13];
  snprintf(buf, sizeof(buf), "\\u{%06x}", c);
  return os << buf;
}

153
}  // namespace
154

155
std::ostream& operator<<(std::ostream& os, const AsReversiblyEscapedUC16& c) {
156 157 158
  return PrintUC16(os, c.value, IsOK);
}

danno's avatar
danno committed
159 160 161
std::ostream& operator<<(std::ostream& os, const AsEscapedUC16ForJSON& c) {
  if (c.value == '\n') return os << "\\n";
  if (c.value == '\r') return os << "\\r";
162
  if (c.value == '\t') return os << "\\t";
danno's avatar
danno committed
163
  if (c.value == '\"') return os << "\\\"";
164
  return PrintUC16ForJSON(os, c.value, IsOK);
danno's avatar
danno committed
165 166
}

167
std::ostream& operator<<(std::ostream& os, const AsUC16& c) {
168
  return PrintUC16(os, c.value, IsPrint);
169
}
170

171 172 173 174
std::ostream& operator<<(std::ostream& os, const AsUC32& c) {
  return PrintUC32(os, c.value, IsPrint);
}

175
std::ostream& operator<<(std::ostream& os, const AsHex& hex) {
176 177 178 179 180 181 182
  // Each byte uses up to two characters. Plus two characters for the prefix,
  // plus null terminator.
  DCHECK_GE(sizeof(hex.value) * 2, hex.min_width);
  static constexpr size_t kMaxHexLength = 3 + sizeof(hex.value) * 2;
  char buf[kMaxHexLength];
  snprintf(buf, kMaxHexLength, "%s%.*" PRIx64, hex.with_prefix ? "0x" : "",
           hex.min_width, hex.value);
183 184 185
  return os << buf;
}

186 187 188 189 190 191 192
std::ostream& operator<<(std::ostream& os, const AsHexBytes& hex) {
  uint8_t bytes = hex.min_bytes;
  while (bytes < sizeof(hex.value) && (hex.value >> (bytes * 8) != 0)) ++bytes;
  for (uint8_t b = 0; b < bytes; ++b) {
    if (b) os << " ";
    uint8_t printed_byte =
        hex.byte_order == AsHexBytes::kLittleEndian ? b : bytes - b - 1;
193
    os << AsHex((hex.value >> (8 * printed_byte)) & 0xFF, 2);
194 195 196 197
  }
  return os;
}

198 199
}  // namespace internal
}  // namespace v8
200 201 202

#undef snprintf
#undef LOG_TAG