2009-05-28 07:08:09 +00:00
|
|
|
// Copyright 2006-2009 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2009-05-28 07:08:09 +00:00
|
|
|
|
|
|
|
#ifndef V8_LOG_UTILS_H_
|
|
|
|
#define V8_LOG_UTILS_H_
|
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/allocation.h"
|
2011-05-06 06:50:20 +00:00
|
|
|
|
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() {
|
2015-01-20 16:06:03 +00:00
|
|
|
return FLAG_log || FLAG_log_api || FLAG_log_code || FLAG_log_gc ||
|
|
|
|
FLAG_log_handles || FLAG_log_suspect || FLAG_log_regexp ||
|
|
|
|
FLAG_ll_prof || FLAG_perf_basic_prof || FLAG_perf_jit_prof ||
|
|
|
|
FLAG_log_internal_timer_events || FLAG_prof_cpp;
|
2013-04-24 14:44:08 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2013-07-18 17:19:31 +00:00
|
|
|
// Utility class for formatting log messages. It fills the message into the
|
|
|
|
// static buffer in Log.
|
|
|
|
class MessageBuilder BASE_EMBEDDED {
|
|
|
|
public:
|
|
|
|
// Create a message builder starting from position 0.
|
|
|
|
// This acquires the mutex in the log as well.
|
|
|
|
explicit MessageBuilder(Log* log);
|
|
|
|
~MessageBuilder() { }
|
|
|
|
|
|
|
|
// Append string data to the log message.
|
|
|
|
void Append(const char* format, ...);
|
|
|
|
|
|
|
|
// Append string data to the log message.
|
|
|
|
void AppendVA(const char* format, va_list args);
|
|
|
|
|
|
|
|
// Append a character to the log message.
|
|
|
|
void Append(const char c);
|
|
|
|
|
|
|
|
// Append double quoted string to the log message.
|
|
|
|
void AppendDoubleQuotedString(const char* string);
|
|
|
|
|
|
|
|
// Append a heap string.
|
|
|
|
void Append(String* str);
|
|
|
|
|
|
|
|
// Appends an address.
|
|
|
|
void AppendAddress(Address addr);
|
|
|
|
|
|
|
|
void AppendSymbolName(Symbol* symbol);
|
|
|
|
|
|
|
|
void AppendDetailed(String* str, bool show_impl_info);
|
|
|
|
|
|
|
|
// Append a portion of a string.
|
|
|
|
void AppendStringPart(const char* str, int len);
|
|
|
|
|
|
|
|
// Write the log message to the log file currently opened.
|
|
|
|
void WriteToLogFile();
|
|
|
|
|
|
|
|
private:
|
|
|
|
Log* log_;
|
2014-06-30 13:25:46 +00:00
|
|
|
base::LockGuard<base::Mutex> lock_guard_;
|
2013-07-18 17:19:31 +00:00
|
|
|
int pos_;
|
|
|
|
};
|
|
|
|
|
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) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(output_handle_ != NULL);
|
2009-11-11 09:50:06 +00:00
|
|
|
size_t rv = fwrite(msg, 1, length, output_handle_);
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(static_cast<size_t>(length) == rv);
|
2009-11-11 09:50:06 +00:00
|
|
|
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.
|
2014-06-30 13:25:46 +00:00
|
|
|
base::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-05-28 07:08:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} } // namespace v8::internal
|
|
|
|
|
|
|
|
#endif // V8_LOG_UTILS_H_
|