Skip to content

Commit

Permalink
fix path for log file
Browse files Browse the repository at this point in the history
  • Loading branch information
jgstew committed Aug 29, 2024
1 parent 578282a commit 91ea1b8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 11 deletions.
46 changes: 44 additions & 2 deletions examples/export_all_sites.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import logging
import logging.handlers
import os
import ntpath
import platform
import shutil
import sys
Expand All @@ -32,6 +33,45 @@
invoke_folder = None


def get_invoke_folder(verbose=0):
"""Get the folder the script was invoked from"""
# using logging here won't actually log it to the file:

if getattr(sys, "frozen", False) and hasattr(sys, "_MEIPASS"):
if verbose:
print("running in a PyInstaller bundle")
invoke_folder = os.path.abspath(os.path.dirname(sys.executable))
else:
if verbose:
print("running in a normal Python process")
invoke_folder = os.path.abspath(os.path.dirname(__file__))

if verbose:
print(f"invoke_folder = {invoke_folder}")

return invoke_folder


def get_invoke_file_name(verbose=0):
"""Get the filename the script was invoked from"""
# using logging here won't actually log it to the file:

if getattr(sys, "frozen", False) and hasattr(sys, "_MEIPASS"):
if verbose:
print("running in a PyInstaller bundle")
invoke_file_path = sys.executable
else:
if verbose:
print("running in a normal Python process")
invoke_file_path = __file__

if verbose:
print(f"invoke_file_path = {invoke_file_path}")

# get just the file name, return without file extension:
return os.path.splitext(ntpath.basename(invoke_file_path))[0]


def main():
"""Execution starts here"""
print("main() start")
Expand All @@ -56,9 +96,11 @@ def main():
verbose = args.verbose

# get folder the script was invoked from:
invoke_folder = besapi.plugin_utilities.get_invoke_folder()
invoke_folder = get_invoke_folder()

log_file_path = invoke_folder + get_invoke_file_name() + ".log"

besapi.plugin_utilities.setup_plugin_logging()
besapi.plugin_utilities.setup_plugin_logging(log_file_path)

logging.info("----- Starting New Session ------")
logging.debug("invoke folder: %s", invoke_folder)
Expand Down
16 changes: 7 additions & 9 deletions src/besapi/plugin_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,25 @@ def setup_plugin_argparse(plugin_args_required=False):
return arg_parser


def setup_plugin_logging(log_file_name="", verbose=0, console=True):
def setup_plugin_logging(log_file_path="", verbose=0, console=True):
"""setup logging for plugin use"""
# get folder the script was invoked from:
invoke_folder = get_invoke_folder(verbose)

if not log_file_name or log_file_name == "":
log_file_name = get_invoke_file_name() + ".log"
if not log_file_path or log_file_path == "":
log_file_path = os.path.join(
get_invoke_folder(verbose), get_invoke_file_name() + ".log"
)

# set different log levels:
log_level = logging.WARNING
if verbose:
log_level = logging.INFO
print("INFO: Log File Path: %s", log_file_path)
if verbose > 1:
log_level = logging.DEBUG

# get path to put log file in:
log_filename = os.path.join(invoke_folder, log_file_name)

handlers = [
logging.handlers.RotatingFileHandler(
log_filename, maxBytes=5 * 1024 * 1024, backupCount=1
log_file_path, maxBytes=5 * 1024 * 1024, backupCount=1
)
]

Expand Down

0 comments on commit 91ea1b8

Please sign in to comment.