MicroLogger/include/logger.hh

57 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();
explicit Logger(std::ostream& out);
Logger() = delete;
Logger(Logger&) = delete;
Logger(Logger&&) = delete;
/// @return success or not (already configured)
bool configure(const std::vector<std::string>& d);
template <typename Metric,
typename = std::enable_if_t<std::is_arithmetic_v<Metric>>>
void add(const std::string& field, Metric metric) {
auto locked = active.load();
// auto locked = active;
auto it = locked->find(field);
if (it == locked->end()) {
throw configErrorMsg;
}
it->second = metric;
}
bool isConfigured() { return configured == CONFIGURED; }
private:
friend Worker;
enum Configuration { NOT_CONFIGURED, CONFIG_IN_PROGRESS, CONFIGURED };
std::atomic<int> configured{NOT_CONFIGURED};
std::unique_ptr<Worker> worker;
std::shared_ptr<map_type> m1, m2;
std::atomic<std::shared_ptr<map_type>> active; // impl may use mutex!
// std::shared_ptr<map_type> active;
};
} // namespace vptyp