\section1 Logging
-\c console.log() and \c console.debug() can be used to print information
+\c console.log(), \c console.debug(), \c console.time(), and \c console.timeEnd() can be used to print information
to the console. See \l{Debugging QML} for more information.
*/
}
\endqml
+\c console.time and console.timeEnd log the time (in milliseconds) that was spent between
+the calls. Both take a string argument that identifies the measurement. For example:
+
+\qml
+function f() {
+ console.time("wholeFunction");
+ console.time("firstPart");
+ // first part
+ console.timeEnd("firstPart");
+ // second part
+ console.timeEnd("wholeFunction");
+}
+\endqml
+
\section1 Debugging Transitions
When a transition doesn't look quite right, it can be helpful to view it in slow
return v8::Undefined();
}
+v8::Handle<v8::Value> consoleTime(const v8::Arguments &args)
+{
+ if (args.Length() != 1)
+ V8THROW_ERROR("console.time(): Invalid arguments");
+ QString name = V8ENGINE()->toString(args[0]);
+ V8ENGINE()->startTimer(name);
+ return v8::Undefined();
+}
+
+v8::Handle<v8::Value> consoleTimeEnd(const v8::Arguments &args)
+{
+ if (args.Length() != 1)
+ V8THROW_ERROR("console.time(): Invalid arguments");
+ QString name = V8ENGINE()->toString(args[0]);
+ bool wasRunning;
+ qint64 elapsed = V8ENGINE()->stopTimer(name, &wasRunning);
+ if (wasRunning) {
+ qDebug("%s: %llims", qPrintable(name), elapsed);
+ }
+ return v8::Undefined();
+}
+
v8::Handle<v8::Value> stringArg(const v8::Arguments &args)
{
QString value = V8ENGINE()->toString(args.This()->ToString());
v8::Handle<v8::Value> qsTrNoOp(const v8::Arguments &args);
v8::Handle<v8::Value> qsTrId(const v8::Arguments &args);
v8::Handle<v8::Value> qsTrIdNoOp(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> stringArg(const v8::Arguments &args);
}
{
using namespace QDeclarativeBuiltinFunctions;
v8::Local<v8::Function> printFn = V8FUNCTION(print, this);
+ v8::Local<v8::Function> consoleTimeFn = V8FUNCTION(consoleTime, this);
+ v8::Local<v8::Function> consoleTimeEndFn = V8FUNCTION(consoleTimeEnd, this);
v8::Local<v8::Object> console = v8::Object::New();
console->Set(v8::String::New("log"), printFn);
console->Set(v8::String::New("debug"), printFn);
+ console->Set(v8::String::New("time"), consoleTimeFn);
+ console->Set(v8::String::New("timeEnd"), consoleTimeEndFn);
v8::Local<v8::Object> qt = v8::Object::New();
emit q->signalHandlerException(scriptValueFromInternal(uncaughtException()));
}
+void QV8Engine::startTimer(const QString &timerName)
+{
+ if (!m_time.isValid())
+ m_time.start();
+ m_startedTimers[timerName] = m_time.elapsed();
+}
+
+qint64 QV8Engine::stopTimer(const QString &timerName, bool *wasRunning)
+{
+ if (!m_startedTimers.contains(timerName)) {
+ *wasRunning = false;
+ return 0;
+ }
+ *wasRunning = true;
+ qint64 startedAt = m_startedTimers.take(timerName);
+ return m_time.elapsed() - startedAt;
+}
+
QThreadStorage<QV8GCCallback::ThreadData *> QV8GCCallback::threadData;
void QV8GCCallback::initializeThreadData()
{
#include <QtCore/qmutex.h>
#include <QtCore/qstack.h>
#include <QtCore/qstringlist.h>
+#include <QtCore/QElapsedTimer>
#include <private/qv8_p.h>
#include <qjsengine.h>
void emitSignalHandlerException();
+ // used for console.time(), console.timeEnd()
+ void startTimer(const QString &timerName);
+ qint64 stopTimer(const QString &timerName, bool *wasRunning);
+
QObject *qtObjectFromJS(v8::Handle<v8::Value> value);
QSet<int> visitedConversionObjects;
protected:
Exception m_exception;
+ QElapsedTimer m_time;
+ QHash<QString, qint64> m_startedTimers;
+
QVariant toBasicVariant(v8::Handle<v8::Value>);
void initializeGlobal(v8::Handle<v8::Object>);