From dfc3f0a54026d48d3a4568b5a6c2e22f5dd7d2c2 Mon Sep 17 00:00:00 2001 From: CPD Date: Mon, 10 Mar 2025 11:47:01 +0100 Subject: [PATCH] Add logging --- cpdctrl/measurement.py | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/cpdctrl/measurement.py b/cpdctrl/measurement.py index 2d3da9d..21314e1 100644 --- a/cpdctrl/measurement.py +++ b/cpdctrl/measurement.py @@ -13,6 +13,9 @@ import time import datetime from queue import Queue +import logging +log = logging.getLogger(__name__) + def measure( vm_dev: VoltageMeasurementDevice, led_dev: LedControlDevice, @@ -91,13 +94,17 @@ def measure( i = 0 led_val = led_script.start() t_iter_start = time.time() - while max_measurements is None or i < max_measurements: + while True: + # using while True and if, to be able to log the stop reason + if max_measurements is not None and i >= max_measurements: + log.info(f"Reached maximum number of measurements ({i}{max_measurements}), stopping measurement") + break # 1) read value(s) if use_buffer: try: values = vm_dev.buffer_read_new_values() except ValueError as e: - print(f"Error in buffer measurement {i}:", e) + # print(f"Error in buffer measurement {i}:", e) values = [] else: values = [vm_dev.read_value()] @@ -110,7 +117,7 @@ def measure( current_data = (i, tval, vval, led_val) data.add_data(*current_data) # 3) write data - print(f"n = {i:6d}, t = {tval: .2f} s, U = {vval: .5f} V, LED = {led_val:03}%" + " "*10, end='\r') + if verbose: print(f"n = {i:6d}, t = {tval: .2f} s, U = {vval: .5f} V, LED = {led_val:03}%" + " "*10, end='\r') if flush_after is not None and (i+1) % flush_after == 0: data.flush(verbose=verbose) # if a queue was given, put the data @@ -122,15 +129,16 @@ def measure( if command_queue is not None and command_queue.qsize() > 0: recv = command_queue.get(block=False) if recv == "stop": + log.info(f"Received 'stop', stopping measuremetn") break elif type(recv) == tuple and recv[0] == "led_script": led_script = recv[1] else: - print(f"Received invalid message: '{recv}'") + log.error(f"Received invalid message: '{recv}'") # 4) sleep - # substract the execution time from the sleep time for a more - # acurate frequency + # subtract the execution time from the sleep time for a more + # accurate frequency dt_sleep = delta_t - (time.time() - t_iter_start) if dt_sleep > 0: # print(f"Sleeping for {dt_sleep}") @@ -138,7 +146,7 @@ def measure( t_iter_start = time.time() # 5) update LED if stop_on_script_end and led_script.is_done(t_iter_start): - if verbose: print("Reached script end") + log.info("Reached led script end, stopping measurement") break new_led_val = led_script.get_state(t_iter_start) if new_led_val != led_val: @@ -146,11 +154,12 @@ def measure( led_dev.set_level(new_led_val) led_val = new_led_val except Exception as e: - print(f"Error setting led to {new_led_val}%:") - print(e) + log.error("Error setting led to {new_led_val}%: {e}") except KeyboardInterrupt: - pass + log.info("Keyboard interrupt, stopping") + except Exception as e: + log.critical(f"Unexpected error, stopping. Error: {e}") if add_measurement_info_to_metadata: data.metadata["measurement_time_stop"] = get_time() # Write again after having updated the stop time