def test_channels(model372): """ Test that messages logged in a channel are propagated to the main instrument. """ inst = model372 # set range to some other value so that it will actually be set in # the next call. inst.sample_heater.range_limits([0, 0.25, 0.5, 1, 2, 3, 4, 7]) inst.sample_heater.set_range_from_temperature(1) with logger.LogCapture(level=logging.DEBUG) as logs_unfiltered: inst.sample_heater.set_range_from_temperature(0.1) # reset without capturing inst.sample_heater.set_range_from_temperature(1) # rerun with instrument filter with logger.LogCapture(level=logging.DEBUG) as logs_filtered,\ logger.filter_instrument(inst, handler=logs_filtered.string_handler): inst.sample_heater.set_range_from_temperature(0.1) logs_filtered = [ l for l in logs_filtered.value.splitlines() if '[lakeshore' in l ] logs_unfiltered = [ l for l in logs_unfiltered.value.splitlines() if '[lakeshore' in l ] for f, u in zip(logs_filtered, logs_unfiltered): assert f == u
def test_handler_level(): with logger.LogCapture(level=logging.INFO) as logs: logging.debug(TEST_LOG_MESSAGE) assert logs.value == '' with logger.LogCapture(level=logging.INFO) as logs: with logger.handler_level(level=logging.DEBUG, handler=logs.string_handler): print(logs.string_handler) logging.debug(TEST_LOG_MESSAGE) assert logs.value.strip() == TEST_LOG_MESSAGE
def test_filter_instrument(AMI430_3D): driver, mag_x, mag_y, mag_z = AMI430_3D logger.start_logger() # filter one instrument driver.cartesian((0, 0, 0)) with logger.LogCapture(level=logging.DEBUG) as logs: with logger.filter_instrument(mag_x, handler=logs.string_handler): driver.cartesian((0, 0, 1)) for line in logs.value.splitlines(): assert '[x(AMI430_VISA)]' in line assert '[y(AMI430_VISA)]' not in line assert '[z(AMI430_VISA)]' not in line # filter multiple instruments driver.cartesian((0, 0, 0)) with logger.LogCapture(level=logging.DEBUG) as logs: with logger.filter_instrument((mag_x, mag_y), handler=logs.string_handler): driver.cartesian((0, 0, 1)) any_x = False any_y = False for line in logs.value.splitlines(): has_x = '[x(AMI430_VISA)]' in line has_y = '[y(AMI430_VISA)]' in line has_z = '[z(AMI430_VISA)]' in line assert has_x or has_y assert not has_z any_x |= has_x any_y |= has_y assert any_x assert any_y
def test_channels_nomessages(model372): """ Test that messages logged in a channel are not propagated to any instrument. """ inst = model372 # test with wrong instrument mock = qc.Instrument('mock') inst.sample_heater.set_range_from_temperature(1) with logger.LogCapture(level=logging.DEBUG) as logs,\ logger.filter_instrument(mock, handler=logs.string_handler): inst.sample_heater.set_range_from_temperature(0.1) logs = [l for l in logs.value.splitlines() if '[lakeshore' in l] assert len(logs) == 0 mock.close()