first commit: echo server

This commit is contained in:
Antonio Cossari
2026-09-07 00:45:35 +02:00
commit b15ce130f9
47 changed files with 2614 additions and 0 deletions
+61
View File
@@ -0,0 +1,61 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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