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_
|
|
|
|
|
2015-10-30 08:03:08 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2018-05-03 12:11:05 +00:00
|
|
|
#include <cstdarg>
|
|
|
|
|
|
|
|
#include "src/allocation.h"
|
|
|
|
#include "src/base/compiler-specific.h"
|
|
|
|
#include "src/base/platform/mutex.h"
|
2015-08-14 09:41:32 +00:00
|
|
|
#include "src/flags.h"
|
2017-10-20 22:07:50 +00:00
|
|
|
#include "src/ostreams.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;
|
|
|
|
|
2017-10-20 23:14:23 +00:00
|
|
|
enum class LogSeparator { kSeparator };
|
|
|
|
|
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:
|
2017-10-20 22:07:50 +00:00
|
|
|
Log(Logger* log, const char* log_file_name);
|
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() {
|
2017-12-20 10:30:32 +00:00
|
|
|
return FLAG_log || FLAG_log_api || FLAG_log_code || FLAG_log_handles ||
|
|
|
|
FLAG_log_suspect || FLAG_ll_prof || FLAG_perf_basic_prof ||
|
2018-03-22 20:12:42 +00:00
|
|
|
FLAG_perf_prof || FLAG_log_source_code || FLAG_gdbjit ||
|
2017-10-30 13:18:12 +00:00
|
|
|
FLAG_log_internal_timer_events || FLAG_prof_cpp || FLAG_trace_ic ||
|
|
|
|
FLAG_log_function_events;
|
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.
|
2017-10-13 16:33:03 +00:00
|
|
|
bool IsEnabled() { return !is_stopped_ && output_handle_ != nullptr; }
|
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.
|
2016-04-12 16:13:08 +00:00
|
|
|
void PRINTF_FORMAT(2, 3) Append(const char* format, ...);
|
2013-07-18 17:19:31 +00:00
|
|
|
|
|
|
|
// Append string data to the log message.
|
2016-04-12 16:13:08 +00:00
|
|
|
void PRINTF_FORMAT(2, 0) AppendVA(const char* format, va_list args);
|
2013-07-18 17:19:31 +00:00
|
|
|
|
|
|
|
void AppendSymbolName(Symbol* symbol);
|
|
|
|
|
|
|
|
void AppendDetailed(String* str, bool show_impl_info);
|
|
|
|
|
2017-10-20 23:14:23 +00:00
|
|
|
// Append and escape a full string.
|
|
|
|
void AppendString(String* source);
|
|
|
|
void AppendString(const char* string);
|
|
|
|
|
|
|
|
// Append and escpae a portion of a string.
|
|
|
|
void AppendStringPart(String* source, int len);
|
2017-11-14 09:12:52 +00:00
|
|
|
void AppendStringPart(const char* str, size_t len);
|
2013-07-18 17:19:31 +00:00
|
|
|
|
2017-10-20 23:14:23 +00:00
|
|
|
void AppendCharacter(const char character);
|
2017-10-20 22:07:50 +00:00
|
|
|
|
|
|
|
// Delegate insertion to the underlying {log_}.
|
2018-03-21 17:29:40 +00:00
|
|
|
// All appended strings are escaped to maintain one-line log entries.
|
2017-10-20 22:07:50 +00:00
|
|
|
template <typename T>
|
|
|
|
MessageBuilder& operator<<(T value) {
|
|
|
|
log_->os_ << value;
|
|
|
|
return *this;
|
|
|
|
}
|
2017-07-27 19:46:32 +00:00
|
|
|
|
2017-10-20 22:07:50 +00:00
|
|
|
// Finish the current log line an flush the it to the log file.
|
2013-07-18 17:19:31 +00:00
|
|
|
void WriteToLogFile();
|
|
|
|
|
|
|
|
private:
|
|
|
|
Log* log_;
|
2018-05-03 12:11:05 +00:00
|
|
|
base::LockGuard<base::Mutex> lock_guard_;
|
2013-07-18 17:19:31 +00:00
|
|
|
};
|
|
|
|
|
2009-05-28 07:08:09 +00:00
|
|
|
private:
|
2017-10-20 22:07:50 +00:00
|
|
|
static FILE* CreateOutputHandle(const char* file_name);
|
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) {
|
[base] Define CHECK comparison for signed vs. unsigned
The current CHECK/DCHECK implementation fails statically if a signed
value is compared against an unsigned value. The common solution is to
cast on each caller, which is tedious and error-prone (might hide bugs).
This CL implements signed vs. unsigned comparisons by executing up to
two comparisons. For example, if i is int32_t and u is uint_32_t, a
DCHECK_LE(i, u) would create the check
i <= 0 || static_cast<uint32_t>(i) <= u.
For checks against constants, at least one of the checks can be removed
by compiler optimizations.
The tradeoff we have to make is to sometimes silently execute an
additional comparison. And we increase code complexity of course, even
though the usage is just as easy (or even easier) as before.
The compile time impact seems to be minimal:
I ran 3 full compilations for Optdebug on my local machine, one time on
the current ToT, one time with this CL plus http://crrev.com/2524093002.
Before: 143.72 +- 1.21 seconds
Now: 144.18 +- 0.67 seconds
In order to check that the new comparisons are working, I refactored
some DCHECKs in wasm to use the new magic, and added unit test cases.
R=ishell@chromium.org, titzer@chromium.org
CC=ahaas@chromium.org, bmeurer@chromium.org
Committed: https://crrev.com/5925074a9dab5a8577766545b91b62f2c531d3dc
Review-Url: https://codereview.chromium.org/2526783002
Cr-Original-Commit-Position: refs/heads/master@{#41275}
Cr-Commit-Position: refs/heads/master@{#41411}
2016-12-01 08:52:31 +00:00
|
|
|
DCHECK_NOT_NULL(output_handle_);
|
2017-10-20 22:07:50 +00:00
|
|
|
os_.write(msg, length);
|
|
|
|
DCHECK(!os_.bad());
|
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_;
|
2017-10-20 22:07:50 +00:00
|
|
|
OFStream os_;
|
2009-05-28 07:08:09 +00:00
|
|
|
|
2018-05-03 12:11:05 +00:00
|
|
|
// mutex_ is a Mutex used for enforcing exclusive
|
|
|
|
// access to the formatting buffer and the log file or log memory buffer.
|
|
|
|
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.
|
2017-10-20 22:07:50 +00:00
|
|
|
char* format_buffer_;
|
2011-03-18 20:35:07 +00:00
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2017-10-20 23:14:23 +00:00
|
|
|
template <>
|
|
|
|
Log::MessageBuilder& Log::MessageBuilder::operator<<<LogSeparator>(
|
|
|
|
LogSeparator separator);
|
|
|
|
template <>
|
2017-10-24 16:34:41 +00:00
|
|
|
Log::MessageBuilder& Log::MessageBuilder::operator<<<void*>(void* pointer);
|
|
|
|
template <>
|
2017-10-20 23:14:23 +00:00
|
|
|
Log::MessageBuilder& Log::MessageBuilder::operator<<<const char*>(
|
|
|
|
const char* string);
|
|
|
|
template <>
|
|
|
|
Log::MessageBuilder& Log::MessageBuilder::operator<<<char>(char c);
|
|
|
|
template <>
|
|
|
|
Log::MessageBuilder& Log::MessageBuilder::operator<<<String*>(String* string);
|
|
|
|
template <>
|
|
|
|
Log::MessageBuilder& Log::MessageBuilder::operator<<<Symbol*>(Symbol* symbol);
|
|
|
|
template <>
|
|
|
|
Log::MessageBuilder& Log::MessageBuilder::operator<<<Name*>(Name* name);
|
2009-05-28 07:08:09 +00:00
|
|
|
|
2015-09-30 13:46:56 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|
2009-05-28 07:08:09 +00:00
|
|
|
|
|
|
|
#endif // V8_LOG_UTILS_H_
|