#include "config.h"
#include "Console.h"
#include "ChromeClient.h"
#include "CString.h"
#include "Frame.h"
#include "FrameLoader.h"
#include "FrameTree.h"
#include "InspectorController.h"
#include "Page.h"
#include "PageGroup.h"
#include "PlatformString.h"
#if USE(JSC)
#include <profiler/Profiler.h>
#endif
#include "ScriptCallStack.h"
#include <stdio.h>
namespace WebCore {
Console::Console(Frame* frame)
: m_frame(frame)
{
}
void Console::disconnectFrame()
{
m_frame = 0;
}
static void printSourceURLAndLine(const String& sourceURL, unsigned lineNumber)
{
if (!sourceURL.isEmpty()) {
if (lineNumber > 0)
printf("%s:%d: ", sourceURL.utf8().data(), lineNumber);
else
printf("%s: ", sourceURL.utf8().data());
}
}
static bool getFirstArgumentAsString(ScriptState* scriptState, const ScriptCallFrame& callFrame, String& result, bool checkForNullOrUndefined = false)
{
if (!callFrame.argumentCount())
return false;
const ScriptValue& value = callFrame.argumentAt(0);
if (checkForNullOrUndefined && (value.isNull() || value.isUndefined()))
return false;
result = value.toString(scriptState);
return true;
}
static void printMessageSourceAndLevelPrefix(MessageSource source, MessageLevel level)
{
const char* sourceString;
switch (source) {
case HTMLMessageSource:
sourceString = "HTML";
break;
case WMLMessageSource:
sourceString = "WML";
break;
case XMLMessageSource:
sourceString = "XML";
break;
case JSMessageSource:
sourceString = "JS";
break;
case CSSMessageSource:
sourceString = "CSS";
break;
default:
ASSERT_NOT_REACHED();
case OtherMessageSource:
sourceString = "OTHER";
break;
}
const char* levelString;
switch (level) {
case TipMessageLevel:
levelString = "TIP";
break;
default:
ASSERT_NOT_REACHED();
case LogMessageLevel:
levelString = "LOG";
break;
case WarningMessageLevel:
levelString = "WARN";
break;
case ErrorMessageLevel:
levelString = "ERROR";
break;
}
printf("%s %s:", sourceString, levelString);
}
void Console::addMessage(MessageSource source, MessageLevel level, const String& message, unsigned lineNumber, const String& sourceURL)
{
Page* page = this->page();
if (!page)
return;
page->chrome()->client()->addMessageToConsole(source, level, message, lineNumber, sourceURL);
if (!Console::shouldPrintExceptions())
return;
printSourceURLAndLine(sourceURL, lineNumber);
printMessageSourceAndLevelPrefix(source, level);
printf(" %s\n", message.utf8().data());
}
void Console::addMessage(MessageLevel level, ScriptCallStack* callStack, bool acceptNoArguments) {
Page* page = this->page();
if (!page)
return;
const ScriptCallFrame& lastCaller = callStack->at(0);
if (!acceptNoArguments && !lastCaller.argumentCount())
return;
String message;
if (getFirstArgumentAsString(callStack->state(), lastCaller, message))
page->chrome()->client()->addMessageToConsole(JSMessageSource, level, message, lastCaller.lineNumber(), lastCaller.sourceURL().prettyURL());
if (!Console::shouldPrintExceptions())
return;
printSourceURLAndLine(lastCaller.sourceURL().prettyURL(), 0);
printMessageSourceAndLevelPrefix(JSMessageSource, level);
for (unsigned i = 0; i < lastCaller.argumentCount(); ++i) {
String argAsString;
if (lastCaller.argumentAt(i).getString(argAsString))
printf(" %s", argAsString.utf8().data());
}
printf("\n");
}
void Console::debug(ScriptCallStack* callStack)
{
log(callStack);
}
void Console::error(ScriptCallStack* callStack)
{
addMessage(ErrorMessageLevel, callStack);
}
void Console::info(ScriptCallStack* callStack)
{
log(callStack);
}
void Console::log(ScriptCallStack* callStack)
{
addMessage(LogMessageLevel, callStack);
}
void Console::dir(ScriptCallStack* callStack)
{
addMessage(ObjectMessageLevel, callStack);
}
void Console::dirxml(ScriptCallStack* callStack)
{
addMessage(NodeMessageLevel, callStack);
}
void Console::trace(ScriptCallStack* callStack)
{
addMessage(TraceMessageLevel, callStack, true);
if (!shouldPrintExceptions())
return;
printf("Stack Trace\n");
for (unsigned i = 0; i < callStack->size(); ++i) {
String functionName = String(callStack->at(i).functionName());
printf("\t%s\n", functionName.utf8().data());
}
}
void Console::assertCondition(bool condition, ScriptCallStack* callStack)
{
if (condition)
return;
addMessage(ErrorMessageLevel, callStack, true);
}
void Console::count(ScriptCallStack* callStack)
{
Page* page = this->page();
if (!page)
return;
const ScriptCallFrame& lastCaller = callStack->at(0);
String title;
getFirstArgumentAsString(callStack->state(), lastCaller, title);
}
#if USE(JSC)
void Console::profile(const JSC::UString& title, ScriptCallStack* callStack)
{
Page* page = this->page();
if (!page)
return;
if (title.isNull())
return;
JSC::Profiler::profiler()->startProfiling(callStack->state(), title);
}
void Console::profileEnd(const JSC::UString& title, ScriptCallStack* callStack)
{
Page* page = this->page();
if (!page)
return;
RefPtr<JSC::Profile> profile = JSC::Profiler::profiler()->stopProfiling(callStack->state(), title);
if (!profile)
return;
m_profiles.append(profile);
}
#endif
void Console::time(const String& title)
{
Page* page = this->page();
if (!page)
return;
if (title.isNull())
return;
}
void Console::timeEnd(const String& title, ScriptCallStack* callStack)
{
UNUSED_PARAM(callStack);
Page* page = this->page();
if (!page)
return;
if (title.isNull())
return;
}
void Console::group(ScriptCallStack* callStack)
{
UNUSED_PARAM(callStack);
Page* page = this->page();
if (!page)
return;
}
void Console::groupEnd()
{
Page* page = this->page();
if (!page)
return;
}
void Console::warn(ScriptCallStack* callStack)
{
addMessage(WarningMessageLevel, callStack);
}
static bool printExceptions = false;
bool Console::shouldPrintExceptions()
{
return printExceptions;
}
void Console::setShouldPrintExceptions(bool print)
{
printExceptions = print;
}
Page* Console::page() const
{
if (!m_frame)
return 0;
return m_frame->page();
}
}