#include "error.h"

#include <iostream>
#include <mutex>
#include <string>

namespace dofs {

static std::mutex g_error_mutex;

std::mutex &error_mutex() noexcept {
        return g_error_mutex;
}

static inline void emit_error_line(std::string_view type,
                                   std::optional<uint64_t> ts,
                                   std::string_view message) noexcept {
        std::lock_guard<std::mutex> lock(g_error_mutex);
        std::cerr << '[' << type << ']';

        if (ts.has_value()) {
                std::cerr << '[' << *ts << ']';
        }

        std::cerr << ": " << message << '\n';
}

void log_error(std::string_view type,
               std::string_view message,
               std::optional<uint64_t> timestamp) noexcept {
        emit_error_line(type, timestamp, message);
}

} // namespace dofs
