pars 0.2.1
Loading...
Searching...
No Matches
log.h
Go to the documentation of this file.
1/*
2Copyright (c) 2025 Giuseppe Roberti.
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without modification,
6are permitted provided that the following conditions are met:
7
81. Redistributions of source code must retain the above copyright notice, this
9list of conditions and the following disclaimer.
10
112. Redistributions in binary form must reproduce the above copyright notice,
12this list of conditions and the following disclaimer in the documentation and/or
13other materials provided with the distribution.
14
153. Neither the name of the copyright holder nor the names of its contributors
16may be used to endorse or promote products derived from this software without
17specific prior written permission.
18
19THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*/
30#pragma once
31
32#include "init.h"
33
34#include <spdlog/common.h>
35
36// include generated config file
37#include "config.h"
38
39// include spdlog
40#undef SPDLOG_ACTIVE_LEVEL
41#define SPDLOG_ACTIVE_LEVEL pars_log_level
42#include <spdlog/spdlog.h>
43
44#include <spdlog/sinks/basic_file_sink.h>
45#include <spdlog/sinks/null_sink.h>
46#include <spdlog/sinks/stdout_color_sinks.h>
47
48#if defined(_WIN32)
49#define PARS_LOG_ENABLE_MSVC
50#include <spdlog/sinks/msvc_sink.h>
51#else
52#define PARS_LOG_ENABLE_SYSLOG
53#include <spdlog/sinks/syslog_sink.h>
54#endif
55
56// spdlog helper macro
57
58#define SL (spdlog::source_loc{__FILE__, __LINE__, SPDLOG_FUNCTION})
59
60namespace pars
61{
62
63template<typename... args_t>
64inline void log(spdlog::source_loc loc, pars::lf lf,
65 spdlog::level::level_enum lvl,
66 spdlog::format_string_t<args_t...> fmt, args_t&&... args)
67{
68 if constexpr (pars_log_enabled)
69 {
70 if (pars_log_flags & lf)
71 ::spdlog::log(loc, lvl, fmt, std::forward<args_t>(args)...);
72 }
73}
74
75template<typename... args_t>
76inline void log(pars::lf lf, spdlog::level::level_enum lvl,
77 spdlog::format_string_t<args_t...> fmt, args_t&&... args)
78{
79 if constexpr (pars_log_enabled)
80 {
81 if (pars_log_flags & lf)
82 ::spdlog::log(lvl, fmt, std::forward<args_t>(args)...);
83 }
84}
85
86template<typename... args_t>
87inline void log(spdlog::source_loc loc, spdlog::level::level_enum lvl,
88 spdlog::format_string_t<args_t...> fmt, args_t&&... args)
89{
90 ::pars::log(loc, pars::lf::user, lvl, fmt, std::forward<args_t>(args)...);
91}
92
93template<typename... args_t>
94inline void log(spdlog::level::level_enum lvl,
95 spdlog::format_string_t<args_t...> fmt, args_t&&... args)
96{
97 ::pars::log(pars::lf::user, lvl, fmt, std::forward<args_t>(args)...);
98}
99
100template<typename... args_t>
101inline void trace(spdlog::source_loc loc, pars::lf lf,
102 spdlog::format_string_t<args_t...> fmt, args_t&&... args)
103{
104 ::pars::log(loc, lf, spdlog::level::trace, fmt,
105 std::forward<args_t>(args)...);
106}
107
108template<typename... args_t>
109inline void trace(pars::lf lf, spdlog::format_string_t<args_t...> fmt,
110 args_t&&... args)
111{
112 ::pars::log(lf, spdlog::level::trace, fmt, std::forward<args_t>(args)...);
113}
114
115template<typename... args_t>
116inline void trace(spdlog::source_loc loc,
117 spdlog::format_string_t<args_t...> fmt, args_t&&... args)
118{
119 ::pars::log(loc, spdlog::level::trace, fmt, std::forward<args_t>(args)...);
120}
121
122template<typename... args_t>
123inline void trace(spdlog::format_string_t<args_t...> fmt, args_t&&... args)
124{
125 ::pars::log(spdlog::level::trace, fmt, std::forward<args_t>(args)...);
126}
127
128template<typename... args_t>
129inline void debug(spdlog::source_loc loc, pars::lf lf,
130 spdlog::format_string_t<args_t...> fmt, args_t&&... args)
131{
132 ::pars::log(loc, lf, spdlog::level::debug, fmt,
133 std::forward<args_t>(args)...);
134}
135
136template<typename... args_t>
137inline void debug(pars::lf lf, spdlog::format_string_t<args_t...> fmt,
138 args_t&&... args)
139{
140 ::pars::log(lf, spdlog::level::debug, fmt, std::forward<args_t>(args)...);
141}
142
143template<typename... args_t>
144inline void debug(spdlog::source_loc loc,
145 spdlog::format_string_t<args_t...> fmt, args_t&&... args)
146{
147 ::pars::log(loc, spdlog::level::debug, fmt, std::forward<args_t>(args)...);
148}
149
150template<typename... args_t>
151inline void debug(spdlog::format_string_t<args_t...> fmt, args_t&&... args)
152{
153 ::pars::log(spdlog::level::debug, fmt, std::forward<args_t>(args)...);
154}
155
156template<typename... args_t>
157inline void info(spdlog::source_loc loc, pars::lf lf,
158 spdlog::format_string_t<args_t...> fmt, args_t&&... args)
159{
160 ::pars::log(loc, lf, spdlog::level::info, fmt, std::forward<args_t>(args)...);
161}
162
163template<typename... args_t>
164inline void info(pars::lf lf, spdlog::format_string_t<args_t...> fmt,
165 args_t&&... args)
166{
167 ::pars::log(lf, spdlog::level::info, fmt, std::forward<args_t>(args)...);
168}
169
170template<typename... args_t>
171inline void info(spdlog::source_loc loc, spdlog::format_string_t<args_t...> fmt,
172 args_t&&... args)
173{
174 ::pars::log(loc, spdlog::level::info, fmt, std::forward<args_t>(args)...);
175}
176
177template<typename... args_t>
178inline void info(spdlog::format_string_t<args_t...> fmt, args_t&&... args)
179{
180 ::pars::log(spdlog::level::info, fmt, std::forward<args_t>(args)...);
181}
182
183template<typename... args_t>
184inline void warn(spdlog::source_loc loc, pars::lf lf,
185 spdlog::format_string_t<args_t...> fmt, args_t&&... args)
186{
187 ::pars::log(loc, lf, spdlog::level::warn, fmt, std::forward<args_t>(args)...);
188}
189
190template<typename... args_t>
191inline void warn(pars::lf lf, spdlog::format_string_t<args_t...> fmt,
192 args_t&&... args)
193{
194 ::pars::log(lf, spdlog::level::warn, fmt, std::forward<args_t>(args)...);
195}
196
197template<typename... args_t>
198inline void warn(spdlog::source_loc loc, spdlog::format_string_t<args_t...> fmt,
199 args_t&&... args)
200{
201 ::pars::log(loc, spdlog::level::warn, fmt, std::forward<args_t>(args)...);
202}
203
204template<typename... args_t>
205inline void warn(spdlog::format_string_t<args_t...> fmt, args_t&&... args)
206{
207 ::pars::log(spdlog::level::warn, fmt, std::forward<args_t>(args)...);
208}
209
210template<typename... args_t>
211inline void err(spdlog::source_loc loc, pars::lf lf,
212 spdlog::format_string_t<args_t...> fmt, args_t&&... args)
213{
214 ::pars::log(loc, lf, spdlog::level::err, fmt, std::forward<args_t>(args)...);
215}
216
217template<typename... args_t>
218inline void err(pars::lf lf, spdlog::format_string_t<args_t...> fmt,
219 args_t&&... args)
220{
221 ::pars::log(lf, spdlog::level::err, fmt, std::forward<args_t>(args)...);
222}
223
224template<typename... args_t>
225inline void err(spdlog::source_loc loc, spdlog::format_string_t<args_t...> fmt,
226 args_t&&... args)
227{
228 ::pars::log(loc, spdlog::level::err, fmt, std::forward<args_t>(args)...);
229}
230
231template<typename... args_t>
232inline void err(spdlog::format_string_t<args_t...> fmt, args_t&&... args)
233{
234 ::pars::log(spdlog::level::err, fmt, std::forward<args_t>(args)...);
235}
236
237template<typename... args_t>
238inline void critical(spdlog::source_loc loc, pars::lf lf,
239 spdlog::format_string_t<args_t...> fmt, args_t&&... args)
240{
241 ::pars::log(lf, loc, spdlog::level::critical, fmt,
242 std::forward<args_t>(args)...);
243}
244
245template<typename... args_t>
246inline void critical(pars::lf lf, spdlog::format_string_t<args_t...> fmt,
247 args_t&&... args)
248{
249 ::pars::log(lf, spdlog::level::critical, fmt, std::forward<args_t>(args)...);
250}
251
252template<typename... args_t>
253inline void critical(spdlog::source_loc loc,
254 spdlog::format_string_t<args_t...> fmt, args_t&&... args)
255{
256 ::pars::log(loc, spdlog::level::critical, fmt, std::forward<args_t>(args)...);
257}
258
259template<typename... args_t>
260inline void critical(spdlog::format_string_t<args_t...> fmt, args_t&&... args)
261{
262 ::pars::log(spdlog::level::critical, fmt, std::forward<args_t>(args)...);
263}
264
265} // namespace pars
void info(spdlog::source_loc loc, pars::lf lf, spdlog::format_string_t< args_t... > fmt, args_t &&... args)
Definition log.h:157
lf
Definition flags.h:40
@ user
Definition flags.h:45
void err(spdlog::source_loc loc, pars::lf lf, spdlog::format_string_t< args_t... > fmt, args_t &&... args)
Definition log.h:211
void log(spdlog::source_loc loc, pars::lf lf, spdlog::level::level_enum lvl, spdlog::format_string_t< args_t... > fmt, args_t &&... args)
Definition log.h:64
void trace(spdlog::source_loc loc, pars::lf lf, spdlog::format_string_t< args_t... > fmt, args_t &&... args)
Definition log.h:101
void debug(spdlog::source_loc loc, pars::lf lf, spdlog::format_string_t< args_t... > fmt, args_t &&... args)
Definition log.h:129
void critical(spdlog::source_loc loc, pars::lf lf, spdlog::format_string_t< args_t... > fmt, args_t &&... args)
Definition log.h:238
void warn(spdlog::source_loc loc, pars::lf lf, spdlog::format_string_t< args_t... > fmt, args_t &&... args)
Definition log.h:184