MicroLogger/include/logger.hh
2025-06-19 08:44:34 +00:00

59 lines
1.4 KiB
C++

#pragma once
#include <atomic>
#include <iostream>
#include <memory>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <variant>
#include <vector>
namespace vptyp {
static constexpr std::string_view configErrorMsg =
"Bruh, incorrect configuration";
class Logger {
// helper class for handling worker thread
class Worker;
using map_type =
std::unordered_map<std::string, std::variant<int64_t, double>>;
public:
virtual ~Logger();
Logger();
Logger(Logger&) = delete;
Logger(Logger&&) = delete;
/// must be called only through initializer list
/// @return success or not (already configured)
bool configure(const std::vector<std::string>& d,
std::ostream& out = std::cout);
template <typename Metric,
typename = std::enable_if_t<std::is_arithmetic_v<Metric>>>
void add(std::string field, Metric metric) {
return;
auto locked = active.load();
auto it = locked->find(field);
if (it == locked->end()) {
throw configErrorMsg;
}
it->second = metric;
}
bool isConfigured() { return configured > CONFIGURED; }
private:
static constexpr int CONFIGURED = 2;
static constexpr int CONFIG_IN_PROGRESS = 1;
static constexpr int NOT_CONFIGURED = 0;
std::atomic<int> configured{0};
std::unique_ptr<Worker> worker;
map_type m1, m2;
std::atomic<std::shared_ptr<map_type>> active; // impl using mutexes!
};
} // namespace vptyp