log-utils.cc 5.84 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/version.h"
13 14 15 16

namespace v8 {
namespace internal {

17

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

21 22

Log::Log(Logger* logger)
23
  : is_stopped_(false),
24 25 26 27 28 29
    output_handle_(NULL),
    message_buffer_(NULL),
    logger_(logger) {
}


30
void Log::Initialize(const char* log_file_name) {
31
  message_buffer_ = NewArray<char>(kMessageBufferSize);
32 33 34 35 36 37 38 39 40

  // --log-all enables all the log flags.
  if (FLAG_log_all) {
    FLAG_log_api = true;
    FLAG_log_code = true;
    FLAG_log_gc = true;
    FLAG_log_suspect = true;
    FLAG_log_handles = true;
    FLAG_log_regexp = true;
41
    FLAG_log_internal_timer_events = true;
42 43 44 45 46 47
  }

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

  // If we're logging anything, we need to open the log file.
48
  if (Log::InitLogAtStart()) {
49
    if (strcmp(log_file_name, kLogToConsole) == 0) {
50
      OpenStdout();
51
    } else if (strcmp(log_file_name, kLogToTemporaryFile) == 0) {
52 53
      OpenTemporaryFile();
    } else {
54
      OpenFile(log_file_name);
55
    }
56 57 58 59 60 61 62 63

    if (output_handle_ != nullptr) {
      Log::MessageBuilder msg(this);
      msg.Append("v8-version,%d,%d,%d,%d,%d", Version::GetMajor(),
                 Version::GetMinor(), Version::GetBuild(), Version::GetPatch(),
                 Version::IsCandidate());
      msg.WriteToLogFile();
    }
64
  }
65 66 67 68
}


void Log::OpenStdout() {
69
  DCHECK(!IsEnabled());
70
  output_handle_ = stdout;
71 72 73 74
}


void Log::OpenTemporaryFile() {
75
  DCHECK(!IsEnabled());
76
  output_handle_ = base::OS::OpenTemporaryFile();
77 78 79 80
}


void Log::OpenFile(const char* name) {
81
  DCHECK(!IsEnabled());
82
  output_handle_ = base::OS::FOpen(name, base::OS::LogFileOpenMode);
83 84 85
}


86 87 88 89 90 91 92 93
FILE* Log::Close() {
  FILE* result = NULL;
  if (output_handle_ != NULL) {
    if (strcmp(FLAG_logfile, kLogToTemporaryFile) != 0) {
      fclose(output_handle_);
    } else {
      result = output_handle_;
    }
94
  }
95
  output_handle_ = NULL;
96

97 98 99
  DeleteArray(message_buffer_);
  message_buffer_ = NULL;

100
  is_stopped_ = false;
101
  return result;
102 103 104
}


105 106
Log::MessageBuilder::MessageBuilder(Log* log)
  : log_(log),
107
    lock_guard_(&log_->mutex_),
108
    pos_(0) {
109
  DCHECK(log_->message_buffer_ != NULL);
110 111 112
}


113
void Log::MessageBuilder::Append(const char* format, ...) {
114
  Vector<char> buf(log_->message_buffer_ + pos_,
115 116 117
                   Log::kMessageBufferSize - pos_);
  va_list args;
  va_start(args, format);
118
  AppendVA(format, args);
119
  va_end(args);
120
  DCHECK(pos_ <= Log::kMessageBufferSize);
121 122 123
}


124
void Log::MessageBuilder::AppendVA(const char* format, va_list args) {
125
  Vector<char> buf(log_->message_buffer_ + pos_,
126
                   Log::kMessageBufferSize - pos_);
127
  int result = v8::internal::VSNPrintF(buf, format, args);
128 129 130 131 132 133 134

  // Result is -1 if output was truncated.
  if (result >= 0) {
    pos_ += result;
  } else {
    pos_ = Log::kMessageBufferSize;
  }
135
  DCHECK(pos_ <= Log::kMessageBufferSize);
136 137 138
}


139
void Log::MessageBuilder::Append(const char c) {
140
  if (pos_ < Log::kMessageBufferSize) {
141
    log_->message_buffer_[pos_++] = c;
142
  }
143
  DCHECK(pos_ <= Log::kMessageBufferSize);
144 145 146
}


147
void Log::MessageBuilder::AppendDoubleQuotedString(const char* string) {
148 149 150 151 152 153 154 155 156 157 158
  Append('"');
  for (const char* p = string; *p != '\0'; p++) {
    if (*p == '"') {
      Append('\\');
    }
    Append(*p);
  }
  Append('"');
}


159
void Log::MessageBuilder::Append(String* str) {
160
  DisallowHeapAllocation no_gc;  // Ensure string stay valid.
161 162 163 164 165 166 167
  int length = str->length();
  for (int i = 0; i < length; i++) {
    Append(static_cast<char>(str->Get(i)));
  }
}


168
void Log::MessageBuilder::AppendAddress(Address addr) {
169
  Append("0x%" V8PRIxPTR, addr);
170 171 172
}


173
void Log::MessageBuilder::AppendSymbolName(Symbol* symbol) {
174
  DCHECK(symbol);
175 176 177 178 179 180 181 182 183 184 185
  Append("symbol(");
  if (!symbol->name()->IsUndefined()) {
    Append("\"");
    AppendDetailed(String::cast(symbol->name()), false);
    Append("\" ");
  }
  Append("hash %x)", symbol->Hash());
}


void Log::MessageBuilder::AppendDetailed(String* str, bool show_impl_info) {
186
  if (str == NULL) return;
187
  DisallowHeapAllocation no_gc;  // Ensure string stay valid.
188 189 190 191
  int len = str->length();
  if (len > 0x1000)
    len = 0x1000;
  if (show_impl_info) {
192
    Append(str->IsOneByteRepresentation() ? 'a' : '2');
193 194
    if (StringShape(str).IsExternal())
      Append('e');
195
    if (StringShape(str).IsInternalized())
196 197 198 199 200 201 202 203 204 205 206 207 208
      Append('#');
    Append(":%i:", str->length());
  }
  for (int i = 0; i < len; i++) {
    uc32 c = str->Get(i);
    if (c > 0xff) {
      Append("\\u%04x", c);
    } else if (c < 32 || c > 126) {
      Append("\\x%02x", c);
    } else if (c == ',') {
      Append("\\,");
    } else if (c == '\\') {
      Append("\\\\");
209 210
    } else if (c == '\"') {
      Append("\"\"");
211 212 213 214 215 216 217
    } else {
      Append("%lc", c);
    }
  }
}


218
void Log::MessageBuilder::AppendStringPart(const char* str, int len) {
219 220
  if (pos_ + len > Log::kMessageBufferSize) {
    len = Log::kMessageBufferSize - pos_;
221
    DCHECK(len >= 0);
222 223
    if (len == 0) return;
  }
224
  Vector<char> buf(log_->message_buffer_ + pos_,
225
                   Log::kMessageBufferSize - pos_);
226
  StrNCpy(buf, str, len);
227
  pos_ += len;
228
  DCHECK(pos_ <= Log::kMessageBufferSize);
229 230 231
}


232
void Log::MessageBuilder::WriteToLogFile() {
233
  DCHECK(pos_ <= Log::kMessageBufferSize);
234
  // Assert that we do not already have a new line at the end.
235
  DCHECK(pos_ == 0 || log_->message_buffer_[pos_ - 1] != '\n');
236 237
  if (pos_ == Log::kMessageBufferSize) pos_--;
  log_->message_buffer_[pos_++] = '\n';
238
  const int written = log_->WriteToFile(log_->message_buffer_, pos_);
239 240 241
  if (written != pos_) {
    log_->stop();
    log_->logger_->LogFailure();
242
  }
243 244
}

245

246 247
}  // namespace internal
}  // namespace v8