1"""Example Python basing logging for SoapySDR. 2 3This permits exceptions to be thrown if the log message is severe enough. 4""" 5from __future__ import print_function 6 7import sys 8import SoapySDR 9from SoapySDR import * #SOAPY_SDR_ constants 10 11class SoapyException(Exception): 12 """SoapySDR has logged an error message (or worse).""" 13 14def set_python_log_handler(exception_level=None): 15 """Replace the default SoapySDR log handler with a Python one. 16 17 The python handler sends the log text to stderr. 18 19 If the log_level is at exception_level or worse then a SoapyException 20 is thrown. 21 """ 22 23 log_level_text = { 24 SOAPY_SDR_FATAL: "FATAL", 25 SOAPY_SDR_CRITICAL: "CRITICAL", 26 SOAPY_SDR_ERROR: "ERROR", 27 SOAPY_SDR_WARNING: "WARNING", 28 SOAPY_SDR_NOTICE: "NOTICE", 29 SOAPY_SDR_INFO: "INFO", 30 SOAPY_SDR_DEBUG: "DEBUG", 31 SOAPY_SDR_TRACE: "TRACE", 32 SOAPY_SDR_SSI: "SSI"} 33 34 def log_handler(log_level, message): 35 level_text = log_level_text[log_level] 36 log_text = "[{}] {}".format(level_text, message) 37 print(log_text, file=sys.stderr) 38 if exception_level is not None and log_level <= exception_level: 39 raise SoapyException(log_text) 40 41 SoapySDR.registerLogHandler(log_handler) 42