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 | // ConsoleScriptingInterface.cpp | 2 | // ConsoleScriptingInterface.cpp |
| 3 | // libraries/script-engine/src | 3 | // libraries/script-engine/src |
| 4 | // | 4 | // |
| 5 | // Created by volansystech on 6/1/17. | 5 | // Created by NeetBhagat on 6/1/17. |
| 6 | // Copyright 2014 High Fidelity, Inc. | 6 | // Copyright 2014 High Fidelity, Inc. |
| 7 | // | 7 | // |
| 8 | // ConsoleScriptingInterface is responsible to print logs | 8 | // ConsoleScriptingInterface is responsible for following functionality |
| 9 | // using "console" object on debug Window and Logs/log file with proper tags. | 9 | // Printing logs with various tags and grouping on debug Window and Logs/log file. |
| 10 | // Used in Javascripts 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 | // Distributed under the Apache License, Version 2.0. | 14 | // Distributed under the Apache License, Version 2.0. |
| 13 | // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html | 15 | // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html |
| @@ -16,6 +18,10 @@ · 2 unmodified lines | |||
| 16 | #include "ConsoleScriptingInterface.h" | 18 | #include "ConsoleScriptingInterface.h" |
| 17 | #include "ScriptEngine.h" | 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 | void ConsoleScriptingInterface::info(QString message) { | 25 | void ConsoleScriptingInterface::info(QString message) { |
| 20 | if (ScriptEngine* scriptEngine = qobject_cast<ScriptEngine*>(engine())) { | 26 | if (ScriptEngine* scriptEngine = qobject_cast<ScriptEngine*>(engine())) { |
| 21 | scriptEngine->scriptInfoMessage(message); | 27 | scriptEngine->scriptInfoMessage(message); |
| @@ -23,12 +29,12 @@ void ConsoleScriptingInterface::info(QString message) { · 1 unmodified line | |||
| 23 | } | 29 | } |
| 24 | 30 | ||
| 25 | void ConsoleScriptingInterface::log(QString message) { | 31 | void ConsoleScriptingInterface::log(QString message) { |
| 26 | if (_isGroupEnd) { | 32 | if (_groupDetails.count() == 0) { |
| 27 | if (ScriptEngine* scriptEngine = qobject_cast<ScriptEngine*>(engine())) { | 33 | if (ScriptEngine* scriptEngine = qobject_cast<ScriptEngine*>(engine())) { |
| 28 | scriptEngine->scriptPrintedMessage(message); | 34 | scriptEngine->scriptPrintedMessage(message); |
| 29 | } | 35 | } |
| 30 | } else { | 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 | void ConsoleScriptingInterface::time(QString labelName) { | 65 | void ConsoleScriptingInterface::time(QString labelName) { |
| 60 | QDateTime _currentTime = QDateTime::currentDateTime().toUTC(); | 66 | _timerDetails.insert(labelName, QDateTime::currentDateTime().toUTC()); |
| 61 | _listOfTimeValues.insert(labelName, _currentTime.toUTC()); | 67 | QString message = QString("%1: timer started").arg(labelName); |
| 68 | this->log(message); | ||
| 62 | } | 69 | } |
| 63 | 70 | ||
| 64 | void ConsoleScriptingInterface::timeEnd(QString labelName) { | 71 | void ConsoleScriptingInterface::timeEnd(QString labelName) { |
| 65 | QDateTime _dateTimeOfLabel; | 72 | if (!_timerDetails.contains(labelName)) { |
| 66 | QString message; | 73 | this->error("No such label found " + labelName); |
| 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); | ||
| 75 | return; | 74 | return; |
| 76 | } | 75 | } |
| 77 | 76 | ||
| 78 | QDateTime _currentDateTimeValue = QDateTime::currentDateTime().toUTC(); | 77 | if (_timerDetails.value(labelName).isNull()) { |
| 79 | _dateTimeOfLabel = _listOfTimeValues.value(labelName); | 78 | _timerDetails.remove(labelName); |
| 80 | 79 | this->error("Invalid start time for " + labelName); | |
| 81 | if (!_dateTimeOfLabel.isNull()) { | 80 | return; |
| 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)); | ||
| 90 | } | 81 | } |
| 91 | log("Time : " + message); | 82 | |
| 92 | } | 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) | 90 | this->log(message); |
| 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())); | ||
| 101 | } | 91 | } |
| 102 | 92 | ||
| 103 | void ConsoleScriptingInterface::asserts(bool condition, QString message) { | 93 | void ConsoleScriptingInterface::asserts(bool condition, QString message) { |
| 104 | if (!condition) { | 94 | if (!condition) { |
| 105 | QString assertFailed = "Assertion failed"; | 95 | QString assertionResult; |
| 106 | if (message.isEmpty()) { | 96 | if (message.isEmpty()) { |
| 107 | message = assertFailed; | 97 | assertionResult = "Assertion failed"; |
| 108 | } else { | 98 | } else { |
| 109 | QString inputType = typeid(message).name(); | 99 | assertionResult = QString("Assertion failed : %1").arg(message); |
| 110 | if (!inputType.compare("QString")) { | ||
| 111 | message = assertFailed; | ||
| 112 | } else { | ||
| 113 | message = assertFailed + " " + message; | ||
| 114 | } | ||
| 115 | } | 100 | } |
| 116 | error(message); | 101 | this->error(assertionResult); |
| 117 | } | 102 | } |
| 118 | } | 103 | } |
| 119 | 104 | ||
| 120 | void ConsoleScriptingInterface::trace() { | 105 | void ConsoleScriptingInterface::trace() { |
| 121 | const auto lineSeparator = "\n "; | ||
| 122 | if (ScriptEngine* scriptEngine = qobject_cast<ScriptEngine*>(engine())) { | 106 | if (ScriptEngine* scriptEngine = qobject_cast<ScriptEngine*>(engine())) { |
| 123 | QStringList backtrace = scriptEngine->currentContext()->backtrace(); | 107 | scriptEngine->scriptPrintedMessage |
| 124 | auto message = QString("\n[Backtrace]%1%2").arg(lineSeparator, backtrace.join(lineSeparator)); | 108 | (QString(STACK_TRACE_FORMAT).arg(LINE_SEPARATOR, |
| 125 | scriptEngine->scriptPrintedMessage(message); | 109 | scriptEngine->currentContext()->backtrace().join(LINE_SEPARATOR))); |
| 126 | } | 110 | } |
| 127 | } | 111 | } |
| 128 | 112 | ||
| 129 | void ConsoleScriptingInterface::clear() { | 113 | void ConsoleScriptingInterface::clear() { |
| 130 | if (ScriptEngine* scriptEngine = qobject_cast<ScriptEngine*>(engine())) { | 114 | if (ScriptEngine* scriptEngine = qobject_cast<ScriptEngine*>(engine())) { |
| 131 | scriptEngine->clearConsole(); | 115 | scriptEngine->clearDebugLogWindow(); |
| 132 | } | 116 | } |
| 133 | } | 117 | } |
| 134 | 118 | ||
| 135 | void ConsoleScriptingInterface::group(QString groupName) { | 119 | void ConsoleScriptingInterface::group(QString groupName) { |
| 136 | _selectedGroup = groupName; | 120 | this->logGroupMessage(groupName); |
| 137 | consoleGroupLog(GROUP, groupName); | 121 | _groupDetails.push_back(groupName); |
| 138 | } | 122 | } |
| 139 | 123 | ||
| 140 | void ConsoleScriptingInterface::groupCollapsed(QString groupName) { | 124 | void ConsoleScriptingInterface::groupCollapsed(QString groupName) { |
| 141 | _selectedGroup = groupName; | 125 | this->logGroupMessage(groupName); |
| 142 | consoleGroupLog(GROUPCOLLAPSED, groupName); | 126 | _groupDetails.push_back(groupName); |
| 143 | } | 127 | } |
| 144 | 128 | ||
| 145 | void ConsoleScriptingInterface::groupEnd() { | 129 | void ConsoleScriptingInterface::groupEnd() { |
| 146 | consoleGroupLog(_selectedGroup, GROUPEND); | 130 | _groupDetails.removeLast(); |
| 147 | } | 131 | } |
| 148 | 132 | ||
| 149 | void ConsoleScriptingInterface::consoleGroupLog(QString currentGroup, QString groupName) { | 133 | void ConsoleScriptingInterface::logGroupMessage(QString message) { |
| 150 | QPair<QString, QString> groupData; | 134 | int _appendIndentation = _groupDetails.count() * INDENTATION; |
| 151 | QString groupKeyName; | 135 | QString logMessage; |
| 152 | 136 | for (int count = 0; count < _appendIndentation; count++) { | |
| 153 | groupData = qMakePair(currentGroup, groupName); | 137 | logMessage.append(" "); |
| 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 | } | ||
| 182 | } | 138 | } |
| 139 | logMessage.append(message); | ||
| 140 | this->debug(logMessage); | ||
| 183 | } | 141 | } |