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 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 const int max_size_; | 80 const int max_size_; |
81 const char* seal_; | 81 const char* seal_; |
82 const int seal_size_; | 82 const int seal_size_; |
83 ScopedVector<char*> blocks_; | 83 ScopedVector<char*> blocks_; |
84 int write_pos_; | 84 int write_pos_; |
85 int block_index_; | 85 int block_index_; |
86 int block_write_pos_; | 86 int block_write_pos_; |
87 bool is_sealed_; | 87 bool is_sealed_; |
88 }; | 88 }; |
89 | 89 |
| 90 class LogData { |
| 91 // Whether logging is stopped (e.g. due to insufficient resources). |
| 92 bool is_stopped_; |
| 93 |
| 94 // When logging is active, either output_handle_ or output_buffer_ is used |
| 95 // to store a pointer to log destination. If logging was opened via OpenStdout |
| 96 // or OpenFile, then output_handle_ is used. If logging was opened |
| 97 // via OpenMemoryBuffer, then output_buffer_ is used. |
| 98 // mutex_ should be acquired before using output_handle_ or output_buffer_. |
| 99 FILE* output_handle_; |
| 100 |
| 101 LogDynamicBuffer* output_buffer_; |
| 102 |
| 103 // mutex_ is a Mutex used for enforcing exclusive |
| 104 // access to the formatting buffer and the log file or log memory buffer. |
| 105 Mutex* mutex_; |
| 106 |
| 107 // Buffer used for formatting log messages. This is a singleton buffer and |
| 108 // mutex_ should be acquired before using it. |
| 109 char* message_buffer_; |
| 110 |
| 111 typedef int (*WritePtr)(const char* msg, int length); |
| 112 // Write functions assume that mutex_ is acquired by the caller. |
| 113 WritePtr Write; |
| 114 |
| 115 // A handler that is called when Log::Write fails. |
| 116 typedef void (*WriteFailureHandler)(); |
| 117 |
| 118 WriteFailureHandler write_failure_handler; |
| 119 |
| 120 void set_write_failure_handler(WriteFailureHandler handler) { |
| 121 write_failure_handler = handler; |
| 122 } |
| 123 |
| 124 friend class V8Context; |
| 125 friend class Log; |
| 126 friend class Logger; |
| 127 friend class LogMessageBuilder; |
| 128 |
| 129 LogData(); |
| 130 DISALLOW_COPY_AND_ASSIGN(LogData); |
| 131 }; |
90 | 132 |
91 // Functions and data for performing output of log messages. | 133 // Functions and data for performing output of log messages. |
92 class Log : public AllStatic { | 134 class Log : public AllStatic { |
93 public: | 135 public: |
94 // Opens stdout for logging. | 136 // Opens stdout for logging. |
95 static void OpenStdout(); | 137 static void OpenStdout(); |
96 | 138 |
97 // Opens file for logging. | 139 // Opens file for logging. |
98 static void OpenFile(const char* name); | 140 static void OpenFile(const char* name); |
99 | 141 |
100 // Opens memory buffer for logging. | 142 // Opens memory buffer for logging. |
101 static void OpenMemoryBuffer(); | 143 static void OpenMemoryBuffer(); |
102 | 144 |
103 // Disables logging, but preserves acquired resources. | 145 // Disables logging, but preserves acquired resources. |
104 static void stop() { is_stopped_ = true; } | 146 static void stop() { |
| 147 v8_context()->log_data_.is_stopped_ = true; |
| 148 } |
105 | 149 |
106 // Frees all resources acquired in Open... functions. | 150 // Frees all resources acquired in Open... functions. |
107 static void Close(); | 151 static void Close(); |
108 | 152 |
109 // See description in include/v8.h. | 153 // See description in include/v8.h. |
110 static int GetLogLines(int from_pos, char* dest_buf, int max_size); | 154 static int GetLogLines(int from_pos, char* dest_buf, int max_size); |
111 | 155 |
112 // Returns whether logging is enabled. | 156 // Returns whether logging is enabled. |
113 static bool IsEnabled() { | 157 static bool IsEnabled() { |
114 return !is_stopped_ && (output_handle_ != NULL || output_buffer_ != NULL); | 158 LogData& log_data = v8_context()->log_data_; |
| 159 return !log_data.is_stopped_ && |
| 160 (log_data.output_handle_ != NULL || log_data.output_buffer_ != NULL); |
115 } | 161 } |
116 | 162 |
117 // Size of buffer used for formatting log messages. | 163 // Size of buffer used for formatting log messages. |
118 static const int kMessageBufferSize = 2048; | 164 static const int kMessageBufferSize = 2048; |
119 | 165 |
120 private: | 166 private: |
121 typedef int (*WritePtr)(const char* msg, int length); | |
122 | |
123 // Initialization function called from Open... functions. | 167 // Initialization function called from Open... functions. |
124 static void Init(); | 168 static void Init(); |
125 | 169 |
126 // Write functions assume that mutex_ is acquired by the caller. | |
127 static WritePtr Write; | |
128 | |
129 // Implementation of writing to a log file. | 170 // Implementation of writing to a log file. |
130 static int WriteToFile(const char* msg, int length) { | 171 static int WriteToFile(const char* msg, int length) { |
131 ASSERT(output_handle_ != NULL); | 172 LogData& log_data = v8_context()->log_data_; |
132 size_t rv = fwrite(msg, 1, length, output_handle_); | 173 ASSERT(log_data.output_handle_ != NULL); |
| 174 size_t rv = fwrite(msg, 1, length, log_data.output_handle_); |
133 ASSERT(static_cast<size_t>(length) == rv); | 175 ASSERT(static_cast<size_t>(length) == rv); |
134 USE(rv); | 176 USE(rv); |
135 return length; | 177 return length; |
136 } | 178 } |
137 | 179 |
138 // Implementation of writing to a memory buffer. | 180 // Implementation of writing to a memory buffer. |
139 static int WriteToMemory(const char* msg, int length) { | 181 static int WriteToMemory(const char* msg, int length) { |
140 ASSERT(output_buffer_ != NULL); | 182 LogData& log_data = v8_context()->log_data_; |
141 return output_buffer_->Write(msg, length); | 183 ASSERT(log_data.output_buffer_ != NULL); |
| 184 return log_data.output_buffer_->Write(msg, length); |
142 } | 185 } |
143 | 186 |
144 // Whether logging is stopped (e.g. due to insufficient resources). | 187 // Size of dynamic buffer block (and dynamic buffer initial size). |
145 static bool is_stopped_; | |
146 | |
147 // When logging is active, either output_handle_ or output_buffer_ is used | |
148 // to store a pointer to log destination. If logging was opened via OpenStdout | |
149 // or OpenFile, then output_handle_ is used. If logging was opened | |
150 // via OpenMemoryBuffer, then output_buffer_ is used. | |
151 // mutex_ should be acquired before using output_handle_ or output_buffer_. | |
152 static FILE* output_handle_; | |
153 | |
154 static LogDynamicBuffer* output_buffer_; | |
155 | |
156 // Size of dynamic buffer block (and dynamic buffer initial size). | |
157 static const int kDynamicBufferBlockSize = 65536; | 188 static const int kDynamicBufferBlockSize = 65536; |
158 | 189 |
159 // Maximum size of dynamic buffer. | 190 // Maximum size of dynamic buffer. |
160 static const int kMaxDynamicBufferSize = 50 * 1024 * 1024; | 191 static const int kMaxDynamicBufferSize = 50 * 1024 * 1024; |
161 | 192 |
162 // Message to "seal" dynamic buffer with. | 193 // Message to "seal" dynamic buffer with. |
163 static const char* kDynamicBufferSeal; | 194 static const char kDynamicBufferSeal[]; |
164 | |
165 // mutex_ is a Mutex used for enforcing exclusive | |
166 // access to the formatting buffer and the log file or log memory buffer. | |
167 static Mutex* mutex_; | |
168 | |
169 // Buffer used for formatting log messages. This is a singleton buffer and | |
170 // mutex_ should be acquired before using it. | |
171 static char* message_buffer_; | |
172 | 195 |
173 friend class LogMessageBuilder; | 196 friend class LogMessageBuilder; |
174 friend class LogRecordCompressor; | 197 friend class LogRecordCompressor; |
175 }; | 198 }; |
176 | 199 |
177 | 200 |
178 // An utility class for performing backward reference compression | 201 // An utility class for performing backward reference compression |
179 // of string ends. It operates using a window of previous strings. | 202 // of string ends. It operates using a window of previous strings. |
180 class LogRecordCompressor { | 203 class LogRecordCompressor { |
181 public: | 204 public: |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 // Does the same at the version without arguments, and sets a prefix. | 287 // Does the same at the version without arguments, and sets a prefix. |
265 bool RetrieveCompressedPrevious(LogRecordCompressor* compressor, | 288 bool RetrieveCompressedPrevious(LogRecordCompressor* compressor, |
266 const char* prefix); | 289 const char* prefix); |
267 | 290 |
268 // Write the log message to the log file currently opened. | 291 // Write the log message to the log file currently opened. |
269 void WriteToLogFile(); | 292 void WriteToLogFile(); |
270 | 293 |
271 // Write a null-terminated string to to the log file currently opened. | 294 // Write a null-terminated string to to the log file currently opened. |
272 void WriteCStringToLogFile(const char* str); | 295 void WriteCStringToLogFile(const char* str); |
273 | 296 |
274 // A handler that is called when Log::Write fails. | |
275 typedef void (*WriteFailureHandler)(); | |
276 | |
277 static void set_write_failure_handler(WriteFailureHandler handler) { | |
278 write_failure_handler = handler; | |
279 } | |
280 | |
281 private: | 297 private: |
282 static WriteFailureHandler write_failure_handler; | 298 LogData& log_data_; |
283 | |
284 ScopedLock sl; | 299 ScopedLock sl; |
285 int pos_; | 300 int pos_; |
286 }; | 301 }; |
287 | 302 |
288 #endif // ENABLE_LOGGING_AND_PROFILING | 303 #endif // ENABLE_LOGGING_AND_PROFILING |
289 | 304 |
290 } } // namespace v8::internal | 305 } } // namespace v8::internal |
291 | 306 |
292 #endif // V8_LOG_UTILS_H_ | 307 #endif // V8_LOG_UTILS_H_ |
OLD | NEW |