Commit a68337a4 authored by Philipp Hörist's avatar Philipp Hörist

Logging: Move logging setup methods to logging_helpers

Also check for GAJIM_DEBUG
parent ae2d2f10
Pipeline #7321 passed with stages
in 9 minutes and 53 seconds
......@@ -34,9 +34,7 @@
# along with Gajim. If not, see <http://www.gnu.org/licenses/>.
import os
import time
import sys
from datetime import datetime
from urllib.parse import unquote
from nbxmpp.namespaces import Namespace
......@@ -358,14 +356,7 @@ def _handle_local_options(self,
if options.contains('gdebug'):
os.environ['G_MESSAGES_DEBUG'] = 'all'
if app.get_debug_mode():
# Redirect has to happen before logging init
self._cleanup_debug_logs()
self._redirect_output()
logging_helpers.init()
logging_helpers.set_verbose()
else:
logging_helpers.init()
logging_helpers.init()
if options.contains('quiet'):
logging_helpers.set_quiet()
......@@ -393,24 +384,6 @@ def warn_with_traceback(message, category, filename, lineno,
warnings.showwarning = warn_with_traceback
warnings.filterwarnings(action="always")
@staticmethod
def _redirect_output():
debug_folder = configpaths.get('DEBUG')
date = datetime.today().strftime('%d%m%Y-%H%M%S')
filename = '%s-debug.log' % date
fd = open(debug_folder / filename, 'a')
sys.stderr = sys.stdout = fd
@staticmethod
def _cleanup_debug_logs():
debug_folder = configpaths.get('DEBUG')
debug_files = list(debug_folder.glob('*-debug.log*'))
now = time.time()
for file in debug_files:
# Delete everything older than 3 days
if file.stat().st_ctime < now - 259200:
file.unlink()
def add_actions(self):
''' Build Application Actions '''
from gajim import app_actions
......
......@@ -17,7 +17,11 @@
import logging
import os
import sys
import time
from datetime import datetime
from gajim.common import app
from gajim.common import configpaths
from gajim.common.i18n import _
def parseLogLevel(arg):
......@@ -136,6 +140,11 @@ def init():
"""
Iinitialize the logging system
"""
if app.get_debug_mode():
_cleanup_debug_logs()
_redirect_output()
use_color = False
if os.name != 'nt':
use_color = sys.stderr.isatty()
......@@ -164,6 +173,12 @@ def init():
root_log.addHandler(consoleloghandler)
root_log.propagate = False
# GAJIM_DEBUG is set only on Windows when using Gajim-Debug.exe
# Gajim-Debug.exe shows a command line prompt and we want to redirect
# log output to it
if app.get_debug_mode() or os.environ.get('GAJIM_DEBUG', False):
set_verbose()
def set_loglevels(loglevels_string):
parseAndSetLogLevels(loglevels_string)
......@@ -176,6 +191,25 @@ def set_quiet():
parseAndSetLogLevels('.nbxmpp=CRITICAL')
def _redirect_output():
debug_folder = configpaths.get('DEBUG')
date = datetime.today().strftime('%d%m%Y-%H%M%S')
filename = '%s-debug.log' % date
fd = open(debug_folder / filename, 'a')
sys.stderr = sys.stdout = fd
def _cleanup_debug_logs():
debug_folder = configpaths.get('DEBUG')
debug_files = list(debug_folder.glob('*-debug.log*'))
now = time.time()
for file in debug_files:
# Delete everything older than 3 days
if file.stat().st_ctime < now - 259200:
file.unlink()
# tests
if __name__ == '__main__':
init()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment