BLT/include/blt/logging/logging.h

187 lines
6.2 KiB
C++

#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 <https://www.gnu.org/licenses/>.
*/
#ifndef BLT_LOGGING_LOGGING_H
#define BLT_LOGGING_LOGGING_H
#include <sstream>
#include <string>
#include <string_view>
#include <vector>
#include <cstring>
#include <functional>
#include <blt/logging/fmt_tokenizer.h>
namespace blt::logging
{
namespace detail
{
}
struct logger_t
{
explicit logger_t() = default;
template <typename... Args>
std::string log(std::string fmt, Args&&... args)
{
compile(std::move(fmt));
auto sequence = std::make_integer_sequence<size_t, sizeof...(Args)>{};
m_arg_print_funcs.clear();
m_arg_print_funcs.resize(sizeof...(Args));
create_conv_funcs(sequence, std::forward<Args>(args)...);
process_strings();
return to_string();
}
std::string to_string();
private:
template <typename... Args, size_t... Indexes>
void create_conv_funcs(std::integer_sequence<size_t, Indexes...>, Args&&... args)
{
((handle_func<Indexes>(std::forward<Args>(args))), ...);
}
template <size_t index, typename T>
void handle_func(const T& t)
{
m_arg_print_funcs[index] = [&t, this](std::ostream& stream, const fmt_spec_t& type)
{
switch (type.sign)
{
case fmt_sign_t::SPACE:
if constexpr (std::is_arithmetic_v<T>)
{
if (type.type != fmt_type_t::BINARY && t >= 0)
stream << ' ';
}
break;
case fmt_sign_t::PLUS:
if constexpr (std::is_arithmetic_v<T>)
{
if (t >= 0)
stream << '+';
}
break;
case fmt_sign_t::MINUS:
break;
}
switch (type.type)
{
case fmt_type_t::BINARY:
{
if constexpr (std::is_trivially_copyable_v<T>)
{
// copy bytes of type
char buffer[sizeof(T)];
std::memcpy(buffer, &t, sizeof(T));
// display prefix
if (type.alternate_form)
stream << '0' << (type.uppercase ? 'B' : 'b');
// print bytes
for (size_t i = 0; i < sizeof(T); ++i)
{
// print bits
for (size_t j = 0; j < 8; ++j)
stream << ((buffer[i] & (1 << j)) ? '1' : '0');
// special seperator defined via sign (weird hack, change?)
if (type.sign == fmt_sign_t::SPACE && i != sizeof(T) - 1)
stream << ' ';
}
} else
{
stream << t;
}
break;
}
case fmt_type_t::CHAR:
if constexpr (std::is_arithmetic_v<T> || std::is_convertible_v<T, char>)
{
stream << static_cast<char>(t);
} else
{
stream << t;
}
break;
case fmt_type_t::GENERAL:
if constexpr (std::is_arithmetic_v<T>)
{
if (static_cast<u64>(t) > 10e12)
exponential(stream);
else
fixed(stream);
stream << t;
} else
{
stream << t;
}
break;
default:
handle_type(stream, type);
stream << t;
}
};
}
void setup_stream(const fmt_spec_t& spec);
void process_strings();
static void handle_type(std::ostream& stream, const fmt_spec_t& spec);
static void exponential(std::ostream& stream);
static void fixed(std::ostream& stream);
void compile(std::string fmt);
std::optional<std::pair<size_t, size_t>> consume_to_next_fmt();
std::string m_fmt;
std::stringstream m_stream;
fmt_parser_t m_parser;
// normal sections of string
std::vector<std::string_view> m_string_sections;
// processed format specs
std::vector<fmt_spec_t> m_fmt_specs;
std::vector<std::function<void(std::ostream&, const fmt_spec_t&)>> m_arg_print_funcs;
size_t m_last_fmt_pos = 0;
size_t m_arg_pos = 0;
};
void print(const std::string& fmt);
void newline();
logger_t& get_global_logger();
template <typename... Args>
void print(std::string fmt, Args&&... args)
{
auto& logger = get_global_logger();
print(logger.log(std::move(fmt), std::forward<Args>(args)...));
}
template <typename... Args>
void println(std::string fmt, Args&&... args)
{
print(std::move(fmt), std::forward<Args>(args)...);
newline();
}
}
#endif // BLT_LOGGING_LOGGING_H