Skip to content

Commit

Permalink
Ensure selfie.log is writable
Browse files Browse the repository at this point in the history
  • Loading branch information
tnunamak committed Mar 20, 2024
1 parent 76e213c commit 15de3fc
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 9 deletions.
3 changes: 1 addition & 2 deletions selfie.spec
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ exe = EXE(
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=True,
console=False,
disable_windowed_traceback=False,
argv_emulation=False,
target_arch=None,
Expand All @@ -74,7 +74,6 @@ coll = COLLECT(
upx_exclude=[],
name='selfie',
debug="all",
console=False,
)

app = BUNDLE(coll,
Expand Down
6 changes: 4 additions & 2 deletions selfie/api/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@

from fastapi import APIRouter

from selfie.logging import get_log_path

router = APIRouter(tags=["Configuration"])


@router.get("/logs")
async def get_logs():
try:
filepath = "selfie.log"
filepath = get_log_path()
file_stats = os.stat(filepath)
with open(filepath, "r") as file:
log = file.read()
return [{
"filename": "selfie.log",
"filename": filepath.split("/")[-1],
"log": log,
"size": file_stats.st_size,
"lines": len(log.split("\n")),
Expand Down
4 changes: 3 additions & 1 deletion selfie/gui.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import selfie.logging

from selfie.logging import get_log_path

import sys
import os
import logging
Expand Down Expand Up @@ -27,7 +29,7 @@ def __init__(self, parent=None):
logger.info("Creating LogWidget")
super().__init__(parent)
self.setReadOnly(True)
self.log_file = "selfie.log" # TODO: Don't hardcode the log file
self.log_file = get_log_path()
self.timer = QTimer()
self.timer.timeout.connect(self.update_logs)
self.timer.start(1000)
Expand Down
43 changes: 39 additions & 4 deletions selfie/logging.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,42 @@
import os
import logging
import platform
from logging.handlers import RotatingFileHandler

log_file = "selfie.log" # TODO: Don't hardcode the log file
file_handler = RotatingFileHandler(log_file, maxBytes=1024*1024, backupCount=5)
file_handler.setFormatter(logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s"))
logging.root.addHandler(file_handler)
# TODO: Don't hardcode these
level = logging.INFO
log_file = "selfie.log"


def get_log_path():
os_name = platform.system()

# Set default log directory based on the operating system
if os_name == 'Darwin': # macOS
log_directory = os.path.expanduser('~/Library/Logs/Selfie')
elif os_name == 'Windows':
log_directory = os.path.join(os.environ['APPDATA'], 'Selfie', 'Logs')
else: # Assume Linux/Unix
log_directory = os.path.expanduser('~/Selfie/Logs')

if not os.path.exists(log_directory):
os.makedirs(log_directory)

return os.path.join(log_directory, log_file)


def setup_logging():
log_path = get_log_path()

logger = logging.getLogger()
logger.setLevel(level)

file_handler = RotatingFileHandler(log_path, maxBytes=1024*1024, backupCount=5)
file_handler.setLevel(level)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
file_handler.setFormatter(formatter)

logger.addHandler(file_handler)


setup_logging()

0 comments on commit 15de3fc

Please sign in to comment.