62 lines
2.9 KiB
C++
62 lines
2.9 KiB
C++
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* Log.hpp :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: acossari <acossari@student.codam.nl> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2026/08/19 20:30:58 by acossari #+# #+# */
|
|
/* Updated: 2026/08/20 22:18:40 by acossari ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#pragma once
|
|
|
|
#include <format>
|
|
#include <string_view>
|
|
|
|
// Free functions in a namespace, not a class: the whole process shares one
|
|
// verbosity level and one destination, so a second instance would have nothing
|
|
// of its own to hold.
|
|
namespace webserv::log {
|
|
|
|
// The "class" keyword here is overloaded: not a class but a scoped enum.
|
|
// Names must be written Level::Info, plus no implicit conversion to int.
|
|
enum class Level { Debug, Info, Warn, Error };
|
|
|
|
// Thin Template Idiom: vlog keeps one ordinary body instead of repeating it
|
|
// for every argument type combination. The small wrappers retain compile time
|
|
// checks, erase those types and converge here at the shared sink. Its v follows
|
|
// the C variadic convention. The sink filters by level before formatting.
|
|
void vlog(Level messageLevel, std::string_view formatString,
|
|
std::format_args args);
|
|
|
|
// format_string uses Args to check at compile time that every placeholder
|
|
// receives a value of the expected type. Args&& is a "forwarding reference",
|
|
// so the same wrapper accepts lvalues and rvalues, as in info("{}", value)
|
|
// and info("{}", 42), without copying them.
|
|
template <typename... Args>
|
|
void debug(std::format_string<Args...> formatString, Args&&... args) {
|
|
// make_format_args packs the arguments together with their type information.
|
|
// vlog then sees only std::format_args and does not need a separate template
|
|
// version for every combination of argument types.
|
|
vlog(Level::Debug, formatString.get(), std::make_format_args(args...));
|
|
}
|
|
|
|
template <typename... Args>
|
|
void info(std::format_string<Args...> formatString, Args&&... args) {
|
|
vlog(Level::Info, formatString.get(), std::make_format_args(args...));
|
|
}
|
|
|
|
template <typename... Args>
|
|
void warn(std::format_string<Args...> formatString, Args&&... args) {
|
|
vlog(Level::Warn, formatString.get(), std::make_format_args(args...));
|
|
}
|
|
|
|
template <typename... Args>
|
|
void error(std::format_string<Args...> formatString, Args&&... args) {
|
|
vlog(Level::Error, formatString.get(), std::make_format_args(args...));
|
|
}
|
|
|
|
} // namespace webserv::log
|