blob: ec8415e4b6ed5e31990cdc220c685d655df0061e [file] [log] [blame]
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +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
lrn@chromium.org1c092762011-05-09 09:42:16 +000031#include "allocation.h"
32
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +000033namespace v8 {
34namespace internal {
35
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000036class Logger;
37
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +000038// Functions and data for performing output of log messages.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000039class Log {
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +000040 public:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000041 // Performs process-wide initialization.
jkummerow@chromium.org10480472013-07-17 08:22:15 +000042 void Initialize(const char* log_file_name);
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +000043
44 // Disables logging, but preserves acquired resources.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000045 void stop() { is_stopped_ = true; }
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +000046
danno@chromium.orgca29dd82013-04-26 11:59:48 +000047 static bool InitLogAtStart() {
48 return FLAG_log || FLAG_log_runtime || FLAG_log_api
49 || FLAG_log_code || FLAG_log_gc || FLAG_log_handles || FLAG_log_suspect
50 || FLAG_log_regexp || FLAG_ll_prof || FLAG_log_internal_timer_events;
51 }
52
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000053 // Frees all resources acquired in Initialize and Open... functions.
whesse@chromium.org030d38e2011-07-13 13:23:34 +000054 // When a temporary file is used for the log, returns its stream descriptor,
55 // leaving the file open.
56 FILE* Close();
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +000057
58 // Returns whether logging is enabled.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000059 bool IsEnabled() {
whesse@chromium.org030d38e2011-07-13 13:23:34 +000060 return !is_stopped_ && output_handle_ != NULL;
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +000061 }
62
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +000063 // Size of buffer used for formatting log messages.
whesse@chromium.org030d38e2011-07-13 13:23:34 +000064 static const int kMessageBufferSize = 2048;
65
66 // This mode is only used in tests, as temporary files are automatically
67 // deleted on close and thus can't be accessed afterwards.
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +000068 static const char* const kLogToTemporaryFile;
jkummerow@chromium.org10480472013-07-17 08:22:15 +000069 static const char* const kLogToConsole;
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +000070
jkummerow@chromium.orgba72ec82013-07-22 09:21:20 +000071 // Utility class for formatting log messages. It fills the message into the
72 // static buffer in Log.
73 class MessageBuilder BASE_EMBEDDED {
74 public:
75 // Create a message builder starting from position 0.
76 // This acquires the mutex in the log as well.
77 explicit MessageBuilder(Log* log);
78 ~MessageBuilder() { }
79
80 // Append string data to the log message.
81 void Append(const char* format, ...);
82
83 // Append string data to the log message.
84 void AppendVA(const char* format, va_list args);
85
86 // Append a character to the log message.
87 void Append(const char c);
88
89 // Append double quoted string to the log message.
90 void AppendDoubleQuotedString(const char* string);
91
92 // Append a heap string.
93 void Append(String* str);
94
95 // Appends an address.
96 void AppendAddress(Address addr);
97
98 void AppendSymbolName(Symbol* symbol);
99
100 void AppendDetailed(String* str, bool show_impl_info);
101
102 // Append a portion of a string.
103 void AppendStringPart(const char* str, int len);
104
105 // Write the log message to the log file currently opened.
106 void WriteToLogFile();
107
108 private:
109 Log* log_;
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +0000110 LockGuard<Mutex> lock_guard_;
jkummerow@chromium.orgba72ec82013-07-22 09:21:20 +0000111 int pos_;
112 };
113
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000114 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000115 explicit Log(Logger* logger);
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000116
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000117 // Opens stdout for logging.
118 void OpenStdout();
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000119
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000120 // Opens file for logging.
121 void OpenFile(const char* name);
122
whesse@chromium.org030d38e2011-07-13 13:23:34 +0000123 // Opens a temporary file for logging.
124 void OpenTemporaryFile();
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000125
126 // Implementation of writing to a log file.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000127 int WriteToFile(const char* msg, int length) {
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000128 ASSERT(output_handle_ != NULL);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000129 size_t rv = fwrite(msg, 1, length, output_handle_);
130 ASSERT(static_cast<size_t>(length) == rv);
131 USE(rv);
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000132 fflush(output_handle_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000133 return length;
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000134 }
135
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000136 // Whether logging is stopped (e.g. due to insufficient resources).
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000137 bool is_stopped_;
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000138
whesse@chromium.org030d38e2011-07-13 13:23:34 +0000139 // When logging is active output_handle_ is used to store a pointer to log
140 // destination. mutex_ should be acquired before using output_handle_.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000141 FILE* output_handle_;
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000142
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000143 // mutex_ is a Mutex used for enforcing exclusive
144 // access to the formatting buffer and the log file or log memory buffer.
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +0000145 Mutex mutex_;
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000146
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000147 // Buffer used for formatting log messages. This is a singleton buffer and
148 // mutex_ should be acquired before using it.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000149 char* message_buffer_;
150
151 Logger* logger_;
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000152
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000153 friend class Logger;
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000154};
155
156
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000157} } // namespace v8::internal
158
159#endif // V8_LOG_UTILS_H_