Skip to content

Commit

Permalink
Improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
kmnhan committed Jun 13, 2024
1 parent 7e7ef60 commit 906dec2
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 51 deletions.
11 changes: 11 additions & 0 deletions src/daq/attributeserver/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import logging
import sys

log = logging.getLogger("attrs")
log.setLevel(logging.DEBUG)
handler = logging.StreamHandler(sys.stdout)
# handler = logging.FileHandler(f"D:/daq_logs/{log.name}.log", mode="a", encoding="utf-8")
handler.setFormatter(
logging.Formatter("%(asctime)s | %(name)s | %(levelname)s - %(message)s")
)
log.addHandler(handler)
44 changes: 23 additions & 21 deletions src/daq/attributeserver/getter.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
"""Loads and parses attributes from shared memory."""

import datetime
import logging
from multiprocessing import shared_memory
from typing import Any

import numpy as np

log = logging.getLogger("attrs")

SLIT_TABLE: tuple[tuple[int, float, bool], ...] = (
(100, 0.05, False),
(200, 0.1, False),
Expand Down Expand Up @@ -75,6 +78,20 @@ def get_pressure_list() -> list[str]:
]


def get_position_list() -> list[str]:
return [str(np.round(v, 4)) for v in get_shared_array("MotorPositions", 6, "f8")]


def get_temperature_list() -> list[str]:
return [
str(v) for v in get_shared_array("Temperatures", len(TEMPERATURE_KEYS), "f8")
]


def get_seqstart() -> datetime.datetime:
return datetime.datetime.fromtimestamp(get_shared_float("seq_start"))


def get_pressure_dict() -> dict[str, str]:
return dict(
zip(
Expand All @@ -85,24 +102,18 @@ def get_pressure_dict() -> dict[str, str]:
)


def get_position_list() -> list[str]:
return [str(np.round(v, 4)) for v in get_shared_array("MotorPositions", 6, "f8")]


def get_position_dict() -> dict[str, str]:
return dict(zip(MANIPULATOR_AXES, get_position_list(), strict=True))


def get_temperature_list() -> list[str]:
return [
str(v) for v in get_shared_array("Temperatures", len(TEMPERATURE_KEYS), "f8")
]


def get_temperature_dict() -> dict[str, str]:
return dict(zip(TEMPERATURE_KEYS, get_temperature_list(), strict=True))


def get_seqstart_dict() -> dict[str, str]:
return {"seq_start": get_seqstart().isoformat()}


def get_slit_dict() -> dict[str, str]:
idx = get_shared_int("slit_idx")
return {
Expand All @@ -112,14 +123,6 @@ def get_slit_dict() -> dict[str, str]:
}


def get_seqstart() -> datetime.datetime:
return datetime.datetime.fromtimestamp(get_shared_float("seq_start"))


def get_seqstart_dict() -> dict[str, str]:
return {"seq_start": get_seqstart().isoformat()}


def get_attribute_dict() -> dict[str, str]:
attrs = {"attrs_time": datetime.datetime.now().isoformat()}
for fn in (
Expand All @@ -131,9 +134,8 @@ def get_attribute_dict() -> dict[str, str]:
):
try:
d = fn()
except FileNotFoundError:
pass
# print(f"Getting attribute from shared memory failed with error {e}")
except Exception:
log.exception("Getting attribute from shared memory failed")
else:
attrs |= d
return attrs
Expand Down
21 changes: 14 additions & 7 deletions src/daq/attributeserver/server.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import threading
import time
from multiprocessing import shared_memory
Expand All @@ -7,11 +8,11 @@

from attributeserver.getter import get_header

log = logging.getLogger("attrs")


class AttributeServer(QtCore.QThread):
PORT = 5556
sigSocketBound = QtCore.Signal()
sigSocketClosed = QtCore.Signal()

def __init__(self):
super().__init__()
Expand All @@ -25,27 +26,33 @@ def run(self):
self.mutex = QtCore.QMutex()
self.stopped.clear()

# Initialize shared memory
self.shm_slit = shared_memory.SharedMemory(name="slit_idx", create=True, size=1)
log.debug("Shared memory slit_idx created")

self.shm_seq = shared_memory.SharedMemory(name="seq_start", create=True, size=8)
log.debug("Shared memory seq_start created")

context = zmq.Context.instance()
if not context:
context = zmq.Context()
socket: zmq.Socket = context.socket(zmq.PUB)
socket.bind(f"tcp://*:{self.PORT}")
self.sigSocketBound.emit()
log.info(f"Attribute server started on TCP port {self.PORT}")

# Broadcast header over socket
while not self.stopped.is_set():
socket.send_string(get_header())
time.sleep(0.005)
time.sleep(0.01)

log.info("Attribute server stopped")

# Remove shared memory
self.shm_slit.close()
self.shm_slit.unlink()
log.debug("Shared memory slit_idx unlinked")

self.shm_seq.close()
self.shm_seq.unlink()
log.debug("Shared memory shm_seq unlinked")

socket.close()
self.sigSocketClosed.emit()
log.debug("Attribute server socket closed")
26 changes: 19 additions & 7 deletions src/daq/attributeserver/widgets.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import os
import sys
import threading
Expand All @@ -14,6 +15,8 @@
except: # noqa: E722
pass

log = logging.getLogger("attrs")


class SlitTableModel(QtCore.QAbstractTableModel):
def __init__(self, *args, **kwargs):
Expand Down Expand Up @@ -88,28 +91,38 @@ def __init__(self) -> None:
def run(self):
self.stopped.clear()

log.info("Status thread started")

while not self.stopped.is_set():
try:
temp = [str(float(v)) for v in get_temperature_list()]
except FileNotFoundError:
# Shared memory not present, temperature controller may be off
log.exception(
"Shared memory not found, check temperature control software"
)
temp = [""] * 3
except ValueError as e:
# Something went wrong while reading shared memory, try again
print(f"Error while reading shared temperature: {e}")
except ValueError:
log.exception("Error while reading temperature from shared memory")
time.sleep(0.5)
continue

self.sigTUpdate.emit(temp)

try:
pressure: list[str] = get_pressure_list()
except FileNotFoundError:
log.exception("Shared memory not found, check mg15 software")
pressure: list[str] = [""]
except ValueError as e:
print(f"Error while reading shared pressure: {e}")
except ValueError:
log.exception("Error while reading pressure from shared memory")
time.sleep(0.5)
continue

self.sigPUpdate.emit(pressure)
time.sleep(0.1)

log.info("Status thread stopped")


class StatusWidget(*uic.loadUiType("attributeserver/status.ui")):
def __init__(self):
Expand All @@ -121,7 +134,6 @@ def __init__(self):
self.attr_server.start()

self.update_thread = StatusThread()

self.update_thread.sigTUpdate.connect(self.update_temperature)
self.update_thread.sigPUpdate.connect(self.update_pressure)
self.update_thread.start()
Expand Down
14 changes: 13 additions & 1 deletion src/daq/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import multiprocessing
import os
import sys
Expand All @@ -12,6 +13,15 @@
except: # noqa: E722
pass

log = logging.getLogger("main")
log.setLevel(logging.DEBUG)
handler = logging.StreamHandler(sys.stdout)
# handler = logging.FileHandler(f"D:/daq_logs/{log.name}.log", mode="a", encoding="utf-8")
handler.setFormatter(
logging.Formatter("%(asctime)s | %(name)s | %(levelname)s - %(message)s")
)
log.addHandler(handler)


class QHLine(QtWidgets.QFrame):
def __init__(self, *args, **kwargs):
Expand Down Expand Up @@ -52,7 +62,7 @@ def closeEvent(self, event: QtGui.QCloseEvent):
f"Remaining threads: {threadpool.activeThreadCount()}",
)

print("Proper Termination Achieved! Yay!")
log.info("All threads properly terminated! Yay!")
super().closeEvent(event)


Expand All @@ -69,6 +79,8 @@ def closeEvent(self, event: QtGui.QCloseEvent):

if __name__ == "__main__":
multiprocessing.freeze_support()

log.info("Starting application")
qapp = QtWidgets.QApplication(sys.argv)
qapp.setStyle("Fusion")
win = MainWindow()
Expand Down
7 changes: 2 additions & 5 deletions src/daq/sescontrol/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@

log = logging.getLogger("scan")

SES_DIR = os.getenv(
"SES_BASE_PATH", "D:/SES_1.9.6_Win64"
) #: The directory where SES is installed
TEMPFILE_PREFIX: str = "_tmp_" #: Prefix to use for working files


Expand Down Expand Up @@ -306,7 +303,7 @@ def run(self):
else:
for i, ax in enumerate(self.motors):
ax.pre_motion()
log.debug(f"Pre-motion for axis {i + 1} complete, checking bounds")
log.debug(f"Pre-motion for axis {i+1} complete, checking bounds")

# Last sanity check of bounds before motion start
if (ax.minimum is not None and min(self.array[:, i]) < ax.minimum) or (
Expand All @@ -322,7 +319,7 @@ def run(self):

for i, ax in enumerate(self.motors):
ax.post_motion()
log.debug(f"Post-motion for axis {i + 1} complete")
log.debug(f"Post-motion for axis {i+1} complete")

# Restore mangled filenames
self._restore_filenames()
Expand Down
4 changes: 3 additions & 1 deletion src/daq/sescontrol/ses_win.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""Functions that use the Windows API to control SES.exe windows and menus."""

import glob
import logging
import os
import sys
from collections.abc import Callable, Iterable, Sequence
from collections.abc import Callable, Iterable

sys.coinit_flags = 2

Expand All @@ -18,6 +19,7 @@
import win32gui

SES_DIR = os.getenv("SES_BASE_PATH", "D:/SES_1.9.6_Win64")
log = logging.getLogger("scan")


def get_ses_proc() -> psutil.Process:
Expand Down
Loading

0 comments on commit 906dec2

Please sign in to comment.