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

  // --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;
40
    FLAG_log_internal_timer_events = true;
41 42 43 44 45 46
  }

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

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

    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();
    }
63
  }
64 65 66 67
}


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


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


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


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

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

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


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


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


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

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


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


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


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

166 167 168
void Log::MessageBuilder::AppendAddress(Address addr) {
  Append("%p", static_cast<void*>(addr));
}
169

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


void Log::MessageBuilder::AppendDetailed(String* str, bool show_impl_info) {
183
  if (str == NULL) return;
184
  DisallowHeapAllocation no_gc;  // Ensure string stay valid.
185 186 187 188
  int len = str->length();
  if (len > 0x1000)
    len = 0x1000;
  if (show_impl_info) {
189
    Append(str->IsOneByteRepresentation() ? 'a' : '2');
190 191
    if (StringShape(str).IsExternal())
      Append('e');
192
    if (StringShape(str).IsInternalized())
193 194 195 196 197 198 199 200 201 202 203 204 205
      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("\\\\");
206 207
    } else if (c == '\"') {
      Append("\"\"");
208 209 210 211 212 213 214
    } else {
      Append("%lc", c);
    }
  }
}


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


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

242

243 244
}  // namespace internal
}  // namespace v8