Commit a00a9142 authored by Benedikt Meurer's avatar Benedikt Meurer Committed by Commit Bot

Revert "[logging] Use OFStream for log events"

This reverts commit 06ff9e97.

Reason for revert: Breaks deopt information with --prof. Deopts no longer show up properly in the logfile / profview

Original change's description:
> [logging] Use OFStream for log events
> 
> This simplifies a few operations and removes the size limitations
> implied by the message buffer used.
> 
> Change-Id: I8b873a0ffa399a037ff5c2501ba4b68158810968
> Reviewed-on: https://chromium-review.googlesource.com/724285
> Commit-Queue: Camillo Bruni <cbruni@chromium.org>
> Reviewed-by: Adam Klein <adamk@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#48766}

TBR=adamk@chromium.org,cbruni@chromium.org

Change-Id: I290da0b2472ad0e765b765b26bdde334253376e3
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Reviewed-on: https://chromium-review.googlesource.com/730164Reviewed-by: 's avatarBenedikt Meurer <bmeurer@chromium.org>
Commit-Queue: Benedikt Meurer <bmeurer@chromium.org>
Cr-Commit-Position: refs/heads/master@{#48776}
parent f0aa474e
...@@ -18,26 +18,15 @@ namespace internal { ...@@ -18,26 +18,15 @@ namespace internal {
const char* const Log::kLogToTemporaryFile = "&"; const char* const Log::kLogToTemporaryFile = "&";
const char* const Log::kLogToConsole = "-"; const char* const Log::kLogToConsole = "-";
// static Log::Log(Logger* logger)
FILE* Log::CreateOutputHandle(const char* file_name) {
// If we're logging anything, we need to open the log file.
if (!Log::InitLogAtStart()) {
return nullptr;
} else if (strcmp(file_name, kLogToConsole) == 0) {
return stdout;
} else if (strcmp(file_name, kLogToTemporaryFile) == 0) {
return base::OS::OpenTemporaryFile();
} else {
return base::OS::FOpen(file_name, base::OS::LogFileOpenMode);
}
}
Log::Log(Logger* logger, const char* file_name)
: is_stopped_(false), : is_stopped_(false),
output_handle_(Log::CreateOutputHandle(file_name)), output_handle_(nullptr),
os_(output_handle_ == nullptr ? stdout : output_handle_), message_buffer_(nullptr),
format_buffer_(NewArray<char>(kMessageBufferSize)), logger_(logger) {}
logger_(logger) {
void Log::Initialize(const char* log_file_name) {
message_buffer_ = NewArray<char>(kMessageBufferSize);
// --log-all enables all the log flags. // --log-all enables all the log flags.
if (FLAG_log_all) { if (FLAG_log_all) {
FLAG_log_api = true; FLAG_log_api = true;
...@@ -51,19 +40,49 @@ Log::Log(Logger* logger, const char* file_name) ...@@ -51,19 +40,49 @@ Log::Log(Logger* logger, const char* file_name)
// --prof implies --log-code. // --prof implies --log-code.
if (FLAG_prof) FLAG_log_code = true; if (FLAG_prof) FLAG_log_code = true;
// If we're logging anything, we need to open the log file.
if (Log::InitLogAtStart()) {
if (strcmp(log_file_name, kLogToConsole) == 0) {
OpenStdout();
} else if (strcmp(log_file_name, kLogToTemporaryFile) == 0) {
OpenTemporaryFile();
} else {
OpenFile(log_file_name);
}
if (output_handle_ != nullptr) { if (output_handle_ != nullptr) {
Log::MessageBuilder msg(this); Log::MessageBuilder msg(this);
if (strlen(Version::GetEmbedder()) == 0) { if (strlen(Version::GetEmbedder()) == 0) {
msg.Append("v8-version,%d,%d,%d,%d,%d", Version::GetMajor(), msg.Append("v8-version,%d,%d,%d,%d,%d", Version::GetMajor(),
Version::GetMinor(), Version::GetBuild(), Version::GetPatch(), Version::GetMinor(), Version::GetBuild(),
Version::IsCandidate()); Version::GetPatch(), Version::IsCandidate());
} else { } else {
msg.Append("v8-version,%d,%d,%d,%d,%s,%d", Version::GetMajor(), msg.Append("v8-version,%d,%d,%d,%d,%s,%d", Version::GetMajor(),
Version::GetMinor(), Version::GetBuild(), Version::GetPatch(), Version::GetMinor(), Version::GetBuild(),
Version::GetEmbedder(), Version::IsCandidate()); Version::GetPatch(), Version::GetEmbedder(),
Version::IsCandidate());
} }
msg.WriteToLogFile(); msg.WriteToLogFile();
} }
}
}
void Log::OpenStdout() {
DCHECK(!IsEnabled());
output_handle_ = stdout;
}
void Log::OpenTemporaryFile() {
DCHECK(!IsEnabled());
output_handle_ = base::OS::OpenTemporaryFile();
}
void Log::OpenFile(const char* name) {
DCHECK(!IsEnabled());
output_handle_ = base::OS::FOpen(name, base::OS::LogFileOpenMode);
} }
FILE* Log::Close() { FILE* Log::Close() {
...@@ -77,63 +96,74 @@ FILE* Log::Close() { ...@@ -77,63 +96,74 @@ FILE* Log::Close() {
} }
output_handle_ = nullptr; output_handle_ = nullptr;
DeleteArray(format_buffer_); DeleteArray(message_buffer_);
format_buffer_ = nullptr; message_buffer_ = nullptr;
is_stopped_ = false; is_stopped_ = false;
return result; return result;
} }
Log::MessageBuilder::MessageBuilder(Log* log) Log::MessageBuilder::MessageBuilder(Log* log)
: log_(log), lock_guard_(&log_->mutex_) { : log_(log),
DCHECK_NOT_NULL(log_->format_buffer_); lock_guard_(&log_->mutex_),
pos_(0) {
DCHECK_NOT_NULL(log_->message_buffer_);
} }
void Log::MessageBuilder::Append(const char* format, ...) { void Log::MessageBuilder::Append(const char* format, ...) {
Vector<char> buf(log_->message_buffer_ + pos_,
Log::kMessageBufferSize - pos_);
va_list args; va_list args;
va_start(args, format); va_start(args, format);
AppendVA(format, args); AppendVA(format, args);
va_end(args); va_end(args);
DCHECK_LE(pos_, Log::kMessageBufferSize);
} }
void Log::MessageBuilder::AppendVA(const char* format, va_list args) { void Log::MessageBuilder::AppendVA(const char* format, va_list args) {
Vector<char> buf(log_->format_buffer_, Log::kMessageBufferSize); Vector<char> buf(log_->message_buffer_ + pos_,
int length = v8::internal::VSNPrintF(buf, format, args); Log::kMessageBufferSize - pos_);
// {length} is -1 if output was truncated. int result = v8::internal::VSNPrintF(buf, format, args);
if (length == -1) {
length = Log::kMessageBufferSize; // Result is -1 if output was truncated.
if (result >= 0) {
pos_ += result;
} else {
pos_ = Log::kMessageBufferSize;
}
DCHECK_LE(pos_, Log::kMessageBufferSize);
}
void Log::MessageBuilder::Append(const char c) {
if (pos_ < Log::kMessageBufferSize) {
log_->message_buffer_[pos_++] = c;
} }
DCHECK_LE(length, Log::kMessageBufferSize); DCHECK_LE(pos_, Log::kMessageBufferSize);
AppendStringPart(log_->format_buffer_, length);
} }
void Log::MessageBuilder::AppendDoubleQuotedString(const char* string) { void Log::MessageBuilder::AppendDoubleQuotedString(const char* string) {
OFStream& os = log_->os_; Append('"');
// TODO(cbruni): unify escaping.
os << '"';
for (const char* p = string; *p != '\0'; p++) { for (const char* p = string; *p != '\0'; p++) {
if (*p == '"') os << '\\'; if (*p == '"') {
os << *p; Append('\\');
} }
os << '"'; Append(*p);
}
Append('"');
} }
void Log::MessageBuilder::AppendDoubleQuotedString(String* string) {
OFStream& os = log_->os_;
os << '"';
// TODO(cbruni): unify escaping.
AppendEscapedString(string);
os << '"';
}
void Log::MessageBuilder::Append(String* string) { void Log::MessageBuilder::Append(String* str) {
DisallowHeapAllocation no_gc; // Ensure string stay valid. DisallowHeapAllocation no_gc; // Ensure string stay valid.
std::unique_ptr<char[]> characters = int length = str->length();
string->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL); for (int i = 0; i < length; i++) {
log_->os_ << characters.get(); Append(static_cast<char>(str->Get(i)));
}
} }
void Log::MessageBuilder::AppendAddress(Address addr) { void Log::MessageBuilder::AppendAddress(Address addr) {
...@@ -142,69 +172,109 @@ void Log::MessageBuilder::AppendAddress(Address addr) { ...@@ -142,69 +172,109 @@ void Log::MessageBuilder::AppendAddress(Address addr) {
void Log::MessageBuilder::AppendSymbolName(Symbol* symbol) { void Log::MessageBuilder::AppendSymbolName(Symbol* symbol) {
DCHECK(symbol); DCHECK(symbol);
OFStream& os = log_->os_; Append("symbol(");
os << "symbol(";
if (!symbol->name()->IsUndefined(symbol->GetIsolate())) { if (!symbol->name()->IsUndefined(symbol->GetIsolate())) {
os << "\""; Append("\"");
AppendDetailed(String::cast(symbol->name()), false); AppendDetailed(String::cast(symbol->name()), false);
os << "\" "; Append("\" ");
} }
os << "hash " << std::hex << symbol->Hash() << std::dec << ")"; Append("hash %x)", symbol->Hash());
} }
void Log::MessageBuilder::AppendDetailed(String* str, bool show_impl_info) { void Log::MessageBuilder::AppendDetailed(String* str, bool show_impl_info) {
if (str == nullptr) return; if (str == nullptr) return;
DisallowHeapAllocation no_gc; // Ensure string stay valid. DisallowHeapAllocation no_gc; // Ensure string stay valid.
OFStream& os = log_->os_;
int len = str->length(); int len = str->length();
if (len > 0x1000) len = 0x1000; if (len > 0x1000)
len = 0x1000;
if (show_impl_info) { if (show_impl_info) {
os << (str->IsOneByteRepresentation() ? 'a' : '2'); Append(str->IsOneByteRepresentation() ? 'a' : '2');
if (StringShape(str).IsExternal()) os << 'e'; if (StringShape(str).IsExternal())
if (StringShape(str).IsInternalized()) os << '#'; Append('e');
os << ':' << str->length() << ':'; if (StringShape(str).IsInternalized())
Append('#');
Append(":%i:", str->length());
}
for (int i = 0; i < len; i++) {
uc32 c = str->Get(i);
if (c > 0xff) {
Append("\\u%04x", c);
} else if (c < 32 || c > 126) {
Append("\\x%02x", c);
} else if (c == ',') {
Append("\\,");
} else if (c == '\\') {
Append("\\\\");
} else if (c == '\"') {
Append("\"\"");
} else {
Append("%lc", c);
}
} }
AppendEscapedString(str, len);
} }
void Log::MessageBuilder::AppendEscapedString(String* str) { void Log::MessageBuilder::AppendUnbufferedHeapString(String* str) {
if (str == nullptr) return; if (str == nullptr) return;
int len = str->length();
AppendEscapedString(str, len);
}
void Log::MessageBuilder::AppendEscapedString(String* str, int len) {
DCHECK_LE(len, str->length());
DisallowHeapAllocation no_gc; // Ensure string stay valid. DisallowHeapAllocation no_gc; // Ensure string stay valid.
OFStream& os = log_->os_; ScopedVector<char> buffer(16);
// TODO(cbruni): unify escaping. int len = str->length();
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
uc32 c = str->Get(i); uc32 c = str->Get(i);
if (c >= 32 && c <= 126) { if (c >= 32 && c <= 126) {
if (c == '\"') { if (c == '\"') {
os << "\"\""; AppendUnbufferedCString("\"\"");
} else if (c == '\\') { } else if (c == '\\') {
os << "\\\\"; AppendUnbufferedCString("\\\\");
} else if (c == ',') {
os << "\\,";
} else { } else {
os << static_cast<char>(c); AppendUnbufferedChar(c);
} }
} else if (c > 0xff) { } else if (c > 0xff) {
Append("\\u%04x", c); int length = v8::internal::SNPrintF(buffer, "\\u%04x", c);
DCHECK_EQ(6, length);
log_->WriteToFile(buffer.start(), length);
} else { } else {
DCHECK(c < 32 || (c > 126 && c <= 0xff)); DCHECK_LE(c, 0xffff);
Append("\\x%02x", c); int length = v8::internal::SNPrintF(buffer, "\\x%02x", c);
DCHECK_EQ(4, length);
log_->WriteToFile(buffer.start(), length);
} }
} }
} }
void Log::MessageBuilder::AppendUnbufferedChar(char c) {
log_->WriteToFile(&c, 1);
}
void Log::MessageBuilder::AppendUnbufferedCString(const char* str) {
log_->WriteToFile(str, static_cast<int>(strlen(str)));
}
void Log::MessageBuilder::AppendStringPart(const char* str, int len) { void Log::MessageBuilder::AppendStringPart(const char* str, int len) {
log_->os_.write(str, len); if (pos_ + len > Log::kMessageBufferSize) {
len = Log::kMessageBufferSize - pos_;
DCHECK_GE(len, 0);
if (len == 0) return;
}
Vector<char> buf(log_->message_buffer_ + pos_,
Log::kMessageBufferSize - pos_);
StrNCpy(buf, str, len);
pos_ += len;
DCHECK_LE(pos_, Log::kMessageBufferSize);
} }
void Log::MessageBuilder::WriteToLogFile() { log_->os_ << std::endl; } void Log::MessageBuilder::WriteToLogFile() {
DCHECK_LE(pos_, Log::kMessageBufferSize);
// Assert that we do not already have a new line at the end.
DCHECK(pos_ == 0 || log_->message_buffer_[pos_ - 1] != '\n');
if (pos_ == Log::kMessageBufferSize) pos_--;
log_->message_buffer_[pos_++] = '\n';
const int written = log_->WriteToFile(log_->message_buffer_, pos_);
if (written != pos_) {
log_->stop();
log_->logger_->LogFailure();
}
}
} // namespace internal } // namespace internal
} // namespace v8 } // namespace v8
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
#include "src/base/compiler-specific.h" #include "src/base/compiler-specific.h"
#include "src/base/platform/mutex.h" #include "src/base/platform/mutex.h"
#include "src/flags.h" #include "src/flags.h"
#include "src/ostreams.h"
namespace v8 { namespace v8 {
namespace internal { namespace internal {
...@@ -23,7 +22,8 @@ class Logger; ...@@ -23,7 +22,8 @@ class Logger;
// Functions and data for performing output of log messages. // Functions and data for performing output of log messages.
class Log { class Log {
public: public:
Log(Logger* log, const char* log_file_name); // Performs process-wide initialization.
void Initialize(const char* log_file_name);
// Disables logging, but preserves acquired resources. // Disables logging, but preserves acquired resources.
void stop() { is_stopped_ = true; } void stop() { is_stopped_ = true; }
...@@ -66,9 +66,11 @@ class Log { ...@@ -66,9 +66,11 @@ class Log {
// Append string data to the log message. // Append string data to the log message.
void PRINTF_FORMAT(2, 0) AppendVA(const char* format, va_list args); void PRINTF_FORMAT(2, 0) 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. // Append double quoted string to the log message.
void AppendDoubleQuotedString(const char* string); void AppendDoubleQuotedString(const char* string);
void AppendDoubleQuotedString(String* string);
// Append a heap string. // Append a heap string.
void Append(String* str); void Append(String* str);
...@@ -86,32 +88,37 @@ class Log { ...@@ -86,32 +88,37 @@ class Log {
// Helpers for appending char, C-string and heap string without // Helpers for appending char, C-string and heap string without
// buffering. This is useful for entries that can exceed the 2kB // buffering. This is useful for entries that can exceed the 2kB
// limit. // limit.
void AppendEscapedString(String* source); void AppendUnbufferedChar(char c);
void AppendEscapedString(String* source, int len); void AppendUnbufferedCString(const char* str);
void AppendUnbufferedHeapString(String* source);
// Delegate insertion to the underlying {log_}.
template <typename T>
MessageBuilder& operator<<(T value) {
log_->os_ << value;
return *this;
}
// Finish the current log line an flush the it to the log file. // Write the log message to the log file currently opened.
void WriteToLogFile(); void WriteToLogFile();
private: private:
Log* log_; Log* log_;
base::LockGuard<base::Mutex> lock_guard_; base::LockGuard<base::Mutex> lock_guard_;
int pos_;
}; };
private: private:
static FILE* CreateOutputHandle(const char* file_name); explicit Log(Logger* logger);
// Opens stdout for logging.
void OpenStdout();
// Opens file for logging.
void OpenFile(const char* name);
// Opens a temporary file for logging.
void OpenTemporaryFile();
// Implementation of writing to a log file. // Implementation of writing to a log file.
int WriteToFile(const char* msg, int length) { int WriteToFile(const char* msg, int length) {
DCHECK_NOT_NULL(output_handle_); DCHECK_NOT_NULL(output_handle_);
os_.write(msg, length); size_t rv = fwrite(msg, 1, length, output_handle_);
DCHECK(!os_.bad()); DCHECK_EQ(length, rv);
USE(rv);
return length; return length;
} }
...@@ -121,7 +128,6 @@ class Log { ...@@ -121,7 +128,6 @@ class Log {
// When logging is active output_handle_ is used to store a pointer to log // When logging is active output_handle_ is used to store a pointer to log
// destination. mutex_ should be acquired before using output_handle_. // destination. mutex_ should be acquired before using output_handle_.
FILE* output_handle_; FILE* output_handle_;
OFStream os_;
// mutex_ is a Mutex used for enforcing exclusive // mutex_ is a Mutex used for enforcing exclusive
// access to the formatting buffer and the log file or log memory buffer. // access to the formatting buffer and the log file or log memory buffer.
...@@ -129,7 +135,7 @@ class Log { ...@@ -129,7 +135,7 @@ class Log {
// Buffer used for formatting log messages. This is a singleton buffer and // Buffer used for formatting log messages. This is a singleton buffer and
// mutex_ should be acquired before using it. // mutex_ should be acquired before using it.
char* format_buffer_; char* message_buffer_;
Logger* logger_; Logger* logger_;
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment