* @brief Creates a new console log file for the given device. */
| 112 | * @brief Creates a new console log file for the given device. |
| 113 | */ |
| 114 | void Console::ExportWorker::createFile(int deviceId) |
| 115 | { |
| 116 | const auto& token = Licensing::CommercialToken::current(); |
| 117 | if (!token.isValid() || !SS_LICENSE_GUARD() |
| 118 | || token.featureTier() < Licensing::FeatureTier::Trial) |
| 119 | return; |
| 120 | |
| 121 | auto it = m_deviceFiles.find(deviceId); |
| 122 | if (it != m_deviceFiles.end()) { |
| 123 | it->second.stream.reset(); |
| 124 | if (it->second.file) |
| 125 | it->second.file->close(); |
| 126 | |
| 127 | m_deviceFiles.erase(it); |
| 128 | } |
| 129 | |
| 130 | const auto dateTime = QDateTime::currentDateTime(); |
| 131 | auto fileName = dateTime.toString(QStringLiteral("yyyy_MMM_dd HH_mm_ss")); |
| 132 | if (deviceId >= 0) |
| 133 | fileName += QStringLiteral("_device%1").arg(deviceId); |
| 134 | |
| 135 | fileName += QStringLiteral(".txt"); |
| 136 | |
| 137 | const auto opMode = AppState::instance().operationMode(); |
| 138 | const auto& projectTitle = DataModel::ProjectModel::instance().title(); |
| 139 | QString subdirName; |
| 140 | switch (opMode) { |
| 141 | case SerialStudio::ProjectFile: |
| 142 | subdirName = projectTitle.isEmpty() ? QStringLiteral("Untitled") : projectTitle; |
| 143 | break; |
| 144 | case SerialStudio::QuickPlot: |
| 145 | subdirName = QStringLiteral("Quick Plot"); |
| 146 | break; |
| 147 | case SerialStudio::ConsoleOnly: |
| 148 | subdirName = QStringLiteral("Console"); |
| 149 | break; |
| 150 | } |
| 151 | |
| 152 | QDir dir(Misc::WorkspaceManager::instance().path("Console")); |
| 153 | if (!dir.exists(subdirName)) |
| 154 | dir.mkpath(subdirName); |
| 155 | |
| 156 | dir.cd(subdirName); |
| 157 | |
| 158 | auto& state = m_deviceFiles[deviceId]; |
| 159 | state.file = std::make_unique<QFile>(dir.filePath(fileName)); |
| 160 | |
| 161 | if (!state.file->open(QIODeviceBase::WriteOnly | QIODevice::Text)) { |
| 162 | Misc::Utilities::showMessageBox(QObject::tr("Console Output File Error"), |
| 163 | QObject::tr("Cannot open file for writing!"), |
| 164 | QMessageBox::Critical); |
| 165 | m_deviceFiles.erase(deviceId); |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | state.stream = std::make_unique<QTextStream>(state.file.get()); |
| 170 | state.stream->setGenerateByteOrderMark(true); |
| 171 | state.stream->setEncoding(QStringConverter::Utf8); |
nothing calls this directly
no test coverage detected