Skip to content
This repository has been archived by the owner on Nov 26, 2023. It is now read-only.

Commit

Permalink
session_id attribute, logfiles older than 5 most recent are zipped
Browse files Browse the repository at this point in the history
  • Loading branch information
kushalkolar committed Mar 11, 2021
1 parent 314888e commit 320a144
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
37 changes: 27 additions & 10 deletions mesmerize/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,16 @@
from glob import glob
import click
import traceback
from zipfile import ZipFile, ZIP_LZMA


session_id = datetime.now().strftime("%Y-%m-%d_%H.%M.%S-%f")


class App(QApplication):
window_manager = WindowManager()
project_manager = ProjectManager()
session_id = session_id


def start_welcome_window():
Expand All @@ -52,38 +57,48 @@ def start_batch_manager(batch_path: str, item_uuid: str):
return (app, bm)


def move_old_logfiles(logger):
zlogfile = ZipFile(
os.path.join(sys_cfg_dir, 'logs.zip'),
mode='a',
compression=ZIP_LZMA,
allowZip64=True,

)

logfiles = glob(os.path.join(sys_cfg_dir, '*.log'))
logfiles.sort()
while len(logfiles) > 5:
logger.info("Moving old logfiles...")
old_logfile = logfiles.pop(-1)
zlogfile.write(old_logfile, os.path.basename(old_logfile))
os.remove(logfiles[-1])
zlogfile.close()


@click.command()
@click.option('--log-level', type=str, default='INFO')
@click.option('--log-max-num-log-files', type=int, default=25)
@click.option('--log-file', type=click.Path(exists=False, writable=True))
@click.option('--log-file-dir', type=click.Path(writable=True))
@click.option('--log-format', type=str)
@click.option('--run-batch', nargs=2, type=str)
@click.option('--open-plot', type=click.Path(exists=True, readable=True))
def main(
log_level,
log_max_num_log_files,
log_file,
log_file_dir,
log_format,
run_batch,
open_plot
):
logfiles = glob(os.path.join(sys_cfg_dir, '*.log'))

while len(logfiles) > log_max_num_log_files:
logfiles = glob(os.path.join(sys_cfg_dir, '*.log'))
logfiles.sort()
os.remove(logfiles[-1])

if log_file_dir is None:
log_file_dir = sys_cfg_dir

if not os.path.exists(log_file_dir):
os.makedirs(log_file_dir, exist_ok=True)

if log_file is None:
log_file = os.path.join(log_file_dir, datetime.now().strftime("%Y_%m_%d-%H:%M:%S-%f.log"))
log_file = os.path.join(log_file_dir, f"{session_id}.log")

if log_format is None:
log_format = "%(asctime)s %(levelname)s %(pathname)s %(lineno)s \n %(message)s "
Expand Down Expand Up @@ -114,6 +129,8 @@ def exception_hook(exc_type, exc_value, traceback):
logging.StreamHandler(sys.stderr)
)

move_old_logfiles(root_logger)

if not len(sys.argv) > 1:
app = start_welcome_window()
app.exec_()
Expand Down
6 changes: 6 additions & 0 deletions mesmerize/common/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ def get_window_manager():
# return wm


def get_session_id():
if not is_app():
raise NotInApplicationError
return getattr(QApplication.instance(), 'session_id')


def is_mesmerize_project(proj_dir: str) -> bool:
if not os.path.isdir(proj_dir + '/dataframes'):
raise NotAMesmerizeProject('dataframes directory not found')
Expand Down
8 changes: 5 additions & 3 deletions mesmerize/common/welcome_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from .pytemplates.welcome_window_pytemplate import *
# from ..project_manager import ProjectManager
from ..common import configuration, system_config_window, doc_pages, get_project_manager, set_project_manager
from ..common import get_window_manager, is_mesmerize_project
from ..common import get_window_manager, is_mesmerize_project, get_session_id
# from viewer.modules import batch_manager
import os
from ..common import start
Expand Down Expand Up @@ -110,13 +110,15 @@ def __init__(self):
def initialize_console_widget(self):
ns = {'configuration': configuration,
'get_window_manager': get_window_manager,
'this': self
'this': self,
'get_session_id': get_session_id
}

txt = '\n'.join(["Namespaces:",
"self as this",
"callables:",
"get_window_manager()"])
"get_window_manager()",
"get_session_id()"])

cmd_history_file = os.path.join(configuration.console_history_path, 'welcome_window.pik')
console = ConsoleWidget(namespace=ns, text=txt, historyFile=cmd_history_file)
Expand Down

0 comments on commit 320a144

Please sign in to comment.