Skip to content

Commit

Permalink
Merge pull request #6675 from jenshnielsen/better_logging
Browse files Browse the repository at this point in the history
Small improvements to Parameter and Instrument logging
  • Loading branch information
jenshnielsen authored Nov 29, 2024
2 parents f8cf8cd + 6380dc8 commit 1280f4a
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/qcodes/instrument/instrument_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def __init__(
self._meta_attrs = ["name", "label"]

self.log: InstrumentLoggerAdapter = get_instrument_logger(self, __name__)
self.log.debug("Created instrument: %s", self.full_name)

@property
def label(self) -> str:
Expand Down
12 changes: 12 additions & 0 deletions src/qcodes/parameters/delegate_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,12 @@ def settable(self) -> bool:
return self.source.settable

def get_raw(self) -> Any:
logger = self._get_logger()
logger.debug(
"Calling get on DelegateParameter %s with source %s",
self.full_name,
self.source,
)
if self.source is None:
raise TypeError(
"Cannot get the value of a DelegateParameter "
Expand All @@ -267,6 +273,12 @@ def get_raw(self) -> Any:
return self.source.get()

def set_raw(self, value: Any) -> None:
logger = self._get_logger()
logger.debug(
"Calling set on DelegateParameter %s with source %s",
self.full_name,
self.source,
)
if self.source is None:
raise TypeError(
"Cannot set the value of a DelegateParameter "
Expand Down
7 changes: 1 addition & 6 deletions src/qcodes/parameters/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,7 @@ def _get_manual_parameter(self: Parameter) -> ParamRawDataType:
def _set_manual_parameter(
self: Parameter, x: ParamRawDataType
) -> ParamRawDataType:
if self.root_instrument is not None:
mylogger: InstrumentLoggerAdapter | logging.Logger = (
self.root_instrument.log
)
else:
mylogger = log
mylogger = self._get_logger()
mylogger.debug(
"Setting raw value of parameter: %s to %s", self.full_name, x
)
Expand Down
11 changes: 11 additions & 0 deletions src/qcodes/parameters/parameter_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from types import TracebackType

from qcodes.instrument.base import InstrumentBase
from qcodes.logger.instrument_logger import InstrumentLoggerAdapter

LOG = logging.getLogger(__name__)

Expand Down Expand Up @@ -355,6 +356,16 @@ def _implements_set_raw(self) -> bool:
)
return implements_set_raw

def _get_logger(self) -> InstrumentLoggerAdapter | logging.Logger:
if self.root_instrument is not None:
mylogger: InstrumentLoggerAdapter | logging.Logger = (
self.root_instrument.log
)
else:
mylogger = LOG

return mylogger

def _build__doc__(self) -> str | None:
return self.__doc__

Expand Down
1 change: 1 addition & 0 deletions tests/parameter/test_parameter_override.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ def test_remove_parameter_from_class_attr_works(request, caplog):
# does not alter the class attribute
assert hasattr(a, "frequency")
assert a.frequency is None
caplog.clear()
with caplog.at_level(logging.WARNING):
a.remove_parameter("frequency")
assert (
Expand Down

0 comments on commit 1280f4a

Please sign in to comment.