OptrisOptrisDocumentation

Documentation/OTC SDK/optris_sdk — Python API referencev12.0

log — functions

Taking the SDK's own log lines into your program's log.

The SDK logs what the camera path is doing -- calibration fetches, flag cycles, USB re-enumeration, NUC, reconnects. When a customer sends a diagnostics bundle those lines are usually the only record of what happened, and without a handler installed they go nowhere a Python application can see.

optris_sdk.log is not logging, and deliberately is not: the handler is a plain callable, so this package keeps no opinion about which logging framework an application uses. Adapting it to a logging.Logger is three lines and to_logger is them.

The handler is process-wide. There is one, setting a second replaces the first, and lines are not attributed to a camera.

Threading, which is where this callback differs from every other one in the package

optris_sdk.callbacks says callbacks arrive on an SDK thread, never on the thread that registered them. A log handler is the exception, because logging happens wherever the SDK happens to be running:

  • Any thread, including yours. A call you make that logs before it returns delivers the message on your thread, inside your call.
  • Possibly two at once. The SDK holds no lock across the handler on purpose -- serialising delivery would put every logging thread behind the slowest handler, and one that blocked would stall the frame path. So the handler must be safe to call concurrently. Most logging frameworks already are; a bare open() file object is not.
  • Promptly. The thread that logged waits for it. Queue the line; do not write to a socket from inside it.
  • Nothing may escape. An exception out of the handler is caught and discarded here before it can reach the SDK's frame. The line is lost and the SDK carries on. Do not rely on it -- catch your own.
  • Do not call set_handler or clear_handler from inside the handler. Both wait for it to return, so it would wait for itself. The SDK refuses rather than deadlocking, which arrives here as an optris_sdk.errors.InvalidArgumentError.
  • A nested line is dropped. If the handler logs -- directly, or by calling an SDK function that logs -- that message is not delivered back into it.
  • It is cleared at interpreter exit, after every camera still open has been closed, so the lines those closes log still arrive. The SDK goes on logging after that, and it cannot call into an interpreter that is shutting down; see optris_sdk._exit.

level_name(level)

A log level as a word, for printing.

set_handler(handler, minimum = LOG_INFO)

Send the SDK's log lines to handler(level, message).

level is one of the LOG_* values and message is a str with no level prefix, no timestamp and no trailing newline -- so a caller formats it the way its own log does.

minimum drops anything less severe before the handler is called, in the SDK, so a LOG_WARNING floor costs nothing per debug line.

handler=None is the same as clear_handler.

Read this module's docstring first: the threading rules for this one callback are not the ones in optris_sdk.callbacks. Printing is enough to see it work::

optris_sdk.log.set_handler(
    lambda level, message: print(
        f'[optris {optris_sdk.log.level_name(level)}] {message}'))

clear_handler()

Stop taking log lines.

When this returns the previous handler has finished and cannot be called again, so whatever it captured is safe to release. That is what makes dropping the ctypes thunk here sound rather than a guess.

Clearing when nothing is installed succeeds and does nothing.

has_handler()

Whether a log handler is installed.

Asks the SDK rather than reporting what this module last did, so it is also true of a handler installed by another binding or by C code in the same process -- the registration is one per process, not one per language.

handler()

The callable set_handler was last given, or None.

This module's own record, which is why has_handler exists beside it: that one asks the SDK, and the two disagree exactly when something outside this package installed a handler.

to_logger(logger)

A handler that forwards into a logging logger.

The three lines this package would otherwise make every caller write::

import logging
optris_sdk.log.set_handler(
    optris_sdk.log.to_logger(logging.getLogger('optris')))

LOG_TRACE maps to level 5, below DEBUG, because logging has no trace level; the other four map to their obvious counterparts.

logging handlers are thread-safe, which is what makes this legal under the concurrency rule in this module's docstring.