ReUseX  0.0.1
3D Point Cloud Processing for Building Reuse
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
15namespace ReUseX::core {
16
17enum class LogLevel {
18 trace = 0,
19 debug = 1,
20 info = 2,
21 warn = 3,
22 error = 4,
24 off = 6,
25};
26
28 std::function<void(LogLevel level, std::string_view message)>;
29
34auto should_log(LogLevel level) -> bool;
35void log_message(LogLevel level, std::string_view message);
36
37class 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
54template <typename... Args>
55inline 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
64inline void log(LogLevel level, std::string_view message) {
65 if (!should_log(level)) [[unlikely]] {
66 return;
67 }
68 log_message(level, message);
69}
70
71template <typename... Args>
72inline void trace(fmt::format_string<Args...> format, Args &&...args) {
73 log(LogLevel::trace, format, std::forward<Args>(args)...);
74}
75
76inline void trace(std::string_view message) { log(LogLevel::trace, message); }
77
78template <typename... Args>
79inline void debug(fmt::format_string<Args...> format, Args &&...args) {
80 log(LogLevel::debug, format, std::forward<Args>(args)...);
81}
82
83inline void debug(std::string_view message) { log(LogLevel::debug, message); }
84
85template <typename... Args>
86inline void info(fmt::format_string<Args...> format, Args &&...args) {
87 log(LogLevel::info, format, std::forward<Args>(args)...);
88}
89
90inline void info(std::string_view message) { log(LogLevel::info, message); }
91
92template <typename... Args>
93inline void warn(fmt::format_string<Args...> format, Args &&...args) {
94 log(LogLevel::warn, format, std::forward<Args>(args)...);
95}
96
97inline void warn(std::string_view message) { log(LogLevel::warn, message); }
98
99template <typename... Args>
100inline void error(fmt::format_string<Args...> format, Args &&...args) {
101 log(LogLevel::error, format, std::forward<Args>(args)...);
102}
103
104inline void error(std::string_view message) { log(LogLevel::error, message); }
105
106template <typename... Args>
107inline void critical(fmt::format_string<Args...> format, Args &&...args) {
108 log(LogLevel::critical, format, std::forward<Args>(args)...);
109}
110
111inline void critical(std::string_view message) {
112 log(LogLevel::critical, message);
113}
114
115} // namespace ReUseX::core
116
117namespace fmt {
118template <> struct formatter<ReUseX::core::stopwatch> : formatter<double> {
119 template <typename FormatContext>
120 auto format(const ReUseX::core::stopwatch &sw, FormatContext &ctx) const {
121 return formatter<double>::format(sw.elapsed(), ctx);
122 }
123};
124} // namespace fmt
auto elapsed() const -> double
Definition logging.hpp:45
void error(fmt::format_string< Args... > format, Args &&...args)
Definition logging.hpp:100
std::function< void(LogLevel level, std::string_view message)> LogHandler
Definition logging.hpp:27
void info(fmt::format_string< Args... > format, Args &&...args)
Definition logging.hpp:86
void set_log_handler(LogHandler handler)
void reset_log_handler()
void set_log_level(LogLevel level)
void log(LogLevel level, fmt::format_string< Args... > format, Args &&...args)
Definition logging.hpp:55
void log_message(LogLevel level, std::string_view message)
auto should_log(LogLevel level) -> bool
void critical(fmt::format_string< Args... > format, Args &&...args)
Definition logging.hpp:107
auto get_log_level() -> LogLevel
void debug(fmt::format_string< Args... > format, Args &&...args)
Definition logging.hpp:79
void warn(fmt::format_string< Args... > format, Args &&...args)
Definition logging.hpp:93
void trace(fmt::format_string< Args... > format, Args &&...args)
Definition logging.hpp:72
auto format(const ReUseX::core::stopwatch &sw, FormatContext &ctx) const
Definition logging.hpp:120