Showing the changes to libraries/script-engine/src/ConsoleScriptingInterface.cpp (1 of 15 files). Show all
Unified · Split
| @@ -2,12 +2,14 @@ · 1 unmodified line | ||
| 2 | 2 | // ConsoleScriptingInterface.cpp |
| 3 | 3 | // libraries/script-engine/src |
| 4 | 4 | // |
| 5 | −// Created by volansystech on 6/1/17. | |
| 5 | +// Created by NeetBhagat on 6/1/17. | |
| 6 | 6 | // Copyright 2014 High Fidelity, Inc. |
| 7 | 7 | // |
| 8 | −// ConsoleScriptingInterface is responsible to print logs | |
| 9 | −// using "console" object on debug Window and Logs/log file with proper tags. | |
| 10 | −// Used in Javascripts file. | |
| 8 | +// ConsoleScriptingInterface is responsible for following functionality | |
| 9 | +// Printing logs with various tags and grouping on debug Window and Logs/log file. | |
| 10 | +// Debugging functionalities like Timer start-end, assertion, trace. | |
| 11 | +// To use these functionalities, use "console" object in Javascript files. | |
| 12 | +// For examples please refer "scripts/developer/tests/consoleObjectTest.js" | |
| 11 | 13 | // |
| 12 | 14 | // Distributed under the Apache License, Version 2.0. |
| 13 | 15 | // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html |
| @@ -16,6 +18,10 @@ · 2 unmodified lines | ||
| 16 | 18 | #include "ConsoleScriptingInterface.h" |
| 17 | 19 | #include "ScriptEngine.h" |
| 18 | 20 | |
| 21 | +#define INDENTATION 4 | |
| 22 | +const QString LINE_SEPARATOR = "\n "; | |
| 23 | +const QString STACK_TRACE_FORMAT = "\n[Stacktrace]%1%2"; | |
| 24 | + | |
| 19 | 25 | void ConsoleScriptingInterface::info(QString message) { |
| 20 | 26 | if (ScriptEngine* scriptEngine = qobject_cast<ScriptEngine*>(engine())) { |
| 21 | 27 | scriptEngine->scriptInfoMessage(message); |
| @@ -23,12 +29,12 @@ void ConsoleScriptingInterface::info(QString message) { · 1 unmodified line | ||
| 23 | 29 | } |
| 24 | 30 | |
| 25 | 31 | void ConsoleScriptingInterface::log(QString message) { |
| 26 | − if (_isGroupEnd) { | |
| 32 | + if (_groupDetails.count() == 0) { | |
| 27 | 33 | if (ScriptEngine* scriptEngine = qobject_cast<ScriptEngine*>(engine())) { |
| 28 | 34 | scriptEngine->scriptPrintedMessage(message); |
| 29 | 35 | } |
| 30 | 36 | } else { |
| 31 | − consoleGroupLog(_selectedGroup, message); | |
| 37 | + this->logGroupMessage(message); | |
| 32 | 38 | } |
| 33 | 39 | } |
| 34 | 40 | |
| @@ -57,127 +63,79 @@ void ConsoleScriptingInterface::exception(QString message) { · 22 unmodified lines | ||
| 57 | 63 | } |
| 58 | 64 | |
| 59 | 65 | void ConsoleScriptingInterface::time(QString labelName) { |
| 60 | − QDateTime _currentTime = QDateTime::currentDateTime().toUTC(); | |
| 61 | − _listOfTimeValues.insert(labelName, _currentTime.toUTC()); | |
| 66 | + _timerDetails.insert(labelName, QDateTime::currentDateTime().toUTC()); | |
| 67 | + QString message = QString("%1: timer started").arg(labelName); | |
| 68 | + this->log(message); | |
| 62 | 69 | } |
| 63 | 70 | |
| 64 | 71 | void ConsoleScriptingInterface::timeEnd(QString labelName) { |
| 65 | − QDateTime _dateTimeOfLabel; | |
| 66 | − QString message; | |
| 67 | − qint64 millisecondsDiff = 0; | |
| 68 | − | |
| 69 | − bool _labelInList = _listOfTimeValues.contains(labelName); | |
| 70 | − | |
| 71 | − //Check if not exist items in list | |
| 72 | − if (!_labelInList) { | |
| 73 | − message = "No such label found " + labelName; | |
| 74 | − error(message); | |
| 72 | + if (!_timerDetails.contains(labelName)) { | |
| 73 | + this->error("No such label found " + labelName); | |
| 75 | 74 | return; |
| 76 | 75 | } |
| 77 | 76 | |
| 78 | − QDateTime _currentDateTimeValue = QDateTime::currentDateTime().toUTC(); | |
| 79 | − _dateTimeOfLabel = _listOfTimeValues.value(labelName); | |
| 80 | − | |
| 81 | − if (!_dateTimeOfLabel.isNull()) { | |
| 82 | − _listOfTimeValues.remove(labelName); | |
| 83 | − } | |
| 84 | − | |
| 85 | − if (_dateTimeOfLabel.toString(TIME_FORMAT) == _currentDateTimeValue.toString(TIME_FORMAT)) { | |
| 86 | − millisecondsDiff = _dateTimeOfLabel.msecsTo(_currentDateTimeValue); | |
| 87 | − message = QString::number(millisecondsDiff) + " ms"; | |
| 88 | − } else { | |
| 89 | − message = secondsToString(_dateTimeOfLabel.secsTo(_currentDateTimeValue)); | |
| 77 | + if (_timerDetails.value(labelName).isNull()) { | |
| 78 | + _timerDetails.remove(labelName); | |
| 79 | + this->error("Invalid start time for " + labelName); | |
| 80 | + return; | |
| 90 | 81 | } |
| 91 | − log("Time : " + message); | |
| 92 | −} | |
| 82 | + | |
| 83 | + QDateTime _startTime = _timerDetails.value(labelName); | |
| 84 | + QDateTime _endTime = QDateTime::currentDateTime().toUTC(); | |
| 85 | + qint64 diffInMS = _startTime.msecsTo(_endTime); | |
| 86 | + | |
| 87 | + QString message = QString("%1: %2ms").arg(labelName).arg(QString::number(diffInMS)); | |
| 88 | + _timerDetails.remove(labelName); | |
| 93 | 89 | |
| 94 | −QString ConsoleScriptingInterface::secondsToString(qint64 seconds) | |
| 95 | −{ | |
| 96 | − qint64 days = seconds / DAY; | |
| 97 | − QTime timeDuration = QTime(0, 0).addSecs(seconds % DAY); | |
| 98 | − | |
| 99 | − return QString("%1 days, %2 hours, %3 minutes, %4 seconds") | |
| 100 | − .arg(QString::number(days)).arg(QString::number(timeDuration.hour())).arg(QString::number(timeDuration.minute())).arg(QString::number(timeDuration.second())); | |
| 90 | + this->log(message); | |
| 101 | 91 | } |
| 102 | 92 | |
| 103 | 93 | void ConsoleScriptingInterface::asserts(bool condition, QString message) { |
| 104 | 94 | if (!condition) { |
| 105 | − QString assertFailed = "Assertion failed"; | |
| 95 | + QString assertionResult; | |
| 106 | 96 | if (message.isEmpty()) { |
| 107 | − message = assertFailed; | |
| 97 | + assertionResult = "Assertion failed"; | |
| 108 | 98 | } else { |
| 109 | − QString inputType = typeid(message).name(); | |
| 110 | − if (!inputType.compare("QString")) { | |
| 111 | − message = assertFailed; | |
| 112 | − } else { | |
| 113 | − message = assertFailed + " " + message; | |
| 114 | − } | |
| 99 | + assertionResult = QString("Assertion failed : %1").arg(message); | |
| 115 | 100 | } |
| 116 | − error(message); | |
| 101 | + this->error(assertionResult); | |
| 117 | 102 | } |
| 118 | 103 | } |
| 119 | 104 | |
| 120 | 105 | void ConsoleScriptingInterface::trace() { |
| 121 | − const auto lineSeparator = "\n "; | |
| 122 | 106 | if (ScriptEngine* scriptEngine = qobject_cast<ScriptEngine*>(engine())) { |
| 123 | − QStringList backtrace = scriptEngine->currentContext()->backtrace(); | |
| 124 | − auto message = QString("\n[Backtrace]%1%2").arg(lineSeparator, backtrace.join(lineSeparator)); | |
| 125 | − scriptEngine->scriptPrintedMessage(message); | |
| 107 | + scriptEngine->scriptPrintedMessage | |
| 108 | + (QString(STACK_TRACE_FORMAT).arg(LINE_SEPARATOR, | |
| 109 | + scriptEngine->currentContext()->backtrace().join(LINE_SEPARATOR))); | |
| 126 | 110 | } |
| 127 | 111 | } |
| 128 | 112 | |
| 129 | 113 | void ConsoleScriptingInterface::clear() { |
| 130 | 114 | if (ScriptEngine* scriptEngine = qobject_cast<ScriptEngine*>(engine())) { |
| 131 | − scriptEngine->clearConsole(); | |
| 115 | + scriptEngine->clearDebugLogWindow(); | |
| 132 | 116 | } |
| 133 | 117 | } |
| 134 | 118 | |
| 135 | 119 | void ConsoleScriptingInterface::group(QString groupName) { |
| 136 | − _selectedGroup = groupName; | |
| 137 | − consoleGroupLog(GROUP, groupName); | |
| 120 | + this->logGroupMessage(groupName); | |
| 121 | + _groupDetails.push_back(groupName); | |
| 138 | 122 | } |
| 139 | 123 | |
| 140 | 124 | void ConsoleScriptingInterface::groupCollapsed(QString groupName) { |
| 141 | − _selectedGroup = groupName; | |
| 142 | − consoleGroupLog(GROUPCOLLAPSED, groupName); | |
| 125 | + this->logGroupMessage(groupName); | |
| 126 | + _groupDetails.push_back(groupName); | |
| 143 | 127 | } |
| 144 | 128 | |
| 145 | 129 | void ConsoleScriptingInterface::groupEnd() { |
| 146 | − consoleGroupLog(_selectedGroup, GROUPEND); | |
| 130 | + _groupDetails.removeLast(); | |
| 147 | 131 | } |
| 148 | 132 | |
| 149 | −void ConsoleScriptingInterface::consoleGroupLog(QString currentGroup, QString groupName) { | |
| 150 | − QPair<QString, QString> groupData; | |
| 151 | − QString groupKeyName; | |
| 152 | − | |
| 153 | − groupData = qMakePair(currentGroup, groupName); | |
| 154 | − groupKeyName = groupData.first; | |
| 155 | − if (groupData.first == GROUP || groupData.first == GROUPCOLLAPSED) { | |
| 156 | − groupKeyName = groupData.second; | |
| 157 | − if (_isSameLevel) { | |
| 158 | − _addSpace = _addSpace.mid(0, _addSpace.length() - 3); | |
| 159 | − } | |
| 160 | − if (_isGroupEnd) { | |
| 161 | − _addSpace = ""; | |
| 162 | − _isGroupEnd = false; | |
| 163 | − } else { | |
| 164 | − _addSpace += " "; | |
| 165 | − } | |
| 166 | − debug(_addSpace.mid(0, _addSpace.length() - 1) + groupData.second); | |
| 167 | − _isSameLevel = false; | |
| 168 | − } else { | |
| 169 | − if (groupData.first == groupKeyName && groupData.first != GROUPCOLLAPSED && groupData.first != GROUP && groupData.second != GROUPEND) { | |
| 170 | − if (!_isSameLevel) { | |
| 171 | − _addSpace += " "; //space inner element | |
| 172 | − _isSameLevel = true; //same level log entry | |
| 173 | − } | |
| 174 | − debug(_addSpace + groupData.second); | |
| 175 | − } | |
| 176 | − } | |
| 177 | − if (groupData.second == GROUPEND) { | |
| 178 | − _addSpace = _addSpace.mid(0, _addSpace.length() - 3); | |
| 179 | − if (_addSpace.length() == 0) { | |
| 180 | − _isGroupEnd = true; | |
| 181 | − } | |
| 133 | +void ConsoleScriptingInterface::logGroupMessage(QString message) { | |
| 134 | + int _appendIndentation = _groupDetails.count() * INDENTATION; | |
| 135 | + QString logMessage; | |
| 136 | + for (int count = 0; count < _appendIndentation; count++) { | |
| 137 | + logMessage.append(" "); | |
| 182 | 138 | } |
| 139 | + logMessage.append(message); | |
| 140 | + this->debug(logMessage); | |
| 183 | 141 | } |