log-utils.cc 7.48 KB
Newer Older
1
// Copyright 2009 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5
#include "src/log-utils.h"
6 7 8 9

#include "src/assert-scope.h"
#include "src/base/platform/platform.h"
#include "src/objects-inl.h"
10
#include "src/string-stream.h"
11
#include "src/utils.h"
12
#include "src/vector.h"
13
#include "src/version.h"
14 15 16 17

namespace v8 {
namespace internal {

18

19
const char* const Log::kLogToTemporaryFile = "&";
20
const char* const Log::kLogToConsole = "-";
21

22 23 24 25 26 27 28 29 30 31 32 33 34
// static
FILE* Log::CreateOutputHandle(const char* file_name) {
  // If we're logging anything, we need to open the log file.
  if (!Log::InitLogAtStart()) {
    return nullptr;
  } else if (strcmp(file_name, kLogToConsole) == 0) {
    return stdout;
  } else if (strcmp(file_name, kLogToTemporaryFile) == 0) {
    return base::OS::OpenTemporaryFile();
  } else {
    return base::OS::FOpen(file_name, base::OS::LogFileOpenMode);
  }
}
35

36 37 38 39 40 41
Log::Log(Logger* logger, const char* file_name)
    : is_stopped_(false),
      output_handle_(Log::CreateOutputHandle(file_name)),
      os_(output_handle_ == nullptr ? stdout : output_handle_),
      format_buffer_(NewArray<char>(kMessageBufferSize)),
      logger_(logger) {
42 43 44 45 46 47
  // --log-all enables all the log flags.
  if (FLAG_log_all) {
    FLAG_log_api = true;
    FLAG_log_code = true;
    FLAG_log_suspect = true;
    FLAG_log_handles = true;
48
    FLAG_log_internal_timer_events = true;
49
    FLAG_log_function_events = true;
50 51 52 53 54
  }

  // --prof implies --log-code.
  if (FLAG_prof) FLAG_log_code = true;

55 56 57 58 59 60 61 62
  if (output_handle_ == nullptr) return;
  Log::MessageBuilder msg(this);
  LogSeparator kNext = LogSeparator::kSeparator;
  msg << "v8-version" << kNext << Version::GetMajor() << kNext
      << Version::GetMinor() << kNext << Version::GetBuild() << kNext
      << Version::GetPatch();
  if (strlen(Version::GetEmbedder()) != 0) {
    msg << kNext << Version::GetEmbedder();
63
  }
64 65
  msg << kNext << Version::IsCandidate();
  msg.WriteToLogFile();
66 67
}

68
FILE* Log::Close() {
69 70
  FILE* result = nullptr;
  if (output_handle_ != nullptr) {
71 72 73 74 75
    if (strcmp(FLAG_logfile, kLogToTemporaryFile) != 0) {
      fclose(output_handle_);
    } else {
      result = output_handle_;
    }
76
  }
77
  output_handle_ = nullptr;
78

79 80
  DeleteArray(format_buffer_);
  format_buffer_ = nullptr;
81

82
  is_stopped_ = false;
83
  return result;
84 85
}

86 87
Log::MessageBuilder::MessageBuilder(Log* log)
    : log_(log), lock_guard_(&log_->mutex_) {
88
  DCHECK_NOT_NULL(log_->format_buffer_);
89 90
}

91
void Log::MessageBuilder::AppendString(String str,
92
                                       base::Optional<int> length_limit) {
93
  if (str.is_null()) return;
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

  DisallowHeapAllocation no_gc;  // Ensure string stays valid.
  int length = str->length();
  if (length_limit) length = std::min(length, *length_limit);
  for (int i = 0; i < length; i++) {
    uint16_t c = str->Get(i);
    if (c <= 0xFF) {
      AppendCharacter(static_cast<char>(c));
    } else {
      // Escape non-ascii characters.
      AppendRawFormatString("\\u%04x", c & 0xFFFF);
    }
  }
}

void Log::MessageBuilder::AppendString(Vector<const char> str) {
  for (auto i = str.begin(); i < str.end(); i++) AppendCharacter(*i);
}

void Log::MessageBuilder::AppendString(const char* str) {
  if (str == nullptr) return;
  AppendString(str, strlen(str));
}

void Log::MessageBuilder::AppendString(const char* str, size_t length) {
  if (str == nullptr) return;

  for (size_t i = 0; i < length; i++) {
    DCHECK_NE(str[i], '\0');
    AppendCharacter(str[i]);
  }
}
126

127
void Log::MessageBuilder::AppendFormatString(const char* format, ...) {
128 129
  va_list args;
  va_start(args, format);
130
  const int length = FormatStringIntoBuffer(format, args);
131
  va_end(args);
132 133 134 135
  for (int i = 0; i < length; i++) {
    DCHECK_NE(log_->format_buffer_[i], '\0');
    AppendCharacter(log_->format_buffer_[i]);
  }
136 137
}

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
void Log::MessageBuilder::AppendCharacter(char c) {
  if (c >= 32 && c <= 126) {
    if (c == ',') {
      // Escape commas to avoid adding column separators.
      AppendRawFormatString("\\x2C");
    } else if (c == '\\') {
      AppendRawFormatString("\\\\");
    } else {
      // Safe, printable ascii character.
      AppendRawCharacter(c);
    }
  } else if (c == '\n') {
    // Escape newlines to avoid adding row separators.
    AppendRawFormatString("\\n");
  } else {
    // Escape non-printable characters.
    AppendRawFormatString("\\x%02x", c & 0xFF);
  }
156 157
}

158 159
void Log::MessageBuilder::AppendSymbolName(Symbol symbol) {
  DCHECK(!symbol.is_null());
160 161
  OFStream& os = log_->os_;
  os << "symbol(";
162
  if (!symbol->name()->IsUndefined()) {
163
    os << "\"";
164
    AppendSymbolNameDetails(String::cast(symbol->name()), false);
165
    os << "\" ";
166
  }
167
  os << "hash " << std::hex << symbol->Hash() << std::dec << ")";
168 169
}

170
void Log::MessageBuilder::AppendSymbolNameDetails(String str,
171
                                                  bool show_impl_info) {
172
  if (str.is_null()) return;
173 174

  DisallowHeapAllocation no_gc;  // Ensure string stays valid.
175
  OFStream& os = log_->os_;
176 177
  int limit = str->length();
  if (limit > 0x1000) limit = 0x1000;
178
  if (show_impl_info) {
179 180 181 182
    os << (str->IsOneByteRepresentation() ? 'a' : '2');
    if (StringShape(str).IsExternal()) os << 'e';
    if (StringShape(str).IsInternalized()) os << '#';
    os << ':' << str->length() << ':';
183
  }
184
  AppendString(str, limit);
185 186
}

187 188 189 190 191 192 193 194 195
int Log::MessageBuilder::FormatStringIntoBuffer(const char* format,
                                                va_list args) {
  Vector<char> buf(log_->format_buffer_, Log::kMessageBufferSize);
  int length = v8::internal::VSNPrintF(buf, format, args);
  // |length| is -1 if output was truncated.
  if (length == -1) length = Log::kMessageBufferSize;
  DCHECK_LE(length, Log::kMessageBufferSize);
  DCHECK_GE(length, 0);
  return length;
196 197
}

198 199 200 201 202 203 204 205
void Log::MessageBuilder::AppendRawFormatString(const char* format, ...) {
  va_list args;
  va_start(args, format);
  const int length = FormatStringIntoBuffer(format, args);
  va_end(args);
  for (int i = 0; i < length; i++) {
    DCHECK_NE(log_->format_buffer_[i], '\0');
    AppendRawCharacter(log_->format_buffer_[i]);
206 207 208
  }
}

209
void Log::MessageBuilder::AppendRawCharacter(char c) { log_->os_ << c; }
210

211
void Log::MessageBuilder::WriteToLogFile() { log_->os_ << std::endl; }
212

213 214 215 216 217 218 219
template <>
Log::MessageBuilder& Log::MessageBuilder::operator<<<const char*>(
    const char* string) {
  this->AppendString(string);
  return *this;
}

220 221 222 223 224 225 226 227 228
template <>
Log::MessageBuilder& Log::MessageBuilder::operator<<<void*>(void* pointer) {
  OFStream& os = log_->os_;
  // Manually format the pointer since on Windows we do not consistently
  // get a "0x" prefix.
  os << "0x" << std::hex << reinterpret_cast<intptr_t>(pointer) << std::dec;
  return *this;
}

229 230 231 232 233 234 235
template <>
Log::MessageBuilder& Log::MessageBuilder::operator<<<char>(char c) {
  this->AppendCharacter(c);
  return *this;
}

template <>
236
Log::MessageBuilder& Log::MessageBuilder::operator<<<String>(String string) {
237 238 239 240 241
  this->AppendString(string);
  return *this;
}

template <>
242
Log::MessageBuilder& Log::MessageBuilder::operator<<<Symbol>(Symbol symbol) {
243 244 245 246 247
  this->AppendSymbolName(symbol);
  return *this;
}

template <>
248
Log::MessageBuilder& Log::MessageBuilder::operator<<<Name>(Name name) {
249 250 251 252 253 254 255 256 257 258 259
  if (name->IsString()) {
    this->AppendString(String::cast(name));
  } else {
    this->AppendSymbolName(Symbol::cast(name));
  }
  return *this;
}

template <>
Log::MessageBuilder& Log::MessageBuilder::operator<<<LogSeparator>(
    LogSeparator separator) {
260 261
  // Skip escaping to create a new column.
  this->AppendRawCharacter(',');
262 263 264
  return *this;
}

265 266
}  // namespace internal
}  // namespace v8