OLD | NEW |
1 // Copyright 2006-2009 the V8 project authors. All rights reserved. | 1 // Copyright 2006-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 15 matching lines...) Expand all Loading... |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 #ifndef V8_LOG_UTILS_H_ | 28 #ifndef V8_LOG_UTILS_H_ |
29 #define V8_LOG_UTILS_H_ | 29 #define V8_LOG_UTILS_H_ |
30 | 30 |
31 namespace v8 { | 31 namespace v8 { |
32 namespace internal { | 32 namespace internal { |
33 | 33 |
34 #ifdef ENABLE_LOGGING_AND_PROFILING | 34 #ifdef ENABLE_LOGGING_AND_PROFILING |
35 | 35 |
| 36 class Logger; |
| 37 |
36 // A memory buffer that increments its size as you write in it. Size | 38 // A memory buffer that increments its size as you write in it. Size |
37 // is incremented with 'block_size' steps, never exceeding 'max_size'. | 39 // is incremented with 'block_size' steps, never exceeding 'max_size'. |
38 // During growth, memory contents are never copied. At the end of the | 40 // During growth, memory contents are never copied. At the end of the |
39 // buffer an amount of memory specified in 'seal_size' is reserved. | 41 // buffer an amount of memory specified in 'seal_size' is reserved. |
40 // When writing position reaches max_size - seal_size, buffer auto-seals | 42 // When writing position reaches max_size - seal_size, buffer auto-seals |
41 // itself with 'seal' and allows no further writes. Data pointed by | 43 // itself with 'seal' and allows no further writes. Data pointed by |
42 // 'seal' must be available during entire LogDynamicBuffer lifetime. | 44 // 'seal' must be available during entire LogDynamicBuffer lifetime. |
43 // | 45 // |
44 // An instance of this class is created dynamically by Log. | 46 // An instance of this class is created dynamically by Log. |
45 class LogDynamicBuffer { | 47 class LogDynamicBuffer { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 const int seal_size_; | 84 const int seal_size_; |
83 ScopedVector<char*> blocks_; | 85 ScopedVector<char*> blocks_; |
84 int write_pos_; | 86 int write_pos_; |
85 int block_index_; | 87 int block_index_; |
86 int block_write_pos_; | 88 int block_write_pos_; |
87 bool is_sealed_; | 89 bool is_sealed_; |
88 }; | 90 }; |
89 | 91 |
90 | 92 |
91 // Functions and data for performing output of log messages. | 93 // Functions and data for performing output of log messages. |
92 class Log : public AllStatic { | 94 class Log { |
93 public: | 95 public: |
94 // Opens stdout for logging. | |
95 static void OpenStdout(); | |
96 | 96 |
97 // Opens file for logging. | 97 // Performs process-wide initialization. |
98 static void OpenFile(const char* name); | 98 void Initialize(); |
99 | |
100 // Opens memory buffer for logging. | |
101 static void OpenMemoryBuffer(); | |
102 | 99 |
103 // Disables logging, but preserves acquired resources. | 100 // Disables logging, but preserves acquired resources. |
104 static void stop() { is_stopped_ = true; } | 101 void stop() { is_stopped_ = true; } |
105 | 102 |
106 // Frees all resources acquired in Open... functions. | 103 // Frees all resources acquired in Initialize and Open... functions. |
107 static void Close(); | 104 void Close(); |
108 | 105 |
109 // See description in include/v8.h. | 106 // See description in include/v8.h. |
110 static int GetLogLines(int from_pos, char* dest_buf, int max_size); | 107 int GetLogLines(int from_pos, char* dest_buf, int max_size); |
111 | 108 |
112 // Returns whether logging is enabled. | 109 // Returns whether logging is enabled. |
113 static bool IsEnabled() { | 110 bool IsEnabled() { |
114 return !is_stopped_ && (output_handle_ != NULL || output_buffer_ != NULL); | 111 return !is_stopped_ && (output_handle_ != NULL || output_buffer_ != NULL); |
115 } | 112 } |
116 | 113 |
117 // Size of buffer used for formatting log messages. | 114 // Size of buffer used for formatting log messages. |
118 static const int kMessageBufferSize = v8::V8::kMinimumSizeForLogLinesBuffer; | 115 static const int kMessageBufferSize = v8::V8::kMinimumSizeForLogLinesBuffer; |
119 | 116 |
120 private: | 117 private: |
121 typedef int (*WritePtr)(const char* msg, int length); | 118 explicit Log(Logger* logger); |
122 | 119 |
123 // Initialization function called from Open... functions. | 120 // Opens stdout for logging. |
124 static void Init(); | 121 void OpenStdout(); |
125 | 122 |
126 // Write functions assume that mutex_ is acquired by the caller. | 123 // Opens file for logging. |
127 static WritePtr Write; | 124 void OpenFile(const char* name); |
| 125 |
| 126 // Opens memory buffer for logging. |
| 127 void OpenMemoryBuffer(); |
128 | 128 |
129 // Implementation of writing to a log file. | 129 // Implementation of writing to a log file. |
130 static int WriteToFile(const char* msg, int length) { | 130 int WriteToFile(const char* msg, int length) { |
131 ASSERT(output_handle_ != NULL); | 131 ASSERT(output_handle_ != NULL); |
132 size_t rv = fwrite(msg, 1, length, output_handle_); | 132 size_t rv = fwrite(msg, 1, length, output_handle_); |
133 ASSERT(static_cast<size_t>(length) == rv); | 133 ASSERT(static_cast<size_t>(length) == rv); |
134 USE(rv); | 134 USE(rv); |
135 fflush(output_handle_); | 135 fflush(output_handle_); |
136 return length; | 136 return length; |
137 } | 137 } |
138 | 138 |
139 // Implementation of writing to a memory buffer. | 139 // Implementation of writing to a memory buffer. |
140 static int WriteToMemory(const char* msg, int length) { | 140 int WriteToMemory(const char* msg, int length) { |
141 ASSERT(output_buffer_ != NULL); | 141 ASSERT(output_buffer_ != NULL); |
142 return output_buffer_->Write(msg, length); | 142 return output_buffer_->Write(msg, length); |
143 } | 143 } |
144 | 144 |
| 145 bool write_to_file_; |
| 146 |
145 // Whether logging is stopped (e.g. due to insufficient resources). | 147 // Whether logging is stopped (e.g. due to insufficient resources). |
146 static bool is_stopped_; | 148 bool is_stopped_; |
147 | 149 |
148 // When logging is active, either output_handle_ or output_buffer_ is used | 150 // When logging is active, either output_handle_ or output_buffer_ is used |
149 // to store a pointer to log destination. If logging was opened via OpenStdout | 151 // to store a pointer to log destination. If logging was opened via OpenStdout |
150 // or OpenFile, then output_handle_ is used. If logging was opened | 152 // or OpenFile, then output_handle_ is used. If logging was opened |
151 // via OpenMemoryBuffer, then output_buffer_ is used. | 153 // via OpenMemoryBuffer, then output_buffer_ is used. |
152 // mutex_ should be acquired before using output_handle_ or output_buffer_. | 154 // mutex_ should be acquired before using output_handle_ or output_buffer_. |
153 static FILE* output_handle_; | 155 FILE* output_handle_; |
154 | 156 |
155 // Used when low-level profiling is active to save code object contents. | 157 // Used when low-level profiling is active to save code object contents. |
156 static FILE* output_code_handle_; | 158 FILE* output_code_handle_; |
157 | 159 |
158 static LogDynamicBuffer* output_buffer_; | 160 LogDynamicBuffer* output_buffer_; |
159 | 161 |
160 // Size of dynamic buffer block (and dynamic buffer initial size). | 162 // Size of dynamic buffer block (and dynamic buffer initial size). |
161 static const int kDynamicBufferBlockSize = 65536; | 163 static const int kDynamicBufferBlockSize = 65536; |
162 | 164 |
163 // Maximum size of dynamic buffer. | 165 // Maximum size of dynamic buffer. |
164 static const int kMaxDynamicBufferSize = 50 * 1024 * 1024; | 166 static const int kMaxDynamicBufferSize = 50 * 1024 * 1024; |
165 | 167 |
166 // Message to "seal" dynamic buffer with. | 168 // Message to "seal" dynamic buffer with. |
167 static const char* kDynamicBufferSeal; | 169 static const char* const kDynamicBufferSeal; |
168 | 170 |
169 // mutex_ is a Mutex used for enforcing exclusive | 171 // mutex_ is a Mutex used for enforcing exclusive |
170 // access to the formatting buffer and the log file or log memory buffer. | 172 // access to the formatting buffer and the log file or log memory buffer. |
171 static Mutex* mutex_; | 173 Mutex* mutex_; |
172 | 174 |
173 // Buffer used for formatting log messages. This is a singleton buffer and | 175 // Buffer used for formatting log messages. This is a singleton buffer and |
174 // mutex_ should be acquired before using it. | 176 // mutex_ should be acquired before using it. |
175 static char* message_buffer_; | 177 char* message_buffer_; |
| 178 |
| 179 Logger* logger_; |
176 | 180 |
177 friend class Logger; | 181 friend class Logger; |
178 friend class LogMessageBuilder; | 182 friend class LogMessageBuilder; |
179 }; | 183 }; |
180 | 184 |
181 | 185 |
182 // Utility class for formatting log messages. It fills the message into the | 186 // Utility class for formatting log messages. It fills the message into the |
183 // static buffer in Log. | 187 // static buffer in Log. |
184 class LogMessageBuilder BASE_EMBEDDED { | 188 class LogMessageBuilder BASE_EMBEDDED { |
185 public: | 189 public: |
186 // Create a message builder starting from position 0. This acquires the mutex | 190 // Create a message builder starting from position 0. This acquires the mutex |
187 // in the log as well. | 191 // in the log as well. |
188 explicit LogMessageBuilder(); | 192 explicit LogMessageBuilder(Logger* logger); |
189 ~LogMessageBuilder() { } | 193 ~LogMessageBuilder() { } |
190 | 194 |
191 // Append string data to the log message. | 195 // Append string data to the log message. |
192 void Append(const char* format, ...); | 196 void Append(const char* format, ...); |
193 | 197 |
194 // Append string data to the log message. | 198 // Append string data to the log message. |
195 void AppendVA(const char* format, va_list args); | 199 void AppendVA(const char* format, va_list args); |
196 | 200 |
197 // Append a character to the log message. | 201 // Append a character to the log message. |
198 void Append(const char c); | 202 void Append(const char c); |
199 | 203 |
200 // Append a heap string. | 204 // Append a heap string. |
201 void Append(String* str); | 205 void Append(String* str); |
202 | 206 |
203 // Appends an address. | 207 // Appends an address. |
204 void AppendAddress(Address addr); | 208 void AppendAddress(Address addr); |
205 | 209 |
206 void AppendDetailed(String* str, bool show_impl_info); | 210 void AppendDetailed(String* str, bool show_impl_info); |
207 | 211 |
208 // Append a portion of a string. | 212 // Append a portion of a string. |
209 void AppendStringPart(const char* str, int len); | 213 void AppendStringPart(const char* str, int len); |
210 | 214 |
211 // Write the log message to the log file currently opened. | 215 // Write the log message to the log file currently opened. |
212 void WriteToLogFile(); | 216 void WriteToLogFile(); |
213 | 217 |
214 // A handler that is called when Log::Write fails. | 218 private: |
215 typedef void (*WriteFailureHandler)(); | |
216 | 219 |
217 static void set_write_failure_handler(WriteFailureHandler handler) { | 220 Log* log_; |
218 write_failure_handler = handler; | |
219 } | |
220 | |
221 private: | |
222 static WriteFailureHandler write_failure_handler; | |
223 | |
224 ScopedLock sl; | 221 ScopedLock sl; |
225 int pos_; | 222 int pos_; |
226 }; | 223 }; |
227 | 224 |
228 #endif // ENABLE_LOGGING_AND_PROFILING | 225 #endif // ENABLE_LOGGING_AND_PROFILING |
229 | 226 |
230 } } // namespace v8::internal | 227 } } // namespace v8::internal |
231 | 228 |
232 #endif // V8_LOG_UTILS_H_ | 229 #endif // V8_LOG_UTILS_H_ |
OLD | NEW |