2009-05-28 07:08:09 +00:00
|
|
|
// Copyright 2006-2009 the V8 project authors. All rights reserved.
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
#ifndef V8_LOG_UTILS_H_
|
|
|
|
#define V8_LOG_UTILS_H_
|
|
|
|
|
2011-05-06 06:50:20 +00:00
|
|
|
#include "allocation.h"
|
|
|
|
|
2009-05-28 07:08:09 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
class Logger;
|
|
|
|
|
2009-05-28 07:08:09 +00:00
|
|
|
// Functions and data for performing output of log messages.
|
2011-03-18 20:35:07 +00:00
|
|
|
class Log {
|
2009-05-28 07:08:09 +00:00
|
|
|
public:
|
2011-03-18 20:35:07 +00:00
|
|
|
// Performs process-wide initialization.
|
2013-07-15 11:35:39 +00:00
|
|
|
void Initialize(const char* log_file_name);
|
2009-05-28 07:08:09 +00:00
|
|
|
|
2009-05-28 13:56:32 +00:00
|
|
|
// Disables logging, but preserves acquired resources.
|
2011-03-18 20:35:07 +00:00
|
|
|
void stop() { is_stopped_ = true; }
|
2009-05-28 13:56:32 +00:00
|
|
|
|
2013-04-24 14:44:08 +00:00
|
|
|
static bool InitLogAtStart() {
|
|
|
|
return FLAG_log || FLAG_log_runtime || FLAG_log_api
|
|
|
|
|| FLAG_log_code || FLAG_log_gc || FLAG_log_handles || FLAG_log_suspect
|
|
|
|
|| FLAG_log_regexp || FLAG_ll_prof || FLAG_log_internal_timer_events;
|
|
|
|
}
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
// Frees all resources acquired in Initialize and Open... functions.
|
2011-07-13 11:31:22 +00:00
|
|
|
// When a temporary file is used for the log, returns its stream descriptor,
|
|
|
|
// leaving the file open.
|
|
|
|
FILE* Close();
|
2009-05-28 07:08:09 +00:00
|
|
|
|
|
|
|
// Returns whether logging is enabled.
|
2011-03-18 20:35:07 +00:00
|
|
|
bool IsEnabled() {
|
2011-07-13 11:31:22 +00:00
|
|
|
return !is_stopped_ && output_handle_ != NULL;
|
2009-05-28 07:08:09 +00:00
|
|
|
}
|
|
|
|
|
2009-09-18 12:05:18 +00:00
|
|
|
// Size of buffer used for formatting log messages.
|
2011-07-13 11:31:22 +00:00
|
|
|
static const int kMessageBufferSize = 2048;
|
|
|
|
|
|
|
|
// This mode is only used in tests, as temporary files are automatically
|
|
|
|
// deleted on close and thus can't be accessed afterwards.
|
2011-08-05 11:32:46 +00:00
|
|
|
static const char* const kLogToTemporaryFile;
|
2013-07-15 11:35:39 +00:00
|
|
|
static const char* const kLogToConsole;
|
2009-09-18 12:05:18 +00:00
|
|
|
|
2009-05-28 07:08:09 +00:00
|
|
|
private:
|
2011-03-18 20:35:07 +00:00
|
|
|
explicit Log(Logger* logger);
|
|
|
|
|
|
|
|
// Opens stdout for logging.
|
|
|
|
void OpenStdout();
|
2009-05-28 07:08:09 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
// Opens file for logging.
|
|
|
|
void OpenFile(const char* name);
|
2009-05-28 07:08:09 +00:00
|
|
|
|
2011-07-13 11:31:22 +00:00
|
|
|
// Opens a temporary file for logging.
|
|
|
|
void OpenTemporaryFile();
|
2009-05-28 07:08:09 +00:00
|
|
|
|
|
|
|
// Implementation of writing to a log file.
|
2011-03-18 20:35:07 +00:00
|
|
|
int WriteToFile(const char* msg, int length) {
|
2009-05-28 07:08:09 +00:00
|
|
|
ASSERT(output_handle_ != NULL);
|
2009-11-11 09:50:06 +00:00
|
|
|
size_t rv = fwrite(msg, 1, length, output_handle_);
|
|
|
|
ASSERT(static_cast<size_t>(length) == rv);
|
|
|
|
USE(rv);
|
2010-09-30 10:50:39 +00:00
|
|
|
fflush(output_handle_);
|
2009-11-11 09:50:06 +00:00
|
|
|
return length;
|
2009-05-28 07:08:09 +00:00
|
|
|
}
|
|
|
|
|
2009-05-28 13:56:32 +00:00
|
|
|
// Whether logging is stopped (e.g. due to insufficient resources).
|
2011-03-18 20:35:07 +00:00
|
|
|
bool is_stopped_;
|
2009-05-28 13:56:32 +00:00
|
|
|
|
2011-07-13 11:31:22 +00:00
|
|
|
// When logging is active output_handle_ is used to store a pointer to log
|
|
|
|
// destination. mutex_ should be acquired before using output_handle_.
|
2011-03-18 20:35:07 +00:00
|
|
|
FILE* output_handle_;
|
2009-05-28 07:08:09 +00:00
|
|
|
|
|
|
|
// mutex_ is a Mutex used for enforcing exclusive
|
|
|
|
// access to the formatting buffer and the log file or log memory buffer.
|
2011-03-18 20:35:07 +00:00
|
|
|
Mutex* mutex_;
|
2009-05-28 07:08:09 +00:00
|
|
|
|
|
|
|
// Buffer used for formatting log messages. This is a singleton buffer and
|
|
|
|
// mutex_ should be acquired before using it.
|
2011-03-18 20:35:07 +00:00
|
|
|
char* message_buffer_;
|
|
|
|
|
|
|
|
Logger* logger_;
|
2009-05-28 07:08:09 +00:00
|
|
|
|
2010-10-19 16:45:11 +00:00
|
|
|
friend class Logger;
|
2009-06-15 09:37:50 +00:00
|
|
|
friend class LogMessageBuilder;
|
2009-05-28 07:08:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Utility class for formatting log messages. It fills the message into the
|
|
|
|
// static buffer in Log.
|
|
|
|
class LogMessageBuilder BASE_EMBEDDED {
|
|
|
|
public:
|
|
|
|
// Create a message builder starting from position 0. This acquires the mutex
|
|
|
|
// in the log as well.
|
2011-03-18 20:35:07 +00:00
|
|
|
explicit LogMessageBuilder(Logger* logger);
|
2009-05-28 07:08:09 +00:00
|
|
|
~LogMessageBuilder() { }
|
|
|
|
|
|
|
|
// Append string data to the log message.
|
|
|
|
void Append(const char* format, ...);
|
|
|
|
|
|
|
|
// Append string data to the log message.
|
2009-06-15 15:49:03 +00:00
|
|
|
void AppendVA(const char* format, va_list args);
|
2009-05-28 07:08:09 +00:00
|
|
|
|
|
|
|
// Append a character to the log message.
|
|
|
|
void Append(const char c);
|
|
|
|
|
2013-06-24 12:55:19 +00:00
|
|
|
// Append double quoted string to the log message.
|
|
|
|
void AppendDoubleQuotedString(const char* string);
|
|
|
|
|
2009-05-28 07:08:09 +00:00
|
|
|
// Append a heap string.
|
|
|
|
void Append(String* str);
|
|
|
|
|
2010-12-07 13:24:22 +00:00
|
|
|
// Appends an address.
|
2009-06-15 09:37:50 +00:00
|
|
|
void AppendAddress(Address addr);
|
|
|
|
|
2009-05-28 07:08:09 +00:00
|
|
|
void AppendDetailed(String* str, bool show_impl_info);
|
|
|
|
|
2009-09-18 12:05:18 +00:00
|
|
|
// Append a portion of a string.
|
|
|
|
void AppendStringPart(const char* str, int len);
|
|
|
|
|
2009-05-28 07:08:09 +00:00
|
|
|
// Write the log message to the log file currently opened.
|
|
|
|
void WriteToLogFile();
|
|
|
|
|
|
|
|
private:
|
2011-03-18 20:35:07 +00:00
|
|
|
Log* log_;
|
2009-05-28 07:08:09 +00:00
|
|
|
ScopedLock sl;
|
|
|
|
int pos_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} } // namespace v8::internal
|
|
|
|
|
|
|
|
#endif // V8_LOG_UTILS_H_
|