6 #include "spdlog/async.h"
7 #include "spdlog/sinks/rotating_file_sink.h"
8 #include "spdlog/sinks/stdout_color_sinks.h"
13 if (!std::filesystem::exists(dir_name)) {
15 std::filesystem::create_directory(dir_name);
16 }
catch (
const std::filesystem::filesystem_error& e) {
17 std::cerr <<
"Error creating directory: " << e.what() << std::endl;
24 auto standard_out = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
25 standard_out->set_color(spdlog::level::critical, standard_out->dark);
26 standard_out->set_color(spdlog::level::err, standard_out->red);
27 standard_out->set_color(spdlog::level::warn, standard_out->yellow);
28 standard_out->set_color(spdlog::level::info, standard_out->green);
29 standard_out->set_color(spdlog::level::debug, standard_out->blue);
30 standard_out->set_color(spdlog::level::trace, standard_out->magenta);
32 spdlog::init_thread_pool(8192, 1);
33 std::shared_ptr<spdlog::logger> new_logger;
36 new_logger = spdlog::rotating_logger_st(
"file_logger",
"logs/log", 1048576 * 5, 3);
39 std::make_shared<spdlog::async_logger>(
"std_logger", standard_out, spdlog::thread_pool(), spdlog::async_overflow_policy::block);
42 new_logger->set_level(spdlog::level::off);
43 new_logger->set_pattern(
"[%H:%M:%S] %^[%l]%$ %v");
49 if (log_level ==
"trace") {
51 }
else if (log_level ==
"debug") {
53 }
else if (log_level ==
"info") {
55 }
else if (log_level ==
"warning") {
57 }
else if (log_level ==
"error") {
59 }
else if (log_level ==
"critical") {
60 logger->set_level(spdlog::level::critical);
61 }
else if (log_level ==
"off") {
64 std::cout <<
"Error: Invalid log level given." << std::endl;
65 throw std::invalid_argument(
"Invalid log level given.");
void createDirectory(const std::string &dir_name)
static void update_level(std::string &log_level)
Sets the log level of the logger.
static std::shared_ptr< spdlog::logger > init_logger(LogType log_type=LogType::STD)
Initializes the logger.
static std::shared_ptr< spdlog::logger > logger
Publically accessible shared pointer to the logger.
LogType
Enum for the type of logger.