Write trace to file for debugging
This commit is contained in:
		
							parent
							
								
									58f6283fe3
								
							
						
					
					
						commit
						013370e51e
					
				@ -11,6 +11,8 @@
 | 
			
		||||
#include "updates.h"
 | 
			
		||||
 | 
			
		||||
#include <QApplication>
 | 
			
		||||
#include <QDesktopServices>
 | 
			
		||||
#include <QMessageBox>
 | 
			
		||||
#include <QNetworkProxy>
 | 
			
		||||
#include <QThread>
 | 
			
		||||
 | 
			
		||||
@ -66,12 +68,14 @@ Manager::~Manager()
 | 
			
		||||
    settings_->lastUpdateCheck = updateAutoChecker_->lastCheckDate();
 | 
			
		||||
    settings_->saveLastUpdateCheck();
 | 
			
		||||
  }
 | 
			
		||||
  setupTrace(false);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Manager::updateSettings()
 | 
			
		||||
{
 | 
			
		||||
  LTRACE() << "updateSettings";
 | 
			
		||||
  SOFT_ASSERT(settings_, return );
 | 
			
		||||
  setupTrace(settings_->writeTrace);
 | 
			
		||||
  setupProxy(*settings_);
 | 
			
		||||
  setupUpdates(*settings_);
 | 
			
		||||
 | 
			
		||||
@ -127,6 +131,40 @@ void Manager::setupUpdates(const Settings &settings)
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Manager::setupTrace(bool isOn)
 | 
			
		||||
{
 | 
			
		||||
  const auto oldFile = debug::traceFileName();
 | 
			
		||||
 | 
			
		||||
  if (!isOn) {
 | 
			
		||||
    debug::setTraceFileName({});
 | 
			
		||||
    debug::isTrace = false;
 | 
			
		||||
 | 
			
		||||
    if (!oldFile.isEmpty())
 | 
			
		||||
      QDesktopServices::openUrl(QUrl::fromLocalFile(oldFile));
 | 
			
		||||
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (!oldFile.isEmpty())
 | 
			
		||||
    return;
 | 
			
		||||
 | 
			
		||||
  const auto traceFile =
 | 
			
		||||
      QStandardPaths::writableLocation(QStandardPaths::TempLocation) +
 | 
			
		||||
      QLatin1String("/multidir-") +
 | 
			
		||||
      QDateTime::currentDateTime().toString("yyyy-MM-dd-hh-mm-ss");
 | 
			
		||||
 | 
			
		||||
  if (!debug::setTraceFileName(traceFile)) {
 | 
			
		||||
    QMessageBox::warning(
 | 
			
		||||
        nullptr, {},
 | 
			
		||||
        QObject::tr("Failed to setup log to file: %1").arg(traceFile));
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  debug::isTrace = true;
 | 
			
		||||
  QMessageBox::information(
 | 
			
		||||
      nullptr, {}, QObject::tr("Started logging to file: %1").arg(traceFile));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Manager::finishTask(const TaskPtr &task)
 | 
			
		||||
{
 | 
			
		||||
  SOFT_ASSERT(task, return );
 | 
			
		||||
 | 
			
		||||
@ -31,6 +31,7 @@ private:
 | 
			
		||||
  void updateSettings();
 | 
			
		||||
  void setupProxy(const Settings &settings);
 | 
			
		||||
  void setupUpdates(const Settings &settings);
 | 
			
		||||
  void setupTrace(bool isOn);
 | 
			
		||||
  void finishTask(const TaskPtr &task);
 | 
			
		||||
 | 
			
		||||
  std::unique_ptr<Settings> settings_;
 | 
			
		||||
 | 
			
		||||
@ -1,6 +1,72 @@
 | 
			
		||||
#include "debug.h"
 | 
			
		||||
 | 
			
		||||
#include <QDateTime>
 | 
			
		||||
#include <QFileInfo>
 | 
			
		||||
#include <QMessageBox>
 | 
			
		||||
#include <QMutex>
 | 
			
		||||
#include <QThread>
 | 
			
		||||
 | 
			
		||||
namespace
 | 
			
		||||
{
 | 
			
		||||
QtMessageHandler original = nullptr;
 | 
			
		||||
QMutex mutex;
 | 
			
		||||
QFile file;
 | 
			
		||||
QTextStream stream;
 | 
			
		||||
 | 
			
		||||
void handler(QtMsgType type, const QMessageLogContext &context,
 | 
			
		||||
             const QString &msg)
 | 
			
		||||
{
 | 
			
		||||
  const auto typeName = QMap<QtMsgType, QByteArray>{{QtDebugMsg, " DEBUG "},
 | 
			
		||||
                                                    {QtInfoMsg, " INFO "},
 | 
			
		||||
                                                    {QtWarningMsg, " WARN "},
 | 
			
		||||
                                                    {QtCriticalMsg, " CRIT "},
 | 
			
		||||
                                                    {QtFatalMsg, " FATAL "}}
 | 
			
		||||
                            .value(type);
 | 
			
		||||
 | 
			
		||||
  const auto message =
 | 
			
		||||
      QDateTime::currentDateTime().toString(Qt::ISODate).toUtf8() + ' ' +
 | 
			
		||||
      QByteArray::number(qintptr(QThread::currentThreadId())) + ' ' +
 | 
			
		||||
      QFileInfo(context.file).fileName().toUtf8() + ':' +
 | 
			
		||||
      QByteArray::number(context.line) + typeName + msg.toUtf8() + '\n';
 | 
			
		||||
 | 
			
		||||
  SOFT_ASSERT(original, return );
 | 
			
		||||
  original(type, context, msg);
 | 
			
		||||
 | 
			
		||||
  QMutexLocker locker(&mutex);
 | 
			
		||||
  file.write(message);
 | 
			
		||||
}
 | 
			
		||||
}  // namespace
 | 
			
		||||
 | 
			
		||||
namespace debug
 | 
			
		||||
{
 | 
			
		||||
std::atomic_bool isTrace;
 | 
			
		||||
 | 
			
		||||
QString traceFileName()
 | 
			
		||||
{
 | 
			
		||||
  QMutexLocker locker(&mutex);
 | 
			
		||||
  return file.fileName();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool setTraceFileName(const QString &fileName)
 | 
			
		||||
{
 | 
			
		||||
  QMutexLocker locker(&mutex);
 | 
			
		||||
 | 
			
		||||
  original = nullptr;
 | 
			
		||||
  qInstallMessageHandler(nullptr);
 | 
			
		||||
 | 
			
		||||
  if (file.isOpen())
 | 
			
		||||
    file.close();
 | 
			
		||||
 | 
			
		||||
  if (fileName.isEmpty())
 | 
			
		||||
    return true;
 | 
			
		||||
 | 
			
		||||
  file.setFileName(fileName);
 | 
			
		||||
 | 
			
		||||
  if (!file.open(QFile::WriteOnly | QFile::Unbuffered))
 | 
			
		||||
    return false;
 | 
			
		||||
 | 
			
		||||
  original = qInstallMessageHandler(handler);
 | 
			
		||||
  return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}  // namespace debug
 | 
			
		||||
 | 
			
		||||
@ -2,6 +2,13 @@
 | 
			
		||||
 | 
			
		||||
#include <QDebug>
 | 
			
		||||
 | 
			
		||||
namespace debug
 | 
			
		||||
{
 | 
			
		||||
extern std::atomic_bool isTrace;
 | 
			
		||||
QString traceFileName();
 | 
			
		||||
bool setTraceFileName(const QString& fileName);
 | 
			
		||||
};  // namespace debug
 | 
			
		||||
 | 
			
		||||
#define SOFT_ASSERT(XXX, WORKAROUND)                                         \
 | 
			
		||||
  if (!(XXX)) {                                                              \
 | 
			
		||||
    qCritical() << "Soft assertion failed at" << __FILE__ << __LINE__ << ":" \
 | 
			
		||||
@ -23,11 +30,6 @@
 | 
			
		||||
    Q_ASSERT(XXX);                                                      \
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
namespace debug
 | 
			
		||||
{
 | 
			
		||||
extern std::atomic_bool isTrace;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#define LTRACE()      \
 | 
			
		||||
  if (debug::isTrace) \
 | 
			
		||||
  qDebug()
 | 
			
		||||
 | 
			
		||||
@ -40,7 +40,6 @@ const QString qs_doTranslation = "doTranslation";
 | 
			
		||||
const QString qs_ignoreSslErrors = "ignoreSslErrors";
 | 
			
		||||
const QString qs_translationLanguage = "translation_language";
 | 
			
		||||
const QString qs_translationTimeout = "translation_timeout";
 | 
			
		||||
const QString qs_debugMode = "translation_debug";
 | 
			
		||||
const QString qs_translators = "translators";
 | 
			
		||||
 | 
			
		||||
const QString qs_representationGroup = "Representation";
 | 
			
		||||
@ -124,6 +123,7 @@ void cleanupOutdated(QSettings& settings)
 | 
			
		||||
  settings.beginGroup(qs_translationGroup);
 | 
			
		||||
  settings.remove("source_language");
 | 
			
		||||
  settings.remove("forceRotateTranslators");
 | 
			
		||||
  settings.remove("translation_debug");
 | 
			
		||||
  settings.endGroup();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -181,7 +181,6 @@ void Settings::save() const
 | 
			
		||||
 | 
			
		||||
  settings.setValue(qs_doTranslation, doTranslation);
 | 
			
		||||
  settings.setValue(qs_ignoreSslErrors, ignoreSslErrors);
 | 
			
		||||
  settings.setValue(qs_debugMode, debugMode);
 | 
			
		||||
  settings.setValue(qs_translationLanguage, targetLanguage);
 | 
			
		||||
  settings.setValue(qs_translationTimeout, int(translationTimeout.count()));
 | 
			
		||||
  settings.setValue(qs_translators, translators);
 | 
			
		||||
@ -272,7 +271,6 @@ void Settings::load()
 | 
			
		||||
  doTranslation = settings.value(qs_doTranslation, doTranslation).toBool();
 | 
			
		||||
  ignoreSslErrors =
 | 
			
		||||
      settings.value(qs_ignoreSslErrors, ignoreSslErrors).toBool();
 | 
			
		||||
  debugMode = settings.value(qs_debugMode, debugMode).toBool();
 | 
			
		||||
  targetLanguage =
 | 
			
		||||
      settings.value(qs_translationLanguage, targetLanguage).toString();
 | 
			
		||||
  translationTimeout = std::chrono::seconds(
 | 
			
		||||
 | 
			
		||||
@ -51,7 +51,7 @@ public:
 | 
			
		||||
  Substitutions userSubstitutions;
 | 
			
		||||
  bool useUserSubstitutions{true};
 | 
			
		||||
 | 
			
		||||
  bool debugMode{false};
 | 
			
		||||
  bool writeTrace{false};
 | 
			
		||||
 | 
			
		||||
  QString tessdataPath;
 | 
			
		||||
  QString sourceLanguage{"eng"};
 | 
			
		||||
 | 
			
		||||
@ -20,8 +20,6 @@ SettingsEditor::SettingsEditor(Manager &manager, update::Loader &updater)
 | 
			
		||||
{
 | 
			
		||||
  ui->setupUi(this);
 | 
			
		||||
 | 
			
		||||
  ui->translatorDebugCheck->hide();
 | 
			
		||||
 | 
			
		||||
  connect(ui->buttonBox, &QDialogButtonBox::clicked,  //
 | 
			
		||||
          this, &SettingsEditor::handleButtonBoxClicked);
 | 
			
		||||
 | 
			
		||||
@ -145,6 +143,7 @@ Settings SettingsEditor::settings() const
 | 
			
		||||
      ui->captureLockedEdit->keySequence().toString();
 | 
			
		||||
 | 
			
		||||
  settings.showMessageOnStart = ui->showOnStart->isChecked();
 | 
			
		||||
  settings.writeTrace = ui->writeTrace->isChecked();
 | 
			
		||||
 | 
			
		||||
  settings.proxyType = ProxyType(ui->proxyTypeCombo->currentIndex());
 | 
			
		||||
  settings.proxyHostName = ui->proxyHostEdit->text();
 | 
			
		||||
@ -161,7 +160,6 @@ Settings SettingsEditor::settings() const
 | 
			
		||||
 | 
			
		||||
  settings.doTranslation = ui->doTranslationCheck->isChecked();
 | 
			
		||||
  settings.ignoreSslErrors = ui->ignoreSslCheck->isChecked();
 | 
			
		||||
  settings.debugMode = ui->translatorDebugCheck->isChecked();
 | 
			
		||||
  settings.translationTimeout =
 | 
			
		||||
      std::chrono::seconds(ui->translateTimeoutSpin->value());
 | 
			
		||||
  settings.targetLanguage =
 | 
			
		||||
@ -206,6 +204,7 @@ void SettingsEditor::setSettings(const Settings &settings)
 | 
			
		||||
  ui->captureLockedEdit->setKeySequence(settings.captureLockedHotkey);
 | 
			
		||||
 | 
			
		||||
  ui->showOnStart->setChecked(settings.showMessageOnStart);
 | 
			
		||||
  ui->writeTrace->setChecked(settings.writeTrace);
 | 
			
		||||
 | 
			
		||||
  ui->proxyTypeCombo->setCurrentIndex(int(settings.proxyType));
 | 
			
		||||
  ui->proxyHostEdit->setText(settings.proxyHostName);
 | 
			
		||||
@ -223,7 +222,6 @@ void SettingsEditor::setSettings(const Settings &settings)
 | 
			
		||||
 | 
			
		||||
  ui->doTranslationCheck->setChecked(settings.doTranslation);
 | 
			
		||||
  ui->ignoreSslCheck->setChecked(settings.ignoreSslErrors);
 | 
			
		||||
  ui->translatorDebugCheck->setChecked(settings.debugMode);
 | 
			
		||||
  ui->translateTimeoutSpin->setValue(settings.translationTimeout.count());
 | 
			
		||||
  ui->translatorsPath->setText(settings.translatorsDir);
 | 
			
		||||
  enabledTranslators_ = settings.translators;
 | 
			
		||||
 | 
			
		||||
@ -216,6 +216,13 @@
 | 
			
		||||
         </property>
 | 
			
		||||
        </spacer>
 | 
			
		||||
       </item>
 | 
			
		||||
       <item row="3" column="1">
 | 
			
		||||
        <widget class="QCheckBox" name="writeTrace">
 | 
			
		||||
         <property name="text">
 | 
			
		||||
          <string>Write trace file</string>
 | 
			
		||||
         </property>
 | 
			
		||||
        </widget>
 | 
			
		||||
       </item>
 | 
			
		||||
      </layout>
 | 
			
		||||
     </widget>
 | 
			
		||||
     <widget class="QWidget" name="pageRecognize">
 | 
			
		||||
@ -307,68 +314,28 @@
 | 
			
		||||
     </widget>
 | 
			
		||||
     <widget class="QWidget" name="pageTranslate">
 | 
			
		||||
      <layout class="QGridLayout" name="gridLayout_9">
 | 
			
		||||
       <item row="3" column="2">
 | 
			
		||||
        <widget class="QSpinBox" name="translateTimeoutSpin">
 | 
			
		||||
         <property name="suffix">
 | 
			
		||||
          <string> secs</string>
 | 
			
		||||
       <item row="3" column="0">
 | 
			
		||||
        <widget class="QLabel" name="label_5">
 | 
			
		||||
         <property name="text">
 | 
			
		||||
          <string>Translators path:</string>
 | 
			
		||||
         </property>
 | 
			
		||||
        </widget>
 | 
			
		||||
       </item>
 | 
			
		||||
       <item row="1" column="0" colspan="2">
 | 
			
		||||
       <item row="0" column="0" colspan="2">
 | 
			
		||||
        <widget class="QCheckBox" name="doTranslationCheck">
 | 
			
		||||
         <property name="text">
 | 
			
		||||
          <string>Translate text</string>
 | 
			
		||||
         </property>
 | 
			
		||||
        </widget>
 | 
			
		||||
       </item>
 | 
			
		||||
       <item row="2" column="0" colspan="2">
 | 
			
		||||
       <item row="1" column="0" colspan="2">
 | 
			
		||||
        <widget class="QCheckBox" name="ignoreSslCheck">
 | 
			
		||||
         <property name="text">
 | 
			
		||||
          <string>Ignore SSL errors</string>
 | 
			
		||||
         </property>
 | 
			
		||||
        </widget>
 | 
			
		||||
       </item>
 | 
			
		||||
       <item row="3" column="0" colspan="2">
 | 
			
		||||
        <widget class="QLabel" name="label_9">
 | 
			
		||||
         <property name="text">
 | 
			
		||||
          <string>Single translator timeout:</string>
 | 
			
		||||
         </property>
 | 
			
		||||
        </widget>
 | 
			
		||||
       </item>
 | 
			
		||||
       <item row="0" column="0">
 | 
			
		||||
        <widget class="QCheckBox" name="translatorDebugCheck">
 | 
			
		||||
         <property name="text">
 | 
			
		||||
          <string>Debug mode</string>
 | 
			
		||||
         </property>
 | 
			
		||||
        </widget>
 | 
			
		||||
       </item>
 | 
			
		||||
       <item row="7" column="0" colspan="3">
 | 
			
		||||
        <widget class="QListWidget" name="translatorList">
 | 
			
		||||
         <property name="editTriggers">
 | 
			
		||||
          <set>QAbstractItemView::NoEditTriggers</set>
 | 
			
		||||
         </property>
 | 
			
		||||
         <property name="dragDropMode">
 | 
			
		||||
          <enum>QAbstractItemView::InternalMove</enum>
 | 
			
		||||
         </property>
 | 
			
		||||
         <property name="defaultDropAction">
 | 
			
		||||
          <enum>Qt::MoveAction</enum>
 | 
			
		||||
         </property>
 | 
			
		||||
        </widget>
 | 
			
		||||
       </item>
 | 
			
		||||
       <item row="5" column="0">
 | 
			
		||||
        <widget class="QLabel" name="label_6">
 | 
			
		||||
         <property name="text">
 | 
			
		||||
          <string>Language:</string>
 | 
			
		||||
         </property>
 | 
			
		||||
         <property name="buddy">
 | 
			
		||||
          <cstring>translateLangCombo</cstring>
 | 
			
		||||
         </property>
 | 
			
		||||
        </widget>
 | 
			
		||||
       </item>
 | 
			
		||||
       <item row="5" column="1" colspan="2">
 | 
			
		||||
        <widget class="QComboBox" name="translateLangCombo"/>
 | 
			
		||||
       </item>
 | 
			
		||||
       <item row="6" column="0" colspan="3">
 | 
			
		||||
       <item row="5" column="0" colspan="3">
 | 
			
		||||
        <widget class="QLabel" name="label_10">
 | 
			
		||||
         <property name="text">
 | 
			
		||||
          <string>Translators</string>
 | 
			
		||||
@ -378,20 +345,53 @@
 | 
			
		||||
         </property>
 | 
			
		||||
        </widget>
 | 
			
		||||
       </item>
 | 
			
		||||
       <item row="4" column="0">
 | 
			
		||||
        <widget class="QLabel" name="label_5">
 | 
			
		||||
         <property name="text">
 | 
			
		||||
          <string>Translators path:</string>
 | 
			
		||||
       <item row="2" column="2">
 | 
			
		||||
        <widget class="QSpinBox" name="translateTimeoutSpin">
 | 
			
		||||
         <property name="suffix">
 | 
			
		||||
          <string> secs</string>
 | 
			
		||||
         </property>
 | 
			
		||||
        </widget>
 | 
			
		||||
       </item>
 | 
			
		||||
       <item row="4" column="1" colspan="2">
 | 
			
		||||
        <widget class="QComboBox" name="translateLangCombo"/>
 | 
			
		||||
       </item>
 | 
			
		||||
       <item row="2" column="0" colspan="2">
 | 
			
		||||
        <widget class="QLabel" name="label_9">
 | 
			
		||||
         <property name="text">
 | 
			
		||||
          <string>Single translator timeout:</string>
 | 
			
		||||
         </property>
 | 
			
		||||
        </widget>
 | 
			
		||||
       </item>
 | 
			
		||||
       <item row="3" column="1" colspan="2">
 | 
			
		||||
        <widget class="QLineEdit" name="translatorsPath">
 | 
			
		||||
         <property name="readOnly">
 | 
			
		||||
          <bool>true</bool>
 | 
			
		||||
         </property>
 | 
			
		||||
        </widget>
 | 
			
		||||
       </item>
 | 
			
		||||
       <item row="4" column="0">
 | 
			
		||||
        <widget class="QLabel" name="label_6">
 | 
			
		||||
         <property name="text">
 | 
			
		||||
          <string>Language:</string>
 | 
			
		||||
         </property>
 | 
			
		||||
         <property name="buddy">
 | 
			
		||||
          <cstring>translateLangCombo</cstring>
 | 
			
		||||
         </property>
 | 
			
		||||
        </widget>
 | 
			
		||||
       </item>
 | 
			
		||||
       <item row="6" column="0" colspan="3">
 | 
			
		||||
        <widget class="QListWidget" name="translatorList">
 | 
			
		||||
         <property name="editTriggers">
 | 
			
		||||
          <set>QAbstractItemView::NoEditTriggers</set>
 | 
			
		||||
         </property>
 | 
			
		||||
         <property name="dragDropMode">
 | 
			
		||||
          <enum>QAbstractItemView::InternalMove</enum>
 | 
			
		||||
         </property>
 | 
			
		||||
         <property name="defaultDropAction">
 | 
			
		||||
          <enum>Qt::MoveAction</enum>
 | 
			
		||||
         </property>
 | 
			
		||||
        </widget>
 | 
			
		||||
       </item>
 | 
			
		||||
      </layout>
 | 
			
		||||
     </widget>
 | 
			
		||||
     <widget class="QWidget" name="pageRepresent">
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user