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.
33 #include "ChromeClient.h"
35 #include "FrameLoader.h"
36 #include "FrameTree.h"
37 #include "InspectorConsoleInstrumentation.h"
38 #include "InspectorController.h"
39 #include "MemoryInfo.h"
41 #include "PageGroup.h"
42 #include "PlatformString.h"
43 #include "ScriptArguments.h"
44 #include "ScriptCallStack.h"
45 #include "ScriptProfile.h"
46 #include "ScriptProfiler.h"
47 #include "ScriptValue.h"
49 #include <wtf/UnusedParam.h>
50 #include <wtf/text/CString.h>
52 #if PLATFORM(CHROMIUM)
53 #include "TraceEvent.h"
58 Console::Console(Frame* frame)
67 Frame* Console::frame() const
72 void Console::disconnectFrame()
79 static void printSourceURLAndLine(const String& sourceURL, unsigned lineNumber)
81 if (!sourceURL.isEmpty()) {
83 printf("%s:%d: ", sourceURL.utf8().data(), lineNumber);
85 printf("%s: ", sourceURL.utf8().data());
89 static void printMessageSourceAndLevelPrefix(MessageSource source, MessageLevel level)
91 const char* sourceString;
93 case HTMLMessageSource:
94 sourceString = "HTML";
96 case XMLMessageSource:
102 case NetworkMessageSource:
103 sourceString = "NETWORK";
105 case ConsoleAPIMessageSource:
106 sourceString = "CONSOLEAPI";
108 case OtherMessageSource:
109 sourceString = "OTHER";
112 ASSERT_NOT_REACHED();
113 sourceString = "UNKNOWN";
117 const char* levelString;
119 case TipMessageLevel:
122 case LogMessageLevel:
125 case WarningMessageLevel:
126 levelString = "WARN";
128 case ErrorMessageLevel:
129 levelString = "ERROR";
131 case DebugMessageLevel:
132 levelString = "DEBUG";
135 ASSERT_NOT_REACHED();
136 levelString = "UNKNOWN";
140 printf("%s %s:", sourceString, levelString);
143 void Console::addMessage(MessageSource source, MessageType type, MessageLevel level, const String& message, unsigned lineNumber, const String& sourceURL)
145 addMessage(source, type, level, message, lineNumber, sourceURL, 0);
148 void Console::addMessage(MessageSource source, MessageType type, MessageLevel level, const String& message, unsigned lineNumber, const String& sourceURL, PassRefPtr<ScriptCallStack> callStack)
150 Page* page = this->page();
154 page->chrome()->client()->addMessageToConsole(source, type, level, message, lineNumber, sourceURL);
157 InspectorInstrumentation::addMessageToConsole(page, source, type, level, message, 0, callStack);
159 InspectorInstrumentation::addMessageToConsole(page, source, type, level, message, lineNumber, sourceURL);
161 if (!Console::shouldPrintExceptions())
164 printSourceURLAndLine(sourceURL, lineNumber);
165 printMessageSourceAndLevelPrefix(source, level);
167 printf(" %s\n", message.utf8().data());
170 void Console::addMessage(MessageType type, MessageLevel level, PassRefPtr<ScriptArguments> prpArguments, PassRefPtr<ScriptCallStack> prpCallStack, bool acceptNoArguments)
172 RefPtr<ScriptArguments> arguments = prpArguments;
173 RefPtr<ScriptCallStack> callStack = prpCallStack;
175 Page* page = this->page();
179 const ScriptCallFrame& lastCaller = callStack->at(0);
181 if (!acceptNoArguments && !arguments->argumentCount())
184 if (Console::shouldPrintExceptions()) {
185 printSourceURLAndLine(lastCaller.sourceURL(), 0);
186 printMessageSourceAndLevelPrefix(ConsoleAPIMessageSource, level);
188 for (unsigned i = 0; i < arguments->argumentCount(); ++i) {
190 if (arguments->argumentAt(i).getString(arguments->globalState(), argAsString))
191 printf(" %s", argAsString.utf8().data());
197 if (arguments->getFirstArgumentAsString(message))
198 page->chrome()->client()->addMessageToConsole(ConsoleAPIMessageSource, type, level, message, lastCaller.lineNumber(), lastCaller.sourceURL());
200 InspectorInstrumentation::addMessageToConsole(page, ConsoleAPIMessageSource, type, level, message, arguments, callStack);
203 void Console::debug(PassRefPtr<ScriptArguments> arguments, PassRefPtr<ScriptCallStack> callStack)
205 // In Firebug, console.debug has the same behavior as console.log. So we'll do the same.
206 log(arguments, callStack);
209 void Console::error(PassRefPtr<ScriptArguments> arguments, PassRefPtr<ScriptCallStack> callStack)
211 addMessage(LogMessageType, ErrorMessageLevel, arguments, callStack);
214 void Console::info(PassRefPtr<ScriptArguments> arguments, PassRefPtr<ScriptCallStack> callStack)
216 log(arguments, callStack);
219 void Console::log(PassRefPtr<ScriptArguments> arguments, PassRefPtr<ScriptCallStack> callStack)
221 addMessage(LogMessageType, LogMessageLevel, arguments, callStack);
224 void Console::dir(PassRefPtr<ScriptArguments> arguments, PassRefPtr<ScriptCallStack> callStack)
226 addMessage(DirMessageType, LogMessageLevel, arguments, callStack);
229 void Console::dirxml(PassRefPtr<ScriptArguments> arguments, PassRefPtr<ScriptCallStack> callStack)
231 addMessage(DirXMLMessageType, LogMessageLevel, arguments, callStack);
234 void Console::trace(PassRefPtr<ScriptArguments> arguments, PassRefPtr<ScriptCallStack> prpCallStack)
236 RefPtr<ScriptCallStack> callStack = prpCallStack;
237 addMessage(TraceMessageType, LogMessageLevel, arguments, callStack, true);
239 if (!shouldPrintExceptions())
242 printf("Stack Trace\n");
243 for (unsigned i = 0; i < callStack->size(); ++i) {
244 String functionName = String(callStack->at(i).functionName());
245 printf("\t%s\n", functionName.utf8().data());
249 void Console::assertCondition(bool condition, PassRefPtr<ScriptArguments> arguments, PassRefPtr<ScriptCallStack> callStack)
254 addMessage(AssertMessageType, ErrorMessageLevel, arguments, callStack, true);
257 void Console::count(PassRefPtr<ScriptArguments> arguments, PassRefPtr<ScriptCallStack> callStack)
259 InspectorInstrumentation::consoleCount(page(), arguments, callStack);
262 void Console::markTimeline(PassRefPtr<ScriptArguments> arguments, PassRefPtr<ScriptCallStack>)
264 InspectorInstrumentation::consoleTimeStamp(page(), arguments);
267 #if ENABLE(JAVASCRIPT_DEBUGGER)
269 void Console::profile(const String& title, ScriptState* state, PassRefPtr<ScriptCallStack> callStack)
271 Page* page = this->page();
275 // FIXME: log a console message when profiling is disabled.
276 if (!InspectorInstrumentation::profilerEnabled(page))
279 String resolvedTitle = title;
280 if (title.isNull()) // no title so give it the next user initiated profile title.
281 resolvedTitle = InspectorInstrumentation::getCurrentUserInitiatedProfileName(page, true);
283 ScriptProfiler::start(state, resolvedTitle);
285 const ScriptCallFrame& lastCaller = callStack->at(0);
286 InspectorInstrumentation::addStartProfilingMessageToConsole(page, resolvedTitle, lastCaller.lineNumber(), lastCaller.sourceURL());
289 void Console::profileEnd(const String& title, ScriptState* state, PassRefPtr<ScriptCallStack> callStack)
291 Page* page = this->page();
295 if (!InspectorInstrumentation::profilerEnabled(page))
298 RefPtr<ScriptProfile> profile = ScriptProfiler::stop(state, title);
302 m_profiles.append(profile);
303 InspectorInstrumentation::addProfile(page, profile, callStack);
308 void Console::time(const String& title)
310 InspectorInstrumentation::startConsoleTiming(page(), title);
311 #if PLATFORM(CHROMIUM)
312 if (PlatformSupport::isTraceEventEnabled())
313 PlatformSupport::traceEventBegin(title.utf8().data(), 0, 0);
317 void Console::timeEnd(const String& title, PassRefPtr<ScriptArguments>, PassRefPtr<ScriptCallStack> callStack)
319 #if PLATFORM(CHROMIUM)
320 if (PlatformSupport::isTraceEventEnabled())
321 PlatformSupport::traceEventEnd(title.utf8().data(), 0, 0);
323 InspectorInstrumentation::stopConsoleTiming(page(), title, callStack);
326 void Console::timeStamp(PassRefPtr<ScriptArguments> arguments, PassRefPtr<ScriptCallStack>)
328 InspectorInstrumentation::consoleTimeStamp(page(), arguments);
331 void Console::group(PassRefPtr<ScriptArguments> arguments, PassRefPtr<ScriptCallStack> callStack)
333 InspectorInstrumentation::addMessageToConsole(page(), ConsoleAPIMessageSource, StartGroupMessageType, LogMessageLevel, String(), arguments, callStack);
336 void Console::groupCollapsed(PassRefPtr<ScriptArguments> arguments, PassRefPtr<ScriptCallStack> callStack)
338 InspectorInstrumentation::addMessageToConsole(page(), ConsoleAPIMessageSource, StartGroupCollapsedMessageType, LogMessageLevel, String(), arguments, callStack);
341 void Console::groupEnd()
343 InspectorInstrumentation::addMessageToConsole(page(), ConsoleAPIMessageSource, EndGroupMessageType, LogMessageLevel, String(), 0, String());
346 bool Console::shouldCaptureFullStackTrace() const
348 #if ENABLE(INSPECTOR)
349 Page* page = this->page();
353 return page->inspectorController()->hasFrontend();
359 void Console::warn(PassRefPtr<ScriptArguments> arguments, PassRefPtr<ScriptCallStack> callStack)
361 addMessage(LogMessageType, WarningMessageLevel, arguments, callStack);
364 MemoryInfo* Console::memory() const
366 m_memory = MemoryInfo::create(m_frame);
367 return m_memory.get();
370 static bool printExceptions = false;
372 bool Console::shouldPrintExceptions()
374 return printExceptions;
377 void Console::setShouldPrintExceptions(bool print)
379 printExceptions = print;
382 Page* Console::page() const
386 return m_frame->page();
389 } // namespace WebCore