OLD | NEW |
1 // Copyright 2009 the V8 project authors. All rights reserved. | 1 // Copyright 2009 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 data_pos += write_size; | 111 data_pos += write_size; |
112 if (block_write_pos_ == block_size_) { | 112 if (block_write_pos_ == block_size_) { |
113 block_write_pos_ = 0; | 113 block_write_pos_ = 0; |
114 AllocateBlock(++block_index_); | 114 AllocateBlock(++block_index_); |
115 } | 115 } |
116 } | 116 } |
117 write_pos_ += data_size; | 117 write_pos_ += data_size; |
118 return data_size; | 118 return data_size; |
119 } | 119 } |
120 | 120 |
| 121 LogData::LogData() |
| 122 :is_stopped_(false), |
| 123 Write(NULL), |
| 124 output_handle_(NULL), |
| 125 mutex_(NULL), |
| 126 message_buffer_(NULL), |
| 127 write_failure_handler(NULL), |
| 128 output_buffer_(NULL) { |
| 129 } |
121 | 130 |
122 bool Log::is_stopped_ = false; | |
123 Log::WritePtr Log::Write = NULL; | |
124 FILE* Log::output_handle_ = NULL; | |
125 LogDynamicBuffer* Log::output_buffer_ = NULL; | |
126 // Must be the same message as in Logger::PauseProfiler. | 131 // Must be the same message as in Logger::PauseProfiler. |
127 const char* Log::kDynamicBufferSeal = "profiler,\"pause\"\n"; | 132 const char Log::kDynamicBufferSeal[] = "profiler,\"pause\"\n"; |
128 Mutex* Log::mutex_ = NULL; | |
129 char* Log::message_buffer_ = NULL; | |
130 | |
131 | 133 |
132 void Log::Init() { | 134 void Log::Init() { |
133 mutex_ = OS::CreateMutex(); | 135 LogData& log_data = v8_context()->log_data_; |
134 message_buffer_ = NewArray<char>(kMessageBufferSize); | 136 log_data.mutex_ = OS::CreateMutex(); |
| 137 log_data.message_buffer_ = NewArray<char>(kMessageBufferSize); |
135 } | 138 } |
136 | 139 |
137 | 140 |
138 void Log::OpenStdout() { | 141 void Log::OpenStdout() { |
139 ASSERT(!IsEnabled()); | 142 ASSERT(!IsEnabled()); |
140 output_handle_ = stdout; | 143 LogData& log_data = v8_context()->log_data_; |
141 Write = WriteToFile; | 144 log_data.output_handle_ = stdout; |
| 145 log_data.Write = WriteToFile; |
142 Init(); | 146 Init(); |
143 } | 147 } |
144 | 148 |
145 | 149 |
146 void Log::OpenFile(const char* name) { | 150 void Log::OpenFile(const char* name) { |
147 ASSERT(!IsEnabled()); | 151 ASSERT(!IsEnabled()); |
148 output_handle_ = OS::FOpen(name, OS::LogFileOpenMode); | 152 LogData& log_data = v8_context()->log_data_; |
149 Write = WriteToFile; | 153 log_data.output_handle_ = OS::FOpen(name, OS::LogFileOpenMode); |
| 154 log_data.Write = WriteToFile; |
150 Init(); | 155 Init(); |
151 } | 156 } |
152 | 157 |
153 | 158 |
154 void Log::OpenMemoryBuffer() { | 159 void Log::OpenMemoryBuffer() { |
155 ASSERT(!IsEnabled()); | 160 ASSERT(!IsEnabled()); |
156 output_buffer_ = new LogDynamicBuffer( | 161 LogData& log_data = v8_context()->log_data_; |
| 162 log_data.output_buffer_ = new LogDynamicBuffer( |
157 kDynamicBufferBlockSize, kMaxDynamicBufferSize, | 163 kDynamicBufferBlockSize, kMaxDynamicBufferSize, |
158 kDynamicBufferSeal, StrLength(kDynamicBufferSeal)); | 164 kDynamicBufferSeal, StrLength(kDynamicBufferSeal)); |
159 Write = WriteToMemory; | 165 log_data.Write = WriteToMemory; |
160 Init(); | 166 Init(); |
161 } | 167 } |
162 | 168 |
163 | 169 |
164 void Log::Close() { | 170 void Log::Close() { |
165 if (Write == WriteToFile) { | 171 LogData& log_data = v8_context()->log_data_; |
166 if (output_handle_ != NULL) fclose(output_handle_); | 172 if (log_data.Write == WriteToFile) { |
167 output_handle_ = NULL; | 173 if (log_data.output_handle_ != NULL) fclose(log_data.output_handle_); |
168 } else if (Write == WriteToMemory) { | 174 log_data.output_handle_ = NULL; |
169 delete output_buffer_; | 175 } else if (log_data.Write == WriteToMemory) { |
170 output_buffer_ = NULL; | 176 delete log_data.output_buffer_; |
| 177 log_data.output_buffer_ = NULL; |
171 } else { | 178 } else { |
172 ASSERT(Write == NULL); | 179 ASSERT(log_data.Write == NULL); |
173 } | 180 } |
174 Write = NULL; | 181 log_data.Write = NULL; |
175 | 182 |
176 DeleteArray(message_buffer_); | 183 DeleteArray(log_data.message_buffer_); |
177 message_buffer_ = NULL; | 184 log_data.message_buffer_ = NULL; |
178 | 185 |
179 delete mutex_; | 186 delete log_data.mutex_; |
180 mutex_ = NULL; | 187 log_data.mutex_ = NULL; |
181 | 188 |
182 is_stopped_ = false; | 189 log_data.is_stopped_ = false; |
183 } | 190 } |
184 | 191 |
185 | 192 |
186 int Log::GetLogLines(int from_pos, char* dest_buf, int max_size) { | 193 int Log::GetLogLines(int from_pos, char* dest_buf, int max_size) { |
187 if (Write != WriteToMemory) return 0; | 194 LogData& log_data = v8_context()->log_data_; |
188 ASSERT(output_buffer_ != NULL); | 195 if (log_data.Write != WriteToMemory) return 0; |
| 196 ASSERT(log_data.output_buffer_ != NULL); |
189 ASSERT(from_pos >= 0); | 197 ASSERT(from_pos >= 0); |
190 ASSERT(max_size >= 0); | 198 ASSERT(max_size >= 0); |
191 int actual_size = output_buffer_->Read(from_pos, dest_buf, max_size); | 199 int actual_size = log_data.output_buffer_->Read(from_pos, dest_buf, max_size); |
192 ASSERT(actual_size <= max_size); | 200 ASSERT(actual_size <= max_size); |
193 if (actual_size == 0) return 0; | 201 if (actual_size == 0) return 0; |
194 | 202 |
195 // Find previous log line boundary. | 203 // Find previous log line boundary. |
196 char* end_pos = dest_buf + actual_size - 1; | 204 char* end_pos = dest_buf + actual_size - 1; |
197 while (end_pos >= dest_buf && *end_pos != '\n') --end_pos; | 205 while (end_pos >= dest_buf && *end_pos != '\n') --end_pos; |
198 actual_size = static_cast<int>(end_pos - dest_buf + 1); | 206 actual_size = static_cast<int>(end_pos - dest_buf + 1); |
199 ASSERT(actual_size <= max_size); | 207 ASSERT(actual_size <= max_size); |
200 return actual_size; | 208 return actual_size; |
201 } | 209 } |
202 | 210 |
203 | 211 LogMessageBuilder::LogMessageBuilder() |
204 LogMessageBuilder::WriteFailureHandler | 212 :log_data_(v8_context()->log_data_), |
205 LogMessageBuilder::write_failure_handler = NULL; | 213 sl(log_data_.mutex_), |
206 | 214 pos_(0) { |
207 | 215 ASSERT(log_data_.message_buffer_ != NULL); |
208 LogMessageBuilder::LogMessageBuilder(): sl(Log::mutex_), pos_(0) { | |
209 ASSERT(Log::message_buffer_ != NULL); | |
210 } | 216 } |
211 | 217 |
212 | 218 |
213 void LogMessageBuilder::Append(const char* format, ...) { | 219 void LogMessageBuilder::Append(const char* format, ...) { |
214 Vector<char> buf(Log::message_buffer_ + pos_, | 220 Vector<char> buf(log_data_.message_buffer_ + pos_, |
215 Log::kMessageBufferSize - pos_); | 221 Log::kMessageBufferSize - pos_); |
216 va_list args; | 222 va_list args; |
217 va_start(args, format); | 223 va_start(args, format); |
218 AppendVA(format, args); | 224 AppendVA(format, args); |
219 va_end(args); | 225 va_end(args); |
220 ASSERT(pos_ <= Log::kMessageBufferSize); | 226 ASSERT(pos_ <= Log::kMessageBufferSize); |
221 } | 227 } |
222 | 228 |
223 | 229 |
224 void LogMessageBuilder::AppendVA(const char* format, va_list args) { | 230 void LogMessageBuilder::AppendVA(const char* format, va_list args) { |
225 Vector<char> buf(Log::message_buffer_ + pos_, | 231 Vector<char> buf(log_data_.message_buffer_ + pos_, |
226 Log::kMessageBufferSize - pos_); | 232 Log::kMessageBufferSize - pos_); |
227 int result = v8::internal::OS::VSNPrintF(buf, format, args); | 233 int result = v8::internal::OS::VSNPrintF(buf, format, args); |
228 | 234 |
229 // Result is -1 if output was truncated. | 235 // Result is -1 if output was truncated. |
230 if (result >= 0) { | 236 if (result >= 0) { |
231 pos_ += result; | 237 pos_ += result; |
232 } else { | 238 } else { |
233 pos_ = Log::kMessageBufferSize; | 239 pos_ = Log::kMessageBufferSize; |
234 } | 240 } |
235 ASSERT(pos_ <= Log::kMessageBufferSize); | 241 ASSERT(pos_ <= Log::kMessageBufferSize); |
236 } | 242 } |
237 | 243 |
238 | 244 |
239 void LogMessageBuilder::Append(const char c) { | 245 void LogMessageBuilder::Append(const char c) { |
240 if (pos_ < Log::kMessageBufferSize) { | 246 if (pos_ < Log::kMessageBufferSize) { |
241 Log::message_buffer_[pos_++] = c; | 247 log_data_.message_buffer_[pos_++] = c; |
242 } | 248 } |
243 ASSERT(pos_ <= Log::kMessageBufferSize); | 249 ASSERT(pos_ <= Log::kMessageBufferSize); |
244 } | 250 } |
245 | 251 |
246 | 252 |
247 void LogMessageBuilder::Append(String* str) { | 253 void LogMessageBuilder::Append(String* str) { |
248 AssertNoAllocation no_heap_allocation; // Ensure string stay valid. | 254 AssertNoAllocation no_heap_allocation; // Ensure string stay valid. |
249 int length = str->length(); | 255 int length = str->length(); |
250 for (int i = 0; i < length; i++) { | 256 for (int i = 0; i < length; i++) { |
251 Append(static_cast<char>(str->Get(i))); | 257 Append(static_cast<char>(str->Get(i))); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 } | 315 } |
310 } | 316 } |
311 | 317 |
312 | 318 |
313 void LogMessageBuilder::AppendStringPart(const char* str, int len) { | 319 void LogMessageBuilder::AppendStringPart(const char* str, int len) { |
314 if (pos_ + len > Log::kMessageBufferSize) { | 320 if (pos_ + len > Log::kMessageBufferSize) { |
315 len = Log::kMessageBufferSize - pos_; | 321 len = Log::kMessageBufferSize - pos_; |
316 ASSERT(len >= 0); | 322 ASSERT(len >= 0); |
317 if (len == 0) return; | 323 if (len == 0) return; |
318 } | 324 } |
319 Vector<char> buf(Log::message_buffer_ + pos_, | 325 Vector<char> buf(log_data_.message_buffer_ + pos_, |
320 Log::kMessageBufferSize - pos_); | 326 Log::kMessageBufferSize - pos_); |
321 OS::StrNCpy(buf, str, len); | 327 OS::StrNCpy(buf, str, len); |
322 pos_ += len; | 328 pos_ += len; |
323 ASSERT(pos_ <= Log::kMessageBufferSize); | 329 ASSERT(pos_ <= Log::kMessageBufferSize); |
324 } | 330 } |
325 | 331 |
326 | 332 |
327 bool LogMessageBuilder::StoreInCompressor(LogRecordCompressor* compressor) { | 333 bool LogMessageBuilder::StoreInCompressor(LogRecordCompressor* compressor) { |
328 return compressor->Store(Vector<const char>(Log::message_buffer_, pos_)); | 334 return compressor->Store(Vector<const char>(log_data_.message_buffer_, pos_)); |
329 } | 335 } |
330 | 336 |
331 | 337 |
332 bool LogMessageBuilder::RetrieveCompressedPrevious( | 338 bool LogMessageBuilder::RetrieveCompressedPrevious( |
333 LogRecordCompressor* compressor, const char* prefix) { | 339 LogRecordCompressor* compressor, const char* prefix) { |
334 pos_ = 0; | 340 pos_ = 0; |
335 if (prefix[0] != '\0') Append(prefix); | 341 if (prefix[0] != '\0') Append(prefix); |
336 Vector<char> prev_record(Log::message_buffer_ + pos_, | 342 Vector<char> prev_record(log_data_.message_buffer_ + pos_, |
337 Log::kMessageBufferSize - pos_); | 343 Log::kMessageBufferSize - pos_); |
338 const bool has_prev = compressor->RetrievePreviousCompressed(&prev_record); | 344 const bool has_prev = compressor->RetrievePreviousCompressed(&prev_record); |
339 if (!has_prev) return false; | 345 if (!has_prev) return false; |
340 pos_ += prev_record.length(); | 346 pos_ += prev_record.length(); |
341 return true; | 347 return true; |
342 } | 348 } |
343 | 349 |
344 | 350 |
345 void LogMessageBuilder::WriteToLogFile() { | 351 void LogMessageBuilder::WriteToLogFile() { |
346 ASSERT(pos_ <= Log::kMessageBufferSize); | 352 ASSERT(pos_ <= Log::kMessageBufferSize); |
347 const int written = Log::Write(Log::message_buffer_, pos_); | 353 const int written = log_data_.Write(log_data_.message_buffer_, pos_); |
348 if (written != pos_ && write_failure_handler != NULL) { | 354 if (written != pos_ && log_data_.write_failure_handler != NULL) { |
349 write_failure_handler(); | 355 log_data_.write_failure_handler(); |
350 } | 356 } |
351 } | 357 } |
352 | 358 |
353 | 359 |
354 void LogMessageBuilder::WriteCStringToLogFile(const char* str) { | 360 void LogMessageBuilder::WriteCStringToLogFile(const char* str) { |
355 const int len = StrLength(str); | 361 const int len = StrLength(str); |
356 const int written = Log::Write(str, len); | 362 const int written = log_data_.Write(str, len); |
357 if (written != len && write_failure_handler != NULL) { | 363 if (written != len && log_data_.write_failure_handler != NULL) { |
358 write_failure_handler(); | 364 log_data_.write_failure_handler(); |
359 } | 365 } |
360 } | 366 } |
361 | 367 |
362 | 368 |
363 // Formatting string for back references to the whole line. E.g. "#2" means | 369 // Formatting string for back references to the whole line. E.g. "#2" means |
364 // "the second line above". | 370 // "the second line above". |
365 const char* LogRecordCompressor::kLineBackwardReferenceFormat = "#%d"; | 371 const char* LogRecordCompressor::kLineBackwardReferenceFormat = "#%d"; |
366 | 372 |
367 // Formatting string for back references. E.g. "#2:10" means | 373 // Formatting string for back references. E.g. "#2:10" means |
368 // "the second line above, start from char 10 (0-based)". | 374 // "the second line above, start from char 10 (0-based)". |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
494 PrintBackwardReference(backref, best.distance, best.copy_from_pos); | 500 PrintBackwardReference(backref, best.distance, best.copy_from_pos); |
495 ASSERT(strlen(backref.start()) - best.backref_size == 0); | 501 ASSERT(strlen(backref.start()) - best.backref_size == 0); |
496 prev_record->Truncate(static_cast<int>(unchanged_len + best.backref_size)); | 502 prev_record->Truncate(static_cast<int>(unchanged_len + best.backref_size)); |
497 } | 503 } |
498 return true; | 504 return true; |
499 } | 505 } |
500 | 506 |
501 #endif // ENABLE_LOGGING_AND_PROFILING | 507 #endif // ENABLE_LOGGING_AND_PROFILING |
502 | 508 |
503 } } // namespace v8::internal | 509 } } // namespace v8::internal |
OLD | NEW |