Skip to main content

Main

Template main.cpp

#include <atomic>
#include <csignal>

#include "spdlog/spdlog.h"
#include "fmt/chrono.h"

static std::atomic<bool> is_running = true;

void sigint_handler(int signum) {
(void) signum;
is_running = false;
}

int main(int argc, char* argv[]) {
signal(SIGINT, sigint_handler);

spdlog::set_pattern("[%^%L%$] [%T.%e] [%t] [%s:%#] %v");

std::time_t t = std::time(nullptr);
auto log_path_raw = fmt::format("$PROJECT_ROOT/logs/{:%H-%M-%S}.txt", fmt::localtime(t));
auto log_path = utils::ExpandVars(log_path_raw);
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(log_path, false);
spdlog::default_logger()->sinks().push_back(file_sink);

return EXIT_SUCCESS;
}
utils.cpp
#include "wordexp.h"

namespace utils {

std::string ExpandVars(std::string path) {
wordexp_t p;
wordexp(path.c_str(), &p, 0);
std::stringstream ss;
for (unsigned int i = 0; i < p.we_wordc; i++) {
ss << p.we_wordv[i];
}
wordfree(&p);
return ss.str();
}

} // namespace utils