2 * Copyright (C) 2007 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "core/frame/Console.h"
32 #include "bindings/core/v8/ScriptCallStackFactory.h"
33 #include "core/inspector/InspectorConsoleInstrumentation.h"
34 #include "core/inspector/InspectorTraceEvents.h"
35 #include "core/inspector/ScriptArguments.h"
36 #include "platform/TraceEvent.h"
37 #include "wtf/text/CString.h"
38 #include "wtf/text/WTFString.h"
42 ConsoleBase::~ConsoleBase()
46 void ConsoleBase::debug(ScriptState* scriptState, PassRefPtrWillBeRawPtr<ScriptArguments> arguments)
48 internalAddMessage(LogMessageType, DebugMessageLevel, scriptState, arguments);
51 void ConsoleBase::error(ScriptState* scriptState, PassRefPtrWillBeRawPtr<ScriptArguments> arguments)
53 internalAddMessage(LogMessageType, ErrorMessageLevel, scriptState, arguments);
56 void ConsoleBase::info(ScriptState* scriptState, PassRefPtrWillBeRawPtr<ScriptArguments> arguments)
58 internalAddMessage(LogMessageType, InfoMessageLevel, scriptState, arguments);
61 void ConsoleBase::log(ScriptState* scriptState, PassRefPtrWillBeRawPtr<ScriptArguments> arguments)
63 internalAddMessage(LogMessageType, LogMessageLevel, scriptState, arguments);
66 void ConsoleBase::warn(ScriptState* scriptState, PassRefPtrWillBeRawPtr<ScriptArguments> arguments)
68 internalAddMessage(LogMessageType, WarningMessageLevel, scriptState, arguments);
71 void ConsoleBase::dir(ScriptState* scriptState, PassRefPtrWillBeRawPtr<ScriptArguments> arguments)
73 internalAddMessage(DirMessageType, LogMessageLevel, scriptState, arguments);
76 void ConsoleBase::dirxml(ScriptState* scriptState, PassRefPtrWillBeRawPtr<ScriptArguments> arguments)
78 internalAddMessage(DirXMLMessageType, LogMessageLevel, scriptState, arguments);
81 void ConsoleBase::table(ScriptState* scriptState, PassRefPtrWillBeRawPtr<ScriptArguments> arguments)
83 internalAddMessage(TableMessageType, LogMessageLevel, scriptState, arguments);
86 void ConsoleBase::clear(ScriptState* scriptState, PassRefPtrWillBeRawPtr<ScriptArguments> arguments)
88 internalAddMessage(ClearMessageType, LogMessageLevel, scriptState, arguments, true);
91 void ConsoleBase::trace(ScriptState* scriptState, PassRefPtrWillBeRawPtr<ScriptArguments> arguments)
93 internalAddMessage(TraceMessageType, LogMessageLevel, scriptState, arguments, true, true);
96 void ConsoleBase::assertCondition(ScriptState* scriptState, PassRefPtrWillBeRawPtr<ScriptArguments> arguments, bool condition)
101 internalAddMessage(AssertMessageType, ErrorMessageLevel, scriptState, arguments, true);
104 void ConsoleBase::count(ScriptState* scriptState, PassRefPtrWillBeRawPtr<ScriptArguments> arguments)
106 RefPtrWillBeRawPtr<ScriptCallStack> callStack(createScriptCallStackForConsole(1));
107 const ScriptCallFrame& lastCaller = callStack->at(0);
108 // Follow Firebug's behavior of counting with null and undefined title in
109 // the same bucket as no argument
111 arguments->getFirstArgumentAsString(title);
112 String identifier = title.isEmpty() ? String(lastCaller.sourceURL() + ':' + String::number(lastCaller.lineNumber()))
113 : String(title + '@');
115 HashCountedSet<String>::AddResult result = m_counts.add(identifier);
116 String message = title + ": " + String::number(result.storedValue->value);
118 RefPtrWillBeRawPtr<ConsoleMessage> consoleMessage = ConsoleMessage::create(ConsoleAPIMessageSource, DebugMessageLevel, message);
119 consoleMessage->setType(CountMessageType);
120 consoleMessage->setScriptState(scriptState);
121 consoleMessage->setCallStack(callStack.release());
122 reportMessageToConsole(consoleMessage.release());
125 void ConsoleBase::markTimeline(const String& title)
130 void ConsoleBase::profile(const String& title)
132 InspectorInstrumentation::consoleProfile(context(), title);
135 void ConsoleBase::profileEnd(const String& title)
137 InspectorInstrumentation::consoleProfileEnd(context(), title);
140 void ConsoleBase::time(const String& title)
142 InspectorInstrumentation::consoleTime(context(), title);
143 TRACE_EVENT_COPY_ASYNC_BEGIN0("blink.console", title.utf8().data(), this);
148 m_times.add(title, monotonicallyIncreasingTime());
151 void ConsoleBase::timeEnd(ScriptState* scriptState, const String& title)
153 TRACE_EVENT_COPY_ASYNC_END0("blink.console", title.utf8().data(), this);
154 InspectorInstrumentation::consoleTimeEnd(context(), title, scriptState);
156 // Follow Firebug's behavior of requiring a title that is not null or
157 // undefined for timing functions
161 HashMap<String, double>::iterator it = m_times.find(title);
162 if (it == m_times.end())
165 double startTime = it->value;
168 double elapsed = monotonicallyIncreasingTime() - startTime;
169 String message = title + String::format(": %.3fms", elapsed * 1000);
171 RefPtrWillBeRawPtr<ConsoleMessage> consoleMessage = ConsoleMessage::create(ConsoleAPIMessageSource, DebugMessageLevel, message);
172 consoleMessage->setType(TimeEndMessageType);
173 consoleMessage->setScriptState(scriptState);
174 consoleMessage->setCallStack(createScriptCallStackForConsole(1));
175 reportMessageToConsole(consoleMessage.release());
178 void ConsoleBase::timeStamp(const String& title)
180 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "TimeStamp", "data", InspectorTimeStampEvent::data(context(), title));
181 // FIXME(361045): remove InspectorInstrumentation calls once DevTools Timeline migrates to tracing.
182 InspectorInstrumentation::consoleTimeStamp(context(), title);
185 static String formatTimelineTitle(const String& title)
187 return String::format("Timeline '%s'", title.utf8().data());
190 void ConsoleBase::timeline(ScriptState* scriptState, const String& title)
192 // FIXME(361045): remove InspectorInstrumentation calls once DevTools Timeline migrates to tracing.
193 InspectorInstrumentation::consoleTimeline(context(), title, scriptState);
195 TRACE_EVENT_COPY_ASYNC_BEGIN0("blink.console", formatTimelineTitle(title).utf8().data(), this);
198 void ConsoleBase::timelineEnd(ScriptState* scriptState, const String& title)
200 // FIXME(361045): remove InspectorInstrumentation calls once DevTools Timeline migrates to tracing.
201 InspectorInstrumentation::consoleTimelineEnd(context(), title, scriptState);
203 TRACE_EVENT_COPY_ASYNC_END0("blink.console", formatTimelineTitle(title).utf8().data(), this);
206 void ConsoleBase::group(ScriptState* scriptState, PassRefPtrWillBeRawPtr<ScriptArguments> arguments)
208 internalAddMessage(StartGroupMessageType, LogMessageLevel, scriptState, arguments, true);
211 void ConsoleBase::groupCollapsed(ScriptState* scriptState, PassRefPtrWillBeRawPtr<ScriptArguments> arguments)
213 internalAddMessage(StartGroupCollapsedMessageType, LogMessageLevel, scriptState, arguments, true);
216 void ConsoleBase::groupEnd()
218 internalAddMessage(EndGroupMessageType, LogMessageLevel, nullptr, nullptr, true);
221 void ConsoleBase::internalAddMessage(MessageType type, MessageLevel level, ScriptState* scriptState, PassRefPtrWillBeRawPtr<ScriptArguments> scriptArguments, bool acceptNoArguments, bool printTrace)
223 RefPtrWillBeRawPtr<ScriptArguments> arguments = scriptArguments;
224 if (!acceptNoArguments && (!arguments || !arguments->argumentCount()))
228 bool gotStringMessage = arguments ? arguments->getFirstArgumentAsString(message) : false;
230 RefPtrWillBeRawPtr<ConsoleMessage> consoleMessage = ConsoleMessage::create(ConsoleAPIMessageSource, level, gotStringMessage? message : String());
231 consoleMessage->setType(type);
232 consoleMessage->setScriptState(scriptState);
233 consoleMessage->setScriptArguments(arguments);
235 size_t stackSize = printTrace ? ScriptCallStack::maxCallStackSizeToCapture : 1;
236 consoleMessage->setCallStack(createScriptCallStackForConsole(stackSize));
237 reportMessageToConsole(consoleMessage.release());