Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
QV8Engine: Console APIs, Extend functionality
Added console.trace, console.profile, console.profileEnd.

Change-Id: Icc38ddd550989eaba0085ece120695a13ec17322
Reviewed-by: Kai Koehne <kai.koehne@nokia.com>
  • Loading branch information
Aurindam Jana authored and Qt by Nokia committed Dec 14, 2011
1 parent f1bcbd1 commit 0f21883
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 61 deletions.
98 changes: 67 additions & 31 deletions src/declarative/debugger/qdeclarativedebugtrace.cpp
Expand Up @@ -98,57 +98,86 @@ void QDeclarativeDebugTrace::initialize()
traceInstance();
}

bool QDeclarativeDebugTrace::startProfiling()
{
return traceInstance()->startProfilingImpl();
}

bool QDeclarativeDebugTrace::stopProfiling()
{
return traceInstance()->stopProfilingImpl();
}

void QDeclarativeDebugTrace::addEvent(EventType t)
{
if (QDeclarativeDebugService::isDebuggingEnabled())
traceInstance()->addEventImpl(t);
traceInstance()->addEventImpl(t);
}

void QDeclarativeDebugTrace::startRange(RangeType t)
{
if (QDeclarativeDebugService::isDebuggingEnabled())
traceInstance()->startRangeImpl(t);
traceInstance()->startRangeImpl(t);
}

void QDeclarativeDebugTrace::rangeData(RangeType t, const QString &data)
{
if (QDeclarativeDebugService::isDebuggingEnabled())
traceInstance()->rangeDataImpl(t, data);
traceInstance()->rangeDataImpl(t, data);
}

void QDeclarativeDebugTrace::rangeData(RangeType t, const QUrl &data)
{
if (QDeclarativeDebugService::isDebuggingEnabled())
traceInstance()->rangeDataImpl(t, data);
traceInstance()->rangeDataImpl(t, data);
}

void QDeclarativeDebugTrace::rangeLocation(RangeType t, const QString &fileName, int line)
{
if (QDeclarativeDebugService::isDebuggingEnabled())
traceInstance()->rangeLocationImpl(t, fileName, line);
traceInstance()->rangeLocationImpl(t, fileName, line);
}

void QDeclarativeDebugTrace::rangeLocation(RangeType t, const QUrl &fileName, int line)
{
if (QDeclarativeDebugService::isDebuggingEnabled())
traceInstance()->rangeLocationImpl(t, fileName, line);
traceInstance()->rangeLocationImpl(t, fileName, line);
}

void QDeclarativeDebugTrace::endRange(RangeType t)
{
if (QDeclarativeDebugService::isDebuggingEnabled())
traceInstance()->endRangeImpl(t);
traceInstance()->endRangeImpl(t);
}

void QDeclarativeDebugTrace::animationFrame(qint64 delta)
{
Q_ASSERT(QDeclarativeDebugService::isDebuggingEnabled());
traceInstance()->animationFrameImpl(delta);
}

void QDeclarativeDebugTrace::sendProfilingData()
{
traceInstance()->sendMessages();
}

bool QDeclarativeDebugTrace::startProfilingImpl()
{
bool success = false;
if (!profilingEnabled()) {
setProfilingEnabled(true);
addEventImpl(StartTrace);
success = true;
}
return success;
}

bool QDeclarativeDebugTrace::stopProfilingImpl()
{
bool success = false;
if (profilingEnabled()) {
addEventImpl(EndTrace);
setProfilingEnabled(false);
success = true;
}
return success;
}

void QDeclarativeDebugTrace::addEventImpl(EventType event)
{
if (status() != Enabled || !m_enabled)
if (!QDeclarativeDebugService::isDebuggingEnabled() || !m_enabled)
return;

QDeclarativeDebugData ed = {m_timer.nsecsElapsed(), (int)Event, (int)event, QString(), -1, 0, 0};
Expand All @@ -157,7 +186,7 @@ void QDeclarativeDebugTrace::addEventImpl(EventType event)

void QDeclarativeDebugTrace::startRangeImpl(RangeType range)
{
if (status() != Enabled || !m_enabled)
if (!QDeclarativeDebugService::isDebuggingEnabled() || !m_enabled)
return;

QDeclarativeDebugData rd = {m_timer.nsecsElapsed(), (int)RangeStart, (int)range, QString(), -1, 0, 0};
Expand All @@ -166,7 +195,7 @@ void QDeclarativeDebugTrace::startRangeImpl(RangeType range)

void QDeclarativeDebugTrace::rangeDataImpl(RangeType range, const QString &rData)
{
if (status() != Enabled || !m_enabled)
if (!QDeclarativeDebugService::isDebuggingEnabled() || !m_enabled)
return;

QDeclarativeDebugData rd = {m_timer.nsecsElapsed(), (int)RangeData, (int)range, rData, -1, 0, 0};
Expand All @@ -175,7 +204,7 @@ void QDeclarativeDebugTrace::rangeDataImpl(RangeType range, const QString &rData

void QDeclarativeDebugTrace::rangeDataImpl(RangeType range, const QUrl &rData)
{
if (status() != Enabled || !m_enabled)
if (!QDeclarativeDebugService::isDebuggingEnabled() || !m_enabled)
return;

QDeclarativeDebugData rd = {m_timer.nsecsElapsed(), (int)RangeData, (int)range, rData.toString(QUrl::FormattingOption(0x100)), -1, 0, 0};
Expand All @@ -184,7 +213,7 @@ void QDeclarativeDebugTrace::rangeDataImpl(RangeType range, const QUrl &rData)

void QDeclarativeDebugTrace::rangeLocationImpl(RangeType range, const QString &fileName, int line)
{
if (status() != Enabled || !m_enabled)
if (!QDeclarativeDebugService::isDebuggingEnabled() || !m_enabled)
return;

QDeclarativeDebugData rd = {m_timer.nsecsElapsed(), (int)RangeLocation, (int)range, fileName, line, 0, 0};
Expand All @@ -193,7 +222,7 @@ void QDeclarativeDebugTrace::rangeLocationImpl(RangeType range, const QString &f

void QDeclarativeDebugTrace::rangeLocationImpl(RangeType range, const QUrl &fileName, int line)
{
if (status() != Enabled || !m_enabled)
if (!QDeclarativeDebugService::isDebuggingEnabled() || !m_enabled)
return;

QDeclarativeDebugData rd = {m_timer.nsecsElapsed(), (int)RangeLocation, (int)range, fileName.toString(QUrl::FormattingOption(0x100)), line, 0, 0};
Expand All @@ -202,7 +231,7 @@ void QDeclarativeDebugTrace::rangeLocationImpl(RangeType range, const QUrl &file

void QDeclarativeDebugTrace::endRangeImpl(RangeType range)
{
if (status() != Enabled || !m_enabled)
if (!QDeclarativeDebugService::isDebuggingEnabled() || !m_enabled)
return;

QDeclarativeDebugData rd = {m_timer.nsecsElapsed(), (int)RangeEnd, (int)range, QString(), -1, 0, 0};
Expand All @@ -211,7 +240,8 @@ void QDeclarativeDebugTrace::endRangeImpl(RangeType range)

void QDeclarativeDebugTrace::animationFrameImpl(qint64 delta)
{
if (status() != Enabled || !m_enabled)
Q_ASSERT(QDeclarativeDebugService::isDebuggingEnabled());
if (!m_enabled)
return;

int animCount = QUnifiedTimer::instance()->runningAnimationCount();
Expand All @@ -234,6 +264,16 @@ void QDeclarativeDebugTrace::processMessage(const QDeclarativeDebugData &message
m_data.append(message);
}

bool QDeclarativeDebugTrace::profilingEnabled()
{
return m_enabled;
}

void QDeclarativeDebugTrace::setProfilingEnabled(bool enable)
{
m_enabled = enable;
}

/*
Send the messages queued up by processMessage
*/
Expand Down Expand Up @@ -262,15 +302,11 @@ void QDeclarativeDebugTrace::messageReceived(const QByteArray &message)

m_messageReceived = true;

if (m_enabled != enabled) {
if (enabled) {
m_enabled = true;
addEventImpl(StartTrace);
} else {
addEventImpl(EndTrace);
m_enabled = false;
if (enabled) {
startProfilingImpl();
} else {
if (stopProfilingImpl())
sendMessages();
}
}
}

Expand Down
16 changes: 14 additions & 2 deletions src/declarative/debugger/qdeclarativedebugtrace_p.h
Expand Up @@ -119,8 +119,9 @@ class Q_DECLARATIVE_EXPORT QDeclarativeDebugTrace : public QDeclarativeDebugServ

static void initialize();

static bool startProfiling();
static bool stopProfiling();
static void addEvent(EventType);

static void startRange(RangeType);
static void rangeData(RangeType, const QString &);
static void rangeData(RangeType, const QUrl &);
Expand All @@ -129,11 +130,17 @@ class Q_DECLARATIVE_EXPORT QDeclarativeDebugTrace : public QDeclarativeDebugServ
static void endRange(RangeType);
static void animationFrame(qint64);

static void sendProfilingData();

QDeclarativeDebugTrace();
~QDeclarativeDebugTrace();

protected:
virtual void messageReceived(const QByteArray &);

private:
bool startProfilingImpl();
bool stopProfilingImpl();
void addEventImpl(EventType);
void startRangeImpl(RangeType);
void rangeDataImpl(RangeType, const QString &);
Expand All @@ -142,8 +149,13 @@ class Q_DECLARATIVE_EXPORT QDeclarativeDebugTrace : public QDeclarativeDebugServ
void rangeLocationImpl(RangeType, const QUrl &, int);
void endRangeImpl(RangeType);
void animationFrameImpl(qint64);
void processMessage(const QDeclarativeDebugData &);

bool profilingEnabled();
void setProfilingEnabled(bool enable);
void sendMessages();
void processMessage(const QDeclarativeDebugData &);

private:
QElapsedTimer m_timer;
bool m_enabled;
bool m_messageReceived;
Expand Down
84 changes: 74 additions & 10 deletions src/declarative/qml/v8/qdeclarativebuiltinfunctions.cpp
Expand Up @@ -48,13 +48,17 @@
#include <private/qdeclarativelocale_p.h>
#include <private/qv8engine_p.h>

#include <private/qv8profilerservice_p.h>
#include <private/qdeclarativedebugtrace_p.h>

#include <QtCore/qstring.h>
#include <QtCore/qdatetime.h>
#include <QtCore/qcryptographichash.h>
#include <QtCore/qrect.h>
#include <QtCore/qsize.h>
#include <QtCore/qpoint.h>
#include <QtCore/qurl.h>
#include <QtCore/qfile.h>
#include <QtCore/qcoreapplication.h>

#include <QtGui/qcolor.h>
Expand Down Expand Up @@ -139,6 +143,58 @@ v8::Handle<v8::Value> gc(const v8::Arguments &args)
return v8::Undefined();
}

v8::Handle<v8::Value> consoleError(const v8::Arguments &args)
{
return console(Error, args);
}

v8::Handle<v8::Value> consoleLog(const v8::Arguments &args)
{
//console.log
//console.debug
//print
return console(Log, args);
}

v8::Handle<v8::Value> consoleProfile(const v8::Arguments &args)
{
//DeclarativeDebugTrace cannot handle nested profiling
//although v8 can handle several profiling at once,
//we do not allow that. Hence, we pass an empty(default) title
Q_UNUSED(args);
QString title;

if (QDeclarativeDebugTrace::startProfiling()) {
QV8ProfilerService::instance()->startProfiling(title);
qDebug("Profiling started.");
} else {
qWarning("Profiling is already in progress. First, end current profiling session.");
}

return v8::Undefined();
}

v8::Handle<v8::Value> consoleProfileEnd(const v8::Arguments &args)
{
//DeclarativeDebugTrace cannot handle nested profiling
//although v8 can handle several profiling at once,
//we do not allow that. Hence, we pass an empty(default) title
Q_UNUSED(args);
QString title;

if (QDeclarativeDebugTrace::stopProfiling()) {
QV8ProfilerService *profiler = QV8ProfilerService::instance();
profiler->stopProfiling(title);
QDeclarativeDebugTrace::sendProfilingData();
profiler->sendProfilingData();
qDebug("Profiling ended.");
} else {
qWarning("Profiling was not started.");
}

return v8::Undefined();
}

v8::Handle<v8::Value> consoleTime(const v8::Arguments &args)
{
if (args.Length() != 1)
Expand All @@ -161,24 +217,32 @@ v8::Handle<v8::Value> consoleTimeEnd(const v8::Arguments &args)
return v8::Undefined();
}

v8::Handle<v8::Value> consoleLog(const v8::Arguments &args)
v8::Handle<v8::Value> consoleTrace(const v8::Arguments &args)
{
//console.log
//console.debug
//print
return console(Log, args);
if (args.Length() != 0)
V8THROW_ERROR("console.trace(): Invalid arguments");

//The v8 default is currently 10 stack frames.
v8::Handle<v8::StackTrace> stackTrace =
v8::StackTrace::CurrentStackTrace(10, v8::StackTrace::kOverview);
int stackCount = stackTrace->GetFrameCount();

for (uint i = 0; i < stackCount; i++) {
v8::Local<v8::StackFrame> frame = stackTrace->GetFrame(i);
v8::String::Utf8Value func_name(frame->GetFunctionName());
v8::String::Utf8Value script_name(frame->GetScriptName());
int lineNumber = frame->GetLineNumber();
int columnNumber = frame->GetColumn();
qDebug("%s (%s:%d:%d)\n", *func_name, *script_name, lineNumber, columnNumber);
}
return v8::Undefined();
}

v8::Handle<v8::Value> consoleWarn(const v8::Arguments &args)
{
return console(Warn, args);
}

v8::Handle<v8::Value> consoleError(const v8::Arguments &args)
{
return console(Error, args);
}

v8::Handle<v8::Value> stringArg(const v8::Arguments &args)
{
QString value = V8ENGINE()->toString(args.This()->ToString());
Expand Down
7 changes: 5 additions & 2 deletions src/declarative/qml/v8/qdeclarativebuiltinfunctions_p.h
Expand Up @@ -61,11 +61,14 @@ QT_BEGIN_NAMESPACE
namespace QDeclarativeBuiltinFunctions
{
v8::Handle<v8::Value> gc(const v8::Arguments &args);
v8::Handle<v8::Value> consoleLog(const v8::Arguments &args);
v8::Handle<v8::Value> consoleWarn(const v8::Arguments &args);
v8::Handle<v8::Value> consoleError(const v8::Arguments &args);
v8::Handle<v8::Value> consoleLog(const v8::Arguments &args);
v8::Handle<v8::Value> consoleProfile(const v8::Arguments &args);
v8::Handle<v8::Value> consoleProfileEnd(const v8::Arguments &args);
v8::Handle<v8::Value> consoleTime(const v8::Arguments &args);
v8::Handle<v8::Value> consoleTimeEnd(const v8::Arguments &args);
v8::Handle<v8::Value> consoleTrace(const v8::Arguments &args);
v8::Handle<v8::Value> consoleWarn(const v8::Arguments &args);
v8::Handle<v8::Value> isQtObject(const v8::Arguments &args);
v8::Handle<v8::Value> rgba(const v8::Arguments &args);
v8::Handle<v8::Value> hsla(const v8::Arguments &args);
Expand Down

0 comments on commit 0f21883

Please sign in to comment.