Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2006-2009 the V8 project authors. All rights reserved. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4 | |
| 5 | #ifndef V8_LOG_UTILS_H_ |
| 6 | #define V8_LOG_UTILS_H_ |
| 7 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8 | #include "src/allocation.h" |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10 | namespace v8 { |
| 11 | namespace internal { |
| 12 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13 | class Logger; |
| 14 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 15 | // Functions and data for performing output of log messages. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 16 | class Log { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17 | public: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 18 | // Performs process-wide initialization. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19 | void Initialize(const char* log_file_name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 20 | |
| 21 | // Disables logging, but preserves acquired resources. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 22 | void stop() { is_stopped_ = true; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 23 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 24 | static bool InitLogAtStart() { |
| 25 | return FLAG_log || FLAG_log_api || FLAG_log_code || FLAG_log_gc |
| 26 | || FLAG_log_handles || FLAG_log_suspect || FLAG_log_regexp |
| 27 | || FLAG_ll_prof || FLAG_perf_basic_prof || FLAG_perf_jit_prof |
| 28 | || FLAG_log_internal_timer_events; |
| 29 | } |
| 30 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 31 | // Frees all resources acquired in Initialize and Open... functions. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 32 | // When a temporary file is used for the log, returns its stream descriptor, |
| 33 | // leaving the file open. |
| 34 | FILE* Close(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 35 | |
| 36 | // Returns whether logging is enabled. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 37 | bool IsEnabled() { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 38 | return !is_stopped_ && output_handle_ != NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | // Size of buffer used for formatting log messages. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 42 | static const int kMessageBufferSize = 2048; |
| 43 | |
| 44 | // This mode is only used in tests, as temporary files are automatically |
| 45 | // deleted on close and thus can't be accessed afterwards. |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 46 | static const char* const kLogToTemporaryFile; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 47 | static const char* const kLogToConsole; |
| 48 | |
| 49 | // Utility class for formatting log messages. It fills the message into the |
| 50 | // static buffer in Log. |
| 51 | class MessageBuilder BASE_EMBEDDED { |
| 52 | public: |
| 53 | // Create a message builder starting from position 0. |
| 54 | // This acquires the mutex in the log as well. |
| 55 | explicit MessageBuilder(Log* log); |
| 56 | ~MessageBuilder() { } |
| 57 | |
| 58 | // Append string data to the log message. |
| 59 | void Append(const char* format, ...); |
| 60 | |
| 61 | // Append string data to the log message. |
| 62 | void AppendVA(const char* format, va_list args); |
| 63 | |
| 64 | // Append a character to the log message. |
| 65 | void Append(const char c); |
| 66 | |
| 67 | // Append double quoted string to the log message. |
| 68 | void AppendDoubleQuotedString(const char* string); |
| 69 | |
| 70 | // Append a heap string. |
| 71 | void Append(String* str); |
| 72 | |
| 73 | // Appends an address. |
| 74 | void AppendAddress(Address addr); |
| 75 | |
| 76 | void AppendSymbolName(Symbol* symbol); |
| 77 | |
| 78 | void AppendDetailed(String* str, bool show_impl_info); |
| 79 | |
| 80 | // Append a portion of a string. |
| 81 | void AppendStringPart(const char* str, int len); |
| 82 | |
| 83 | // Write the log message to the log file currently opened. |
| 84 | void WriteToLogFile(); |
| 85 | |
| 86 | private: |
| 87 | Log* log_; |
| 88 | base::LockGuard<base::Mutex> lock_guard_; |
| 89 | int pos_; |
| 90 | }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 91 | |
| 92 | private: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 93 | explicit Log(Logger* logger); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 94 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 95 | // Opens stdout for logging. |
| 96 | void OpenStdout(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 97 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 98 | // Opens file for logging. |
| 99 | void OpenFile(const char* name); |
| 100 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 101 | // Opens a temporary file for logging. |
| 102 | void OpenTemporaryFile(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 103 | |
| 104 | // Implementation of writing to a log file. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 105 | int WriteToFile(const char* msg, int length) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 106 | DCHECK(output_handle_ != NULL); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 107 | size_t rv = fwrite(msg, 1, length, output_handle_); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 108 | DCHECK(static_cast<size_t>(length) == rv); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 109 | USE(rv); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 110 | fflush(output_handle_); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 111 | return length; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 114 | // Whether logging is stopped (e.g. due to insufficient resources). |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 115 | bool is_stopped_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 116 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 117 | // When logging is active output_handle_ is used to store a pointer to log |
| 118 | // destination. mutex_ should be acquired before using output_handle_. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 119 | FILE* output_handle_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 120 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 121 | // mutex_ is a Mutex used for enforcing exclusive |
| 122 | // access to the formatting buffer and the log file or log memory buffer. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 123 | base::Mutex mutex_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 124 | |
| 125 | // Buffer used for formatting log messages. This is a singleton buffer and |
| 126 | // mutex_ should be acquired before using it. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 127 | char* message_buffer_; |
| 128 | |
| 129 | Logger* logger_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 130 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 131 | friend class Logger; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 135 | } } // namespace v8::internal |
| 136 | |
| 137 | #endif // V8_LOG_UTILS_H_ |