360toPerspective/cpp/include/logger.hpp

32 lines
1.2 KiB
C++
Raw Permalink Normal View History

#ifndef H_Logger
#define H_Logger
#include <iostream>
#include <chrono>
#include <ctime>
#include <iomanip>
/// @brief Implementation of the Logger class to print messages to the console. It is defined as a template class to save the .cpp file.
template <int _T=1>
class Logger_impl {
public:
// static constexpr char date_format[] = "%FT%T%z";// Works on Linux but not on Windows...
static constexpr char date_format[] = "%Y-%m-%dT%H:%M:%S";
static void log(const std::string& message) {
auto now = std::chrono::system_clock::now();
std::time_t now_time = std::chrono::system_clock::to_time_t(now);
std::cout << std::put_time(std::localtime(&now_time), date_format) << " " << message << std::endl;
}
static void error(const std::string& message) {
auto now = std::chrono::system_clock::now();
std::time_t now_time = std::chrono::system_clock::to_time_t(now);
std::cerr << std::put_time(std::localtime(&now_time), date_format) << " ERROR: " << message << std::endl;
}
};
/// @brief Logger class to print messages to the console.
using Logger = Logger_impl<1>;
#endif