Skip to main content

Logging

libstb tells you what it decided on every call, when you ask: which SIMD backend ran, how many threads, the sizes of what it decoded. It does so through a logger named libstb, with children such as libstb.decode, libstb.resize and libstb.simd. Nothing is printed unless a level lets the messages through. Warnings and worse reach stderr by default.

import logging
import sys

import libstb as stb


class Collect(logging.Handler):
"""Keeps the name of the logger of every record it gets."""

def __init__(self):
super().__init__()
self.names = []

def emit(self, record):
self.names.append(record.name)


# libstb logs through Python's logging, to a logger named "libstb" with children such as "libstb.decode".
# It is silent unless a level lets its messages through.
collect = Collect()
library = logging.getLogger("libstb")
library.setLevel(logging.DEBUG)
library.addHandler(collect)

img = stb.Image.open("astronaut.png").resize(3000, 2000)
print("loggers heard:", ", ".join(sorted(set(collect.names))))

# A level silences it again.
heard = len(collect.names)
library.setLevel(logging.CRITICAL)
img.resize(750, 500)
print("new records at level critical:", len(collect.names) - heard)

# Your own messages go through the same machinery.
mine = logging.getLogger("myapp")
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(logging.Formatter("%(levelname)s %(name)s: %(message)s"))
mine.addHandler(handler)
mine.setLevel(logging.INFO)
mine.info("done")
Output
loggers heard: libstb.decode, libstb.resize
new records at level critical: 0
INFO myapp: done

In Python​

The logger is a plain logging.getLogger("libstb"). Everything you know about the standard logging module applies: levels, handlers, formatters, filters, basicConfig, a handler that writes a file. Importing libstb connects the C++ messages to it. stb.logging_bridge(False) disconnects them, and then libstb goes back to its own output (the LIBSTB_LOG variable and stderr), and the C++ side does not even format the messages.

In C++​

Switch to C++ in the top bar for the C++ side of the same design.

Levels​

From the quietest to the loudest: debug, info, warning, error, critical. A logger passes a message whose level is at or above its own. libstb logs what it decided at debug, so set debug on libstb to see it, and critical to hear nothing.

Your own messages​

The myapp logger in the example is yours and not libstb's. It goes through the same machinery, so one handler and one format can serve your code and the library's.

API at a glance​

The library loggerlogging.getLogger("libstb")
A levellogger.setLevel(logging.DEBUG)
A handlerlogger.addHandler(handler)
Every record to a functiona logging.Handler with emit
A formathandler.setFormatter(logging.Formatter(...))
Disconnect the bridgestb.logging_bridge(False)