blob: d336d714b968dafceca88d90d60435bcbfd9f6e6 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// 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
Ben Murdoch257744e2011-11-30 15:57:28 +000031#include "allocation.h"
32
Steve Blocka7e24c12009-10-30 11:49:00 +000033namespace v8 {
34namespace internal {
35
Steve Block44f0eee2011-05-26 01:26:41 +010036class Logger;
37
Steve Blocka7e24c12009-10-30 11:49:00 +000038// Functions and data for performing output of log messages.
Steve Block44f0eee2011-05-26 01:26:41 +010039class Log {
Steve Blocka7e24c12009-10-30 11:49:00 +000040 public:
Steve Block44f0eee2011-05-26 01:26:41 +010041 // Performs process-wide initialization.
42 void Initialize();
Steve Blocka7e24c12009-10-30 11:49:00 +000043
44 // Disables logging, but preserves acquired resources.
Steve Block44f0eee2011-05-26 01:26:41 +010045 void stop() { is_stopped_ = true; }
Steve Blocka7e24c12009-10-30 11:49:00 +000046
Steve Block44f0eee2011-05-26 01:26:41 +010047 // Frees all resources acquired in Initialize and Open... functions.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000048 // When a temporary file is used for the log, returns its stream descriptor,
49 // leaving the file open.
50 FILE* Close();
Steve Blocka7e24c12009-10-30 11:49:00 +000051
52 // Returns whether logging is enabled.
Steve Block44f0eee2011-05-26 01:26:41 +010053 bool IsEnabled() {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000054 return !is_stopped_ && output_handle_ != NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +000055 }
56
57 // Size of buffer used for formatting log messages.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000058 static const int kMessageBufferSize = 2048;
59
60 // This mode is only used in tests, as temporary files are automatically
61 // deleted on close and thus can't be accessed afterwards.
62 static const char* kLogToTemporaryFile;
Steve Blocka7e24c12009-10-30 11:49:00 +000063
64 private:
Steve Block44f0eee2011-05-26 01:26:41 +010065 explicit Log(Logger* logger);
Steve Blocka7e24c12009-10-30 11:49:00 +000066
Steve Block44f0eee2011-05-26 01:26:41 +010067 // Opens stdout for logging.
68 void OpenStdout();
Steve Blocka7e24c12009-10-30 11:49:00 +000069
Steve Block44f0eee2011-05-26 01:26:41 +010070 // Opens file for logging.
71 void OpenFile(const char* name);
72
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000073 // Opens a temporary file for logging.
74 void OpenTemporaryFile();
Steve Blocka7e24c12009-10-30 11:49:00 +000075
76 // Implementation of writing to a log file.
Steve Block44f0eee2011-05-26 01:26:41 +010077 int WriteToFile(const char* msg, int length) {
Steve Blocka7e24c12009-10-30 11:49:00 +000078 ASSERT(output_handle_ != NULL);
Steve Blockd0582a62009-12-15 09:54:21 +000079 size_t rv = fwrite(msg, 1, length, output_handle_);
80 ASSERT(static_cast<size_t>(length) == rv);
81 USE(rv);
Ben Murdochf87a2032010-10-22 12:50:53 +010082 fflush(output_handle_);
Steve Blockd0582a62009-12-15 09:54:21 +000083 return length;
Steve Blocka7e24c12009-10-30 11:49:00 +000084 }
85
Steve Blocka7e24c12009-10-30 11:49:00 +000086 // Whether logging is stopped (e.g. due to insufficient resources).
Steve Block44f0eee2011-05-26 01:26:41 +010087 bool is_stopped_;
Steve Blocka7e24c12009-10-30 11:49:00 +000088
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000089 // When logging is active output_handle_ is used to store a pointer to log
90 // destination. mutex_ should be acquired before using output_handle_.
Steve Block44f0eee2011-05-26 01:26:41 +010091 FILE* output_handle_;
Steve Blocka7e24c12009-10-30 11:49:00 +000092
Ben Murdoch257744e2011-11-30 15:57:28 +000093 // Used when low-level profiling is active.
94 FILE* ll_output_handle_;
Ben Murdochf87a2032010-10-22 12:50:53 +010095
Steve Blocka7e24c12009-10-30 11:49:00 +000096 // mutex_ is a Mutex used for enforcing exclusive
97 // access to the formatting buffer and the log file or log memory buffer.
Steve Block44f0eee2011-05-26 01:26:41 +010098 Mutex* mutex_;
Steve Blocka7e24c12009-10-30 11:49:00 +000099
100 // Buffer used for formatting log messages. This is a singleton buffer and
101 // mutex_ should be acquired before using it.
Steve Block44f0eee2011-05-26 01:26:41 +0100102 char* message_buffer_;
103
104 Logger* logger_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000105
Ben Murdochf87a2032010-10-22 12:50:53 +0100106 friend class Logger;
Steve Blocka7e24c12009-10-30 11:49:00 +0000107 friend class LogMessageBuilder;
Steve Blocka7e24c12009-10-30 11:49:00 +0000108};
109
110
111// Utility class for formatting log messages. It fills the message into the
112// static buffer in Log.
113class LogMessageBuilder BASE_EMBEDDED {
114 public:
115 // Create a message builder starting from position 0. This acquires the mutex
116 // in the log as well.
Steve Block44f0eee2011-05-26 01:26:41 +0100117 explicit LogMessageBuilder(Logger* logger);
Steve Blocka7e24c12009-10-30 11:49:00 +0000118 ~LogMessageBuilder() { }
119
120 // Append string data to the log message.
121 void Append(const char* format, ...);
122
123 // Append string data to the log message.
124 void AppendVA(const char* format, va_list args);
125
126 // Append a character to the log message.
127 void Append(const char c);
128
129 // Append a heap string.
130 void Append(String* str);
131
Ben Murdochb0fe1622011-05-05 13:52:32 +0100132 // Appends an address.
Steve Blocka7e24c12009-10-30 11:49:00 +0000133 void AppendAddress(Address addr);
134
Steve Blocka7e24c12009-10-30 11:49:00 +0000135 void AppendDetailed(String* str, bool show_impl_info);
136
137 // Append a portion of a string.
138 void AppendStringPart(const char* str, int len);
139
Steve Blocka7e24c12009-10-30 11:49:00 +0000140 // Write the log message to the log file currently opened.
141 void WriteToLogFile();
142
Steve Blocka7e24c12009-10-30 11:49:00 +0000143 private:
Steve Blocka7e24c12009-10-30 11:49:00 +0000144
Steve Block44f0eee2011-05-26 01:26:41 +0100145 Log* log_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000146 ScopedLock sl;
147 int pos_;
148};
149
Steve Blocka7e24c12009-10-30 11:49:00 +0000150} } // namespace v8::internal
151
152#endif // V8_LOG_UTILS_H_