Quantum Resource Manager and Compiler Infrastructure (QRM&CI) 0.1.0
Logger.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2026 MQSS Maintainers
3 * All rights reserved.
4 *
5 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 */
7
11
12#pragma once
13
14#include <cstddef>
15#include <memory>
16#include <spdlog/sinks/rotating_file_sink.h>
17#include <spdlog/sinks/stdout_color_sinks.h>
18#include <spdlog/spdlog.h>
19#include <string>
20#include <string_view>
21#include <vector>
22
23namespace mqss::qrmci {
24
26inline constexpr std::size_t MaxLogFileBytes = 50UL * 1024 * 1024;
28inline constexpr std::size_t MaxLogFiles = 5;
29
35inline std::shared_ptr<spdlog::logger>
36makeLogger(std::string_view name, std::string_view logFile = {}) {
37 std::vector<spdlog::sink_ptr> sinks{
38 std::make_shared<spdlog::sinks::stdout_color_sink_mt>(),
39 };
40
41 if (!logFile.empty()) {
42 // A log directory that cannot be written is a deployment problem worth
43 // reporting, but it is not a reason to refuse to run: the console sink
44 // still carries every line. Letting the exception out would abort the
45 // process before it could say why.
46 try {
47 sinks.push_back(std::make_shared<spdlog::sinks::rotating_file_sink_mt>(
48 std::string(logFile), MaxLogFileBytes, MaxLogFiles));
49 } catch (const spdlog::spdlog_ex &e) {
50 spdlog::warn("Logging to console only; could not open '{}': {}", logFile,
51 e.what());
52 }
53 }
54
55 auto logger = std::make_shared<spdlog::logger>(std::string(name),
56 sinks.begin(), sinks.end());
57
58 logger->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%n] [%^%l%$] %v");
59 logger->set_level(spdlog::level::info);
60 logger->flush_on(spdlog::level::info);
61
62 return logger;
63}
64
65} // namespace mqss::qrmci
std::shared_ptr< spdlog::logger > makeLogger(std::string_view name, std::string_view logFile={})
Create a logger writing to the console and, if logFile is given, to a rotating file alongside it.
Definition Logger.h:36
constexpr std::size_t MaxLogFiles
How many rotated log files are kept alongside the current one.
Definition Logger.h:28
constexpr std::size_t MaxLogFileBytes
Largest a single log file grows before it is rotated.
Definition Logger.h:26