ReUseX
0.0.5
3D Point Cloud Processing for Building Reuse
Toggle main menu visibility
Loading...
Searching...
No Matches
logging.hpp
Go to the documentation of this file.
1
// SPDX-FileCopyrightText: 2025 Povl Filip Sonne-Frederiksen
2
//
3
// SPDX-License-Identifier: GPL-3.0-or-later
4
//
5
#pragma once
6
#include <fmt/format.h>
7
#include <fmt/std.h>
8
9
#include <chrono>
10
#include <functional>
11
#include <string>
12
#include <string_view>
13
#include <utility>
14
15
namespace
reusex::core
{
16
17
enum class
LogLevel
{
18
trace
= 0,
19
debug
= 1,
20
info
= 2,
21
warn
= 3,
22
error
= 4,
23
critical
= 5,
24
off
= 6,
25
};
26
27
using
LogHandler
=
28
std::function<void(
LogLevel
level, std::string_view message)>;
29
30
void
set_log_handler
(
LogHandler
handler);
31
void
reset_log_handler
();
32
void
set_log_level
(
LogLevel
level);
33
auto
get_log_level
() ->
LogLevel
;
34
auto
should_log
(
LogLevel
level) -> bool;
35
void
log_message
(
LogLevel
level, std::string_view message);
36
37
class
stopwatch
{
38
public
:
39
// Lightweight replacement for spdlog::stopwatch used by existing timing
40
// logs. elapsed() reports seconds as a double.
41
stopwatch
() =
default
;
42
43
void
reset
() { start_ = clock::now(); }
44
45
[[nodiscard]]
auto
elapsed
() const ->
double
{
46
return
std::chrono::duration<double>(clock::now() - start_).count();
47
}
48
49
private
:
50
using
clock = std::chrono::steady_clock;
51
std::chrono::time_point<clock> start_ = clock::now();
52
};
53
54
template
<
typename
... Args>
55
inline
void
log
(
LogLevel
level, fmt::format_string<Args...> format,
56
Args &&...args) {
57
// Early return avoids formatting cost when the message is filtered out.
58
if
(!
should_log
(level)) [[unlikely]] {
59
return
;
60
}
61
log_message
(level, fmt::format(format, std::forward<Args>(args)...));
62
}
63
64
inline
void
log
(
LogLevel
level, std::string_view message) {
65
if
(!
should_log
(level)) [[unlikely]] {
66
return
;
67
}
68
log_message
(level, message);
69
}
70
71
template
<
typename
... Args>
72
inline
void
trace
(fmt::format_string<Args...> format, Args &&...args) {
73
log
(
LogLevel::trace
, format, std::forward<Args>(args)...);
74
}
75
76
inline
void
trace
(std::string_view message) {
log
(
LogLevel::trace
, message); }
77
78
template
<
typename
... Args>
79
inline
void
debug
(fmt::format_string<Args...> format, Args &&...args) {
80
log
(
LogLevel::debug
, format, std::forward<Args>(args)...);
81
}
82
83
inline
void
debug
(std::string_view message) {
log
(
LogLevel::debug
, message); }
84
85
template
<
typename
... Args>
86
inline
void
info
(fmt::format_string<Args...> format, Args &&...args) {
87
log
(
LogLevel::info
, format, std::forward<Args>(args)...);
88
}
89
90
inline
void
info
(std::string_view message) {
log
(
LogLevel::info
, message); }
91
92
template
<
typename
... Args>
93
inline
void
warn
(fmt::format_string<Args...> format, Args &&...args) {
94
log
(
LogLevel::warn
, format, std::forward<Args>(args)...);
95
}
96
97
inline
void
warn
(std::string_view message) {
log
(
LogLevel::warn
, message); }
98
99
template
<
typename
... Args>
100
inline
void
error
(fmt::format_string<Args...> format, Args &&...args) {
101
log
(
LogLevel::error
, format, std::forward<Args>(args)...);
102
}
103
104
inline
void
error
(std::string_view message) {
log
(
LogLevel::error
, message); }
105
106
template
<
typename
... Args>
107
inline
void
critical
(fmt::format_string<Args...> format, Args &&...args) {
108
log
(
LogLevel::critical
, format, std::forward<Args>(args)...);
109
}
110
111
inline
void
critical
(std::string_view message) {
112
log
(
LogLevel::critical
, message);
113
}
114
115
}
// namespace reusex::core
116
117
// Promote logging functions to ReUseX namespace for convenience
118
// Only promotes logging functions, avoiding conflicts with external libraries
119
namespace
reusex
{
120
using
core::critical
;
121
using
core::debug
;
122
using
core::error
;
123
using
core::info
;
124
using
core::log
;
125
using
core::LogLevel
;
126
using
core::stopwatch;
127
using
core::trace
;
128
using
core::warn
;
129
}
// namespace reusex
130
131
namespace
fmt
{
132
template
<>
struct
formatter<
reusex
::core::stopwatch> : formatter<double> {
133
template
<
typename
FormatContext>
134
auto
format
(
const
reusex::core::stopwatch
&sw, FormatContext &ctx)
const
{
135
return
formatter<double>::format(sw.
elapsed
(), ctx);
136
}
137
};
138
}
// namespace fmt
reusex::core::stopwatch
Definition
logging.hpp:37
reusex::core::stopwatch::stopwatch
stopwatch()=default
reusex::core::stopwatch::reset
void reset()
Definition
logging.hpp:43
reusex::core::stopwatch::elapsed
auto elapsed() const -> double
Definition
logging.hpp:45
fmt
Definition
logging.hpp:131
reusex::core
Definition
filter_expression.hpp:17
reusex::core::log_message
void log_message(LogLevel level, std::string_view message)
reusex::core::debug
void debug(fmt::format_string< Args... > format, Args &&...args)
Definition
logging.hpp:79
reusex::core::log
void log(LogLevel level, fmt::format_string< Args... > format, Args &&...args)
Definition
logging.hpp:55
reusex::core::get_log_level
auto get_log_level() -> LogLevel
reusex::core::set_log_level
void set_log_level(LogLevel level)
reusex::core::set_log_handler
void set_log_handler(LogHandler handler)
reusex::core::LogHandler
std::function< void(LogLevel level, std::string_view message)> LogHandler
Definition
logging.hpp:27
reusex::core::critical
void critical(fmt::format_string< Args... > format, Args &&...args)
Definition
logging.hpp:107
reusex::core::should_log
auto should_log(LogLevel level) -> bool
reusex::core::warn
void warn(fmt::format_string< Args... > format, Args &&...args)
Definition
logging.hpp:93
reusex::core::trace
void trace(fmt::format_string< Args... > format, Args &&...args)
Definition
logging.hpp:72
reusex::core::error
void error(fmt::format_string< Args... > format, Args &&...args)
Definition
logging.hpp:100
reusex::core::LogLevel
LogLevel
Definition
logging.hpp:17
reusex::core::LogLevel::trace
@ trace
Definition
logging.hpp:18
reusex::core::LogLevel::warn
@ warn
Definition
logging.hpp:21
reusex::core::LogLevel::off
@ off
Definition
logging.hpp:24
reusex::core::LogLevel::critical
@ critical
Definition
logging.hpp:23
reusex::core::LogLevel::debug
@ debug
Definition
logging.hpp:19
reusex::core::LogLevel::info
@ info
Definition
logging.hpp:20
reusex::core::LogLevel::error
@ error
Definition
logging.hpp:22
reusex::core::info
void info(fmt::format_string< Args... > format, Args &&...args)
Definition
logging.hpp:86
reusex::core::reset_log_handler
void reset_log_handler()
reusex
Definition
filter_expression.hpp:12
fmt::formatter< reusex::core::stopwatch >::format
auto format(const reusex::core::stopwatch &sw, FormatContext &ctx) const
Definition
logging.hpp:134
libs
reusex
include
core
logging.hpp
Generated by
1.17.0