Air-Trap 1.0.0
A multiplayer R-Type clone game engine built with C++23 and ECS architecture
Loading...
Searching...
No Matches
Logger.tpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** R-Type
4** File description:
5** Logger.tpp
6*/
7
8/*
9** MIT License
10**
11** Copyright (c) 2025 Robin Toillon
12**
13** Permission is hereby granted, free of charge, to any person obtaining
14** a copy of this software and associated documentation files (the
15** "Software"), to deal in the Software without restriction, including
16** without limitation the rights to use, copy, modify, merge, publish,
17** distribute, sublicense, and/or sell copies of the Software, and to
18** permit persons to whom the Software is furnished to do so, subject to
19** the following conditions:
20**
21** The above copyright notice and this permission notice shall be
22** included in all copies or substantial portions of the Software.
23**
24** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
28** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
29** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
30** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31*/
32
33/**
34 * @file Logger.tpp
35 * @brief Logger template implementations
36 * @author Robin Toillon
37 * @details Implements the templated logging functions (debug, info,
38 * error, warning, fatal) with format string support and automatic
39 * source location capture.
40 */
41
42namespace rtp::log
43{
44 ///////////////////////////////////////////////////////////////////////////
45 // Private API
46 ///////////////////////////////////////////////////////////////////////////
47
48 namespace detail
49 {
50 template <typename... Args>
51 void safeLog(Level level, const std::source_location &loc,
52 std::format_string<Args...> fmt, Args &&...args) noexcept
53 {
54 try {
55 std::string msg = std::format(fmt,
56 std::forward<Args>(args)...);
57 logImpl(level, msg, loc);
58 } catch (...) {
59 logImpl(Level::Error,
60 "LOGGER FORMAT ERROR (OOM?)", loc);
61 }
62 }
63 }
64
65 ///////////////////////////////////////////////////////////////////////////
66 // Public API
67 ///////////////////////////////////////////////////////////////////////////
68
69 template <typename... Args>
70 void debug(LogFmt<std::type_identity_t<Args>...> wrapper,
71 Args &&...args) noexcept
72 {
73 detail::safeLog(Level::Debug, wrapper.loc, wrapper.fmt,
74 std::forward<Args>(args)...);
75 }
76
77 template <typename... Args>
78 void info(LogFmt<std::type_identity_t<Args>...> wrapper,
79 Args &&...args) noexcept
80 {
81 detail::safeLog(Level::Info, wrapper.loc, wrapper.fmt,
82 std::forward<Args>(args)...);
83 }
84
85 template <typename... Args>
86 void error(LogFmt<std::type_identity_t<Args>...> wrapper,
87 Args &&...args) noexcept
88 {
89 detail::safeLog(Level::Error, wrapper.loc, wrapper.fmt,
90 std::forward<Args>(args)...);
91 }
92
93 template <typename... Args>
94 void warning(LogFmt<std::type_identity_t<Args>...> wrapper,
95 Args &&...args) noexcept
96 {
97 detail::safeLog(Level::Warning, wrapper.loc, wrapper.fmt,
98 std::forward<Args>(args)...);
99 }
100
101 template <typename... Args>
102 void fatal(LogFmt<std::type_identity_t<Args>...> wrapper,
103 Args &&...args) noexcept
104 {
105 detail::safeLog(Level::Fatal, wrapper.loc, wrapper.fmt,
106 std::forward<Args>(args)...);
107 }
108}