sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 1 | // Copyright 2009 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include "v8.h" |
| 29 | |
| 30 | #include "log-utils.h" |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 31 | #include "string-stream.h" |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 32 | |
| 33 | namespace v8 { |
| 34 | namespace internal { |
| 35 | |
| 36 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 37 | |
| 38 | LogDynamicBuffer::LogDynamicBuffer( |
| 39 | int block_size, int max_size, const char* seal, int seal_size) |
| 40 | : block_size_(block_size), |
| 41 | max_size_(max_size - (max_size % block_size_)), |
| 42 | seal_(seal), |
| 43 | seal_size_(seal_size), |
| 44 | blocks_(max_size_ / block_size_ + 1), |
| 45 | write_pos_(0), block_index_(0), block_write_pos_(0), is_sealed_(false) { |
| 46 | ASSERT(BlocksCount() > 0); |
| 47 | AllocateBlock(0); |
| 48 | for (int i = 1; i < BlocksCount(); ++i) { |
| 49 | blocks_[i] = NULL; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | |
| 54 | LogDynamicBuffer::~LogDynamicBuffer() { |
| 55 | for (int i = 0; i < BlocksCount(); ++i) { |
| 56 | DeleteArray(blocks_[i]); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | |
| 61 | int LogDynamicBuffer::Read(int from_pos, char* dest_buf, int buf_size) { |
| 62 | if (buf_size == 0) return 0; |
| 63 | int read_pos = from_pos; |
| 64 | int block_read_index = BlockIndex(from_pos); |
| 65 | int block_read_pos = PosInBlock(from_pos); |
| 66 | int dest_buf_pos = 0; |
| 67 | // Read until dest_buf is filled, or write_pos_ encountered. |
| 68 | while (read_pos < write_pos_ && dest_buf_pos < buf_size) { |
| 69 | const int read_size = Min(write_pos_ - read_pos, |
| 70 | Min(buf_size - dest_buf_pos, block_size_ - block_read_pos)); |
| 71 | memcpy(dest_buf + dest_buf_pos, |
| 72 | blocks_[block_read_index] + block_read_pos, read_size); |
| 73 | block_read_pos += read_size; |
| 74 | dest_buf_pos += read_size; |
| 75 | read_pos += read_size; |
| 76 | if (block_read_pos == block_size_) { |
| 77 | block_read_pos = 0; |
| 78 | ++block_read_index; |
| 79 | } |
| 80 | } |
| 81 | return dest_buf_pos; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | int LogDynamicBuffer::Seal() { |
| 86 | WriteInternal(seal_, seal_size_); |
| 87 | is_sealed_ = true; |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | |
| 92 | int LogDynamicBuffer::Write(const char* data, int data_size) { |
| 93 | if (is_sealed_) { |
| 94 | return 0; |
| 95 | } |
| 96 | if ((write_pos_ + data_size) <= (max_size_ - seal_size_)) { |
| 97 | return WriteInternal(data, data_size); |
| 98 | } else { |
| 99 | return Seal(); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | |
| 104 | int LogDynamicBuffer::WriteInternal(const char* data, int data_size) { |
| 105 | int data_pos = 0; |
| 106 | while (data_pos < data_size) { |
| 107 | const int write_size = |
| 108 | Min(data_size - data_pos, block_size_ - block_write_pos_); |
| 109 | memcpy(blocks_[block_index_] + block_write_pos_, data + data_pos, |
| 110 | write_size); |
| 111 | block_write_pos_ += write_size; |
| 112 | data_pos += write_size; |
| 113 | if (block_write_pos_ == block_size_) { |
| 114 | block_write_pos_ = 0; |
| 115 | AllocateBlock(++block_index_); |
| 116 | } |
| 117 | } |
| 118 | write_pos_ += data_size; |
| 119 | return data_size; |
| 120 | } |
| 121 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 122 | // Must be the same message as in Logger::PauseProfiler. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 123 | const char* const Log::kDynamicBufferSeal = "profiler,\"pause\"\n"; |
| 124 | |
| 125 | Log::Log(Logger* logger) |
| 126 | : write_to_file_(false), |
| 127 | is_stopped_(false), |
| 128 | output_handle_(NULL), |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame^] | 129 | ll_output_handle_(NULL), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 130 | output_buffer_(NULL), |
| 131 | mutex_(NULL), |
| 132 | message_buffer_(NULL), |
| 133 | logger_(logger) { |
| 134 | } |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 135 | |
| 136 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 137 | static void AddIsolateIdIfNeeded(StringStream* stream) { |
| 138 | Isolate* isolate = Isolate::Current(); |
| 139 | if (isolate->IsDefaultIsolate()) return; |
| 140 | stream->Add("isolate-%p-", isolate); |
| 141 | } |
| 142 | |
| 143 | |
| 144 | void Log::Initialize() { |
| 145 | #ifdef ENABLE_LOGGING_AND_PROFILING |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 146 | mutex_ = OS::CreateMutex(); |
| 147 | message_buffer_ = NewArray<char>(kMessageBufferSize); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 148 | |
| 149 | // --log-all enables all the log flags. |
| 150 | if (FLAG_log_all) { |
| 151 | FLAG_log_runtime = true; |
| 152 | FLAG_log_api = true; |
| 153 | FLAG_log_code = true; |
| 154 | FLAG_log_gc = true; |
| 155 | FLAG_log_suspect = true; |
| 156 | FLAG_log_handles = true; |
| 157 | FLAG_log_regexp = true; |
| 158 | } |
| 159 | |
| 160 | // --prof implies --log-code. |
| 161 | if (FLAG_prof) FLAG_log_code = true; |
| 162 | |
| 163 | // --prof_lazy controls --log-code, implies --noprof_auto. |
| 164 | if (FLAG_prof_lazy) { |
| 165 | FLAG_log_code = false; |
| 166 | FLAG_prof_auto = false; |
| 167 | } |
| 168 | |
| 169 | bool start_logging = FLAG_log || FLAG_log_runtime || FLAG_log_api |
| 170 | || FLAG_log_code || FLAG_log_gc || FLAG_log_handles || FLAG_log_suspect |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame^] | 171 | || FLAG_log_regexp || FLAG_log_state_changes || FLAG_ll_prof; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 172 | |
| 173 | bool open_log_file = start_logging || FLAG_prof_lazy; |
| 174 | |
| 175 | // If we're logging anything, we need to open the log file. |
| 176 | if (open_log_file) { |
| 177 | if (strcmp(FLAG_logfile, "-") == 0) { |
| 178 | OpenStdout(); |
| 179 | } else if (strcmp(FLAG_logfile, "*") == 0) { |
| 180 | OpenMemoryBuffer(); |
| 181 | } else { |
| 182 | if (strchr(FLAG_logfile, '%') != NULL || |
| 183 | !Isolate::Current()->IsDefaultIsolate()) { |
| 184 | // If there's a '%' in the log file name we have to expand |
| 185 | // placeholders. |
| 186 | HeapStringAllocator allocator; |
| 187 | StringStream stream(&allocator); |
| 188 | AddIsolateIdIfNeeded(&stream); |
| 189 | for (const char* p = FLAG_logfile; *p; p++) { |
| 190 | if (*p == '%') { |
| 191 | p++; |
| 192 | switch (*p) { |
| 193 | case '\0': |
| 194 | // If there's a % at the end of the string we back up |
| 195 | // one character so we can escape the loop properly. |
| 196 | p--; |
| 197 | break; |
| 198 | case 't': { |
| 199 | // %t expands to the current time in milliseconds. |
| 200 | double time = OS::TimeCurrentMillis(); |
| 201 | stream.Add("%.0f", FmtElm(time)); |
| 202 | break; |
| 203 | } |
| 204 | case '%': |
| 205 | // %% expands (contracts really) to %. |
| 206 | stream.Put('%'); |
| 207 | break; |
| 208 | default: |
| 209 | // All other %'s expand to themselves. |
| 210 | stream.Put('%'); |
| 211 | stream.Put(*p); |
| 212 | break; |
| 213 | } |
| 214 | } else { |
| 215 | stream.Put(*p); |
| 216 | } |
| 217 | } |
| 218 | SmartPointer<const char> expanded = stream.ToCString(); |
| 219 | OpenFile(*expanded); |
| 220 | } else { |
| 221 | OpenFile(FLAG_logfile); |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | #endif |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | |
| 229 | void Log::OpenStdout() { |
| 230 | ASSERT(!IsEnabled()); |
| 231 | output_handle_ = stdout; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 232 | write_to_file_ = true; |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame^] | 236 | // Extension added to V8 log file name to get the low-level log name. |
| 237 | static const char kLowLevelLogExt[] = ".ll"; |
| 238 | |
| 239 | // File buffer size of the low-level log. We don't use the default to |
| 240 | // minimize the associated overhead. |
| 241 | static const int kLowLevelLogBufferSize = 2 * MB; |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 242 | |
| 243 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 244 | void Log::OpenFile(const char* name) { |
| 245 | ASSERT(!IsEnabled()); |
| 246 | output_handle_ = OS::FOpen(name, OS::LogFileOpenMode); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 247 | write_to_file_ = true; |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 248 | if (FLAG_ll_prof) { |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame^] | 249 | // Open the low-level log file. |
| 250 | size_t len = strlen(name); |
| 251 | ScopedVector<char> ll_name(static_cast<int>(len + sizeof(kLowLevelLogExt))); |
| 252 | memcpy(ll_name.start(), name, len); |
| 253 | memcpy(ll_name.start() + len, kLowLevelLogExt, sizeof(kLowLevelLogExt)); |
| 254 | ll_output_handle_ = OS::FOpen(ll_name.start(), OS::LogFileOpenMode); |
| 255 | setvbuf(ll_output_handle_, NULL, _IOFBF, kLowLevelLogBufferSize); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 256 | } |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | |
| 260 | void Log::OpenMemoryBuffer() { |
| 261 | ASSERT(!IsEnabled()); |
| 262 | output_buffer_ = new LogDynamicBuffer( |
| 263 | kDynamicBufferBlockSize, kMaxDynamicBufferSize, |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 264 | kDynamicBufferSeal, StrLength(kDynamicBufferSeal)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 265 | write_to_file_ = false; |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | |
| 269 | void Log::Close() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 270 | if (write_to_file_) { |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 271 | if (output_handle_ != NULL) fclose(output_handle_); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 272 | output_handle_ = NULL; |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame^] | 273 | if (ll_output_handle_ != NULL) fclose(ll_output_handle_); |
| 274 | ll_output_handle_ = NULL; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 275 | } else { |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 276 | delete output_buffer_; |
| 277 | output_buffer_ = NULL; |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 278 | } |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 279 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 280 | DeleteArray(message_buffer_); |
| 281 | message_buffer_ = NULL; |
| 282 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 283 | delete mutex_; |
| 284 | mutex_ = NULL; |
| 285 | |
| 286 | is_stopped_ = false; |
| 287 | } |
| 288 | |
| 289 | |
| 290 | int Log::GetLogLines(int from_pos, char* dest_buf, int max_size) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 291 | if (write_to_file_) return 0; |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 292 | ASSERT(output_buffer_ != NULL); |
| 293 | ASSERT(from_pos >= 0); |
| 294 | ASSERT(max_size >= 0); |
| 295 | int actual_size = output_buffer_->Read(from_pos, dest_buf, max_size); |
| 296 | ASSERT(actual_size <= max_size); |
| 297 | if (actual_size == 0) return 0; |
| 298 | |
| 299 | // Find previous log line boundary. |
| 300 | char* end_pos = dest_buf + actual_size - 1; |
| 301 | while (end_pos >= dest_buf && *end_pos != '\n') --end_pos; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 302 | actual_size = static_cast<int>(end_pos - dest_buf + 1); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 303 | // If the assertion below is hit, it means that there was no line end |
| 304 | // found --- something wrong has happened. |
| 305 | ASSERT(actual_size > 0); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 306 | ASSERT(actual_size <= max_size); |
| 307 | return actual_size; |
| 308 | } |
| 309 | |
| 310 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 311 | LogMessageBuilder::LogMessageBuilder(Logger* logger) |
| 312 | : log_(logger->log_), |
| 313 | sl(log_->mutex_), |
| 314 | pos_(0) { |
| 315 | ASSERT(log_->message_buffer_ != NULL); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | |
| 319 | void LogMessageBuilder::Append(const char* format, ...) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 320 | Vector<char> buf(log_->message_buffer_ + pos_, |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 321 | Log::kMessageBufferSize - pos_); |
| 322 | va_list args; |
| 323 | va_start(args, format); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 324 | AppendVA(format, args); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 325 | va_end(args); |
| 326 | ASSERT(pos_ <= Log::kMessageBufferSize); |
| 327 | } |
| 328 | |
| 329 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 330 | void LogMessageBuilder::AppendVA(const char* format, va_list args) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 331 | Vector<char> buf(log_->message_buffer_ + pos_, |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 332 | Log::kMessageBufferSize - pos_); |
| 333 | int result = v8::internal::OS::VSNPrintF(buf, format, args); |
| 334 | |
| 335 | // Result is -1 if output was truncated. |
| 336 | if (result >= 0) { |
| 337 | pos_ += result; |
| 338 | } else { |
| 339 | pos_ = Log::kMessageBufferSize; |
| 340 | } |
| 341 | ASSERT(pos_ <= Log::kMessageBufferSize); |
| 342 | } |
| 343 | |
| 344 | |
| 345 | void LogMessageBuilder::Append(const char c) { |
| 346 | if (pos_ < Log::kMessageBufferSize) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 347 | log_->message_buffer_[pos_++] = c; |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 348 | } |
| 349 | ASSERT(pos_ <= Log::kMessageBufferSize); |
| 350 | } |
| 351 | |
| 352 | |
| 353 | void LogMessageBuilder::Append(String* str) { |
| 354 | AssertNoAllocation no_heap_allocation; // Ensure string stay valid. |
| 355 | int length = str->length(); |
| 356 | for (int i = 0; i < length; i++) { |
| 357 | Append(static_cast<char>(str->Get(i))); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 362 | void LogMessageBuilder::AppendAddress(Address addr) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 363 | Append("0x%" V8PRIxPTR, addr); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 367 | void LogMessageBuilder::AppendDetailed(String* str, bool show_impl_info) { |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame^] | 368 | if (str == NULL) return; |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 369 | AssertNoAllocation no_heap_allocation; // Ensure string stay valid. |
| 370 | int len = str->length(); |
| 371 | if (len > 0x1000) |
| 372 | len = 0x1000; |
| 373 | if (show_impl_info) { |
| 374 | Append(str->IsAsciiRepresentation() ? 'a' : '2'); |
| 375 | if (StringShape(str).IsExternal()) |
| 376 | Append('e'); |
| 377 | if (StringShape(str).IsSymbol()) |
| 378 | Append('#'); |
| 379 | Append(":%i:", str->length()); |
| 380 | } |
| 381 | for (int i = 0; i < len; i++) { |
| 382 | uc32 c = str->Get(i); |
| 383 | if (c > 0xff) { |
| 384 | Append("\\u%04x", c); |
| 385 | } else if (c < 32 || c > 126) { |
| 386 | Append("\\x%02x", c); |
| 387 | } else if (c == ',') { |
| 388 | Append("\\,"); |
| 389 | } else if (c == '\\') { |
| 390 | Append("\\\\"); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 391 | } else if (c == '\"') { |
| 392 | Append("\"\""); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 393 | } else { |
| 394 | Append("%lc", c); |
| 395 | } |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 400 | void LogMessageBuilder::AppendStringPart(const char* str, int len) { |
| 401 | if (pos_ + len > Log::kMessageBufferSize) { |
| 402 | len = Log::kMessageBufferSize - pos_; |
| 403 | ASSERT(len >= 0); |
| 404 | if (len == 0) return; |
| 405 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 406 | Vector<char> buf(log_->message_buffer_ + pos_, |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 407 | Log::kMessageBufferSize - pos_); |
| 408 | OS::StrNCpy(buf, str, len); |
| 409 | pos_ += len; |
| 410 | ASSERT(pos_ <= Log::kMessageBufferSize); |
| 411 | } |
| 412 | |
| 413 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 414 | void LogMessageBuilder::WriteToLogFile() { |
| 415 | ASSERT(pos_ <= Log::kMessageBufferSize); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 416 | const int written = log_->write_to_file_ ? |
| 417 | log_->WriteToFile(log_->message_buffer_, pos_) : |
| 418 | log_->WriteToMemory(log_->message_buffer_, pos_); |
| 419 | if (written != pos_) { |
| 420 | log_->stop(); |
| 421 | log_->logger_->LogFailure(); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 425 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 426 | #endif // ENABLE_LOGGING_AND_PROFILING |
| 427 | |
| 428 | } } // namespace v8::internal |