Add commandline option for logging on windows

Redirect all output to one log file or if -w is
provided do not redirect output
This commit is contained in:
Philipp Hörist 2016-12-30 18:22:47 +01:00
parent 815ecdcf47
commit 2676d7ea94
2 changed files with 34 additions and 27 deletions

View File

@ -50,6 +50,10 @@ 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 '
@ -119,26 +123,6 @@ class GajimApplication(Gtk.Application):
# libintl.bindtextdomain('gajim_plugins', plugins_locale_dir)
# libintl.bind_textdomain_codeset('gajim_plugins', 'UTF-8')
class MyStderr(object):
_file = None
_error = None
def write(self, text):
fname = os.path.join(common.configpaths.gajimpaths.cache_root,
os.path.split(sys.executable)[1]+'.log')
if self._file is None and self._error is None:
try:
self._file = open(fname, '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()
sys.stderr = MyStderr()
# PyGTK2.10+ only throws a warning
warnings.filterwarnings('error', module='Gtk')
try:

View File

@ -36,25 +36,48 @@
##
import sys
import os
if '--version' in sys.argv or '-V' in sys.argv:
from common.defs import version
print(version)
sys.exit(0)
import os
import warnings
WINDEV = False
if '--windev' in sys.argv or '-w' in sys.argv:
WINDEV = True
if os.name == 'nt':
if os.name == 'nt' and not WINDEV:
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')
fout = open(log_file, 'a')
sys.stdout = fout
sys.stderr = fout
warnings.filterwarnings(action='ignore')
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
# Test here for all required versions so we dont have to
# test multiple times in every module. nbxmpp also needs GLib.