LCOV - code coverage report
Current view: top level - src/tools - setup.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 23 0.0 %
Date: 2025-05-02 21:49:55 Functions: 0 2 0.0 %

          Line data    Source code
       1             : // SPDX-FileCopyrightText: 2024 Pairinteraction Developers
       2             : // SPDX-License-Identifier: LGPL-3.0-or-later
       3             : 
       4             : #include "pairinteraction/tools/setup.hpp"
       5             : 
       6             : #include "pairinteraction/utils/paths.hpp"
       7             : 
       8             : #include <cstdlib>
       9             : #include <filesystem>
      10             : #include <mutex>
      11             : #include <spdlog/async.h>
      12             : #include <spdlog/sinks/rotating_file_sink.h>
      13             : #include <spdlog/sinks/stdout_color_sinks.h>
      14             : #include <spdlog/spdlog.h>
      15             : #include <vector>
      16             : 
      17             : namespace pairinteraction {
      18           0 : void setup() {
      19             : 
      20             :     // Configure a logger
      21           0 :     std::filesystem::path logdir = paths::get_cache_directory() / "logs";
      22             : 
      23           0 :     if (!std::filesystem::exists(logdir)) {
      24           0 :         std::filesystem::create_directories(logdir);
      25           0 :     } else if (!std::filesystem::is_directory(logdir)) {
      26           0 :         throw std::runtime_error("Log path is not a directory.");
      27             :     }
      28             : 
      29           0 :     std::filesystem::path logfile = logdir / "cpp.log";
      30             : 
      31             :     static std::once_flag flag_default_logger;
      32           0 :     std::call_once(flag_default_logger, [&logfile] {
      33           0 :         spdlog::init_thread_pool(8192, 1);
      34             :         auto stdout_sink =
      35           0 :             std::make_shared<spdlog::sinks::stdout_color_sink_mt>(spdlog::color_mode::always);
      36           0 :         auto file_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(logfile.string(),
      37           0 :                                                                                 1048576 * 5, 10);
      38           0 :         std::vector<spdlog::sink_ptr> sinks{stdout_sink, file_sink};
      39           0 :         auto logger = std::make_shared<spdlog::async_logger>("logger", sinks.begin(), sinks.end(),
      40           0 :                                                              spdlog::thread_pool(),
      41           0 :                                                              spdlog::async_overflow_policy::block);
      42           0 :         logger->set_pattern("[%Y-%m-%d %H:%M:%S.%e %t] [%^%l%$] [%s:%#] %v");
      43           0 :         spdlog::set_default_logger(logger);
      44             : 
      45           0 :         const char *log_level = std::getenv("SPDLOG_LEVEL");
      46           0 :         if (log_level != nullptr) {
      47           0 :             spdlog::set_level(spdlog::level::from_str(log_level));
      48             :         }
      49           0 :     });
      50           0 : }
      51             : } // namespace pairinteraction

Generated by: LCOV version 1.16