From 2fced8122e72966a3023e5519bbf8b76f227996f Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 1 Dec 2021 18:29:17 +0100 Subject: [PATCH] Attempt at fixing up logging --- convo.py | 4 +- dataobjs.py | 4 +- irc.py | 4 +- logging.conf => logging.ini.example | 10 +- luaquirks.py | 4 +- memos.py | 4 +- oyoyo/client.py | 4 +- oyoyo/cmdhandler.py | 4 +- oyoyo/helpers.py | 4 +- parsetools.py | 4 +- pesterchum.py | 185 +++++++++++++++------------- profile.py | 4 +- pyinstaller.py | 2 +- pyquirks.py | 4 +- quirks.py | 4 +- randomer.py | 4 +- setup.py | 2 +- toast.py | 4 +- 18 files changed, 148 insertions(+), 107 deletions(-) rename logging.conf => logging.ini.example (70%) diff --git a/convo.py b/convo.py index cbd5c67..4f84af8 100644 --- a/convo.py +++ b/convo.py @@ -1,5 +1,7 @@ import logging, logging.config -logging.config.fileConfig('logging.conf') +import ostools +_datadir = ostools.getDataDir() +logging.config.fileConfig(_datadir + "logging.ini") PchumLog = logging.getLogger('pchumLogger') from string import Template import re diff --git a/dataobjs.py b/dataobjs.py index e40075a..c1dd4d1 100644 --- a/dataobjs.py +++ b/dataobjs.py @@ -1,5 +1,7 @@ import logging, logging.config -logging.config.fileConfig('logging.conf') +import ostools +_datadir = ostools.getDataDir() +logging.config.fileConfig(_datadir + "logging.ini") PchumLog = logging.getLogger('pchumLogger') from PyQt5 import QtCore, QtGui from datetime import * diff --git a/irc.py b/irc.py index 4884942..c646e56 100644 --- a/irc.py +++ b/irc.py @@ -1,5 +1,7 @@ import logging, logging.config -logging.config.fileConfig('logging.conf') +import ostools +_datadir = ostools.getDataDir() +logging.config.fileConfig(_datadir + "logging.ini") PchumLog = logging.getLogger('pchumLogger') from PyQt5 import QtCore, QtGui from oyoyo.client import IRCClient diff --git a/logging.conf b/logging.ini.example similarity index 70% rename from logging.conf rename to logging.ini.example index 1d14a3f..2a40a06 100644 --- a/logging.conf +++ b/logging.ini.example @@ -2,7 +2,7 @@ keys=root,pchumLogger [handlers] -keys=consoleHandler,FileHandler +keys=consoleHandler [formatters] keys=simpleFormatter @@ -13,7 +13,7 @@ handlers=consoleHandler [logger_pchumLogger] level=WARNING -handlers=consoleHandler,FileHandler +handlers=consoleHandler qualname=pchumLogger propagate=0 @@ -23,12 +23,6 @@ level=WARNING formatter=simpleFormatter args=(sys.stdout,) -[handler_FileHandler] -class=FileHandler -level=WARNING -formatter=simpleFormatter -args=('pesterchum.log', 'a') - [formatter_simpleFormatter] format=%(asctime)s - %(levelname)s - %(module)s - %(threadName)s - %(message)s datefmt= diff --git a/luaquirks.py b/luaquirks.py index a2055a5..ad04263 100644 --- a/luaquirks.py +++ b/luaquirks.py @@ -1,5 +1,7 @@ import logging, logging.config -logging.config.fileConfig('logging.conf') +import ostools +_datadir = ostools.getDataDir() +logging.config.fileConfig(_datadir + "logging.ini") PchumLog = logging.getLogger('pchumLogger') import os, sys, re, ostools try: diff --git a/memos.py b/memos.py index 9261c59..57bd1ac 100644 --- a/memos.py +++ b/memos.py @@ -1,5 +1,7 @@ import logging, logging.config -logging.config.fileConfig('logging.conf') +import ostools +_datadir = ostools.getDataDir() +logging.config.fileConfig(_datadir + "logging.ini") PchumLog = logging.getLogger('pchumLogger') from string import Template import re diff --git a/oyoyo/client.py b/oyoyo/client.py index 11a1606..2bebde9 100644 --- a/oyoyo/client.py +++ b/oyoyo/client.py @@ -16,7 +16,9 @@ # THE SOFTWARE. import logging, logging.config -logging.config.fileConfig('logging.conf') +import ostools +_datadir = ostools.getDataDir() +logging.config.fileConfig(_datadir + "logging.ini") PchumLog = logging.getLogger('pchumLogger') import logging diff --git a/oyoyo/cmdhandler.py b/oyoyo/cmdhandler.py index 263c969..98c20f6 100644 --- a/oyoyo/cmdhandler.py +++ b/oyoyo/cmdhandler.py @@ -15,7 +15,9 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. import logging, logging.config -logging.config.fileConfig('logging.conf') +import ostools +_datadir = ostools.getDataDir() +logging.config.fileConfig(_datadir + "logging.ini") PchumLog = logging.getLogger('pchumLogger') import inspect diff --git a/oyoyo/helpers.py b/oyoyo/helpers.py index 30a16dc..4823a27 100644 --- a/oyoyo/helpers.py +++ b/oyoyo/helpers.py @@ -18,7 +18,9 @@ """ contains helper functions for common irc commands """ import logging, logging.config -logging.config.fileConfig('logging.conf') +import ostools +_datadir = ostools.getDataDir() +logging.config.fileConfig(_datadir + "logging.ini") PchumLog = logging.getLogger('pchumLogger') import random diff --git a/parsetools.py b/parsetools.py index 2ed1152..a8d7b0f 100644 --- a/parsetools.py +++ b/parsetools.py @@ -1,5 +1,7 @@ import logging, logging.config -logging.config.fileConfig('logging.conf') +import ostools +_datadir = ostools.getDataDir() +logging.config.fileConfig(_datadir + "logging.ini") PchumLog = logging.getLogger('pchumLogger') import re import random diff --git a/pesterchum.py b/pesterchum.py index 4ce7929..0c96fa6 100644 --- a/pesterchum.py +++ b/pesterchum.py @@ -4,6 +4,7 @@ import sys import os import shutil import getopt +import configparser try: QString = unicode except NameError: @@ -17,32 +18,18 @@ print("Use -h/--help to see the available options.\nLogging is configured in log # Help if ('--help' in sys.argv[1:]) or ('-h' in sys.argv[1:]): print("Possible arguments:") - #help_arguments = " -l, --logging\n Specify level of logging, possible values are:\n" + \ - # " CRITICAL, ERROR, WARNING, INFO, DEBUG, NOTSET.\n" + \ - # " The default value is WARNING.\n" + \ - # " (See https://docs.python.org/3/library/logging.html)\n\n" + \ + help_arguments = " -l, --logging\n Specify level of logging, possible values are:\n" + \ + " CRITICAL, ERROR, WARNING, INFO, DEBUG, NOTSET.\n" + \ + " The default value is WARNING.\n" + \ + " (See https://docs.python.org/3/library/logging.html)\n\n" + \ help_arguments = " -s, --server\n Specify server override. (legacy)\n\n" + \ " -p, --port\n Specify port override. (legacy)\n\n" + \ " --advanced\n Enable advanced.\n\n" + \ " --no-honk\n Disable honking.\n" print(help_arguments) sys.exit() -import logging, logging.config -logging.config.fileConfig('logging.conf') -PchumLog = logging.getLogger('pchumLogger') -#PchumLog = logging.getLogger(__name__) -#Logfile = logging.FileHandler("pesterchum.log") -#fileformat = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') -#Logfile.setFormatter(fileformat) -#PchumLog.addHandler(Logfile) - -#stream = logging.StreamHandler() -#streamformat = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') -#stream.setFormatter(streamformat) -#PchumLog.addHandler(stream) - -#logging.basicConfig(filename="pesterchum.log") +import logging from datetime import * import random import re @@ -58,17 +45,6 @@ except ImportError: # Fall back on the old location - just in case #PchumLog.warning("Couldn't load attrdict from new loc; falling back") from pnc.dep.attrdict import AttrDict -try: - import console -except ImportError: - _CONSOLE = False - PchumLog.warning("Console file not shipped; skipping.") -except Exception as err: - _CONSOLE = False - # Consider erroring? - PchumLog.error("Failed to load console!", exc_info=err) -else: - _CONSOLE = True reqmissing = [] optmissing = [] @@ -79,7 +55,7 @@ except ImportError as e: if module.startswith("No module named ") or \ module.startswith("cannot import name "): reqmissing.append(module[module.rfind(" ")+1:]) - else: PchumLog.critical(e) + else: logging.critical(e) del module # Because pygame intro msg :3c @@ -94,11 +70,11 @@ except ImportError as e: pygame = None module = str(e) if module[:16] == "No module named ": optmissing.append(module[16:]) - else: PchumLog.critical(e) + else: logging.critical(e) del module if reqmissing: - PchumLog.critical("ERROR: The following modules are required for Pesterchum to run and are missing on your system:") - for m in reqmissing: PchumLog.critical("* "+m) + logging.critical("ERROR: The following modules are required for Pesterchum to run and are missing on your system:") + for m in reqmissing: logging.critical("* "+m) # False flag for some reason. #exit() vnum = QtCore.qVersion() @@ -108,8 +84,8 @@ if vnum.find(".", vnum.find(".")+1) != -1: else: minor = int(vnum[vnum.find(".")+1:]) if not ((major > 5) or (major == 5 and minor >= 0)): - PchumLog.critical("ERROR: Pesterchum requires at least Qt version >= 5.0") - PchumLog.critical("You currently have version " + vnum + ". Please upgrade Qt.") + logging.critical("ERROR: Pesterchum requires at least Qt version >= 5.0") + logging.critical("You currently have version " + vnum + ". Please upgrade Qt.") exit() from version import _pcVersion @@ -127,53 +103,96 @@ _datadir = ostools.getDataDir() # (on Linux, same as using xdg). To stay safe with older versions, copy any # data (profiles, logs, etc) from old location to new data directory. -#loglevel = 30# Warning (Default) -#if ('--logging' in sys.argv[1:]) or ('-l' in sys.argv[1:]) & (False == ('--logging' in sys.argv[1:]) and ('-l' in sys.argv[1:])): -# try: -# # If both are specified, this does not run. -# if ('-l' in sys.argv[1:]): -# loglevel = sys.argv[sys.argv.index('-l') + 1] -# if ('--logging' in sys.argv[1:]): -# loglevel = sys.argv[sys.argv.index('--logging') + 1] -# -# loglevel = loglevel.upper() -# -# if loglevel == "CRITICAL": -# loglevel = 50 -# print("Logging Level is CRITICAL") -# elif loglevel == "ERROR": -# print("Logging Level is ERROR") -# elif loglevel == "WARNING": -# loglevel = 30 -# print("Logging Level is WARNING") -# elif loglevel == "INFO": -# loglevel = 20 -# print("Logging Level is INFO") -# elif loglevel == "DEBUG": -# loglevel = 10 -# print("Logging Level is DEBUG") -# elif loglevel == "NOTSET": -# loglevel = 0 -# print("Logging Level is NOTSET") -# else: -# raise Exception -# stream.setLevel(loglevel) -# Logfile.setLevel(loglevel) -# -# # Remove from argv because the rest of the code can't handle it :/ -# if ('-l' in sys.argv[1:]): -# sys.argv.pop(sys.argv.index('-l') + 1) -# sys.argv.pop(sys.argv.index('-l')) -# if ('--logging' in sys.argv[1:]): -# sys.argv.pop(sys.argv.index('--logging') + 1) -# sys.argv.pop(sys.argv.index('--logging')) -# except: -# print("Invalid syntax.") -# stream.setLevel(loglevel) -# Logfile.setLevel(loglevel) -#else: -# stream.setLevel(loglevel) -# Logfile.setLevel(loglevel) +config = configparser.ConfigParser() +# Create logging.conf +if os.path.exists(_datadir + "logging.ini") == False: + config.read('logging.ini.example') + + # Enable file logging + config['handlers']['keys'] = 'consoleHandler,FileHandler' + config['logger_pchumLogger']['handlers'] = 'consoleHandler,FileHandler' + + #(r'C:\Users\X\AppData\Local\pesterchum\pesterchum.log', 'a') + config['handler_FileHandler'] = {'class': 'FileHandler', + 'level': 'WARNING', + 'formatter': 'simpleFormatter', + 'args': (_datadir + 'pesterchum.log', 'a')} + + print(config.sections()) +else: + config.read(_datadir + 'logging.ini') + +loglevel = "30"# Warning (Default) +if ('--logging' in sys.argv[1:]) or ('-l' in sys.argv[1:]) & (False == ('--logging' in sys.argv[1:]) and ('-l' in sys.argv[1:])): + try: + # If both are specified, this does not run. + if ('-l' in sys.argv[1:]): + loglevel = sys.argv[sys.argv.index('-l') + 1] + if ('--logging' in sys.argv[1:]): + loglevel = sys.argv[sys.argv.index('--logging') + 1] + + loglevel = loglevel.upper().strip() + + config.read(_datadir + 'logging.ini') + + print("loglevel = " + loglevel) + + if loglevel == "50" or loglevel == "CRITICAL": + loglevel = "CRITICAL" + print("Logging Level is CRITICAL") + elif loglevel == "40" or loglevel == "ERROR": + loglevel = "ERROR" + print("Logging Level is ERROR") + elif loglevel == "30" or loglevel == "WARNING": + loglevel = "WARNING" + print("Logging Level is WARNING") + elif loglevel == "20" or loglevel == "INFO": + loglevel = "INFO" + print("Logging Level is INFO") + elif loglevel == "10" or loglevel == "DEBUG": + loglevel = "DEBUG" + print("Logging Level is DEBUG") + elif loglevel == "0" or loglevel == "NOTSET": + loglevel = "NOTSET" + print("Logging Level is NOTSET") + else: + loglevel = "WARNING" + print("Logging Level is WARNING") + + config['logger_root']['level'] = loglevel + config['logger_pchumLogger']['level'] = loglevel + config['handler_consoleHandler']['level'] = loglevel + + # Remove from argv because the rest of the code can't handle it :/ + if ('-l' in sys.argv[1:]): + sys.argv.pop(sys.argv.index('-l') + 1) + sys.argv.pop(sys.argv.index('-l')) + if ('--logging' in sys.argv[1:]): + sys.argv.pop(sys.argv.index('--logging') + 1) + sys.argv.pop(sys.argv.index('--logging')) + except: + logging.exception("Invalid syntax?") + +# Update logging.conf +with open(_datadir + "logging.ini", 'w') as configfile: + config.write(configfile) + +# Load logging.conf +import logging.config +logging.config.fileConfig(_datadir + "logging.ini") +PchumLog = logging.getLogger('pchumLogger') + +try: + import console +except ImportError: + _CONSOLE = False + logging.warning("Console file not shipped; skipping.") +except Exception as err: + _CONSOLE = False + # Consider erroring? + logging.error("Failed to load console!", exc_info=err) +else: + _CONSOLE = True if _datadir: if not os.path.exists(_datadir): diff --git a/profile.py b/profile.py index 085114f..fea075d 100644 --- a/profile.py +++ b/profile.py @@ -1,5 +1,7 @@ import logging, logging.config -logging.config.fileConfig('logging.conf') +import ostools +_datadir = ostools.getDataDir() +logging.config.fileConfig(_datadir + "logging.ini") PchumLog = logging.getLogger('pchumLogger') import os from string import Template diff --git a/pyinstaller.py b/pyinstaller.py index 4b2821f..d3f6cdd 100644 --- a/pyinstaller.py +++ b/pyinstaller.py @@ -93,7 +93,7 @@ add_data = ['quirks;quirks', 'CHANGELOG.md;.', 'PCskins.png;.', 'Pesterchum.png;.', - 'logging.conf;.'] + 'logging.ini.example;.'] upx_exclude = ["qwindows.dll", "Qt5Core.dll", diff --git a/pyquirks.py b/pyquirks.py index 9b51ab4..8fa3026 100644 --- a/pyquirks.py +++ b/pyquirks.py @@ -1,5 +1,7 @@ import logging, logging.config -logging.config.fileConfig('logging.conf') +import ostools +_datadir = ostools.getDataDir() +logging.config.fileConfig(_datadir + "logging.ini") PchumLog = logging.getLogger('pchumLogger') import os, sys, imp, re, ostools from quirks import ScriptQuirks diff --git a/quirks.py b/quirks.py index 68ae185..3945acb 100644 --- a/quirks.py +++ b/quirks.py @@ -1,5 +1,7 @@ import logging, logging.config -logging.config.fileConfig('logging.conf') +import ostools +_datadir = ostools.getDataDir() +logging.config.fileConfig(_datadir + "logging.ini") PchumLog = logging.getLogger('pchumLogger') import os, sys, re, ostools from PyQt5 import QtCore, QtGui, QtWidgets diff --git a/randomer.py b/randomer.py index 4c363c7..b7ffdc9 100644 --- a/randomer.py +++ b/randomer.py @@ -1,5 +1,7 @@ import logging, logging.config -logging.config.fileConfig('logging.conf') +import ostools +_datadir = ostools.getDataDir() +logging.config.fileConfig(_datadir + "logging.ini") PchumLog = logging.getLogger('pchumLogger') from PyQt5 import QtCore, QtGui, QtWidgets diff --git a/setup.py b/setup.py index 1463d17..d6683a7 100644 --- a/setup.py +++ b/setup.py @@ -31,7 +31,7 @@ includefiles = ["quirks", "CHANGELOG.md", "PCskins.png", "Pesterchum.png", - "logging.conf"] + "logging.ini.example"] build_exe_options = { ## "includes": ["PyQt5.QtCore", ## "PyQt5.QtGui", diff --git a/toast.py b/toast.py index e0a5f54..a995804 100644 --- a/toast.py +++ b/toast.py @@ -1,5 +1,7 @@ import logging, logging.config -logging.config.fileConfig('logging.conf') +import ostools +_datadir = ostools.getDataDir() +logging.config.fileConfig(_datadir + "logging.ini") PchumLog = logging.getLogger('pchumLogger') import inspect #import threading