Skip to content

Commit

Permalink
Merge pull request #2259 from HullSeals/enhancement/2215/move-logging…
Browse files Browse the repository at this point in the history
…-dir

[2215] Move Logging Directory
  • Loading branch information
Rixxan authored Jul 22, 2024
2 parents 9ddf7aa + 38286cf commit ac9b7b4
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 19 deletions.
10 changes: 3 additions & 7 deletions EDMCLogging.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import logging.handlers
import os
import pathlib
import tempfile
import warnings
from contextlib import suppress
from fnmatch import fnmatch
Expand All @@ -51,9 +50,7 @@
from time import gmtime
from traceback import print_exc
from typing import TYPE_CHECKING, cast

import config as config_mod
from config import appcmdname, appname, config
from config import appcmdname, appname, config, trace_on

# TODO: Tests:
#
Expand Down Expand Up @@ -104,7 +101,7 @@


def _trace_if(self: logging.Logger, condition: str, message: str, *args, **kwargs) -> None:
if any(fnmatch(condition, p) for p in config_mod.trace_on):
if any(fnmatch(condition, p) for p in trace_on):
self._log(logging.TRACE, message, args, **kwargs) # type: ignore # we added it
return

Expand Down Expand Up @@ -184,8 +181,7 @@ def __init__(self, logger_name: str, loglevel: int | str = _default_loglevel):
# We want the files in %TEMP%\{appname}\ as {logger_name}-debug.log and
# rotated versions.
# This is {logger_name} so that EDMC.py logs to a different file.
logfile_rotating = pathlib.Path(tempfile.gettempdir())
logfile_rotating /= f'{appname}'
logfile_rotating = pathlib.Path(config.app_dir_path / 'logs')
logfile_rotating.mkdir(exist_ok=True)
logfile_rotating /= f'{logger_name}-debug.log'

Expand Down
10 changes: 6 additions & 4 deletions EDMarketConnector.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import sys
import threading
import webbrowser
import tempfile
from os import chdir, environ, path
from time import localtime, strftime, time
from typing import TYPE_CHECKING, Any, Literal
Expand All @@ -42,16 +41,19 @@
# not frozen.
chdir(pathlib.Path(__file__).parent)


# config will now cause an appname logger to be set up, so we need the
# console redirect before this
if __name__ == '__main__':
# Keep this as the very first code run to be as sure as possible of no
# output until after this redirect is done, if needed.
if getattr(sys, 'frozen', False):
from config import config
# By default py2exe tries to write log to dirname(sys.executable) which fails when installed
# unbuffered not allowed for text in python3, so use `1 for line buffering
log_file_path = path.join(tempfile.gettempdir(), f'{appname}.log')
log_file_path = pathlib.Path(config.app_dir_path / 'logs')
log_file_path.mkdir(exist_ok=True)
log_file_path /= f'{appname}.log'

sys.stdout = sys.stderr = open(log_file_path, mode='wt', buffering=1) # Do NOT use WITH here.
# TODO: Test: Make *sure* this redirect is working, else py2exe is going to cause an exit popup

Expand Down Expand Up @@ -619,7 +621,7 @@ def open_window(systray: 'SysTrayIcon', *args) -> None:
self.help_menu.add_command(command=lambda: self.updater.check_for_updates()) # Check for Updates...
# About E:D Market Connector
self.help_menu.add_command(command=lambda: not self.HelpAbout.showing and self.HelpAbout(self.w))
logfile_loc = pathlib.Path(tempfile.gettempdir()) / appname
logfile_loc = pathlib.Path(config.app_dir_path / 'logs')
self.help_menu.add_command(command=lambda: prefs.open_folder(logfile_loc)) # Open Log Folder
self.help_menu.add_command(command=lambda: prefs.help_open_system_profiler(self)) # Open Log Folde

Expand Down
6 changes: 2 additions & 4 deletions debug_webserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@
import gzip
import json
import pathlib
import tempfile
import threading
import zlib
from http import server
from typing import Any, Callable, Literal
from urllib.parse import parse_qs

from config import appname
import config
from EDMCLogging import get_main_logger

logger = get_main_logger()

output_lock = threading.Lock()
output_data_path = pathlib.Path(tempfile.gettempdir()) / f'{appname}' / 'http_debug'
output_data_path = pathlib.Path(config.app_dir_path / 'logs' / 'http_debug')
SAFE_TRANSLATE = str.maketrans(dict.fromkeys("!@#$%^&*()./\\\r\n[]-+='\";:?<>,~`", '_'))


Expand Down
6 changes: 2 additions & 4 deletions prefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import pathlib
import subprocess
import sys
import tempfile
import tkinter as tk
import warnings
from os import system
Expand All @@ -20,7 +19,6 @@
import plug
from config import appversion_nobuild, config
from EDMCLogging import edmclogger, get_main_logger
from constants import appname
from hotkey import hotkeymgr
from l10n import translations as tr
from monitor import monitor
Expand All @@ -43,7 +41,7 @@ def help_open_log_folder() -> None:
"""Open the folder logs are stored in."""
warnings.warn('prefs.help_open_log_folder is deprecated, use open_log_folder instead. '
'This function will be removed in 6.0 or later', DeprecationWarning, stacklevel=2)
open_folder(pathlib.Path(tempfile.gettempdir()) / appname)
open_folder(pathlib.Path(config.app_dir_path / 'logs'))


def open_folder(file: pathlib.Path) -> None:
Expand Down Expand Up @@ -324,7 +322,7 @@ def __init__(self, parent: tk.Tk, callback: Optional[Callable]):
self.geometry(f"+{position.left}+{position.top}")

# Set Log Directory
self.logfile_loc = pathlib.Path(tempfile.gettempdir()) / appname
self.logfile_loc = pathlib.Path(config.app_dir_path / 'logs')

# Set minimum size to prevent content cut-off
self.update_idletasks() # Update "requested size" from geometry manager
Expand Down

0 comments on commit ac9b7b4

Please sign in to comment.