log-utils.cc 5.19 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 6 7

#include "v8.h"

#include "log-utils.h"
8
#include "string-stream.h"
9 10 11 12

namespace v8 {
namespace internal {

13

14
const char* const Log::kLogToTemporaryFile = "&";
15
const char* const Log::kLogToConsole = "-";
16

17 18

Log::Log(Logger* logger)
19
  : is_stopped_(false),
20 21 22 23 24 25
    output_handle_(NULL),
    message_buffer_(NULL),
    logger_(logger) {
}


26
void Log::Initialize(const char* log_file_name) {
27
  message_buffer_ = NewArray<char>(kMessageBufferSize);
28 29 30 31 32 33 34 35 36

  // --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;
37
    FLAG_log_internal_timer_events = true;
38 39 40 41 42 43
  }

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

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


void Log::OpenStdout() {
  ASSERT(!IsEnabled());
  output_handle_ = stdout;
59 60 61 62 63 64
}


void Log::OpenTemporaryFile() {
  ASSERT(!IsEnabled());
  output_handle_ = i::OS::OpenTemporaryFile();
65 66 67 68 69 70 71 72 73
}


void Log::OpenFile(const char* name) {
  ASSERT(!IsEnabled());
  output_handle_ = OS::FOpen(name, OS::LogFileOpenMode);
}


74 75 76 77 78 79 80 81
FILE* Log::Close() {
  FILE* result = NULL;
  if (output_handle_ != NULL) {
    if (strcmp(FLAG_logfile, kLogToTemporaryFile) != 0) {
      fclose(output_handle_);
    } else {
      result = output_handle_;
    }
82
  }
83
  output_handle_ = NULL;
84

85 86 87
  DeleteArray(message_buffer_);
  message_buffer_ = NULL;

88
  is_stopped_ = false;
89
  return result;
90 91 92
}


93 94
Log::MessageBuilder::MessageBuilder(Log* log)
  : log_(log),
95
    lock_guard_(&log_->mutex_),
96 97
    pos_(0) {
  ASSERT(log_->message_buffer_ != NULL);
98 99 100
}


101
void Log::MessageBuilder::Append(const char* format, ...) {
102
  Vector<char> buf(log_->message_buffer_ + pos_,
103 104 105
                   Log::kMessageBufferSize - pos_);
  va_list args;
  va_start(args, format);
106
  AppendVA(format, args);
107 108 109 110 111
  va_end(args);
  ASSERT(pos_ <= Log::kMessageBufferSize);
}


112
void Log::MessageBuilder::AppendVA(const char* format, va_list args) {
113
  Vector<char> buf(log_->message_buffer_ + pos_,
114 115 116 117 118 119 120 121 122 123 124 125 126
                   Log::kMessageBufferSize - pos_);
  int result = v8::internal::OS::VSNPrintF(buf, format, args);

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


127
void Log::MessageBuilder::Append(const char c) {
128
  if (pos_ < Log::kMessageBufferSize) {
129
    log_->message_buffer_[pos_++] = c;
130 131 132 133 134
  }
  ASSERT(pos_ <= Log::kMessageBufferSize);
}


135
void Log::MessageBuilder::AppendDoubleQuotedString(const char* string) {
136 137 138 139 140 141 142 143 144 145 146
  Append('"');
  for (const char* p = string; *p != '\0'; p++) {
    if (*p == '"') {
      Append('\\');
    }
    Append(*p);
  }
  Append('"');
}


147
void Log::MessageBuilder::Append(String* str) {
148
  DisallowHeapAllocation no_gc;  // Ensure string stay valid.
149 150 151 152 153 154 155
  int length = str->length();
  for (int i = 0; i < length; i++) {
    Append(static_cast<char>(str->Get(i)));
  }
}


156
void Log::MessageBuilder::AppendAddress(Address addr) {
157
  Append("0x%" V8PRIxPTR, addr);
158 159 160
}


161 162 163 164 165 166 167 168 169 170 171 172 173
void Log::MessageBuilder::AppendSymbolName(Symbol* symbol) {
  ASSERT(symbol);
  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) {
174
  if (str == NULL) return;
175
  DisallowHeapAllocation no_gc;  // Ensure string stay valid.
176 177 178 179
  int len = str->length();
  if (len > 0x1000)
    len = 0x1000;
  if (show_impl_info) {
180
    Append(str->IsOneByteRepresentation() ? 'a' : '2');
181 182
    if (StringShape(str).IsExternal())
      Append('e');
183
    if (StringShape(str).IsInternalized())
184 185 186 187 188 189 190 191 192 193 194 195 196
      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("\\\\");
197 198
    } else if (c == '\"') {
      Append("\"\"");
199 200 201 202 203 204 205
    } else {
      Append("%lc", c);
    }
  }
}


206
void Log::MessageBuilder::AppendStringPart(const char* str, int len) {
207 208 209 210 211
  if (pos_ + len > Log::kMessageBufferSize) {
    len = Log::kMessageBufferSize - pos_;
    ASSERT(len >= 0);
    if (len == 0) return;
  }
212
  Vector<char> buf(log_->message_buffer_ + pos_,
213 214
                   Log::kMessageBufferSize - pos_);
  OS::StrNCpy(buf, str, len);
215 216 217 218 219
  pos_ += len;
  ASSERT(pos_ <= Log::kMessageBufferSize);
}


220
void Log::MessageBuilder::WriteToLogFile() {
221
  ASSERT(pos_ <= Log::kMessageBufferSize);
222
  const int written = log_->WriteToFile(log_->message_buffer_, pos_);
223 224 225
  if (written != pos_) {
    log_->stop();
    log_->logger_->LogFailure();
226
  }
227 228
}

229

230
} }  // namespace v8::internal