#pragma once /* * Copyright (C) 2024 Brett Terpstra * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #ifndef BLT_LOGGING_LOGGING_H #define BLT_LOGGING_LOGGING_H #include #include #include #include namespace blt::logging { struct logger_t { explicit logger_t(std::string fmt): fmt(std::move(fmt)) { } template std::string make_string(T&& t) { if constexpr (std::is_same_v || std::is_convertible_v) return std::forward(t); else if constexpr (std::is_same_v || std::is_same_v, char*> || std::is_convertible_v< T, std::string_view>) return std::string(std::forward(t)); else if constexpr (std::is_same_v) return std::string() + std::forward(t); else if constexpr (std::is_arithmetic_v) return std::to_string(std::forward(t)); else { BLT_UNREACHABLE; } } void compile(); void insert_next_value(const std::string& arg); template const std::string& log(Args&&... args) { (insert_next_value(make_string(std::forward(args))), ...); return fmt; } private: std::string fmt; }; void print(const std::string& fmt); void newline(); template void print(std::string fmt, Args&&... args) { logger_t logger{std::move(fmt)}; logger.compile(); print(logger.log(std::forward(args)...)); } template void println(std::string fmt, Args&&... args) { print(std::move(fmt), std::forward(args)...); newline(); } } #endif // BLT_LOGGING_LOGGING_H