Skip to content

Commit 6571d03

Browse files
iandobbiecarandraug
authored andcommitted
device-server: add --logging-dir option (#267)
1 parent fdbee53 commit 6571d03

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

microscope/device_server.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import importlib.util
4141
import logging
4242
import multiprocessing
43+
import os.path
4344
import signal
4445
import sys
4546
import time
@@ -197,6 +198,7 @@ class DeviceServerOptions:
197198

198199
config_fpath: str
199200
logging_level: int
201+
logging_dir: str
200202

201203

202204
def _check_autoproxy_feature() -> None:
@@ -308,6 +310,7 @@ def run(self):
308310
# based on a unique identifier for the device. Some devices
309311
# don't have UIDs available until after initialization, so
310312
# log to stderr until then.
313+
311314
stderr_handler = StreamHandler(sys.stderr)
312315
stderr_handler.setFormatter(_create_log_formatter(cls_name))
313316
root_logger.addHandler(stderr_handler)
@@ -351,7 +354,10 @@ def run(self):
351354
pyro_daemon = Pyro4.Daemon(port=port, host=host)
352355

353356
log_handler = RotatingFileHandler(
354-
"%s_%s_%s.log" % (cls_name, host, port)
357+
os.path.join(
358+
self._options.logging_dir,
359+
"%s_%s_%s.log" % (cls_name, host, port),
360+
)
355361
)
356362
log_handler.setFormatter(_create_log_formatter(cls_name))
357363
root_logger.addHandler(log_handler)
@@ -560,6 +566,14 @@ def _parse_cmd_line_args(args: typing.Sequence[str]) -> DeviceServerOptions:
560566
choices=["debug", "info", "warning", "error", "critical"],
561567
help="Set logging level",
562568
)
569+
parser.add_argument(
570+
"--logging-dir",
571+
action="store",
572+
type=str,
573+
default="",
574+
help="Directory where log files are written to",
575+
)
576+
563577
parser.add_argument(
564578
"config_fpath",
565579
action="store",
@@ -571,6 +585,7 @@ def _parse_cmd_line_args(args: typing.Sequence[str]) -> DeviceServerOptions:
571585
return DeviceServerOptions(
572586
config_fpath=parsed.config_fpath,
573587
logging_level=getattr(logging, parsed.logging_level.upper()),
588+
logging_dir=parsed.logging_dir,
574589
)
575590

576591

microscope/testsuite/test_device_server.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ def setUp(self):
112112
options = microscope.device_server.DeviceServerOptions(
113113
config_fpath="",
114114
logging_level=logging.INFO,
115+
logging_dir="",
115116
)
116117
self.p = multiprocessing.Process(
117118
target=microscope.device_server.serve_devices,
@@ -274,6 +275,7 @@ class TestServingFloatingDevicesWithWrongUID(BaseTestDeviceServer):
274275
microscope.device_server.DeviceServerOptions(
275276
config_fpath="",
276277
logging_level=logging.INFO,
278+
logging_dir="",
277279
),
278280
{"bar": "127.0.0.1"},
279281
{"bar": 8001},
@@ -307,6 +309,7 @@ class TestFunctionInDeviceDefinition(BaseTestDeviceServer):
307309
microscope.device_server.DeviceServerOptions(
308310
config_fpath="",
309311
logging_level=logging.INFO,
312+
logging_dir="",
310313
),
311314
{},
312315
{},

0 commit comments

Comments
 (0)