From e4d9b70040487a7decb35fbb62c41c092c1380a0 Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Tue, 17 Mar 2020 22:01:16 -0600 Subject: [PATCH] better logging for translation extensions --- extensions/bingtranslate.cpp | 2 +- extensions/googlecloudtranslate.cpp | 8 ++++---- extensions/googletranslate.cpp | 2 +- extensions/translatewrapper.cpp | 30 ++++++++++++----------------- 4 files changed, 18 insertions(+), 24 deletions(-) diff --git a/extensions/bingtranslate.cpp b/extensions/bingtranslate.cpp index 9fdd35b..a85fac9 100644 --- a/extensions/bingtranslate.cpp +++ b/extensions/bingtranslate.cpp @@ -89,6 +89,6 @@ std::pair Translate(const std::wstring& text) }) // Response formatted as JSON: translation starts with text":" and ends with ","to if (std::wsmatch results; std::regex_search(httpRequest.response, results, std::wregex(L"text\":\"(.+)\",\"t"))) return { true, results[1] }; - else return { false, TRANSLATION_ERROR }; + else return { false, FormatString(L"%s: %s", TRANSLATION_ERROR, httpRequest.response) }; else return { false, FormatString(L"%s (code=%u)", TRANSLATION_ERROR, httpRequest.errorCode) }; } diff --git a/extensions/googlecloudtranslate.cpp b/extensions/googlecloudtranslate.cpp index 055e3da..29a2e05 100644 --- a/extensions/googlecloudtranslate.cpp +++ b/extensions/googlecloudtranslate.cpp @@ -6,7 +6,7 @@ extern const wchar_t* TRANSLATION_ERROR; extern const char* API_KEY; extern QFormLayout* display; -extern QSettings* settings; +extern QSettings settings; extern Synchronized translateTo; const char* TRANSLATION_PROVIDER = "Google Cloud Translate"; @@ -82,9 +82,9 @@ BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved { case DLL_PROCESS_ATTACH: { - auto keyInput = new QLineEdit(settings->value(API_KEY).toString()); + auto keyInput = new QLineEdit(settings.value(API_KEY).toString()); key->assign(S(keyInput->text())); - QObject::connect(keyInput, &QLineEdit::textChanged, [](QString key) { settings->setValue(API_KEY, S(::key->assign(S(key)))); }); + QObject::connect(keyInput, &QLineEdit::textChanged, [](QString key) { settings.setValue(API_KEY, S(::key->assign(S(key)))); }); display->addRow(API_KEY, keyInput); auto googleCloudInfo = new QLabel( "https://codelabs.developers.google.com/codelabs/cloud-translation-intro" @@ -113,7 +113,7 @@ std::pair Translate(const std::wstring& text) { // Response formatted as JSON: starts with "translatedText": " and translation is enclosed in quotes followed by a comma if (std::wsmatch results; std::regex_search(httpRequest.response, results, std::wregex(L"\"translatedText\": \"(.+?)\","))) return { true, results[1] }; - return { false, TRANSLATION_ERROR }; + return { false, FormatString(L"%s: %s", TRANSLATION_ERROR, httpRequest.response) }; } else return { false, FormatString(L"%s (code=%u)", TRANSLATION_ERROR, httpRequest.errorCode) }; } diff --git a/extensions/googletranslate.cpp b/extensions/googletranslate.cpp index e808ffd..d718fe7 100644 --- a/extensions/googletranslate.cpp +++ b/extensions/googletranslate.cpp @@ -169,7 +169,7 @@ std::pair Translate(const std::wstring& text) if (!IsHash(results[1])) translation += std::wstring(results[1]) + L" "; if (!translation.empty()) return { true, translation }; } - return { false, FormatString(L"%s (TKK=%u)", TRANSLATION_ERROR, _InterlockedExchange(&TKK, 0)) }; + return { false, FormatString(L"%s (TKK=%u): %s", TRANSLATION_ERROR, _InterlockedExchange(&TKK, 0), httpRequest.response) }; } else return { false, FormatString(L"%s (code=%u)", TRANSLATION_ERROR, httpRequest.errorCode) }; } diff --git a/extensions/translatewrapper.cpp b/extensions/translatewrapper.cpp index 06acaaa..08f1de9 100644 --- a/extensions/translatewrapper.cpp +++ b/extensions/translatewrapper.cpp @@ -5,7 +5,6 @@ #include #include #include -#include extern const char* NATIVE_LANGUAGE; extern const char* TRANSLATE_TO; @@ -19,7 +18,7 @@ const char* LANGUAGE = u8"Language"; const std::string TRANSLATION_CACHE_FILE = FormatString("%s Cache.txt", TRANSLATION_PROVIDER); QFormLayout* display; -QSettings* settings; +QSettings settings = openSettings(); Synchronized translateTo = L"en"; Synchronized> translationCache; @@ -40,21 +39,20 @@ public: Window() : QDialog(nullptr, Qt::WindowMinMaxButtonsHint) { - display = &layout; - ::settings = &settings; - - languageBox.addItems(languages); + display = new QFormLayout(this); settings.beginGroup(TRANSLATION_PROVIDER); - int language = -1; - if (settings.contains(LANGUAGE)) language = languageBox.findText(settings.value(LANGUAGE).toString(), Qt::MatchEndsWith); - if (language < 0) language = languageBox.findText(NATIVE_LANGUAGE, Qt::MatchStartsWith); - if (language < 0) language = languageBox.findText("English", Qt::MatchStartsWith); - languageBox.setCurrentIndex(language); - saveLanguage(languageBox.currentText()); - connect(&languageBox, &QComboBox::currentTextChanged, this, &Window::saveLanguage); - layout.addRow(TRANSLATE_TO, &languageBox); + auto languageBox = new QComboBox(this); + languageBox->addItems(languages); + int language = -1; + if (settings.contains(LANGUAGE)) language = languageBox->findText(settings.value(LANGUAGE).toString(), Qt::MatchEndsWith); + if (language < 0) language = languageBox->findText(NATIVE_LANGUAGE, Qt::MatchStartsWith); + if (language < 0) language = languageBox->findText("English", Qt::MatchStartsWith); + languageBox->setCurrentIndex(language); + saveLanguage(languageBox->currentText()); + connect(languageBox, &QComboBox::currentTextChanged, this, &Window::saveLanguage); + display->addRow(TRANSLATE_TO, languageBox); setWindowTitle(TRANSLATION_PROVIDER); QMetaObject::invokeMethod(this, &QWidget::show, Qt::QueuedConnection); @@ -80,10 +78,6 @@ private: { settings.setValue(LANGUAGE, S(translateTo->assign(S(language.split(": ")[1])))); } - - QFormLayout layout{ this }; - QComboBox languageBox{ this }; - QSettings settings{ openSettings(this) }; } window; bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)