blob: 43610497ef6382e72b3b73b9d68f8fa5781fa061 [file] [log] [blame]
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00001// Copyright 2009 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "log-utils.h"
31
32namespace v8 {
33namespace internal {
34
35#ifdef ENABLE_LOGGING_AND_PROFILING
36
37LogDynamicBuffer::LogDynamicBuffer(
38 int block_size, int max_size, const char* seal, int seal_size)
39 : block_size_(block_size),
40 max_size_(max_size - (max_size % block_size_)),
41 seal_(seal),
42 seal_size_(seal_size),
43 blocks_(max_size_ / block_size_ + 1),
44 write_pos_(0), block_index_(0), block_write_pos_(0), is_sealed_(false) {
45 ASSERT(BlocksCount() > 0);
46 AllocateBlock(0);
47 for (int i = 1; i < BlocksCount(); ++i) {
48 blocks_[i] = NULL;
49 }
50}
51
52
53LogDynamicBuffer::~LogDynamicBuffer() {
54 for (int i = 0; i < BlocksCount(); ++i) {
55 DeleteArray(blocks_[i]);
56 }
57}
58
59
60int LogDynamicBuffer::Read(int from_pos, char* dest_buf, int buf_size) {
61 if (buf_size == 0) return 0;
62 int read_pos = from_pos;
63 int block_read_index = BlockIndex(from_pos);
64 int block_read_pos = PosInBlock(from_pos);
65 int dest_buf_pos = 0;
66 // Read until dest_buf is filled, or write_pos_ encountered.
67 while (read_pos < write_pos_ && dest_buf_pos < buf_size) {
68 const int read_size = Min(write_pos_ - read_pos,
69 Min(buf_size - dest_buf_pos, block_size_ - block_read_pos));
70 memcpy(dest_buf + dest_buf_pos,
71 blocks_[block_read_index] + block_read_pos, read_size);
72 block_read_pos += read_size;
73 dest_buf_pos += read_size;
74 read_pos += read_size;
75 if (block_read_pos == block_size_) {
76 block_read_pos = 0;
77 ++block_read_index;
78 }
79 }
80 return dest_buf_pos;
81}
82
83
84int LogDynamicBuffer::Seal() {
85 WriteInternal(seal_, seal_size_);
86 is_sealed_ = true;
87 return 0;
88}
89
90
91int LogDynamicBuffer::Write(const char* data, int data_size) {
92 if (is_sealed_) {
93 return 0;
94 }
95 if ((write_pos_ + data_size) <= (max_size_ - seal_size_)) {
96 return WriteInternal(data, data_size);
97 } else {
98 return Seal();
99 }
100}
101
102
103int LogDynamicBuffer::WriteInternal(const char* data, int data_size) {
104 int data_pos = 0;
105 while (data_pos < data_size) {
106 const int write_size =
107 Min(data_size - data_pos, block_size_ - block_write_pos_);
108 memcpy(blocks_[block_index_] + block_write_pos_, data + data_pos,
109 write_size);
110 block_write_pos_ += write_size;
111 data_pos += write_size;
112 if (block_write_pos_ == block_size_) {
113 block_write_pos_ = 0;
114 AllocateBlock(++block_index_);
115 }
116 }
117 write_pos_ += data_size;
118 return data_size;
119}
120
121
122bool Log::is_stopped_ = false;
123Log::WritePtr Log::Write = NULL;
124FILE* Log::output_handle_ = NULL;
125LogDynamicBuffer* Log::output_buffer_ = NULL;
126// Must be the same message as in Logger::PauseProfiler
127const char* Log::kDynamicBufferSeal = "profiler,\"pause\"\n";
128Mutex* Log::mutex_ = NULL;
129char* Log::message_buffer_ = NULL;
130
131
132void Log::Init() {
133 mutex_ = OS::CreateMutex();
134 message_buffer_ = NewArray<char>(kMessageBufferSize);
135}
136
137
138void Log::OpenStdout() {
139 ASSERT(!IsEnabled());
140 output_handle_ = stdout;
141 Write = WriteToFile;
142 Init();
143}
144
145
146void Log::OpenFile(const char* name) {
147 ASSERT(!IsEnabled());
148 output_handle_ = OS::FOpen(name, OS::LogFileOpenMode);
149 Write = WriteToFile;
150 Init();
151}
152
153
154void Log::OpenMemoryBuffer() {
155 ASSERT(!IsEnabled());
156 output_buffer_ = new LogDynamicBuffer(
157 kDynamicBufferBlockSize, kMaxDynamicBufferSize,
158 kDynamicBufferSeal, strlen(kDynamicBufferSeal));
159 Write = WriteToMemory;
160 Init();
161}
162
163
164void Log::Close() {
165 if (Write == WriteToFile) {
166 fclose(output_handle_);
167 output_handle_ = NULL;
168 } else if (Write == WriteToMemory) {
169 delete output_buffer_;
170 output_buffer_ = NULL;
171 } else {
172 ASSERT(Write == NULL);
173 }
174 Write = NULL;
175
176 delete mutex_;
177 mutex_ = NULL;
178
179 is_stopped_ = false;
180}
181
182
183int Log::GetLogLines(int from_pos, char* dest_buf, int max_size) {
184 if (Write != WriteToMemory) return 0;
185 ASSERT(output_buffer_ != NULL);
186 ASSERT(from_pos >= 0);
187 ASSERT(max_size >= 0);
188 int actual_size = output_buffer_->Read(from_pos, dest_buf, max_size);
189 ASSERT(actual_size <= max_size);
190 if (actual_size == 0) return 0;
191
192 // Find previous log line boundary.
193 char* end_pos = dest_buf + actual_size - 1;
194 while (end_pos >= dest_buf && *end_pos != '\n') --end_pos;
195 actual_size = end_pos - dest_buf + 1;
196 ASSERT(actual_size <= max_size);
197 return actual_size;
198}
199
200
201LogMessageBuilder::WriteFailureHandler
202 LogMessageBuilder::write_failure_handler = NULL;
203
204
205LogMessageBuilder::LogMessageBuilder(): sl(Log::mutex_), pos_(0) {
206 ASSERT(Log::message_buffer_ != NULL);
207}
208
209
210void LogMessageBuilder::Append(const char* format, ...) {
211 Vector<char> buf(Log::message_buffer_ + pos_,
212 Log::kMessageBufferSize - pos_);
213 va_list args;
214 va_start(args, format);
215 Append(format, args);
216 va_end(args);
217 ASSERT(pos_ <= Log::kMessageBufferSize);
218}
219
220
221void LogMessageBuilder::Append(const char* format, va_list args) {
222 Vector<char> buf(Log::message_buffer_ + pos_,
223 Log::kMessageBufferSize - pos_);
224 int result = v8::internal::OS::VSNPrintF(buf, format, args);
225
226 // Result is -1 if output was truncated.
227 if (result >= 0) {
228 pos_ += result;
229 } else {
230 pos_ = Log::kMessageBufferSize;
231 }
232 ASSERT(pos_ <= Log::kMessageBufferSize);
233}
234
235
236void LogMessageBuilder::Append(const char c) {
237 if (pos_ < Log::kMessageBufferSize) {
238 Log::message_buffer_[pos_++] = c;
239 }
240 ASSERT(pos_ <= Log::kMessageBufferSize);
241}
242
243
244void LogMessageBuilder::Append(String* str) {
245 AssertNoAllocation no_heap_allocation; // Ensure string stay valid.
246 int length = str->length();
247 for (int i = 0; i < length; i++) {
248 Append(static_cast<char>(str->Get(i)));
249 }
250}
251
252
253void LogMessageBuilder::AppendDetailed(String* str, bool show_impl_info) {
254 AssertNoAllocation no_heap_allocation; // Ensure string stay valid.
255 int len = str->length();
256 if (len > 0x1000)
257 len = 0x1000;
258 if (show_impl_info) {
259 Append(str->IsAsciiRepresentation() ? 'a' : '2');
260 if (StringShape(str).IsExternal())
261 Append('e');
262 if (StringShape(str).IsSymbol())
263 Append('#');
264 Append(":%i:", str->length());
265 }
266 for (int i = 0; i < len; i++) {
267 uc32 c = str->Get(i);
268 if (c > 0xff) {
269 Append("\\u%04x", c);
270 } else if (c < 32 || c > 126) {
271 Append("\\x%02x", c);
272 } else if (c == ',') {
273 Append("\\,");
274 } else if (c == '\\') {
275 Append("\\\\");
276 } else {
277 Append("%lc", c);
278 }
279 }
280}
281
282
283void LogMessageBuilder::WriteToLogFile() {
284 ASSERT(pos_ <= Log::kMessageBufferSize);
285 const int written = Log::Write(Log::message_buffer_, pos_);
286 if (written != pos_ && write_failure_handler != NULL) {
287 write_failure_handler();
288 }
289}
290
291
292void LogMessageBuilder::WriteCStringToLogFile(const char* str) {
293 const int len = strlen(str);
294 const int written = Log::Write(str, len);
295 if (written != len && write_failure_handler != NULL) {
296 write_failure_handler();
297 }
298}
299
300#endif // ENABLE_LOGGING_AND_PROFILING
301
302} } // namespace v8::internal