Skip to content

Commit 668ca9b

Browse files
authored
Merge pull request #22 from MrClock8163/feature/logging-improvements
Logging improvements
2 parents 6c2f9a3 + f16f8d7 commit 668ca9b

File tree

17 files changed

+790
-373
lines changed

17 files changed

+790
-373
lines changed

docs/about.rst

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
Information
22
===========
33

4+
Documentation
5+
-------------
6+
7+
The different commands are documented in their application groups to give
8+
a better overview of the processes. Every page shows the actual command to
9+
invoke the program, some additional context and optional examples, and an
10+
automatically generated view of the ``--help`` page of the command.
11+
412
Command Structure
513
-----------------
614

@@ -21,10 +29,19 @@ commands:
2129
iman validate sets -h
2230
iman calc sets -h
2331
24-
Documentation
25-
-------------
32+
Logging
33+
-------
2634

27-
The different commands are documented in their application groups to give
28-
a better overview of the processes. Every page shows the actual command to
29-
invoke the program, some additional context and optional examples, and an
30-
automatically generated view of the ``--help`` page of the command.
35+
Various commands (primarily the measurement programs) support logging. The
36+
logging can be set up through the logging options of the root command ``iman``.
37+
These have to be set before specifying the command groups.
38+
39+
.. code-block:: shell
40+
41+
iman --info --file iman.log measure ...
42+
43+
Usage
44+
-----
45+
46+
.. click:: instrumentman:cli
47+
:prog: iman

docs/commands/sets/measure.rst

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,6 @@ controlling computer (e.g. Task Scheduler on Windows, crontab on Linux).
5050
Examples
5151
--------
5252

53-
.. code-block:: shell
54-
:caption: Logging to file
55-
56-
iman measure sets --debug COM3 targets.json results >> tps.log 2>&1
57-
5853
.. code-block:: shell
5954
:caption: Enabling connection retries and timeout recovery attempts (might be useful with bluetooth connections)
6055

src/instrumentman/__init__.py

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1+
from pathlib import Path
2+
13
from click_extra import extra_group, version_option
24

35
try:
46
from ._version import __version__ as __version__
57
except Exception:
68
__version__ = "0.0.0" # Placeholder value for source installs
79

10+
from .utils import (
11+
logging_option_group,
12+
logging_levels_constraint,
13+
logging_output_constraint,
14+
logging_target_constraint,
15+
logging_rotation_constraint,
16+
configure_logging
17+
)
818
from . import morse
919
from . import terminal
1020
from . import setup
@@ -18,12 +28,47 @@
1828
from . import settings
1929

2030

21-
@extra_group("iman", params=None) # type: ignore[misc]
31+
@extra_group(
32+
"iman",
33+
params=None,
34+
context_settings={"auto_envvar_prefix": None}
35+
) # type: ignore[misc]
2236
@version_option()
23-
def cli() -> None:
37+
@logging_option_group()
38+
@logging_levels_constraint()
39+
@logging_output_constraint()
40+
@logging_target_constraint()
41+
@logging_rotation_constraint()
42+
def cli(
43+
protocol: bool = False,
44+
debug: bool = False,
45+
info: bool = False,
46+
warning: bool = False,
47+
error: bool = False,
48+
critical: bool = False,
49+
file: Path | None = None,
50+
stdout: bool = False,
51+
stderr: bool = False,
52+
format: str = "{message}",
53+
dateformat: str = "%Y-%m-%d %H:%M:%S",
54+
rotate: tuple[int, int] | None = None
55+
) -> None:
2456
"""Automated measurement programs and related utilities for surveying
2557
instruments."""
26-
pass
58+
configure_logging(
59+
protocol,
60+
debug,
61+
info,
62+
warning,
63+
error,
64+
critical,
65+
file,
66+
stderr,
67+
stdout,
68+
format,
69+
dateformat,
70+
rotate
71+
)
2772

2873

2974
@cli.group("measure") # type: ignore[misc]

src/instrumentman/datatransfer/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
default=True
4242
)
4343
@option(
44-
"--inclide-eof/--no-include-eof",
44+
"--include-eof/--no-include-eof",
4545
help=(
4646
"wether the EOF marker is part of the output format "
4747
"(or just sent by the instrument regardless of the format in question)"

src/instrumentman/datatransfer/app.py

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from io import BufferedWriter, TextIOWrapper
2+
from logging import getLogger
23

34
from serial import SerialTimeoutException
45
from rich.progress import Progress, TextColumn
@@ -18,39 +19,50 @@ def main_download(
1819
include_eof: bool = False
1920
) -> None:
2021
eof_bytes = eof.encode("ascii")
22+
logger = getLogger("iman.data.download")
2123
with open_serial(
2224
port,
2325
speed=baud,
24-
timeout=timeout
26+
timeout=timeout,
27+
logger=logger.getChild("com")
2528
) as com:
2629
eol_bytes = com.eombytes
2730
started = False
31+
logger.info("Starting data download")
32+
logger.debug("Waiting for first line...")
2833
while True:
2934
try:
3035
data = com.receive_binary()
31-
started = True
36+
if not started:
37+
started = True
38+
logger.debug("Received first line...")
3239

3340
if data == eof_bytes and autoclose and not include_eof:
3441
echo_green("Download finished (end-of-file)")
35-
return
42+
logger.info("Download finished (end-of-file)")
43+
break
3644

3745
echo(data.decode("ascii", "replace"))
3846
if output is not None:
3947
output.write(data + eol_bytes)
4048

4149
if data == eof_bytes and autoclose:
4250
echo_green("Download finished (end-of-file)")
43-
return
51+
logger.info("Download finished (end-of-file)")
52+
break
4453
except SerialTimeoutException:
4554
if started and autoclose:
4655
echo_green("Download finished (timeout)")
47-
return
56+
logger.info("Download finished (timeout)")
57+
break
4858
except KeyboardInterrupt:
4959
echo_yellow("Download stopped manually")
50-
return
60+
logger.info("Download stopped manually")
61+
break
5162
except Exception as e:
5263
echo_red(f"Download interrupted by error ({e})")
53-
return
64+
logger.exception("Download interrupted by error")
65+
break
5466

5567

5668
def main_upload(
@@ -60,12 +72,16 @@ def main_upload(
6072
timeout: int = 15,
6173
skip: int = 0
6274
) -> None:
75+
logger = getLogger("iman.data.upload")
6376
with open_serial(
6477
port,
6578
speed=baud,
66-
timeout=timeout
79+
timeout=timeout,
80+
logger=logger.getChild("com")
6781
) as com:
6882
try:
83+
logger.info("Starting data upload")
84+
logger.debug(f"Skipping {skip} line(s)")
6985
for _ in range(skip):
7086
next(file)
7187

@@ -76,8 +92,12 @@ def main_upload(
7692
for line in progress.track(file, description="Uploading..."):
7793
com.send(line)
7894

95+
except KeyboardInterrupt:
96+
echo_yellow("Upload cancelled")
97+
logger.info("Upload cancelled by user")
7998
except Exception as e:
8099
echo_red(f"Upload interrupted by error ({e})")
81-
return
82-
83-
echo_green("Upload finished")
100+
logger.exception("Upload interrupted by error")
101+
else:
102+
echo_green("Upload finished")
103+
logger.info("Upload finished")

0 commit comments

Comments
 (0)