You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
3.2 KiB
Python
102 lines
3.2 KiB
Python
4 years ago
|
#!/usr/bin/python3
|
||
|
# -*- coding: utf-8 -*-
|
||
4 years ago
|
# key-mapper - GTK based GUI for device specific keyboard mappings
|
||
4 years ago
|
# Copyright (C) 2020 sezanzeb <proxima@hip70890b.de>
|
||
|
#
|
||
4 years ago
|
# This file is part of key-mapper.
|
||
4 years ago
|
#
|
||
4 years ago
|
# key-mapper is free software: you can redistribute it and/or modify
|
||
4 years ago
|
# it under the terms of the GNU General Public License as published by
|
||
|
# the Free Software Foundation, either version 3 of the License, or
|
||
|
# (at your option) any later version.
|
||
|
#
|
||
4 years ago
|
# key-mapper is distributed in the hope that it will be useful,
|
||
4 years ago
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
# GNU General Public License for more details.
|
||
|
#
|
||
|
# You should have received a copy of the GNU General Public License
|
||
4 years ago
|
# along with key-mapper. If not, see <https://www.gnu.org/licenses/>.
|
||
4 years ago
|
|
||
|
|
||
4 years ago
|
"""Logging setup for key-mapper."""
|
||
4 years ago
|
|
||
|
|
||
|
import os
|
||
|
import logging
|
||
|
import pkg_resources
|
||
|
|
||
|
|
||
|
class Formatter(logging.Formatter):
|
||
|
"""Overwritten Formatter to print nicer logs."""
|
||
|
def format(self, record):
|
||
|
debug = logger.level == logging.DEBUG
|
||
|
if record.levelno == logging.INFO and not debug:
|
||
|
# if not launched with --debug, then don't print "INFO:"
|
||
|
self._style._fmt = '%(message)s' # noqa
|
||
|
else:
|
||
|
# see https://en.wikipedia.org/wiki/ANSI_escape_code#3/4_bit
|
||
|
# for those numbers
|
||
|
color = {
|
||
|
logging.WARNING: 33,
|
||
|
logging.ERROR: 31,
|
||
|
logging.FATAL: 31,
|
||
|
logging.DEBUG: 36,
|
||
|
logging.INFO: 32,
|
||
|
}.get(record.levelno, 0)
|
||
|
if debug:
|
||
|
self._style._fmt = ( # noqa
|
||
|
f'\033[{color}m%(levelname)s\033[0m: '
|
||
|
'%(filename)s, line %(lineno)d, %(message)s'
|
||
|
)
|
||
|
else:
|
||
|
self._style._fmt = ( # noqa
|
||
|
f'\033[{color}m%(levelname)s\033[0m: %(message)s'
|
||
|
)
|
||
|
return super().format(record)
|
||
|
|
||
|
|
||
|
logger = logging.getLogger()
|
||
|
handler = logging.StreamHandler()
|
||
|
handler.setFormatter(Formatter())
|
||
|
logger.addHandler(handler)
|
||
|
logger.setLevel(logging.INFO)
|
||
|
|
||
|
|
||
|
def log_info():
|
||
|
"""Log version and name to the console"""
|
||
|
# read values from setup.py
|
||
|
version = pkg_resources.require('setxkbmapgtk')[0].version
|
||
|
name = pkg_resources.require('setxkbmapgtk')[0].project_name
|
||
|
logger.info('%s %s', version, name)
|
||
|
|
||
|
|
||
|
def update_verbosity(debug):
|
||
|
"""Set the logging verbosity according to the settings object."""
|
||
|
if debug:
|
||
|
logger.setLevel(logging.DEBUG)
|
||
|
else:
|
||
|
logger.setLevel(logging.INFO)
|
||
|
|
||
|
|
||
|
def debug_log_on():
|
||
|
return logger.level == logging.DEBUG
|
||
|
|
||
|
|
||
|
def add_filehandler():
|
||
|
"""Clear the existing logfile and start logging to it."""
|
||
|
# jack also logs to ~/.log
|
||
|
log_path = os.path.expanduser('~/.log/setxkbmapgtk')
|
||
|
log_file = os.path.join(log_path, 'log')
|
||
|
|
||
|
if not os.path.exists(log_path):
|
||
|
os.makedirs(log_path)
|
||
|
|
||
|
if os.path.exists(log_file):
|
||
|
# keep the log path small, start from scratch each time
|
||
|
os.remove(log_file)
|
||
|
|
||
|
file_handler = logging.FileHandler(log_file)
|
||
|
file_handler.setFormatter(Formatter())
|
||
|
logger.addHandler(file_handler)
|