datactl/logging.cpp

datactl/logging.cpp

datactl/logging.cpp

Namespaces

Name
Syntalos
Syntalos::datactl

Source code

/*
 * Copyright (C) 2025-2026 Matthias Klumpp <matthias@tenstral.net>
 *
 * Licensed under the GNU Lesser General Public License Version 3
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the license, or
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this software.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "logging.h"

#include <cstdio>
#include <string>
#include <string_view>

namespace Syntalos::datactl
{

// Head of the global category linked list.
// Zero-initialized (constant initialization) before any dynamic init fires,
// so LogCategory constructors in other TUs can safely prepend to this list.
static std::atomic<LogCategory *> g_categoryListHead{nullptr};

static void defaultHandler(const LogMessage &m)
{
    auto out = (m.severity >= LogSeverity::Warning) ? stderr : stdout;
    std::fprintf(out, "%s: %.*s\n", m.category, static_cast<int>(m.message.size()), m.message.data());
}

static LogHandlerFn g_handler{defaultHandler};

LogCategory::LogCategory(const char *name, LogSeverity defaultSeverity) noexcept
    : m_name(name),
      m_threshold(static_cast<int>(defaultSeverity)),
      m_next(nullptr)
{
    // wait-free CAS prepend - safe even when multiple TUs register simultaneously
    auto head = g_categoryListHead.load(std::memory_order_relaxed);
    do {
        m_next = head;
    } while (
        !g_categoryListHead.compare_exchange_weak(head, this, std::memory_order_release, std::memory_order_relaxed));
}

namespace detail
{

std::atomic<bool> g_handlerActive{true}; // default handler is always installed

void dispatchLog(
    const LogCategory &cat,
    LogSeverity sev,
    const char *file,
    int line,
    const char *function,
    const std::string &message)
{
    const LogMessage lm{sev, cat.name(), file, line, function, std::string_view(message)};
    g_handler(lm);
}

} // namespace detail

// ---- Public API ----

void setLogHandler(LogHandlerFn handler)
{
    g_handler = std::move(handler);
    detail::g_handlerActive.store(bool(g_handler), std::memory_order_release);
}

void setLogSeverity(LogSeverity min)
{
    auto cat = g_categoryListHead.load(std::memory_order_acquire);
    while (cat) {
        cat->m_threshold.store(static_cast<int>(min), std::memory_order_relaxed);
        cat = cat->m_next;
    }
}

void setLogSeverity(const char *category, LogSeverity min)
{
    const std::string_view name(category);
    auto cat = g_categoryListHead.load(std::memory_order_acquire);
    while (cat) {
        if (std::string_view(cat->m_name) == name)
            cat->m_threshold.store(static_cast<int>(min), std::memory_order_relaxed);
        cat = cat->m_next;
    }
}

} // namespace Syntalos::datactl

Updated on 2026-12-04 at 23:44:27 +0000