From 341ac18f1ad6d238d551b114c53f15b9f29f7f1a Mon Sep 17 00:00:00 2001
From: pazos <paziusss@gmail.com>
Date: Tue, 8 May 2018 18:13:29 +0200
Subject: [PATCH] log qt/qml to easylogging, add --log-file cmdline option

---
 Logger.cpp                 | 49 ++++++++++++++++++++++++++++++++++++++
 Logger.h                   |  8 +++++++
 main.cpp                   | 23 +++++++++++-------
 monero-wallet-gui.pro      |  2 ++
 pages/Settings.qml         |  4 ++--
 src/libwalletqt/Wallet.cpp | 11 ---------
 src/libwalletqt/Wallet.h   |  1 -
 7 files changed, 75 insertions(+), 23 deletions(-)
 create mode 100644 Logger.cpp
 create mode 100644 Logger.h

diff --git a/Logger.cpp b/Logger.cpp
new file mode 100644
index 00000000..660bafcc
--- /dev/null
+++ b/Logger.cpp
@@ -0,0 +1,49 @@
+#include <QCoreApplication>
+#include <QStandardPaths>
+#include <QFileInfo>
+#include <QString>
+
+#include "Logger.h"
+#include "wallet/api/wallet2_api.h"
+
+// default log path by OS (should be writable)
+static const QString default_name = "monero-wallet-gui.log";
+#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
+    static const QString osPath = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).at(0);
+#elif defined(Q_OS_WIN)
+    static const QString osPath = QCoreApplication::applicationDirPath();
+#elif defined(Q_OS_MAC)
+    static const QString osPath = QStandardPaths::standardLocations(QStandardPaths::HomeLocation).at(0) + "/Library/Logs";
+#else // linux + bsd
+    static const QString osPath = QStandardPaths::standardLocations(QStandardPaths::HomeLocation).at(0);
+#endif
+
+
+// return the absolute path of the logfile
+const QString getLogPath(const QString logPath)
+{
+    const QFileInfo fi(logPath);
+
+    if(!logPath.isEmpty() && !fi.isDir())
+        return fi.absoluteFilePath();
+    else
+        return osPath + "/" + default_name;
+}
+
+
+// custom messageHandler that foward all messages to easylogging
+void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &message)
+{
+    (void) context; // context isn't used in release builds
+    const std::string cat = "frontend"; // category displayed in the log
+    const std::string msg = message.toStdString();
+    switch(type)
+    {
+        case QtDebugMsg: Monero::Wallet::debug(cat, msg); break;
+        case QtInfoMsg: Monero::Wallet::info(cat, msg); break;
+        case QtWarningMsg: Monero::Wallet::warning(cat, msg); break;
+        case QtCriticalMsg: Monero::Wallet::error(cat, msg); break;
+        case QtFatalMsg: Monero::Wallet::error(cat, msg); break;
+    }
+}
+
diff --git a/Logger.h b/Logger.h
new file mode 100644
index 00000000..8442816b
--- /dev/null
+++ b/Logger.h
@@ -0,0 +1,8 @@
+#ifndef LOGGER_H
+#define LOGGER_H
+
+const QString getLogPath(const QString logPath);
+void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &message);
+
+#endif // LOGGER_H
+
diff --git a/main.cpp b/main.cpp
index 14097164..ab232156 100644
--- a/main.cpp
+++ b/main.cpp
@@ -54,6 +54,7 @@
 #include "Subaddress.h"
 #include "model/SubaddressModel.h"
 #include "wallet/api/wallet2_api.h"
+#include "Logger.h"
 #include "MainApp.h"
 
 // IOS exclusions
@@ -70,12 +71,6 @@ bool isAndroid = false;
 bool isWindows = false;
 bool isDesktop = false;
 
-void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
-{
-    // Send all message types to logger
-    Monero::Wallet::debug("qml", msg.toStdString());
-}
-
 int main(int argc, char *argv[])
 {
     // platform dependant settings
@@ -117,16 +112,24 @@ int main(int argc, char *argv[])
     app.installEventFilter(eventFilter);
 
     QCommandLineParser parser;
+    QCommandLineOption logPathOption(QStringList() << "l" << "log-file",
+        QCoreApplication::translate("main", "Log to specified file"),
+        QCoreApplication::translate("main", "file"));
+    parser.addOption(logPathOption);
     parser.addHelpOption();
     parser.process(app);
 
     Monero::Utils::onStartup();
 
     // Log settings
-    Monero::Wallet::init(argv[0], "monero-wallet-gui");
-//    qInstallMessageHandler(messageHandler);
+    const QString logPath = getLogPath(parser.value(logPathOption));
+    Monero::Wallet::init(argv[0], "monero-wallet-gui", logPath.toStdString().c_str());
+    qInstallMessageHandler(messageHandler);
 
-    qDebug() << "app startd";
+
+    // loglevel is configured in main.qml. Anything lower than
+    // qWarning is not shown here.
+    qWarning().noquote() << "app startd" << "(log: " + logPath + ")";
 
     // screen settings
     // Mobile is designed on 128dpi
@@ -217,6 +220,8 @@ int main(int argc, char *argv[])
 
     engine.rootContext()->setContextProperty("qtRuntimeVersion", qVersion());
 
+    engine.rootContext()->setContextProperty("walletLogPath", logPath);
+
 // Exclude daemon manager from IOS
 #ifndef Q_OS_IOS
     const QStringList arguments = QCoreApplication::arguments();
diff --git a/monero-wallet-gui.pro b/monero-wallet-gui.pro
index cd48a4b7..5470148f 100644
--- a/monero-wallet-gui.pro
+++ b/monero-wallet-gui.pro
@@ -45,6 +45,7 @@ HEADERS += \
     src/libwalletqt/Subaddress.h \
     src/zxcvbn-c/zxcvbn.h \
     src/libwalletqt/UnsignedTransaction.h \
+    Logger.h \
     MainApp.h
 
 SOURCES += main.cpp \
@@ -70,6 +71,7 @@ SOURCES += main.cpp \
     src/libwalletqt/Subaddress.cpp \
     src/zxcvbn-c/zxcvbn.c \
     src/libwalletqt/UnsignedTransaction.cpp \
+    Logger.cpp \
     MainApp.cpp
 
 CONFIG(DISABLE_PASS_STRENGTH_METER) {
diff --git a/pages/Settings.qml b/pages/Settings.qml
index 231c2d79..0f617d8c 100644
--- a/pages/Settings.qml
+++ b/pages/Settings.qml
@@ -730,13 +730,13 @@ Rectangle {
             TextBlock {
                 Layout.fillWidth: true
                 font.pixelSize: 14
-                text:  (!currentWallet) ? "" : qsTr("Wallet log path: ") + translationManager.emptyString
+                text: qsTr("Wallet log path: ") + translationManager.emptyString
             }
 
             TextBlock {
                 Layout.fillWidth: true
                 font.pixelSize: 14
-                text: currentWallet.walletLogPath + translationManager.emptyString
+                text: walletLogPath
             }
         }
     }
diff --git a/src/libwalletqt/Wallet.cpp b/src/libwalletqt/Wallet.cpp
index 9bc267d8..7643f379 100644
--- a/src/libwalletqt/Wallet.cpp
+++ b/src/libwalletqt/Wallet.cpp
@@ -722,17 +722,6 @@ QString Wallet::getDaemonLogPath() const
     return QString::fromStdString(m_walletImpl->getDefaultDataDir()) + "/bitmonero.log";
 }
 
-QString Wallet::getWalletLogPath() const
-{
-    const QString filename("monero-wallet-gui.log");
-
-#ifdef Q_OS_MACOS
-    return QStandardPaths::standardLocations(QStandardPaths::HomeLocation).at(0) + "/Library/Logs/" + filename;
-#else
-    return QCoreApplication::applicationDirPath() + "/" + filename;
-#endif
-}
-
 bool Wallet::blackballOutput(const QString &pubkey)
 {
     QList<QString> list;
diff --git a/src/libwalletqt/Wallet.h b/src/libwalletqt/Wallet.h
index f5b8a348..f37d556d 100644
--- a/src/libwalletqt/Wallet.h
+++ b/src/libwalletqt/Wallet.h
@@ -50,7 +50,6 @@ class Wallet : public QObject
     Q_PROPERTY(QString secretSpendKey READ getSecretSpendKey)
     Q_PROPERTY(QString publicSpendKey READ getPublicSpendKey)
     Q_PROPERTY(QString daemonLogPath READ getDaemonLogPath CONSTANT)
-    Q_PROPERTY(QString walletLogPath READ getWalletLogPath CONSTANT)
     Q_PROPERTY(quint64 walletCreationHeight READ getWalletCreationHeight WRITE setWalletCreationHeight NOTIFY walletCreationHeightChanged)
 
 public: