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