OLD | NEW |
---|---|
1 // Copyright 2009 the V8 project authors. All rights reserved. | 1 // Copyright 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 16 matching lines...) Expand all Loading... | |
27 | 27 |
28 #include "v8.h" | 28 #include "v8.h" |
29 | 29 |
30 #include "log-utils.h" | 30 #include "log-utils.h" |
31 | 31 |
32 namespace v8 { | 32 namespace v8 { |
33 namespace internal { | 33 namespace internal { |
34 | 34 |
35 #ifdef ENABLE_LOGGING_AND_PROFILING | 35 #ifdef ENABLE_LOGGING_AND_PROFILING |
36 | 36 |
37 LogDynamicBuffer::LogDynamicBuffer(int block_size, int max_size) | 37 LogDynamicBuffer::LogDynamicBuffer( |
38 int block_size, int max_size, const char* seal, int seal_size) | |
38 : block_size_(block_size), | 39 : block_size_(block_size), |
39 max_size_(max_size - (max_size % block_size_)), | 40 max_size_(max_size - (max_size % block_size_)), |
41 seal_(seal), | |
42 seal_size_(seal_size), | |
40 blocks_(max_size_ / block_size_ + 1), | 43 blocks_(max_size_ / block_size_ + 1), |
41 write_pos_(0), block_index_(0), block_write_pos_(0) { | 44 write_pos_(0), block_index_(0), block_write_pos_(0), is_sealed_(false) { |
42 ASSERT(BlocksCount() > 0); | 45 ASSERT(BlocksCount() > 0); |
43 AllocateBlock(0); | 46 AllocateBlock(0); |
44 for (int i = 1; i < BlocksCount(); ++i) { | 47 for (int i = 1; i < BlocksCount(); ++i) { |
45 blocks_[i] = NULL; | 48 blocks_[i] = NULL; |
46 } | 49 } |
47 } | 50 } |
48 | 51 |
49 | 52 |
50 LogDynamicBuffer::~LogDynamicBuffer() { | 53 LogDynamicBuffer::~LogDynamicBuffer() { |
51 for (int i = 0; i < BlocksCount(); ++i) { | 54 for (int i = 0; i < BlocksCount(); ++i) { |
(...skipping 19 matching lines...) Expand all Loading... | |
71 read_pos += read_size; | 74 read_pos += read_size; |
72 if (block_read_pos == block_size_) { | 75 if (block_read_pos == block_size_) { |
73 block_read_pos = 0; | 76 block_read_pos = 0; |
74 ++block_read_index; | 77 ++block_read_index; |
75 } | 78 } |
76 } | 79 } |
77 return dest_buf_pos; | 80 return dest_buf_pos; |
78 } | 81 } |
79 | 82 |
80 | 83 |
84 int LogDynamicBuffer::Seal() { | |
85 WriteInternal(seal_, seal_size_); | |
86 is_sealed_ = true; | |
87 return 0; | |
88 } | |
89 | |
90 | |
81 int LogDynamicBuffer::Write(const char* data, int data_size) { | 91 int LogDynamicBuffer::Write(const char* data, int data_size) { |
82 if ((write_pos_ + data_size) > max_size_) return 0; | 92 if (is_sealed_) return 0; |
Søren Thygesen Gjesse
2009/05/28 13:23:35
Please use {} for one line if's.
Mikhail Naganov
2009/05/28 13:52:52
Done.
| |
93 if ((write_pos_ + data_size) <= (max_size_ - seal_size_)) { | |
94 return WriteInternal(data, data_size); | |
95 } else { | |
96 return Seal(); | |
97 } | |
98 } | |
99 | |
100 | |
101 int LogDynamicBuffer::WriteInternal(const char* data, int data_size) { | |
83 int data_pos = 0; | 102 int data_pos = 0; |
84 while (data_pos < data_size) { | 103 while (data_pos < data_size) { |
85 const int write_size = | 104 const int write_size = |
86 Min(data_size - data_pos, block_size_ - block_write_pos_); | 105 Min(data_size - data_pos, block_size_ - block_write_pos_); |
87 memcpy(blocks_[block_index_] + block_write_pos_, data + data_pos, | 106 memcpy(blocks_[block_index_] + block_write_pos_, data + data_pos, |
88 write_size); | 107 write_size); |
89 block_write_pos_ += write_size; | 108 block_write_pos_ += write_size; |
90 data_pos += write_size; | 109 data_pos += write_size; |
91 if (block_write_pos_ == block_size_) { | 110 if (block_write_pos_ == block_size_) { |
92 block_write_pos_ = 0; | 111 block_write_pos_ = 0; |
93 AllocateBlock(++block_index_); | 112 AllocateBlock(++block_index_); |
94 } | 113 } |
95 } | 114 } |
96 write_pos_ += data_size; | 115 write_pos_ += data_size; |
97 return data_size; | 116 return data_size; |
98 } | 117 } |
99 | 118 |
100 | 119 |
120 bool Log::is_stopped_ = false; | |
101 Log::WritePtr Log::Write = NULL; | 121 Log::WritePtr Log::Write = NULL; |
102 FILE* Log::output_handle_ = NULL; | 122 FILE* Log::output_handle_ = NULL; |
103 LogDynamicBuffer* Log::output_buffer_ = NULL; | 123 LogDynamicBuffer* Log::output_buffer_ = NULL; |
124 // Must be the same message as in Logger::PauseProfiler | |
125 const char* Log::kDynamicBufferSeal = "profiler,\"pause\"\n"; | |
104 Mutex* Log::mutex_ = NULL; | 126 Mutex* Log::mutex_ = NULL; |
105 char* Log::message_buffer_ = NULL; | 127 char* Log::message_buffer_ = NULL; |
106 | 128 |
107 | 129 |
108 void Log::Init() { | 130 void Log::Init() { |
109 mutex_ = OS::CreateMutex(); | 131 mutex_ = OS::CreateMutex(); |
110 message_buffer_ = NewArray<char>(kMessageBufferSize); | 132 message_buffer_ = NewArray<char>(kMessageBufferSize); |
111 } | 133 } |
112 | 134 |
113 | 135 |
114 void Log::OpenStdout() { | 136 void Log::OpenStdout() { |
115 ASSERT(!IsEnabled()); | 137 ASSERT(!IsEnabled()); |
116 output_handle_ = stdout; | 138 output_handle_ = stdout; |
117 Write = WriteToFile; | 139 Write = WriteToFile; |
118 Init(); | 140 Init(); |
119 } | 141 } |
120 | 142 |
121 | 143 |
122 void Log::OpenFile(const char* name) { | 144 void Log::OpenFile(const char* name) { |
123 ASSERT(!IsEnabled()); | 145 ASSERT(!IsEnabled()); |
124 output_handle_ = OS::FOpen(name, OS::LogFileOpenMode); | 146 output_handle_ = OS::FOpen(name, OS::LogFileOpenMode); |
125 Write = WriteToFile; | 147 Write = WriteToFile; |
126 Init(); | 148 Init(); |
127 } | 149 } |
128 | 150 |
129 | 151 |
130 void Log::OpenMemoryBuffer() { | 152 void Log::OpenMemoryBuffer() { |
131 ASSERT(!IsEnabled()); | 153 ASSERT(!IsEnabled()); |
132 output_buffer_ = new LogDynamicBuffer( | 154 output_buffer_ = new LogDynamicBuffer( |
133 kDynamicBufferBlockSize, kMaxDynamicBufferSize); | 155 kDynamicBufferBlockSize, kMaxDynamicBufferSize, |
156 kDynamicBufferSeal, strlen(kDynamicBufferSeal)); | |
134 Write = WriteToMemory; | 157 Write = WriteToMemory; |
135 Init(); | 158 Init(); |
136 } | 159 } |
137 | 160 |
138 | 161 |
139 void Log::Close() { | 162 void Log::Close() { |
140 if (Write == WriteToFile) { | 163 if (Write == WriteToFile) { |
141 fclose(output_handle_); | 164 fclose(output_handle_); |
142 output_handle_ = NULL; | 165 output_handle_ = NULL; |
143 } else if (Write == WriteToMemory) { | 166 } else if (Write == WriteToMemory) { |
144 delete output_buffer_; | 167 delete output_buffer_; |
145 output_buffer_ = NULL; | 168 output_buffer_ = NULL; |
146 } else { | 169 } else { |
147 ASSERT(Write == NULL); | 170 ASSERT(Write == NULL); |
148 } | 171 } |
149 Write = NULL; | 172 Write = NULL; |
150 | 173 |
151 delete mutex_; | 174 delete mutex_; |
152 mutex_ = NULL; | 175 mutex_ = NULL; |
176 | |
177 is_stopped_ = false; | |
153 } | 178 } |
154 | 179 |
155 | 180 |
156 int Log::GetLogLines(int from_pos, char* dest_buf, int max_size) { | 181 int Log::GetLogLines(int from_pos, char* dest_buf, int max_size) { |
157 if (Write != WriteToMemory) return 0; | 182 if (Write != WriteToMemory) return 0; |
158 ASSERT(output_buffer_ != NULL); | 183 ASSERT(output_buffer_ != NULL); |
159 ASSERT(from_pos >= 0); | 184 ASSERT(from_pos >= 0); |
160 ASSERT(max_size >= 0); | 185 ASSERT(max_size >= 0); |
161 int actual_size = output_buffer_->Read(from_pos, dest_buf, max_size); | 186 int actual_size = output_buffer_->Read(from_pos, dest_buf, max_size); |
162 ASSERT(actual_size <= max_size); | 187 ASSERT(actual_size <= max_size); |
163 if (actual_size == 0) return 0; | 188 if (actual_size == 0) return 0; |
164 | 189 |
165 // Find previous log line boundary. | 190 // Find previous log line boundary. |
166 char* end_pos = dest_buf + actual_size - 1; | 191 char* end_pos = dest_buf + actual_size - 1; |
167 while (end_pos >= dest_buf && *end_pos != '\n') --end_pos; | 192 while (end_pos >= dest_buf && *end_pos != '\n') --end_pos; |
168 actual_size = end_pos - dest_buf + 1; | 193 actual_size = end_pos - dest_buf + 1; |
169 ASSERT(actual_size <= max_size); | 194 ASSERT(actual_size <= max_size); |
170 return actual_size; | 195 return actual_size; |
171 } | 196 } |
172 | 197 |
173 | 198 |
199 LogMessageBuilder::WriteFailureHandler | |
200 LogMessageBuilder::write_failure_handler = NULL; | |
201 | |
202 | |
174 LogMessageBuilder::LogMessageBuilder(): sl(Log::mutex_), pos_(0) { | 203 LogMessageBuilder::LogMessageBuilder(): sl(Log::mutex_), pos_(0) { |
175 ASSERT(Log::message_buffer_ != NULL); | 204 ASSERT(Log::message_buffer_ != NULL); |
176 } | 205 } |
177 | 206 |
178 | 207 |
179 void LogMessageBuilder::Append(const char* format, ...) { | 208 void LogMessageBuilder::Append(const char* format, ...) { |
180 Vector<char> buf(Log::message_buffer_ + pos_, | 209 Vector<char> buf(Log::message_buffer_ + pos_, |
181 Log::kMessageBufferSize - pos_); | 210 Log::kMessageBufferSize - pos_); |
182 va_list args; | 211 va_list args; |
183 va_start(args, format); | 212 va_start(args, format); |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
244 Append("\\\\"); | 273 Append("\\\\"); |
245 } else { | 274 } else { |
246 Append("%lc", c); | 275 Append("%lc", c); |
247 } | 276 } |
248 } | 277 } |
249 } | 278 } |
250 | 279 |
251 | 280 |
252 void LogMessageBuilder::WriteToLogFile() { | 281 void LogMessageBuilder::WriteToLogFile() { |
253 ASSERT(pos_ <= Log::kMessageBufferSize); | 282 ASSERT(pos_ <= Log::kMessageBufferSize); |
254 Log::Write(Log::message_buffer_, pos_); | 283 if (pos_ != Log::Write(Log::message_buffer_, pos_)) { |
Søren Thygesen Gjesse
2009/05/28 13:23:35
I would suggest the following:
int written = Log:
Mikhail Naganov
2009/05/28 13:52:52
Done. Thanks for noticing.
| |
284 if (write_failure_handler != NULL) write_failure_handler(); | |
285 } | |
255 } | 286 } |
256 | 287 |
257 | 288 |
258 void LogMessageBuilder::WriteCStringToLogFile(const char* str) { | 289 void LogMessageBuilder::WriteCStringToLogFile(const char* str) { |
259 int len = strlen(str); | 290 int len = strlen(str); |
260 Log::Write(str, len); | 291 if (len != Log::Write(str, len)) { |
Søren Thygesen Gjesse
2009/05/28 13:23:35
Ditto.
Mikhail Naganov
2009/05/28 13:52:52
Done.
| |
292 if (write_failure_handler != NULL) write_failure_handler(); | |
293 } | |
261 } | 294 } |
262 | 295 |
263 #endif // ENABLE_LOGGING_AND_PROFILING | 296 #endif // ENABLE_LOGGING_AND_PROFILING |
264 | 297 |
265 } } // namespace v8::internal | 298 } } // namespace v8::internal |
OLD | NEW |