Add logging

This commit is contained in:
CPD 2025-03-10 11:47:01 +01:00
parent 63f082831c
commit dfc3f0a540

View File

@ -13,6 +13,9 @@ import time
import datetime import datetime
from queue import Queue from queue import Queue
import logging
log = logging.getLogger(__name__)
def measure( def measure(
vm_dev: VoltageMeasurementDevice, vm_dev: VoltageMeasurementDevice,
led_dev: LedControlDevice, led_dev: LedControlDevice,
@ -91,13 +94,17 @@ def measure(
i = 0 i = 0
led_val = led_script.start() led_val = led_script.start()
t_iter_start = time.time() 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) # 1) read value(s)
if use_buffer: if use_buffer:
try: try:
values = vm_dev.buffer_read_new_values() values = vm_dev.buffer_read_new_values()
except ValueError as e: except ValueError as e:
print(f"Error in buffer measurement {i}:", e) # print(f"Error in buffer measurement {i}:", e)
values = [] values = []
else: else:
values = [vm_dev.read_value()] values = [vm_dev.read_value()]
@ -110,7 +117,7 @@ def measure(
current_data = (i, tval, vval, led_val) current_data = (i, tval, vval, led_val)
data.add_data(*current_data) data.add_data(*current_data)
# 3) write 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: if flush_after is not None and (i+1) % flush_after == 0:
data.flush(verbose=verbose) data.flush(verbose=verbose)
# if a queue was given, put the data # 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: if command_queue is not None and command_queue.qsize() > 0:
recv = command_queue.get(block=False) recv = command_queue.get(block=False)
if recv == "stop": if recv == "stop":
log.info(f"Received 'stop', stopping measuremetn")
break break
elif type(recv) == tuple and recv[0] == "led_script": elif type(recv) == tuple and recv[0] == "led_script":
led_script = recv[1] led_script = recv[1]
else: else:
print(f"Received invalid message: '{recv}'") log.error(f"Received invalid message: '{recv}'")
# 4) sleep # 4) sleep
# substract the execution time from the sleep time for a more # subtract the execution time from the sleep time for a more
# acurate frequency # accurate frequency
dt_sleep = delta_t - (time.time() - t_iter_start) dt_sleep = delta_t - (time.time() - t_iter_start)
if dt_sleep > 0: if dt_sleep > 0:
# print(f"Sleeping for {dt_sleep}") # print(f"Sleeping for {dt_sleep}")
@ -138,7 +146,7 @@ def measure(
t_iter_start = time.time() t_iter_start = time.time()
# 5) update LED # 5) update LED
if stop_on_script_end and led_script.is_done(t_iter_start): 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 break
new_led_val = led_script.get_state(t_iter_start) new_led_val = led_script.get_state(t_iter_start)
if new_led_val != led_val: if new_led_val != led_val:
@ -146,11 +154,12 @@ def measure(
led_dev.set_level(new_led_val) led_dev.set_level(new_led_val)
led_val = new_led_val led_val = new_led_val
except Exception as e: except Exception as e:
print(f"Error setting led to {new_led_val}%:") log.error("Error setting led to {new_led_val}%: {e}")
print(e)
except KeyboardInterrupt: 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: if add_measurement_info_to_metadata:
data.metadata["measurement_time_stop"] = get_time() data.metadata["measurement_time_stop"] = get_time()
# Write again after having updated the stop time # Write again after having updated the stop time