OptrisOptrisDocumentation

Documentation/OTC SDK/Overviewv12.0

Logging

Overview

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 the hardware was doing when the problem happened, so an application that keeps its own log wants them in it.

Without a handler, the SDK writes its log to standard error, never to standard output, from the first line it logs: with a real camera, the simulated camera, a recording or anything else. A handler takes it instead; see The handler replaces the SDK's own output.

One handler per process, not one per camera. Setting a second replaces the first.

This is the only callback in the SDK that can arrive on the caller's own thread, which is why Threading is a section rather than a sentence.

SetLogHandler

optris::sdk::SetLogHandler(
    [](optris::sdk::LogLevel level, const std::string &message) {
      std::println("[optris] {}: {}", static_cast<int>(level), message);
    },
    optris::sdk::LogLevel::kInfo);

The message is the text only: no level prefix, no timestamp, no trailing newline. Add whatever your own log format wants. It is borrowed and is not retained after the call returns.

The second argument is the floor, and it filters inside the SDK, so a Warning floor costs nothing per debug line. It defaults to Info, which is what is worth having in a diagnostics bundle; Debug and Trace are for reproducing a fault.

LogLevel

Level Value What it is
Trace 0 Very fine detail. Off in a normal build.
Debug 1 Detail useful when diagnosing a problem.
Info 2 Normal progress: what the camera path is doing.
Warning 3 Something unexpected that was recovered from.
Error 4 Something failed.

Ordered by increasing severity, so level >= Warning is a meaningful test in every binding. The spellings are optris::sdk::LogLevel::kInfo, OPTRIS_SDK_LOG_INFO, SdkLogLevel.Info, optris_sdk.log.LOG_INFO and optris_sdk::LogLevel::Info.

Warning: These are not the legacy numbers. optris::Sdk::setLogCallback() runs the other way: there 0 is Error and 4 is Trace. See Moving from Sdk::setLogCallback.

ClearLogHandler

optris::sdk::ClearLogHandler();

Clearing when nothing is set succeeds and does nothing. Passing an empty handler is the same as clearing: an empty std::function, a NULL function pointer, a null delegate.

Clearing waits. It blocks until every call already running has returned, so once it returns the old handler cannot be called again and anything it captured is safe to destroy. That guarantee is what makes it safe for a binding to free a delegate, and it is what makes Shutting down a two-line rule rather than a race.

The handler replaces the SDK's own output

While a handler is installed the SDK's console and file sinks are silenced, and clearing it restores them.

This is deliberate. An application that captures the log should not also be leaving a second copy in a file it never told the user about, and a support ticket arriving with two logs that disagree about timestamps is worse than one log. If you want the SDK's own file as well, write it yourself from the handler; then one place decides where the log goes.

Threading

Every other callback in the SDK arrives on an SDK thread, never on the thread that registered it. A log handler is the exception, because logging happens wherever the SDK happens to be running. Each row below is a guarantee with a test behind it, not a hope.

Which thread Whichever one emitted the message. Usually an SDK thread, but yours, inside your call, when something you called logs before returning.
Concurrency Two SDK threads logging at once call the handler at once. It must be safe to call concurrently. The SDK holds no lock across the call on purpose: serialising delivery would put every logging thread behind the slowest handler, and one that blocked would stall the frame path.
Duration The thread that logged is blocked until the handler returns. Time spent here is time the frame pump is not running. Queue the line and return; do not write to a network socket from inside it.
Throwing Caught and discarded at the boundary. The message is lost and the SDK carries on. That is a safety net and not a contract: an exception unwinding out of a handler into an SDK frame would be undefined behaviour, so it is stopped there.
Re-entry If the handler logs, directly or by calling an SDK function that logs, the nested message is dropped rather than delivered back into it. Otherwise a handler that logged would call itself forever.
Setting from inside Refused rather than deadlocked. Setting or clearing waits for the handler to return, so from inside one it would wait for itself. C++ throws std::logic_error, C returns OPTRIS_SDK_ERROR_INVALID_ARGUMENT, .NET throws OptrisSdkException, Rust returns Status::InvalidArgument.

Two language-specific consequences of the concurrency rule:

  • Python holds the GIL for the duration. Nothing else in the interpreter runs while the handler does, and an exception out of it is invisible except on stderr, because the caller is C. See The Optris SDK from Python.
  • Most logging frameworks are already thread-safe and a bare file object is not. A StreamWriter, a plain open() file or an std::ofstream written from two threads at once interleaves.

Which camera a line is about

A line that is about one camera begins with [S/N <serial>]: shutter and NUC cycles, auto-skim steps, flag-cycle decisions. The serial is the number on the camera, the same one enumeration reports, so a handler can route or filter on it with a prefix match.

Not every line has one. Anything emitted before a camera is identified, and anything about the process rather than a camera (the GPU adapter chosen, the log directory, an enumeration sweep), has no camera to name. A few lines carry the serial mid-sentence in the older (S/N <serial>) spelling; those are the connect, disconnect and re-open lines, left as prose because that is what they read as.

S/N 0 means the component emitting the line was never told whose camera it is. That is a gap in the SDK rather than a camera with serial zero; treat it as unattributed.

This arrived in 12.0.0. Before it, a message emitted while two cameras were streaming did not say which one it came from, and a log from a fifteen-camera host could not be used at all.

Into your own logging framework

The handler is a plain callable in every binding, with no dependency on a logging framework, so that this part is the application's choice. Two bindings ship the adapter anyway, because it is the same three lines every caller would write.

Sdk.SetLogHandler((level, message) => logger.Log(
    level switch
    {
        SdkLogLevel.Trace => LogLevel.Trace,
        SdkLogLevel.Debug => LogLevel.Debug,
        SdkLogLevel.Warning => LogLevel.Warning,
        SdkLogLevel.Error => LogLevel.Error,
        _ => LogLevel.Information,
    },
    "{Message}", message));

optris_sdk.log.to_logger() maps LOG_TRACE to level 5, below DEBUG, because logging has no trace level; the other four map to their obvious counterparts. optris_sdk.log.level_name() gives the level's name for a format of your own.

HasLogHandler

bool installed = optris::sdk::HasLogHandler();

It asks the SDK rather than reporting what one binding 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. Python keeps both answers apart deliberately, optris_sdk.log.handler() being its own record and has_handler() being the SDK's; the two disagree exactly when something outside the package installed one.

Shutting down

Clear the handler before you tear down whatever it writes into.

The SDK keeps logging for as long as it is doing anything, and that includes closing cameras and releasing resources while an application is on its way out. A handler still installed then will still be called, and if the log it appends to has already been destroyed that is a crash during shutdown, which is the least diagnosable kind.

Clearing is the whole answer, and it is the only ordering requirement: it waits for calls in flight, so once it returns nothing can reach the handler again. The sink itself is owned by the logger and cannot be destroyed out from under a call in progress.

Python clears it for you when the interpreter exits. It first closes every camera, recording session and process interface still open and stops a device monitor still running, then clears the handler, so the lines those closes log still arrive. It has to: once Python has begun to shut down it cannot run a handler on another thread, and up to Python 3.13 it ends any thread that tries, which a program saw as a crash at exit.

See Lifetimes for the same question asked about cameras, frames and the handles hanging off them.

FFmpeg's messages

The SDK reads and writes video through FFmpeg, and FFmpeg's messages arrive in the SDK's log like any other line: in your handler, at the SDK's level, or on the SDK's console when there is no handler. FFmpeg never writes to standard error on its own from inside the SDK. Before 12.0.23 it did, whatever the program had set.

Each of these lines begins with [FFmpeg <name>], where the name is the encoder, decoder or file format the line is about, or just [FFmpeg] when FFmpeg names none:

[FFmpeg hevc_amf] DLL amfrt64.dll failed to open
[FFmpeg AVFormatContext] Unable to choose an output format for 'clip.xyz'; use a standard extension for the filename or specify the format manually.
FFmpeg's level SDK level While the SDK tries encoders
panic, fatal Error Debug
error, warning Warning Debug
info, verbose Debug Trace
debug, trace Trace Trace

Trying encoders is how the SDK finds out which encoder takes an export: IsCodecAvailable() and choosing the encoder in Create(), in every binding. Each encoder the computer does not have answers with an FFmpeg error ("DLL amfrt64.dll failed to open" without AMD graphics, "Error creating a MFX session" without Intel's), and that answer is what the SDK asked for, not a fault. So those lines are Debug: at the default level they print nothing, and at Debug they explain why an export chose the encoder it did.

An FFmpeg error anywhere else is a Warning, not an Error, because the SDK reports the failure itself, as an exception or a status and its own error line, and FFmpeg's line is the detail underneath it.

FFmpeg's own threshold still decides which messages exist at all. It is info unless a program changes it with FFmpeg's av_log_set_level(), so the lines in the SDK's log are the lines FFmpeg used to print.

If your program uses FFmpeg itself. FFmpeg keeps one log callback per process and cannot say which one is installed, so the SDK cannot see whether your program set one. The SDK installs its own the first time it uses FFmpeg, and never again:

  • A program that never exports, records or plays back video keeps whatever callback it had.
  • A program with its own, separate FFmpeg is not affected at all. The SDK uses the FFmpeg it ships with.
  • A program that shares the SDK's FFmpeg and wants FFmpeg's messages for itself calls av_log_set_callback() after its first SDK call that uses video. IsCodecAvailable() is a cheap one. The SDK does not put its own callback back.

Moving from Sdk::setLogCallback

The legacy v11 API has optris::Sdk::setLogCallback(callback, exclusive), and in C# Optris.OtcSdk.Sdk.setLogCallback. Two differences matter:

  • The level numbers are reversed. The legacy callback is given 0 for Error through 4 for Trace. LogLevel runs the other way, 0 for Trace through 4 for Error, which is the conventional order and is what lets >= mean "at least this severe". Map the value; do not pass the integer through, or every line lands at the mirror image of its real severity.
  • There is no exclusive argument. Installing a handler is what the legacy exclusive: true was documented to do, and it is now the only behaviour. See The handler replaces the SDK's own output.

Migration Guide: v11 to v12 covers the rest of the move.