Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: src/log.cc

Issue 113763: Merge in changes from readability review. (Closed)
Patch Set: Created 11 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 283 matching lines...) Expand 10 before | Expand all | Expand 10 after
294 294
295 // Opens file for logging. 295 // Opens file for logging.
296 static void OpenFile(const char* name); 296 static void OpenFile(const char* name);
297 297
298 // Opens memory buffer for logging. 298 // Opens memory buffer for logging.
299 static void OpenMemoryBuffer(); 299 static void OpenMemoryBuffer();
300 300
301 // Frees all resources acquired in Open... functions. 301 // Frees all resources acquired in Open... functions.
302 static void Close(); 302 static void Close();
303 303
304 // See description in v8.h. 304 // See description in include/v8.h.
305 static int GetLogLines(int from_pos, char* dest_buf, int max_size); 305 static int GetLogLines(int from_pos, char* dest_buf, int max_size);
306 306
307 static bool is_enabled() { return output_.handle != NULL; } 307 // Returns whether logging is enabled.
308 static bool is_enabled() {
Søren Thygesen Gjesse 2009/05/25 06:38:31 How about changing this to IsEnabled() as well? Th
Mikhail Naganov 2009/05/25 08:38:51 Good idea. Done.
309 return output_handle_ != NULL || output_buffer_ != NULL;
310 }
308 311
312 private:
309 typedef int (*WritePtr)(const char* msg, int length); 313 typedef int (*WritePtr)(const char* msg, int length);
310 private: 314
315 // Initialization function called from Open... functions.
311 static void Init(); 316 static void Init();
312 317
313 // Write functions assume that mutex_ is acquired by the caller. 318 // Write functions assume that mutex_ is acquired by the caller.
314 static WritePtr Write; 319 static WritePtr Write;
315 320
321 // Implementation of writing to a log file.
316 static int WriteToFile(const char* msg, int length) { 322 static int WriteToFile(const char* msg, int length) {
317 ASSERT(output_.handle != NULL); 323 ASSERT(output_handle_ != NULL);
318 int rv = fwrite(msg, 1, length, output_.handle); 324 int rv = fwrite(msg, 1, length, output_handle_);
319 ASSERT(length == rv); 325 ASSERT(length == rv);
320 return rv; 326 return rv;
321 } 327 }
322 328
329 // Implementation of writing to a memory buffer.
323 static int WriteToMemory(const char* msg, int length) { 330 static int WriteToMemory(const char* msg, int length) {
324 ASSERT(output_.buffer != NULL); 331 ASSERT(output_buffer_ != NULL);
325 ASSERT(output_buffer_write_pos_ >= output_.buffer); 332 ASSERT(output_buffer_write_pos_ >= output_buffer_);
326 if (output_buffer_write_pos_ + length 333 if (output_buffer_write_pos_ + length
327 <= output_.buffer + kOutputBufferSize) { 334 <= output_buffer_ + kOutputBufferSize) {
328 memcpy(output_buffer_write_pos_, msg, length); 335 memcpy(output_buffer_write_pos_, msg, length);
329 output_buffer_write_pos_ += length; 336 output_buffer_write_pos_ += length;
330 return length; 337 return length;
331 } else { 338 } else {
332 // Memory buffer is full, ignore write. 339 // Memory buffer is full, ignore write.
333 return 0; 340 return 0;
334 } 341 }
335 } 342 }
336 343
337 // When logging is active, output_ refers the file or memory buffer 344 // When logging is active, either output_handle_ or output_buffer_ is used
338 // events are written to. 345 // to store a pointer to log destination. If logging was opened via OpenStdout
339 // mutex_ should be acquired before using output_. 346 // or OpenFile, then output_handle_ is used. If logging was opened
340 union Output { 347 // via OpenMemoryBuffer, then output_buffer_ is used.
341 FILE* handle; 348 // mutex_ should be acquired before using output_handle_ or output_buffer_.
342 char* buffer; 349 static FILE* output_handle_;
343 }; 350
344 static Output output_; 351 static char* output_buffer_;
345 352
346 // mutex_ is a Mutex used for enforcing exclusive 353 // mutex_ is a Mutex used for enforcing exclusive
347 // access to the formatting buffer and the log file or log memory buffer. 354 // access to the formatting buffer and the log file or log memory buffer.
348 static Mutex* mutex_; 355 static Mutex* mutex_;
349 356
350 // Size of buffer used for memory logging. 357 // Size of buffer used for memory logging.
351 static const int kOutputBufferSize = 2 * 1024 * 1024; 358 static const int kOutputBufferSize = 2 * 1024 * 1024;
352 359
353 // Writing position in a memory buffer. 360 // Writing position in a memory buffer.
354 static char* output_buffer_write_pos_; 361 static char* output_buffer_write_pos_;
355 362
356 // Size of buffer used for formatting log messages. 363 // Size of buffer used for formatting log messages.
357 static const int kMessageBufferSize = 2048; 364 static const int kMessageBufferSize = 2048;
358 365
359 // Buffer used for formatting log messages. This is a singleton buffer and 366 // Buffer used for formatting log messages. This is a singleton buffer and
360 // mutex_ should be acquired before using it. 367 // mutex_ should be acquired before using it.
361 static char* message_buffer_; 368 static char* message_buffer_;
362 369
363 friend class LogMessageBuilder; 370 friend class LogMessageBuilder;
364 }; 371 };
365 372
366 373
367 Log::WritePtr Log::Write = NULL; 374 Log::WritePtr Log::Write = NULL;
368 Log::Output Log::output_ = {NULL}; 375 FILE* Log::output_handle_ = NULL;
376 char* Log::output_buffer_ = NULL;
369 Mutex* Log::mutex_ = NULL; 377 Mutex* Log::mutex_ = NULL;
370 char* Log::output_buffer_write_pos_ = NULL; 378 char* Log::output_buffer_write_pos_ = NULL;
371 char* Log::message_buffer_ = NULL; 379 char* Log::message_buffer_ = NULL;
372 380
373 381
374 void Log::Init() { 382 void Log::Init() {
375 mutex_ = OS::CreateMutex(); 383 mutex_ = OS::CreateMutex();
376 message_buffer_ = NewArray<char>(kMessageBufferSize); 384 message_buffer_ = NewArray<char>(kMessageBufferSize);
377 } 385 }
378 386
379 387
380 void Log::OpenStdout() { 388 void Log::OpenStdout() {
381 ASSERT(output_.handle == NULL); 389 ASSERT(!is_enabled());
382 output_.handle = stdout; 390 output_handle_ = stdout;
383 Write = WriteToFile; 391 Write = WriteToFile;
384 Init(); 392 Init();
385 } 393 }
386 394
387 395
388 void Log::OpenFile(const char* name) { 396 void Log::OpenFile(const char* name) {
389 ASSERT(output_.handle == NULL); 397 ASSERT(!is_enabled());
390 output_.handle = OS::FOpen(name, OS::LogFileOpenMode); 398 output_handle_ = OS::FOpen(name, OS::LogFileOpenMode);
391 Write = WriteToFile; 399 Write = WriteToFile;
392 Init(); 400 Init();
393 } 401 }
394 402
395 403
396 void Log::OpenMemoryBuffer() { 404 void Log::OpenMemoryBuffer() {
397 ASSERT(output_.buffer == NULL); 405 ASSERT(!is_enabled());
398 output_.buffer = NewArray<char>(kOutputBufferSize); 406 output_buffer_ = NewArray<char>(kOutputBufferSize);
399 output_buffer_write_pos_ = output_.buffer; 407 output_buffer_write_pos_ = output_buffer_;
400 Write = WriteToMemory; 408 Write = WriteToMemory;
401 Init(); 409 Init();
402 } 410 }
403 411
404 412
405 void Log::Close() { 413 void Log::Close() {
406 if (Write == WriteToFile) { 414 if (Write == WriteToFile) {
407 fclose(output_.handle); 415 fclose(output_handle_);
408 output_.handle = NULL; 416 output_handle_ = NULL;
409 } else if (Write == WriteToMemory) { 417 } else if (Write == WriteToMemory) {
410 DeleteArray(output_.buffer); 418 DeleteArray(output_buffer_);
411 output_.buffer = NULL; 419 output_buffer_ = NULL;
412 } else { 420 } else {
413 ASSERT(Write == NULL); 421 ASSERT(Write == NULL);
414 } 422 }
415 Write = NULL; 423 Write = NULL;
416 424
417 delete mutex_; 425 delete mutex_;
418 mutex_ = NULL; 426 mutex_ = NULL;
419 427
420 DeleteArray(message_buffer_); 428 DeleteArray(message_buffer_);
421 message_buffer_ = NULL; 429 message_buffer_ = NULL;
422 } 430 }
423 431
424 432
425 int Log::GetLogLines(int from_pos, char* dest_buf, int max_size) { 433 int Log::GetLogLines(int from_pos, char* dest_buf, int max_size) {
426 if (Write != WriteToMemory) return 0; 434 if (Write != WriteToMemory) return 0;
427 ASSERT(output_.buffer != NULL); 435 ASSERT(output_buffer_ != NULL);
428 ASSERT(output_buffer_write_pos_ >= output_.buffer); 436 ASSERT(output_buffer_write_pos_ >= output_buffer_);
429 ASSERT(from_pos >= 0); 437 ASSERT(from_pos >= 0);
430 ASSERT(max_size >= 0); 438 ASSERT(max_size >= 0);
431 int actual_size = max_size; 439 int actual_size = max_size;
432 char* buffer_read_pos = output_.buffer + from_pos; 440 char* buffer_read_pos = output_buffer_ + from_pos;
433 ScopedLock sl(mutex_); 441 ScopedLock sl(mutex_);
434 if (actual_size == 0 442 if (actual_size == 0
435 || output_buffer_write_pos_ == output_.buffer 443 || output_buffer_write_pos_ == output_buffer_
436 || buffer_read_pos >= output_buffer_write_pos_) { 444 || buffer_read_pos >= output_buffer_write_pos_) {
437 // No data requested or can be returned. 445 // No data requested or can be returned.
438 return 0; 446 return 0;
439 } 447 }
440 if (buffer_read_pos + actual_size > output_buffer_write_pos_) { 448 if (buffer_read_pos + actual_size > output_buffer_write_pos_) {
441 // Requested size overlaps with current writing position and 449 // Requested size overlaps with current writing position and
442 // needs to be truncated. 450 // needs to be truncated.
443 actual_size = output_buffer_write_pos_ - buffer_read_pos; 451 actual_size = output_buffer_write_pos_ - buffer_read_pos;
444 ASSERT(actual_size == 0 || buffer_read_pos[actual_size - 1] == '\n'); 452 ASSERT(actual_size == 0 || buffer_read_pos[actual_size - 1] == '\n');
445 } else { 453 } else {
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
577 // 585 //
578 // Logger class implementation. 586 // Logger class implementation.
579 // 587 //
580 Ticker* Logger::ticker_ = NULL; 588 Ticker* Logger::ticker_ = NULL;
581 Profiler* Logger::profiler_ = NULL; 589 Profiler* Logger::profiler_ = NULL;
582 VMState* Logger::current_state_ = NULL; 590 VMState* Logger::current_state_ = NULL;
583 VMState Logger::bottom_state_(EXTERNAL); 591 VMState Logger::bottom_state_(EXTERNAL);
584 SlidingStateWindow* Logger::sliding_state_window_ = NULL; 592 SlidingStateWindow* Logger::sliding_state_window_ = NULL;
585 593
586 594
587 bool Logger::is_enabled() { 595 bool Logger::IsEnabled() {
588 return Log::is_enabled(); 596 return Log::is_enabled();
589 } 597 }
590 598
591 #endif // ENABLE_LOGGING_AND_PROFILING 599 #endif // ENABLE_LOGGING_AND_PROFILING
592 600
593 601
594 void Logger::Preamble(const char* content) { 602 void Logger::Preamble(const char* content) {
595 #ifdef ENABLE_LOGGING_AND_PROFILING 603 #ifdef ENABLE_LOGGING_AND_PROFILING
596 if (!Log::is_enabled() || !FLAG_log_code) return; 604 if (!Log::is_enabled() || !FLAG_log_code) return;
597 LogMessageBuilder msg; 605 LogMessageBuilder msg;
(...skipping 781 matching lines...) Expand 10 before | Expand all | Expand 10 after
1379 } else if (previous_->state_ == EXTERNAL) { 1387 } else if (previous_->state_ == EXTERNAL) {
1380 // We are leaving V8. 1388 // We are leaving V8.
1381 Heap::Protect(); 1389 Heap::Protect();
1382 } 1390 }
1383 } 1391 }
1384 #endif 1392 #endif
1385 } 1393 }
1386 #endif 1394 #endif
1387 1395
1388 } } // namespace v8::internal 1396 } } // namespace v8::internal
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698