Skip to content

Commit

Permalink
Merge pull request #4 from mantidproject/merge_logging
Browse files Browse the repository at this point in the history
Merge mantid and python logging
  • Loading branch information
peterfpeterson authored Apr 26, 2023
2 parents 8339c4d + 59c57c3 commit ca8191b
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 20 deletions.
3 changes: 3 additions & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ name: livereduce

channels:
- conda-forge
- mantid

dependencies:
- mantid # framework only
- pyinotify
- pre-commit
2 changes: 1 addition & 1 deletion livereduce.spec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

Summary: %{summary}
Name: python-%{srcname}
Version: 1.7
Version: 1.8
Release: %{release}%{?dist}
Source0: %{srcname}-%{version}.tar.gz
License: MIT
Expand Down
2 changes: 1 addition & 1 deletion ruff.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
line-length = 120
# https://beta.ruff.rs/docs/rules/
select = ["A", "ARG", "BLE", "E", "F", "I", "PT", "UP"]
select = ["A", "ARG", "BLE", "E", "F", "I", "PL", "PT", "RUF", "S", "UP"]
ignore = []
53 changes: 37 additions & 16 deletions scripts/livereduce.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,41 @@
import mantid # for clearer error message
import pyinotify
from mantid.simpleapi import StartLiveData
from mantid.utils.logging import log_to_python as mtd_log_to_python
from packging.version import parse as parse_version

# ##################
# configure logging
# ##################
LOG_NAME = "livereduce" # constant for logging
LOG_FILE = "/var/log/SNS_applications/livereduce.log"
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

# mantid should let python logging do the work
mtd_log_to_python("information")
logging.getLogger("Mantid").setLevel(logging.INFO)

# create a file handler
if os.environ["USER"] == "snsdata":
handler = logging.FileHandler(LOG_FILE)
fileHandler = logging.FileHandler(LOG_FILE)
else:
handler = logging.FileHandler("livereduce.log")
handler.setLevel(logging.INFO)

# create a logging format
fileHandler = logging.FileHandler("livereduce.log")
fileHandler.setLevel(logging.INFO)
# set the logging format
logformat = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
handler.setFormatter(logging.Formatter(logformat))
fileHandler.setFormatter(logging.Formatter(logformat))

# create a stream handler
streamHandler = logging.StreamHandler()
streamHandler.setLevel(logging.INFO)
streamHandler.addFilter(logging.Filter("Mantid"))

# add the handlers to the root logger
logging.getLogger().addHandler(fileHandler)
logging.getLogger().addHandler(streamHandler)

# add the handlers to the logger
logger.addHandler(handler)

logging.getLogger(LOG_NAME).setLevel(logging.INFO)
logger = logging.getLogger(LOG_NAME)

logger.info("logging started by user '" + os.environ["USER"] + "'")
logger.info(f"using python interpreter {sys.executable}")
Expand All @@ -40,12 +53,14 @@
class LiveDataManager:
"""class for handling ``StartLiveData`` and ``MonitorLiveData``"""

logger = logger or logging.getLogger("LiveDataManager")
logger = logging.getLogger(LOG_NAME + ".LiveDataManager")

def __init__(self, config):
self.config = config

def start(self):
mtd_log_to_python("information")

liveArgs = self.config.toStartLiveArgs()
self.logger.info("StartLiveData(" + json.dumps(liveArgs, sort_keys=True, indent=2) + ")")
try:
Expand Down Expand Up @@ -106,7 +121,7 @@ class Config:

def __init__(self, filename):
r"""Optional argument is the json formatted config file"""
self.logger = logger or logging.getLogger("Config")
self.logger = logging.getLogger(LOG_NAME + ".Config")

# read file from json into a dict
self.filename = None
Expand All @@ -127,7 +142,7 @@ def __init__(self, filename):
self.logger.info(f'mantid_loc="{os.path.dirname(mantid.__file__)}"')

try:
from mantid.kernel import UsageService # noqa
from mantid.kernel import UsageService

# to differentiate from other apps
UsageService.setApplicationName("livereduce")
Expand Down Expand Up @@ -254,7 +269,7 @@ def toJson(self, **kwargs):

####################
class EventHandler(pyinotify.ProcessEvent):
logger = logger or logging.getLogger("EventHandler")
logger = logging.getLogger(LOG_NAME + ".EventHandler")

def __init__(self, config, livemanager):
# files that we actually care about
Expand All @@ -273,7 +288,12 @@ def __init__(self, config, livemanager):

def _md5(self, filename):
if filename and os.path.exists(filename):
return md5(open(filename, "rb").read()).hexdigest()
# starting in python 3.9 one can point out md5 is not used in security context
if parse_version(sys.version) < parse_version("3.9"):
md5sum = md5(open(filename, "rb").read()) # noqa: S324
else:
md5sum = md5(open(filename, "rb").read(), usedforsecurity=False)
return md5sum.hexdigest()
else:
return ""

Expand Down Expand Up @@ -325,9 +345,10 @@ def process_default(self, event):
# for passing into the eventhandler for inotify
liveDataMgr = LiveDataManager(config)

handler = EventHandler(config, LiveDataManager(config))
handler = EventHandler(config, liveDataMgr)
wm = pyinotify.WatchManager()
notifier = pyinotify.Notifier(wm, handler)

# watched events
mask = pyinotify.IN_DELETE | pyinotify.IN_MODIFY | pyinotify.IN_CREATE
logger.info(f"WATCHING:{handler.filestowatch()}")
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name="livereduce",
version="1.7",
version="1.8",
description="Need a description",
author="Pete Peterson",
author_email="petersonpf@ornl.gov",
Expand Down
3 changes: 2 additions & 1 deletion test/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
This should be a fully working example. All that you need is to add a
working version of mantid. Please note that the server and client need
to be started separately.
to be started separately and are configured to be executed with the
conda environment activated

Start Live Data Server
----------------------
Expand Down
1 change: 1 addition & 0 deletions test/fake.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"instrument": "ISIS_Histogram",
"script_dir": "test",
"update_every": 3,
"CONDA_ENV": "livereduce",
"accum_method":"Replace",
"periods":[1,3]
}

0 comments on commit ca8191b

Please sign in to comment.