Molecular Dynamics Simulation  1.0
Logger.cpp
Go to the documentation of this file.
1 #include "Logger.h"
2 
3 #include <filesystem>
4 #include <iostream>
5 
6 #include "spdlog/async.h"
7 #include "spdlog/sinks/rotating_file_sink.h"
8 #include "spdlog/sinks/stdout_color_sinks.h"
9 
10 std::shared_ptr<spdlog::logger> Logger::logger = init_logger();
11 
12 void createDirectory(const std::string& dir_name) {
13  if (!std::filesystem::exists(dir_name)) {
14  try {
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;
18  throw e;
19  }
20  }
21 }
22 
23 std::shared_ptr<spdlog::logger> Logger::init_logger(LogType log_type) {
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);
31 
32  spdlog::init_thread_pool(8192, 1);
33  std::shared_ptr<spdlog::logger> new_logger;
34  if (log_type == LogType::FILE) {
35  createDirectory("logs");
36  new_logger = spdlog::rotating_logger_st("file_logger", "logs/log", 1048576 * 5, 3);
37  } else {
38  new_logger =
39  std::make_shared<spdlog::async_logger>("std_logger", standard_out, spdlog::thread_pool(), spdlog::async_overflow_policy::block);
40  }
41 
42  new_logger->set_level(spdlog::level::off);
43  new_logger->set_pattern("[%H:%M:%S] %^[%l]%$ %v");
44 
45  return new_logger;
46 }
47 
48 void Logger::update_level(std::string& log_level) {
49  if (log_level == "trace") {
50  Logger::logger->set_level(spdlog::level::trace);
51  } else if (log_level == "debug") {
52  Logger::logger->set_level(spdlog::level::debug);
53  } else if (log_level == "info") {
54  Logger::logger->set_level(spdlog::level::info);
55  } else if (log_level == "warning") {
56  Logger::logger->set_level(spdlog::level::warn);
57  } else if (log_level == "error") {
58  Logger::logger->set_level(spdlog::level::err);
59  } else if (log_level == "critical") {
60  logger->set_level(spdlog::level::critical);
61  } else if (log_level == "off") {
62  Logger::logger->set_level(spdlog::level::off);
63  } else {
64  std::cout << "Error: Invalid log level given." << std::endl;
65  throw std::invalid_argument("Invalid log level given.");
66  }
67 }
void createDirectory(const std::string &dir_name)
Definition: Logger.cpp:12
static void update_level(std::string &log_level)
Sets the log level of the logger.
Definition: Logger.cpp:48
static std::shared_ptr< spdlog::logger > init_logger(LogType log_type=LogType::STD)
Initializes the logger.
Definition: Logger.cpp:23
static std::shared_ptr< spdlog::logger > logger
Publically accessible shared pointer to the logger.
Definition: Logger.h:35
LogType
Enum for the type of logger.
Definition: Logger.h:23