std::unique_ptr<V8ConsoleMessage> V8ConsoleMessage::createForConsoleAPI(
v8::Local<v8::Context> v8Context, int contextId, int groupId,
V8InspectorImpl* inspector, double timestamp, ConsoleAPIType type,
v8::MemorySpan<const v8::Local<v8::Value>> arguments,
const String16& consoleContext,
std::unique_ptr<V8StackTraceImpl> stackTrace) {
v8::Isolate* isolate = v8::Isolate::GetCurrent();
std::unique_ptr<V8ConsoleMessage> message(
new V8ConsoleMessage(V8MessageOrigin::kConsole, timestamp, String16()));
if (stackTrace && !stackTrace->isEmpty()) {
message->m_url = toString16(stackTrace->topSourceURL());
message->m_lineNumber = stackTrace->topLineNumber();
message->m_columnNumber = stackTrace->topColumnNumber();
}
message->m_stackTrace = std::move(stackTrace);
message->m_consoleContext = consoleContext;
message->m_type = type;
message->m_contextId = contextId;
for (v8::Local<v8::Value> arg : arguments) {
std::unique_ptr<v8::Global<v8::Value>> argument(
new v8::Global<v8::Value>(isolate, arg));
argument->AnnotateStrongRetainer(kGlobalConsoleMessageHandleLabel);
message->m_arguments.push_back(std::move(argument));
message->m_v8Size += v8::debug::EstimatedValueSize(isolate, arg);
}
bool sep = false;
for (v8::Local<v8::Value> arg : arguments) {
if (sep) {
message->m_message += String16(" ");
} else {
sep = true;
}
message->m_message += V8ValueStringBuilder::toString(arg, v8Context);
}
v8::Isolate::MessageErrorLevel clientLevel = v8::Isolate::kMessageInfo;
if (type == ConsoleAPIType::kDebug || type == ConsoleAPIType::kCount ||
type == ConsoleAPIType::kTimeEnd) {
clientLevel = v8::Isolate::kMessageDebug;
} else if (type == ConsoleAPIType::kError ||
type == ConsoleAPIType::kAssert) {
clientLevel = v8::Isolate::kMessageError;
} else if (type == ConsoleAPIType::kWarning) {
clientLevel = v8::Isolate::kMessageWarning;
} else if (type == ConsoleAPIType::kInfo) {
clientLevel = v8::Isolate::kMessageInfo;
} else if (type == ConsoleAPIType::kLog) {
clientLevel = v8::Isolate::kMessageLog;
}
if (type != ConsoleAPIType::kClear) {
inspector->client()->consoleAPIMessage(
groupId, clientLevel, toStringView(message->m_message),
toStringView(message->m_url), message->m_lineNumber,
message->m_columnNumber, message->m_stackTrace.get());
}
return message;
}