File: soapy_log_handle.py

package info (click to toggle)
soapysdr 0.8.1-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 948 kB
  • sloc: cpp: 5,378; ansic: 471; python: 311; sh: 21; makefile: 18
file content (41 lines) | stat: -rw-r--r-- 1,285 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""Example Python basing logging for SoapySDR.

This permits exceptions to be thrown if the log message is severe enough.
"""
from __future__ import print_function

import sys
import SoapySDR
from SoapySDR import * #SOAPY_SDR_ constants

class SoapyException(Exception):
    """SoapySDR has logged an error message (or worse)."""

def set_python_log_handler(exception_level=None):
    """Replace the default SoapySDR log handler with a Python one.

    The python handler sends the log text to stderr.

    If the log_level is at exception_level or worse then a SoapyException
    is thrown.
    """

    log_level_text = {
        SOAPY_SDR_FATAL: "FATAL",
        SOAPY_SDR_CRITICAL: "CRITICAL",
        SOAPY_SDR_ERROR: "ERROR",
        SOAPY_SDR_WARNING: "WARNING",
        SOAPY_SDR_NOTICE: "NOTICE",
        SOAPY_SDR_INFO: "INFO",
        SOAPY_SDR_DEBUG: "DEBUG",
        SOAPY_SDR_TRACE: "TRACE",
        SOAPY_SDR_SSI: "SSI"}

    def log_handler(log_level, message):
        level_text = log_level_text[log_level]
        log_text = "[{}] {}".format(level_text, message)
        print(log_text, file=sys.stderr)
        if exception_level is not None and log_level <= exception_level:
            raise SoapyException(log_text)

    SoapySDR.registerLogHandler(log_handler)