Add troubleshooting info to about window
This commit is contained in:
@@ -24,6 +24,8 @@ from lzma import FORMAT_XZ, PRESET_DEFAULT
|
||||
from os import PathLike
|
||||
from pathlib import Path
|
||||
|
||||
from src import shared
|
||||
|
||||
|
||||
class SessionFileHandler(StreamHandler):
|
||||
"""
|
||||
@@ -39,15 +41,15 @@ class SessionFileHandler(StreamHandler):
|
||||
"""Create the log dir if needed"""
|
||||
self.filename.parent.mkdir(exist_ok=True, parents=True)
|
||||
|
||||
def rotate_file(self, file: Path):
|
||||
def rotate_file(self, path: Path):
|
||||
"""Rotate a file's number suffix and remove it if it's too old"""
|
||||
|
||||
# Skip non interesting dir entries
|
||||
if not (file.is_file() and file.name.startswith(self.filename.name)):
|
||||
if not (path.is_file() and path.name.startswith(self.filename.name)):
|
||||
return
|
||||
|
||||
# Compute the new number suffix
|
||||
suffixes = file.suffixes
|
||||
suffixes = path.suffixes
|
||||
has_number = len(suffixes) != len(self.filename.suffixes)
|
||||
current_number = 0 if not has_number else int(suffixes[-1][1:])
|
||||
new_number = current_number + 1
|
||||
@@ -56,40 +58,43 @@ class SessionFileHandler(StreamHandler):
|
||||
if has_number:
|
||||
suffixes.pop()
|
||||
suffixes.append(f".{new_number}")
|
||||
stem = file.name.split(".", maxsplit=1)[0]
|
||||
stem = path.name.split(".", maxsplit=1)[0]
|
||||
new_name = stem + "".join(suffixes)
|
||||
file = file.rename(file.with_name(new_name))
|
||||
path = path.rename(path.with_name(new_name))
|
||||
|
||||
# Remove older files
|
||||
if new_number > self.backup_count:
|
||||
file.unlink()
|
||||
path.unlink()
|
||||
return
|
||||
|
||||
def file_sort_key(self, file: Path) -> int:
|
||||
def file_sort_key(self, path: Path) -> int:
|
||||
"""Key function used to sort files"""
|
||||
if not file.name.startswith(self.filename.name):
|
||||
if not path.name.startswith(self.filename.name):
|
||||
# First all files that aren't logs
|
||||
return -1
|
||||
if file.name == self.filename.name:
|
||||
if path.name == self.filename.name:
|
||||
# Then the latest log file
|
||||
return 0
|
||||
# Then in order the other log files
|
||||
return int(file.suffixes[-1][1:])
|
||||
return int(path.suffixes[-1][1:])
|
||||
|
||||
def get_files(self) -> list[Path]:
|
||||
return list(self.filename.parent.iterdir())
|
||||
|
||||
def rotate(self) -> None:
|
||||
"""Rotate the numbered suffix on the log files and remove old ones"""
|
||||
files = list(self.filename.parent.iterdir())
|
||||
files.sort(key=self.file_sort_key, reverse=True)
|
||||
for file in files:
|
||||
self.rotate_file(file)
|
||||
(files := self.get_files()).sort(key=self.file_sort_key, reverse=True)
|
||||
for path in files:
|
||||
self.rotate_file(path)
|
||||
|
||||
def __init__(self, filename: PathLike, backup_count: int = 2) -> None:
|
||||
self.filename = Path(filename)
|
||||
self.backup_count = backup_count
|
||||
self.create_dir()
|
||||
self.rotate()
|
||||
shared.log_files = self.get_files()
|
||||
self.log_file = lzma.open(
|
||||
self.filename, "at", format=FORMAT_XZ, preset=PRESET_DEFAULT
|
||||
self.filename, "xt", format=FORMAT_XZ, preset=PRESET_DEFAULT
|
||||
)
|
||||
super().__init__(self.log_file)
|
||||
|
||||
|
||||
@@ -54,7 +54,7 @@ def setup_logging():
|
||||
"formatter": "file_formatter",
|
||||
"level": "DEBUG",
|
||||
"filename": log_filename,
|
||||
"backup_count": 3,
|
||||
"backup_count": 2,
|
||||
},
|
||||
"app_console_handler": {
|
||||
"class": "logging.StreamHandler",
|
||||
|
||||
@@ -16,7 +16,9 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
import json
|
||||
import lzma
|
||||
import sys
|
||||
|
||||
import gi
|
||||
@@ -141,6 +143,11 @@ class CartridgesApplication(Adw.Application):
|
||||
shared.store.add_game(game, {"skip_save": True})
|
||||
|
||||
def on_about_action(self, *_args):
|
||||
debug_str = ""
|
||||
for path in shared.log_files:
|
||||
log_file = lzma.open(path, "rt", encoding="utf-8")
|
||||
debug_str += log_file.read()
|
||||
|
||||
about = Adw.AboutWindow(
|
||||
transient_for=self.win,
|
||||
application_name=_("Cartridges"),
|
||||
@@ -162,6 +169,7 @@ class CartridgesApplication(Adw.Application):
|
||||
website="https://github.com/kra-mo/cartridges",
|
||||
# Translators: Replace this with your name for it to show up in the about window
|
||||
translator_credits=_("translator_credits"),
|
||||
debug_info=debug_str,
|
||||
)
|
||||
about.present()
|
||||
|
||||
|
||||
@@ -61,4 +61,5 @@ image_size = (200 * scale_factor, 300 * scale_factor)
|
||||
# pylint: disable=invalid-name
|
||||
win = None
|
||||
importer = None
|
||||
store = None
|
||||
store = None
|
||||
log_files = None
|
||||
|
||||
Reference in New Issue
Block a user