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