Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2006-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 #ifndef V8_LOG_UTILS_H_ | |
| 29 #define V8_LOG_UTILS_H_ | |
| 30 | |
| 31 namespace v8 { | |
| 32 namespace internal { | |
| 33 | |
| 34 #ifdef ENABLE_LOGGING_AND_PROFILING | |
| 35 | |
| 36 // A memory buffer that increments its size as you write in it. Size | |
| 37 // is incremented with 'block_size' steps, never exceeding 'max_size'. | |
| 38 // During growth, memory contents are never copied. | |
| 39 // | |
| 40 // An instance of this class is created dynamically by Log. | |
| 41 class LogDynamicBuffer { | |
| 42 public: | |
| 43 LogDynamicBuffer(int block_size, int max_size); | |
| 44 | |
| 45 ~LogDynamicBuffer(); | |
| 46 | |
| 47 // Reads contents of the buffer starting from 'from_pos'. Upon | |
| 48 // return, 'dest_buf' is filled with the data. Actual amount of data | |
| 49 // filled is returned, it is <= that 'buf_size'. | |
|
Søren Thygesen Gjesse
2009/05/27 12:00:40
Remove that
Mikhail Naganov
2009/05/28 07:05:35
Done.
| |
| 50 int Read(int from_pos, char* dest_buf, int buf_size); | |
| 51 | |
| 52 // Writes 'data' to the buffer, making it larger if necessary. If | |
| 53 // data is too big to fit in the buffer, it doesn't get written at | |
| 54 // all. Returns amount of data written (it is either 'data_size', or | |
| 55 // 0, if 'data' is too big). | |
| 56 int Write(const char* data, int data_size); | |
| 57 | |
| 58 private: | |
| 59 int BlockIndex(int pos) const { return pos / block_size_; } | |
| 60 | |
| 61 int BlocksCount() const { return BlockIndex(max_size_) + 1; } | |
| 62 | |
| 63 int PosInBlock(int pos) const { return pos % block_size_; } | |
| 64 | |
| 65 const int block_size_; | |
| 66 const int max_size_; | |
| 67 ScopedVector<char*> blocks_; | |
| 68 int write_pos_; | |
| 69 int block_index_; | |
| 70 int block_write_pos_; | |
| 71 }; | |
| 72 | |
| 73 | |
| 74 // Functions and data for performing output of log messages. | |
| 75 class Log : public AllStatic { | |
| 76 public: | |
| 77 // Opens stdout for logging. | |
| 78 static void OpenStdout(); | |
| 79 | |
| 80 // Opens file for logging. | |
| 81 static void OpenFile(const char* name); | |
| 82 | |
| 83 // Opens memory buffer for logging. | |
| 84 static void OpenMemoryBuffer(); | |
| 85 | |
| 86 // Frees all resources acquired in Open... functions. | |
| 87 static void Close(); | |
| 88 | |
| 89 // See description in include/v8.h. | |
| 90 static int GetLogLines(int from_pos, char* dest_buf, int max_size); | |
| 91 | |
| 92 // Returns whether logging is enabled. | |
| 93 static bool IsEnabled() { | |
| 94 return output_handle_ != NULL || output_buffer_ != NULL; | |
| 95 } | |
| 96 | |
| 97 private: | |
| 98 typedef int (*WritePtr)(const char* msg, int length); | |
| 99 | |
| 100 // Initialization function called from Open... functions. | |
| 101 static void Init(); | |
| 102 | |
| 103 // Write functions assume that mutex_ is acquired by the caller. | |
| 104 static WritePtr Write; | |
| 105 | |
| 106 // Implementation of writing to a log file. | |
| 107 static int WriteToFile(const char* msg, int length) { | |
| 108 ASSERT(output_handle_ != NULL); | |
| 109 int rv = fwrite(msg, 1, length, output_handle_); | |
| 110 ASSERT(length == rv); | |
| 111 return rv; | |
| 112 } | |
| 113 | |
| 114 // Implementation of writing to a memory buffer. | |
| 115 static int WriteToMemory(const char* msg, int length) { | |
| 116 ASSERT(output_buffer_ != NULL); | |
| 117 return output_buffer_->Write(msg, length); | |
| 118 } | |
| 119 | |
| 120 // When logging is active, either output_handle_ or output_buffer_ is used | |
| 121 // to store a pointer to log destination. If logging was opened via OpenStdout | |
| 122 // or OpenFile, then output_handle_ is used. If logging was opened | |
| 123 // via OpenMemoryBuffer, then output_buffer_ is used. | |
| 124 // mutex_ should be acquired before using output_handle_ or output_buffer_. | |
| 125 static FILE* output_handle_; | |
| 126 | |
| 127 static LogDynamicBuffer* output_buffer_; | |
| 128 | |
| 129 // Size of dynamic buffer block (and dynamic buffer initial size). | |
| 130 static const int kDynamicBufferBlockSize = 65536; | |
| 131 | |
| 132 // Maximum size of dynamic buffer. | |
| 133 static const int kMaxDynamicBufferSize = 50 * 1024 * 1024; | |
| 134 | |
| 135 // mutex_ is a Mutex used for enforcing exclusive | |
| 136 // access to the formatting buffer and the log file or log memory buffer. | |
| 137 static Mutex* mutex_; | |
| 138 | |
| 139 // Size of buffer used for formatting log messages. | |
| 140 static const int kMessageBufferSize = 2048; | |
| 141 | |
| 142 // Buffer used for formatting log messages. This is a singleton buffer and | |
| 143 // mutex_ should be acquired before using it. | |
| 144 static char* message_buffer_; | |
| 145 | |
| 146 friend class LogMessageBuilder; | |
| 147 }; | |
| 148 | |
| 149 | |
| 150 // Utility class for formatting log messages. It fills the message into the | |
| 151 // static buffer in Log. | |
| 152 class LogMessageBuilder BASE_EMBEDDED { | |
| 153 public: | |
| 154 // Create a message builder starting from position 0. This acquires the mutex | |
| 155 // in the log as well. | |
| 156 explicit LogMessageBuilder(); | |
| 157 ~LogMessageBuilder() { } | |
| 158 | |
| 159 // Append string data to the log message. | |
| 160 void Append(const char* format, ...); | |
| 161 | |
| 162 // Append string data to the log message. | |
| 163 void Append(const char* format, va_list args); | |
| 164 | |
| 165 // Append a character to the log message. | |
| 166 void Append(const char c); | |
| 167 | |
| 168 // Append a heap string. | |
| 169 void Append(String* str); | |
| 170 | |
| 171 void AppendDetailed(String* str, bool show_impl_info); | |
| 172 | |
| 173 // Write the log message to the log file currently opened. | |
| 174 void WriteToLogFile(); | |
| 175 | |
| 176 // Write a null-terminated string to to the log file currently opened. | |
| 177 void WriteCStringToLogFile(const char* str); | |
| 178 | |
| 179 private: | |
| 180 ScopedLock sl; | |
| 181 int pos_; | |
| 182 }; | |
| 183 | |
| 184 #endif // ENABLE_LOGGING_AND_PROFILING | |
| 185 | |
| 186 } } // namespace v8::internal | |
| 187 | |
| 188 #endif // V8_LOG_UTILS_H_ | |
| OLD | NEW |