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/ConsoleMessage.h"
34 #include "core/inspector/InspectorConsoleInstrumentation.h"
35 #include "core/inspector/InspectorTraceEvents.h"
36 #include "core/inspector/ScriptArguments.h"
37 #include "platform/TraceEvent.h"
38 #include "wtf/text/CString.h"
39 #include "wtf/text/WTFString.h"
43 ConsoleBase::~ConsoleBase()
47 void ConsoleBase::debug(ScriptState
* scriptState
, PassRefPtrWillBeRawPtr
<ScriptArguments
> arguments
)
49 internalAddMessage(LogMessageType
, DebugMessageLevel
, scriptState
, arguments
);
52 void ConsoleBase::error(ScriptState
* scriptState
, PassRefPtrWillBeRawPtr
<ScriptArguments
> arguments
)
54 internalAddMessage(LogMessageType
, ErrorMessageLevel
, scriptState
, arguments
, false);
57 void ConsoleBase::info(ScriptState
* scriptState
, PassRefPtrWillBeRawPtr
<ScriptArguments
> arguments
)
59 internalAddMessage(LogMessageType
, InfoMessageLevel
, scriptState
, arguments
);
62 void ConsoleBase::log(ScriptState
* scriptState
, PassRefPtrWillBeRawPtr
<ScriptArguments
> arguments
)
64 internalAddMessage(LogMessageType
, LogMessageLevel
, scriptState
, arguments
);
67 void ConsoleBase::warn(ScriptState
* scriptState
, PassRefPtrWillBeRawPtr
<ScriptArguments
> arguments
)
69 internalAddMessage(LogMessageType
, WarningMessageLevel
, scriptState
, arguments
);
72 void ConsoleBase::dir(ScriptState
* scriptState
, PassRefPtrWillBeRawPtr
<ScriptArguments
> arguments
)
74 internalAddMessage(DirMessageType
, LogMessageLevel
, scriptState
, arguments
);
77 void ConsoleBase::dirxml(ScriptState
* scriptState
, PassRefPtrWillBeRawPtr
<ScriptArguments
> arguments
)
79 internalAddMessage(DirXMLMessageType
, LogMessageLevel
, scriptState
, arguments
);
82 void ConsoleBase::table(ScriptState
* scriptState
, PassRefPtrWillBeRawPtr
<ScriptArguments
> arguments
)
84 internalAddMessage(TableMessageType
, LogMessageLevel
, scriptState
, arguments
);
87 void ConsoleBase::clear(ScriptState
* scriptState
, PassRefPtrWillBeRawPtr
<ScriptArguments
> arguments
)
89 internalAddMessage(ClearMessageType
, LogMessageLevel
, scriptState
, arguments
, true);
92 void ConsoleBase::trace(ScriptState
* scriptState
, PassRefPtrWillBeRawPtr
<ScriptArguments
> arguments
)
94 internalAddMessage(TraceMessageType
, LogMessageLevel
, scriptState
, arguments
, true);
97 void ConsoleBase::assertCondition(ScriptState
* scriptState
, PassRefPtrWillBeRawPtr
<ScriptArguments
> arguments
, bool condition
)
102 internalAddMessage(AssertMessageType
, ErrorMessageLevel
, scriptState
, arguments
, true);
105 void ConsoleBase::count(ScriptState
* scriptState
, PassRefPtrWillBeRawPtr
<ScriptArguments
> arguments
)
107 RefPtrWillBeRawPtr
<ScriptCallStack
> callStack(currentScriptCallStackForConsole(1));
108 const ScriptCallFrame
& lastCaller
= callStack
->at(0);
109 // Follow Firebug's behavior of counting with null and undefined title in
110 // the same bucket as no argument
112 arguments
->getFirstArgumentAsString(title
);
113 String identifier
= title
.isEmpty() ? String(lastCaller
.sourceURL() + ':' + String::number(lastCaller
.lineNumber()))
114 : String(title
+ '@');
116 HashCountedSet
<String
>::AddResult result
= m_counts
.add(identifier
);
117 String message
= title
+ ": " + String::number(result
.storedValue
->value
);
119 RefPtrWillBeRawPtr
<ConsoleMessage
> consoleMessage
= ConsoleMessage::create(ConsoleAPIMessageSource
, DebugMessageLevel
, message
);
120 consoleMessage
->setType(CountMessageType
);
121 consoleMessage
->setScriptState(scriptState
);
122 consoleMessage
->setCallStack(callStack
.release());
123 reportMessageToConsole(consoleMessage
.release());
126 void ConsoleBase::markTimeline(const String
& title
)
131 void ConsoleBase::profile(const String
& title
)
133 InspectorInstrumentation::consoleProfile(context(), title
);
136 void ConsoleBase::profileEnd(const String
& title
)
138 InspectorInstrumentation::consoleProfileEnd(context(), title
);
141 void ConsoleBase::time(const String
& 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);
155 // Follow Firebug's behavior of requiring a title that is not null or
156 // undefined for timing functions
160 HashMap
<String
, double>::iterator it
= m_times
.find(title
);
161 if (it
== m_times
.end())
164 double startTime
= it
->value
;
167 double elapsed
= monotonicallyIncreasingTime() - startTime
;
168 String message
= title
+ String::format(": %.3fms", elapsed
* 1000);
170 RefPtrWillBeRawPtr
<ConsoleMessage
> consoleMessage
= ConsoleMessage::create(ConsoleAPIMessageSource
, DebugMessageLevel
, message
);
171 consoleMessage
->setType(TimeEndMessageType
);
172 consoleMessage
->setScriptState(scriptState
);
173 consoleMessage
->setCallStack(currentScriptCallStackForConsole(1));
174 reportMessageToConsole(consoleMessage
.release());
177 void ConsoleBase::timeStamp(const String
& title
)
179 TRACE_EVENT_INSTANT1("devtools.timeline", "TimeStamp", TRACE_EVENT_SCOPE_THREAD
, "data", InspectorTimeStampEvent::data(context(), title
));
182 static String
formatTimelineTitle(const String
& title
)
184 return String::format("Timeline '%s'", title
.utf8().data());
187 void ConsoleBase::timeline(ScriptState
* scriptState
, const String
& title
)
189 TRACE_EVENT_COPY_ASYNC_BEGIN0("blink.console", formatTimelineTitle(title
).utf8().data(), this);
192 void ConsoleBase::timelineEnd(ScriptState
* scriptState
, const String
& title
)
194 TRACE_EVENT_COPY_ASYNC_END0("blink.console", formatTimelineTitle(title
).utf8().data(), this);
197 void ConsoleBase::group(ScriptState
* scriptState
, PassRefPtrWillBeRawPtr
<ScriptArguments
> arguments
)
199 internalAddMessage(StartGroupMessageType
, LogMessageLevel
, scriptState
, arguments
, true);
202 void ConsoleBase::groupCollapsed(ScriptState
* scriptState
, PassRefPtrWillBeRawPtr
<ScriptArguments
> arguments
)
204 internalAddMessage(StartGroupCollapsedMessageType
, LogMessageLevel
, scriptState
, arguments
, true);
207 void ConsoleBase::groupEnd()
209 internalAddMessage(EndGroupMessageType
, LogMessageLevel
, nullptr, nullptr, true);
212 void ConsoleBase::internalAddMessage(MessageType type
, MessageLevel level
, ScriptState
* scriptState
, PassRefPtrWillBeRawPtr
<ScriptArguments
> scriptArguments
, bool acceptNoArguments
)
214 RefPtrWillBeRawPtr
<ScriptArguments
> arguments
= scriptArguments
;
215 if (!acceptNoArguments
&& (!arguments
|| !arguments
->argumentCount()))
218 if (scriptState
&& !scriptState
->contextIsValid())
222 arguments
->getFirstArgumentAsString(message
);
223 RefPtrWillBeRawPtr
<ConsoleMessage
> consoleMessage
= ConsoleMessage::create(ConsoleAPIMessageSource
, level
, message
);
224 consoleMessage
->setType(type
);
225 consoleMessage
->setScriptState(scriptState
);
226 consoleMessage
->setScriptArguments(arguments
);
227 consoleMessage
->setCallStack(currentScriptCallStackForConsole(ScriptCallStack::maxCallStackSizeToCapture
));
228 reportMessageToConsole(consoleMessage
.release());