From b77ecde410ace1545ac6fdad7466e64e4fc635da Mon Sep 17 00:00:00 2001 From: Aurindam Jana Date: Tue, 17 Jan 2012 14:38:06 +0100 Subject: [PATCH] Console API: Add console.exception console.exception writes a message to the console and prints the JavaScript stack trace at the point where it is called. Change-Id: Idd2ff5982826accae0895db44c7ecf6130338cc7 Reviewed-by: Kai Koehne --- doc/src/declarative/qdeclarativedebugging.qdoc | 5 ++ .../qml/v8/qdeclarativebuiltinfunctions.cpp | 10 ++++ .../qml/v8/qdeclarativebuiltinfunctions_p.h | 1 + src/declarative/qml/v8/qv8engine.cpp | 1 + .../qdeclarativeconsole/data/exception.qml | 58 ++++++++++++++++++++++ .../tst_qdeclarativeconsole.cpp | 21 ++++++++ 6 files changed, 96 insertions(+) create mode 100644 tests/auto/declarative/qdeclarativeconsole/data/exception.qml diff --git a/doc/src/declarative/qdeclarativedebugging.qdoc b/doc/src/declarative/qdeclarativedebugging.qdoc index 49fe224..ab64b91 100644 --- a/doc/src/declarative/qdeclarativedebugging.qdoc +++ b/doc/src/declarative/qdeclarativedebugging.qdoc @@ -117,6 +117,11 @@ function f() { } \endqml +\section2 Exception + +\c console.exception prints an error message together with the stack trace of JavaScript +execution at the point where it is called. + \section1 Debugging Transitions When a transition doesn't look quite right, it can be helpful to view it in slow diff --git a/src/declarative/qml/v8/qdeclarativebuiltinfunctions.cpp b/src/declarative/qml/v8/qdeclarativebuiltinfunctions.cpp index 5f0b3ff..830b521 100644 --- a/src/declarative/qml/v8/qdeclarativebuiltinfunctions.cpp +++ b/src/declarative/qml/v8/qdeclarativebuiltinfunctions.cpp @@ -299,6 +299,16 @@ v8::Handle consoleAssert(const v8::Arguments &args) return v8::Undefined(); } +v8::Handle consoleException(const v8::Arguments &args) +{ + if (args.Length() == 0) + V8THROW_ERROR("console.exception(): Missing argument"); + console(Error, args); + printStack(); + + return v8::Undefined(); +} + v8::Handle stringArg(const v8::Arguments &args) { QString value = V8ENGINE()->toString(args.This()->ToString()); diff --git a/src/declarative/qml/v8/qdeclarativebuiltinfunctions_p.h b/src/declarative/qml/v8/qdeclarativebuiltinfunctions_p.h index cdb31b6..62dcffb 100644 --- a/src/declarative/qml/v8/qdeclarativebuiltinfunctions_p.h +++ b/src/declarative/qml/v8/qdeclarativebuiltinfunctions_p.h @@ -71,6 +71,7 @@ v8::Handle consoleCount(const v8::Arguments &args); v8::Handle consoleTrace(const v8::Arguments &args); v8::Handle consoleWarn(const v8::Arguments &args); v8::Handle consoleAssert(const v8::Arguments &args); +v8::Handle consoleException(const v8::Arguments &args); v8::Handle isQtObject(const v8::Arguments &args); v8::Handle rgba(const v8::Arguments &args); v8::Handle hsla(const v8::Arguments &args); diff --git a/src/declarative/qml/v8/qv8engine.cpp b/src/declarative/qml/v8/qv8engine.cpp index fa35533..e7fe2c7 100644 --- a/src/declarative/qml/v8/qv8engine.cpp +++ b/src/declarative/qml/v8/qv8engine.cpp @@ -538,6 +538,7 @@ void QV8Engine::initializeGlobal(v8::Handle global) console->Set(v8::String::New("time"), V8FUNCTION(consoleTime, this)); console->Set(v8::String::New("timeEnd"), V8FUNCTION(consoleTimeEnd, this)); console->Set(v8::String::New("trace"), V8FUNCTION(consoleTrace, this)); + console->Set(v8::String::New("exception"), V8FUNCTION(consoleException, this)); v8::Local qt = v8::Object::New(); diff --git a/tests/auto/declarative/qdeclarativeconsole/data/exception.qml b/tests/auto/declarative/qdeclarativeconsole/data/exception.qml new file mode 100644 index 0000000..d1a32be --- /dev/null +++ b/tests/auto/declarative/qdeclarativeconsole/data/exception.qml @@ -0,0 +1,58 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.0 + +QtObject { + function exceptionFail() { + console.exception("Exception 2") + } + + Component.onCompleted: { + try { + console.exception("Exception 1") + } catch (e) { + console.log(e); + } + + exceptionFail(); + } +} diff --git a/tests/auto/declarative/qdeclarativeconsole/tst_qdeclarativeconsole.cpp b/tests/auto/declarative/qdeclarativeconsole/tst_qdeclarativeconsole.cpp index 9a4a39f..34b0f65 100644 --- a/tests/auto/declarative/qdeclarativeconsole/tst_qdeclarativeconsole.cpp +++ b/tests/auto/declarative/qdeclarativeconsole/tst_qdeclarativeconsole.cpp @@ -55,6 +55,7 @@ private slots: void tracing(); void profiling(); void assert(); + void exception(); private: QDeclarativeEngine engine; @@ -141,6 +142,26 @@ void tst_qdeclarativeconsole::assert() delete object; } +void tst_qdeclarativeconsole::exception() +{ + QUrl testUrl = testFileUrl("exception.qml"); + + // exception() + QTest::ignoreMessage(QtCriticalMsg, "Exception 1"); + QTest::ignoreMessage(QtCriticalMsg, "Exception 2"); + QString trace1 = QString::fromLatin1("onCompleted (%1:%2:%3)\n").arg(testUrl.toString()).arg(51).arg(21); + QString trace2 = QString::fromLatin1("onCompleted (%1:%2:%3)\n").arg(testUrl.toString()).arg(56).arg(9); + QString trace3 = QString::fromLatin1("exceptionFail (%1:%2:%3)\n").arg(testUrl.toString()).arg(46).arg(17); + QTest::ignoreMessage(QtDebugMsg, qPrintable(trace1)); + QTest::ignoreMessage(QtDebugMsg, qPrintable(trace2)); + QTest::ignoreMessage(QtDebugMsg, qPrintable(trace3)); + + QDeclarativeComponent component(&engine, testUrl); + QObject *object = component.create(); + QVERIFY(object != 0); + delete object; +} + QTEST_MAIN(tst_qdeclarativeconsole) #include "tst_qdeclarativeconsole.moc" -- 2.7.4