feat: Added statistics and crash log uploading (#1149)

Co-authored-by: Justus Garbe <gihihoh@gmail.com>
This commit is contained in:
Nik
2023-06-20 11:55:56 +02:00
committed by GitHub
parent 54061c56bf
commit f703b15165
23 changed files with 401 additions and 94 deletions

View File

@@ -1,10 +1,12 @@
#include <hex.hpp>
#include <hex/helpers/http_requests.hpp>
#include <hex/api/event.hpp>
#include <hex/api/content_registry.hpp>
#include <hex/api/localization.hpp>
#include <hex/api/plugin_manager.hpp>
#include <hex/api/theme_manager.hpp>
#include <hex/api/layout_manager.hpp>
#include <hex/api_urls.hpp>
#include <hex/ui/view.hpp>
#include <hex/helpers/fs.hpp>
#include <hex/helpers/logger.hpp>
@@ -26,6 +28,7 @@
#include <content/popups/popup_notification.hpp>
#include <content/popups/popup_question.hpp>
#include <content/popups/popup_telemetry_request.hpp>
#include <content/recent.hpp>
#include <string>
@@ -46,20 +49,28 @@ namespace hex::plugin::builtin {
std::fs::path m_logFilePath;
std::function<void()> m_restoreCallback;
std::function<void()> m_deleteCallback;
bool m_reportError = true;
public:
PopupRestoreBackup(std::fs::path logFilePath, std::function<void()> restoreCallback, std::function<void()> deleteCallback)
PopupRestoreBackup(const std::fs::path &logFilePath, const std::function<void()> &restoreCallback, const std::function<void()> &deleteCallback)
: Popup("hex.builtin.popup.safety_backup.title"),
m_logFilePath(logFilePath),
m_restoreCallback(restoreCallback),
m_deleteCallback(deleteCallback) { }
m_deleteCallback(deleteCallback) {
this->m_reportError = ContentRegistry::Settings::read("hex.builtin.setting.general", "hex.builtin.setting.general.upload_crash_logs", 1);
}
void drawContent() override {
ImGui::TextUnformatted("hex.builtin.popup.safety_backup.desc"_lang);
if (!this->m_logFilePath.empty()) {
ImGui::NewLine();
ImGui::TextUnformatted("hex.builtin.popup.safety_backup.log_file"_lang);
ImGui::SameLine(0, 2_scaled);
if (ImGui::Hyperlink(this->m_logFilePath.filename().string().c_str())) {
fs::openFolderWithSelectionExternal(this->m_logFilePath);
}
ImGui::Checkbox("hex.builtin.popup.safety_backup.report_error"_lang, &this->m_reportError);
ImGui::NewLine();
}
@@ -69,6 +80,31 @@ namespace hex::plugin::builtin {
this->m_restoreCallback();
this->m_deleteCallback();
if (this->m_reportError) {
wolv::io::File logFile(this->m_logFilePath, wolv::io::File::Mode::Read);
if (logFile.isValid()) {
// Read current log file data
auto data = logFile.readString();
// Anonymize the log file
{
for (u32 pathType = 0; pathType < u32(fs::ImHexPath::END); pathType++) {
for (auto &folder : fs::getDefaultPaths(static_cast<fs::ImHexPath>(pathType))) {
auto parent = wolv::util::toUTF8String(folder.parent_path());
data = wolv::util::replaceStrings(data, parent, "<*****>");
}
}
}
TaskManager::createBackgroundTask("Upload Crash report", [path = this->m_logFilePath, data](auto&){
HttpRequest request("POST", ImHexApiURL + std::string("/crash_upload"));
request.uploadFile(std::vector<u8>(data.begin(), data.end()), "file", path.filename()).wait();
});
}
}
ContentRegistry::Settings::write("hex.builtin.setting.general", "hex.builtin.setting.general.upload_crash_logs", i64(this->m_reportError));
this->close();
}
ImGui::SameLine();
@@ -398,23 +434,14 @@ namespace hex::plugin::builtin {
loadDefaultLayout();
});
#if defined(HEX_UPDATE_CHECK)
EventManager::subscribe<EventWindowInitialized>([] {
// documentation of the value above the setting definition
auto showCheckForUpdates = ContentRegistry::Settings::read("hex.builtin.setting.general", "hex.builtin.setting.general.check_for_updates", 2);
if (showCheckForUpdates == 2) {
ContentRegistry::Settings::write("hex.builtin.setting.general", "hex.builtin.setting.general.check_for_updates", 0);
PopupQuestion::open("hex.builtin.welcome.check_for_updates_text"_lang,
[] {
ContentRegistry::Settings::write("hex.builtin.setting.general", "hex.builtin.setting.general.check_for_updates", 1);
},
[] {
}
);
auto allowServerContact = ContentRegistry::Settings::read("hex.builtin.setting.general", "hex.builtin.setting.general.server_contact", 2);
if (allowServerContact == 2) {
ContentRegistry::Settings::write("hex.builtin.setting.general", "hex.builtin.setting.general.server_contact", 0);
PopupTelemetryRequest::open();
}
});
#endif
// Clear project context if we go back to the welcome screen
EventManager::subscribe<EventProviderChanged>([](hex::prv::Provider *oldProvider, hex::prv::Provider *newProvider) {
@@ -452,7 +479,7 @@ namespace hex::plugin::builtin {
auto backupFilePath = path / BackupFileName;
bool hasBackupFile = wolv::io::fs::exists(backupFilePath);
PopupRestoreBackup::open(
// path of log file
crashFileData.value("logFile", ""),