λ61
Loading...
Searching...
No Matches
Logger.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2025 Tetex7
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18//
19// Created by tete on 05/13/2025.
20//
21#pragma once
22
23#ifndef L61_LOGGER_HPP
24#define L61_LOGGER_HPP
25#include <l61/defs.hpp>
26#include <chrono>
27#include <ctime>
28#include <format>
29
30namespace l61
31{
32enum class LogLevel : std::uint8_t
33{
38};
39
48template<typename... Ty>
49constexpr void toLogger(LogLevel level, std::format_string<Ty...> fmt, Ty&&... args)
50{
51 if (!getCentralStatusObject().procStat.verbose && ((level == LogLevel::WARN) || (level == LogLevel::INFO))) return;
52
53 std::string_view le;
54 switch (level)
55 {
56 case LogLevel::INFO:
57 le = "INFO";
58 break;
59 case LogLevel::WARN:
60 le = "WARN";
61 break;
62 case LogLevel::ERROR:
63 le = "ERROR";
64 break;
65 case LogLevel::FATAL:
66 le = "FATAL";
67 break;
68 }
69
70 auto now = std::chrono::system_clock::now();
71
72 // Convert to time_t to use with std::localtime
73 std::time_t now_time_t = std::chrono::system_clock::to_time_t(now);
74
75 // Convert to tm structure for broken-down time
76 std::tm* now_tm = std::localtime(&now_time_t);
77
78 auto& v = (level >= LogLevel::ERROR) ? std::cerr : std::cout;
79
80 std::println(
81 v,
82 "[{}:{}:{}][{}][{}]: {}",
83 now_tm->tm_hour,
84 now_tm->tm_min,
85 now_tm->tm_sec,
86 le,
87 "l61-rt",
88 std::format(fmt, std::forward<Ty>(args)...)
89 );
90}
91
92}
93
94#endif //L61_LOGGER_HPP
Definition Object.hpp:34
constexpr void toLogger(LogLevel level, std::format_string< Ty... > fmt, Ty &&... args)
Definition Logger.hpp:49
l61_stat & getCentralStatusObject()
Definition getCentralStatusObject.cpp:39
LogLevel
Definition Logger.hpp:33
@ FATAL
Definition Logger.hpp:37
@ WARN
Definition Logger.hpp:35
@ INFO
Definition Logger.hpp:34
@ ERROR
Definition Logger.hpp:36