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