GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Replace v2 logging stub with thread-safe asynchronous provider · Issue #142 · exasol/udf-runner-cpp

Replace v2 logging stub with thread-safe asynchronous provider #142

New issue
Copy link
New issue
Copy link
In review
#152
In review
Replace v2 logging stub with thread-safe asynchronous provider#142
#152
Copy link

Description

@tkilias
tkilias
opened on Nov 19, 2026
Issue body actions

Summary

Follow up on PR #141 by replacing the v2 logging stub with a thread-safe asynchronous implementation. Preserve the existing std::ostream& severity accessors.

Design

Per-thread streams and record boundaries

  • Return a distinct thread-local std::ostream for each provider state and severity; never share an ostream object across producer threads.
  • Back each stream with a custom std::streambuf holding per-thread pending text and next-record group.
  • Newline or flush() commits a record. Implement xsputn() to split bulk writes at every newline, overflow() for single characters, and sync() for flush.
  • Thread-local streams retain shared provider state while they exist. Callers stop using the provider before destruction. Text without newline or flush stays buffered until a later commit or thread-local stream cleanup.

Illustrative stream buffer:

class QueueStreamBuf final : public std::streambuf {
public:
    QueueStreamBuf(std::shared_ptr<LogState> state, Severity severity,
                   std::string group);
protected:
    std::streamsize xsputn(const char*, std::streamsize) override;
    int_type overflow(int_type) override;
    int sync() override;
private:
    void commit_record();
    std::shared_ptr<LogState> state_;
    Severity severity_;
    std::string pending_;
    std::string group_;
};

Connecting thread_local_stream and QueueStreamBuf

thread_local_stream(state, severity) is the factory/cache used by each provider accessor. It returns a stable std::ostream& owned by a thread-local entry. On the first request for a (LogState, severity) pair on that thread, it constructs a QueueStreamBuf with the shared state, severity, and the provider group from LogState, then constructs an std::ostream whose rdbuf() points to that buffer. Later accessor calls on that same thread and pair return the same stream. Different threads get different entries and buffers.

Illustrative ownership shape:

struct ThreadLocalLogStream {
    QueueStreamBuf buffer;
    std::ostream stream;
    ThreadLocalLogStream(std::shared_ptr<LogState> state, Severity severity)
        : buffer(state, severity, state->group), stream(&buffer) {}
};

std::ostream& thread_local_stream(std::shared_ptr<LogState> state, Severity severity);

The thread-local cache owns ThreadLocalLogStream entries; each entry owns the buffer and stream together, ensuring the stream never refers to a destroyed buffer. Cache identity must distinguish provider states safely (for example, weak/shared ownership identity rather than a raw address that can be reused).

Groups

  • Do not add a stream manipulator or change LoggingProviderIf; other provider implementations do not support custom manipulators.
  • Supply the group as a constructor parameter to LoggingProvider. The provider assigns that group to every record emitted through its streams.
  • Render tagged records with level and group, e.g. [DEBUG][protocol] ..., followed by existing source/thread information and message. An empty group identifies ungrouped records.
LoggingProvider provider("protocol", filter_config);
provider.debug() << "received frame " << frame_id << '\\n';

Filtering and queue

  • Queue complete records with severity, group, and rendered text using the existing unbounded moodycamel BlockingMpmcQueue.
  • Filter on the producer when a record is committed, before enqueueing, so disabled records use no queue memory or writer time.
  • Publish filter updates as immutable snapshots (e.g. std::atomic<std::shared_ptr<const LogFilter>>) to avoid producer-side mutex locking. Updates may allocate and are expected to be infrequent.
  • Empty enabled-group set means all groups, including ungrouped records. Non-empty set allows only listed groups; ungrouped records are excluded.
  • Queue has no capacity limit and records are not intentionally dropped. Allocation failure is exceptional and must not be silently treated as a normal drop.
struct LogRecord {
    Severity level;
    std::string group;
    std::string text;
};
struct LogFilter {
    LevelMask enabled_levels;
    std::unordered_set<std::string> enabled_groups;
    bool allows(Severity level, std::string_view group) const;
};

Shared LogState

LogState is the shared implementation state for one LoggingProvider. The provider creates and owns a std::shared_ptr<LogState>; its writer thread and each thread-local QueueStreamBuf retain shared ownership while they need the state. It contains the queue, current immutable filter snapshot, fixed provider group, and writer-thread lifecycle. This keeps thread-local streams from holding dangling references to provider members. The provider remains responsible for shutdown: callers stop using it, it enqueues the stop item, and it joins the writer before releasing its ownership.

Illustrative shape:

struct LogState {
    const LogFilter filter;
    BlockingMpmcQueue<LogItem> queue;
    std::string group;
    std::thread writer;
};

Writer lifecycle and provider example

One writer waits for queue items and writes each accepted record as one operation to std::cerr. A stop item follows all accepted work; destruction enqueues it after callers stop using the provider, drains prior records, then joins the writer. Destruction may wait; producer logging calls do not wait for output I/O.

class LoggingProvider final : public LoggingProviderIf {
public:
    explicit LoggingProvider(std::string group, LogFilter filter = default_filter());
    ~LoggingProvider() override; // enqueue stop marker, join writer
    std::ostream& error() override {
        return thread_local_stream(state_, Severity::Error);
    }
    std::ostream& warning() override {
        return thread_local_stream(state_, Severity::Warning);
    }
    std::ostream& notice() override {
        return thread_local_stream(state_, Severity::Notice);
    }
    std::ostream& debug() override {
        return thread_local_stream(state_, Severity::Debug);
    }
    std::ostream& verbose() override {
        return thread_local_stream(state_, Severity::Verbose);
    }
private:
    std::shared_ptr<LogState> state_;
};

Compatibility

  • Preserve severity accessor signatures, UDF_LOG_LEVEL parsing/default behavior, lazy logging, and release-build verbose suppression.
  • Use uppercase labels: ERROR, WARNING, NOTICE, DEBUG, VERBOSE.
  • Target v2 and build on the utilities introduced by PR #73: Added logging-provider and log-utils. #141.

Acceptance criteria

  • Concurrent producers yield complete, non-interleaved records with correct level/group.
  • Group applies to one record and resets after newline, std::endl, or explicit flush.
  • Empty group allowlist accepts grouped and ungrouped records; a non-empty allowlist accepts only listed groups and excludes ungrouped records.
  • Level/group filtering occurs before enqueue using the provider’s fixed constructor-supplied configuration.
  • Newline, flush, bulk writes, multiple newlines per write, and pending text without final newline have defined tested behavior.
  • Disabled levels/groups are not enqueued; existing parsing, lazy creation, and release verbose suppression remain covered.
  • Shutdown drains accepted records and joins the writer. Producer calls do not wait for output I/O.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.