From 5db0178a1520883d07d8c95bfa4063db0419858a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Philipp=20H=C3=B6rist?= <philipp@hoerist.com>
Date: Sun, 23 Sep 2018 14:25:02 +0200
Subject: [PATCH] Windows: Add debug logging switch in preferences

If debug logging is enabled verbose output is redirected to a file
in the user dir
---
 gajim/application.py                 | 19 ++++++++++++++++++-
 gajim/common/app.py                  | 16 ++++++++++++++++
 gajim/common/configpaths.py          |  1 +
 gajim/data/gui/preferences_window.ui | 19 +++++++++++++++++++
 gajim/gtk/preferences.py             |  9 +++++++++
 5 files changed, 63 insertions(+), 1 deletion(-)

diff --git a/gajim/application.py b/gajim/application.py
index 0ea34f0255..2748528470 100644
--- a/gajim/application.py
+++ b/gajim/application.py
@@ -34,6 +34,8 @@
 # along with Gajim. If not, see <http://www.gnu.org/licenses/>.
 
 import sys
+from datetime import datetime
+from pathlib import Path
 from urllib.parse import unquote
 
 from gi.repository import GLib, Gio, Gtk
@@ -291,7 +293,14 @@ class GajimApplication(Gtk.Application):
             configpaths.set_config_root(path)
 
         configpaths.init()
-        logging_helpers.init()
+
+        if app.get_win_debug_mode():
+            # Redirect has to happen before logging init
+            self._redirect_output()
+            logging_helpers.init()
+            logging_helpers.set_verbose()
+        else:
+            logging_helpers.init()
 
         if options.contains('quiet'):
             logging_helpers.set_quiet()
@@ -318,6 +327,14 @@ class GajimApplication(Gtk.Application):
         warnings.showwarning = warn_with_traceback
         warnings.filterwarnings(action="always")
 
+    @staticmethod
+    def _redirect_output():
+        debug_folder = Path(configpaths.get('DEBUG'))
+        date = datetime.today().strftime('%Y-%m-%d')
+        filename = '%s-debug.log' % date
+        fd = open(debug_folder / filename, 'a')
+        sys.stderr = sys.stdout = fd
+
     def add_actions(self):
         ''' Build Application Actions '''
         from gajim import app_actions
diff --git a/gajim/common/app.py b/gajim/common/app.py
index b8dd434a18..5ca8590464 100644
--- a/gajim/common/app.py
+++ b/gajim/common/app.py
@@ -646,3 +646,19 @@ def get_app_window(cls, account=None):
 def load_css_config():
     global css_config
     css_config = CSSConfig()
+
+def set_win_debug_mode(enable: bool) -> None:
+    debug_folder = Path(configpaths.get('DEBUG'))
+    debug_enabled = debug_folder / 'debug-enabled'
+    if enable:
+        debug_enabled.touch()
+    else:
+        if debug_enabled.exists():
+            debug_enabled.unlink()
+
+def get_win_debug_mode() -> bool:
+    if sys.platform != 'win32':
+        return False
+    debug_folder = Path(configpaths.get('DEBUG'))
+    debug_enabled = debug_folder / 'debug-enabled'
+    return debug_enabled.exists()
diff --git a/gajim/common/configpaths.py b/gajim/common/configpaths.py
index 020e852444..0649d6f2a3 100644
--- a/gajim/common/configpaths.py
+++ b/gajim/common/configpaths.py
@@ -195,6 +195,7 @@ class ConfigPaths:
             # Data paths
             ('SECRETS_FILE', 'secrets', PathLocation.DATA, PathType.FILE),
             ('MY_PEER_CERTS', 'certs', PathLocation.DATA, PathType.FOLDER),
+            ('DEBUG', 'debug', PathLocation.DATA, PathType.FOLDER),
 
             # Config paths
             ('CONFIG_FILE', 'config', PathLocation.CONFIG, PathType.FILE),
diff --git a/gajim/data/gui/preferences_window.ui b/gajim/data/gui/preferences_window.ui
index 67814e62be..0ba4e66faa 100644
--- a/gajim/data/gui/preferences_window.ui
+++ b/gajim/data/gui/preferences_window.ui
@@ -1509,6 +1509,7 @@ $T will be replaced by auto-not-available timeout</property>
         <property name="orientation">vertical</property>
         <property name="spacing">12</property>
         <child>
+          <placeholder/>
         </child>
         <child>
           <object class="GtkFrame" id="frame3">
@@ -1640,6 +1641,7 @@ $T will be replaced by auto-not-available timeout</property>
           </packing>
         </child>
         <child>
+          <placeholder/>
         </child>
       </object>
       <packing>
@@ -2384,6 +2386,23 @@ to discover one from server.</property>
                     <property name="position">0</property>
                   </packing>
                 </child>
+                <child>
+                  <object class="GtkCheckButton" id="enable_logging">
+                    <property name="label" translatable="yes">Enable debug logging</property>
+                    <property name="can_focus">True</property>
+                    <property name="receives_default">False</property>
+                    <property name="no_show_all">True</property>
+                    <property name="halign">start</property>
+                    <property name="use_underline">True</property>
+                    <property name="draw_indicator">True</property>
+                    <signal name="toggled" handler="on_enable_logging_toggled" swapped="no"/>
+                  </object>
+                  <packing>
+                    <property name="expand">False</property>
+                    <property name="fill">True</property>
+                    <property name="position">1</property>
+                  </packing>
+                </child>
               </object>
             </child>
             <child type="label">
diff --git a/gajim/gtk/preferences.py b/gajim/gtk/preferences.py
index d2407ad2fe..b1988a96f5 100644
--- a/gajim/gtk/preferences.py
+++ b/gajim/gtk/preferences.py
@@ -13,6 +13,7 @@
 # along with Gajim. If not, see <http://www.gnu.org/licenses/>.
 
 import os
+import sys
 
 from gi.repository import Gtk
 from gi.repository import Gdk
@@ -449,6 +450,11 @@ class Preferences(Gtk.ApplicationWindow):
         else:
             w.set_active(st)
 
+        if sys.platform == 'win32':
+            w = self.xml.get_object('enable_logging')
+            w.set_active(app.get_win_debug_mode())
+            w.show()
+
         self.xml.connect_signals(self)
         self.connect('key-press-event', self._on_key_press)
 
@@ -998,3 +1004,6 @@ class Preferences(Gtk.ApplicationWindow):
         else:
             app.interface.instances['advanced_config'] = \
                 AdvancedConfigurationWindow(self)
+
+    def on_enable_logging_toggled(self, widget):
+        app.set_win_debug_mode(widget.get_active())
-- 
GitLab