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