λ61
Loading...
Searching...
No Matches
l61Core
include
l61
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
30
namespace
l61
31
{
32
enum class
LogLevel
: std::uint8_t
33
{
34
INFO
,
35
WARN
,
36
ERROR
,
37
FATAL
38
};
39
48
template
<
typename
... Ty>
49
constexpr
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
defs.hpp
l61
Definition
Object.hpp:34
l61::toLogger
constexpr void toLogger(LogLevel level, std::format_string< Ty... > fmt, Ty &&... args)
Definition
Logger.hpp:49
l61::getCentralStatusObject
l61_stat & getCentralStatusObject()
Definition
getCentralStatusObject.cpp:39
l61::LogLevel
LogLevel
Definition
Logger.hpp:33
l61::LogLevel::FATAL
@ FATAL
Definition
Logger.hpp:37
l61::LogLevel::WARN
@ WARN
Definition
Logger.hpp:35
l61::LogLevel::INFO
@ INFO
Definition
Logger.hpp:34
l61::LogLevel::ERROR
@ ERROR
Definition
Logger.hpp:36
Generated by
1.14.0