From f9269d5ce076944ceb2c16d5d488d86f2a43adba Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Philipp=20H=C3=B6rist?= <forenjunkie@chello.at>
Date: Sat, 11 Feb 2017 20:57:03 +0100
Subject: [PATCH] Save log file into config dir when frozen

---
 src/application.py | 40 ++++++++++++++++++++++++++++++++++++----
 src/gajim.py       | 39 ---------------------------------------
 2 files changed, 36 insertions(+), 43 deletions(-)

diff --git a/src/application.py b/src/application.py
index 98bd38f26..1d46b2e1b 100644
--- a/src/application.py
+++ b/src/application.py
@@ -59,10 +59,6 @@ class GajimApplication(Gtk.Application):
                              GLib.OptionArg.NONE,
                              _('Print XML stanzas and other debug '
                                'information'))
-        self.add_main_option('windev', ord('w'), GLib.OptionFlags.NONE,
-                             GLib.OptionArg.NONE,
-                             _('Print stdout/stderr to the console '
-                               'on Windows'))
         self.add_main_option('profile', ord('p'), GLib.OptionFlags.NONE,
                              GLib.OptionArg.STRING,
                              _('Use defined profile in configuration '
@@ -93,6 +89,10 @@ class GajimApplication(Gtk.Application):
         import common.configpaths
         common.configpaths.gajimpaths.init(
             self.config_path, self.profile, self.profile_separation)
+
+        if hasattr(sys, 'frozen'):
+            self.frozen_logging(common.configpaths.gajimpaths.config_root)
+
         from common import gajim
         from common import check_paths
         from common import exceptions
@@ -243,3 +243,35 @@ class GajimApplication(Gtk.Application):
             logging_helpers.set_loglevels(string)
         self.activate()
         return 0
+
+    def frozen_logging(self, path):
+        import warnings
+        if not os.path.exists(path):
+            os.mkdir(path, 0o700)
+
+        class MyStd(object):
+            _file = None
+            _error = None
+            log_file = os.path.join(path, 'gajim.log')
+
+            def write(self, text):
+                if self._file is None and self._error is None:
+                    try:
+                        self._file = open(self.log_file, 'a')
+                    except Exception as details:
+                        self._error = details
+                if self._file is not None:
+                    self._file.write(text)
+                    self._file.flush()
+
+            def flush(self):
+                if self._file is not None:
+                    self._file.flush()
+
+            def isatty(self):
+                return False
+
+        outerr = MyStd()
+        sys.stdout = outerr
+        sys.stderr = outerr
+        warnings.filterwarnings(action='ignore')
diff --git a/src/gajim.py b/src/gajim.py
index 5560a9207..4f2002a25 100644
--- a/src/gajim.py
+++ b/src/gajim.py
@@ -36,51 +36,12 @@
 ##
 
 import sys
-import os
 
 if '--version' in sys.argv or '-V' in sys.argv:
     from common.defs import version
     print(version)
     sys.exit(0)
 
-WINDEV = False
-if '--windev' in sys.argv or '-w' in sys.argv:
-    WINDEV = True
-
-if os.name == 'nt' and not WINDEV:
-    import warnings
-    log_path = os.path.join(os.environ['APPDATA'], 'Gajim')
-    if not os.path.exists(log_path):
-        os.mkdir(log_path, 0o700)
-    log_file = os.path.join(log_path, 'gajim.log')
-
-    class MyStd(object):
-        _file = None
-        _error = None
-
-        def write(self, text):
-            if self._file is None and self._error is None:
-                try:
-                    self._file = open(log_file, 'a')
-                except Exception as details:
-                    self._error = details
-            if self._file is not None:
-                self._file.write(text)
-                self._file.flush()
-
-        def flush(self):
-            if self._file is not None:
-                self._file.flush()
-
-        def isatty(self):
-            return False
-
-    outerr = MyStd()
-    sys.stdout = outerr
-    sys.stderr = outerr
-    warnings.filterwarnings(action='ignore')
-
-
 # Test here for all required versions so we dont have to
 # test multiple times in every module. nbxmpp also needs GLib.
 import gi