silly code
parent
4ac592beca
commit
74878d6b43
|
@ -1,6 +1,6 @@
|
|||
cmake_minimum_required(VERSION 3.20)
|
||||
include(cmake/color.cmake)
|
||||
set(BLT_VERSION 5.1.1)
|
||||
set(BLT_VERSION 5.1.2)
|
||||
|
||||
set(BLT_TARGET BLT)
|
||||
|
||||
|
|
|
@ -23,85 +23,119 @@
|
|||
#include <string>
|
||||
#include <vector>
|
||||
#include <blt/meta/meta.h>
|
||||
#include <blt/logging/fmt_tokenizer.h>
|
||||
|
||||
namespace blt::logging
|
||||
{
|
||||
struct logger_t
|
||||
{
|
||||
explicit logger_t() = default;
|
||||
struct logger_t
|
||||
{
|
||||
explicit logger_t() = default;
|
||||
|
||||
template <typename T>
|
||||
std::string print_value(T&& t)
|
||||
{
|
||||
static_assert(meta::is_streamable_v<T>, "T must be streamable in order to work with blt::logging!");
|
||||
m_stream.str("");
|
||||
m_stream.clear();
|
||||
m_stream << std::forward<T>(t);
|
||||
return m_stream.str();
|
||||
}
|
||||
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)>{};
|
||||
while (auto pair = consume_until_fmt())
|
||||
{
|
||||
auto [begin, end] = *pair;
|
||||
if (end - begin > 0)
|
||||
{
|
||||
auto format_data = handle_fmt(m_fmt.substr(begin + 1, begin - end - 1));
|
||||
auto [arg_pos, fmt_type] = format_data;
|
||||
if (arg_pos == -1)
|
||||
arg_pos = static_cast<i64>(m_arg_pos++);
|
||||
if (fmt_type)
|
||||
{
|
||||
if (fmt_type == fmt_type_t::GENERAL)
|
||||
{
|
||||
apply_func([this](auto&& value)
|
||||
{
|
||||
if (static_cast<u64>(value) > 0xFFFFFFFFFul)
|
||||
exponential();
|
||||
else
|
||||
fixed();
|
||||
m_stream << std::forward<decltype(value)>(value);
|
||||
}, arg_pos, sequence, std::forward<Args>(args)...);
|
||||
} else if (fmt_type == fmt_type_t::CHAR)
|
||||
{
|
||||
|
||||
template <typename... Args>
|
||||
std::string log(std::string fmt, Args&&... args)
|
||||
{
|
||||
compile(std::move(fmt));
|
||||
m_args_to_str.clear();
|
||||
m_args_to_str.resize(sizeof...(Args));
|
||||
insert(std::make_integer_sequence<size_t, sizeof...(Args)>{}, std::forward<Args>(args)...);
|
||||
finish();
|
||||
return to_string();
|
||||
}
|
||||
} else if (fmt_type == fmt_type_t::BINARY)
|
||||
{
|
||||
|
||||
std::string to_string();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
apply_func([this](auto&& value)
|
||||
{
|
||||
m_stream << std::forward<decltype(value)>(value);
|
||||
}, arg_pos, sequence, std::forward<Args>(args)...);
|
||||
}
|
||||
}
|
||||
else
|
||||
apply_func([this](auto&& value)
|
||||
{
|
||||
m_stream << std::forward<decltype(value)>(value);
|
||||
}, m_arg_pos++, sequence, std::forward<Args>(args)...);
|
||||
}
|
||||
finish();
|
||||
return to_string();
|
||||
}
|
||||
|
||||
private:
|
||||
template<typename... Args, size_t... Indexes>
|
||||
void insert(std::integer_sequence<size_t, Indexes...>, Args&&... args)
|
||||
{
|
||||
((handle_insert<Indexes>(std::forward<Args>(args))), ...);
|
||||
}
|
||||
std::string to_string();
|
||||
|
||||
template<size_t index, typename T>
|
||||
void handle_insert(T&& t)
|
||||
{
|
||||
m_args_to_str[index] = print_value(std::forward<T>(t));
|
||||
}
|
||||
private:
|
||||
template <typename Func, typename... Args, size_t... Indexes>
|
||||
void apply_func(const Func& func, const size_t arg, std::integer_sequence<size_t, Indexes...>, Args&&... args)
|
||||
{
|
||||
((handle_func<Indexes>(func, arg, std::forward<Args>(args))), ...);
|
||||
}
|
||||
|
||||
void handle_fmt(std::string_view fmt);
|
||||
template <size_t index, typename Func, typename T>
|
||||
void handle_func(const Func& func, const size_t arg, T&& t)
|
||||
{
|
||||
if (index == arg)
|
||||
func(std::forward<T>(t));
|
||||
}
|
||||
|
||||
const std::string& get(size_t index);
|
||||
[[nodiscard]] std::pair<i64, std::optional<fmt_type_t>> handle_fmt(std::string_view fmt);
|
||||
|
||||
void compile(std::string fmt);
|
||||
void exponential();
|
||||
void fixed();
|
||||
|
||||
bool consume_until_fmt();
|
||||
void compile(std::string fmt);
|
||||
|
||||
void finish();
|
||||
std::optional<std::pair<size_t, size_t>> consume_until_fmt();
|
||||
|
||||
std::string m_fmt;
|
||||
std::stringstream m_stream;
|
||||
std::vector<std::string> m_args_to_str;
|
||||
size_t m_last_fmt_pos = 0;
|
||||
size_t m_arg_pos = 0;
|
||||
};
|
||||
void finish();
|
||||
|
||||
void print(const std::string& fmt);
|
||||
std::string m_fmt;
|
||||
std::stringstream m_stream;
|
||||
fmt_parser_t m_parser;
|
||||
size_t m_last_fmt_pos = 0;
|
||||
size_t m_arg_pos = 0;
|
||||
};
|
||||
|
||||
void newline();
|
||||
void print(const std::string& fmt);
|
||||
|
||||
logger_t& get_global_logger();
|
||||
void newline();
|
||||
|
||||
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)...));
|
||||
}
|
||||
logger_t& get_global_logger();
|
||||
|
||||
template <typename... Args>
|
||||
void println(std::string fmt, Args&&... args)
|
||||
{
|
||||
print(std::move(fmt), std::forward<Args>(args)...);
|
||||
newline();
|
||||
}
|
||||
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
|
||||
|
|
|
@ -116,7 +116,8 @@ namespace blt::logging
|
|||
{
|
||||
consume();
|
||||
parse_fmt_spec_stage_1();
|
||||
}else
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cerr << "Expected ':' when parsing format field after arg id!" << std::endl;
|
||||
std::exit(EXIT_FAILURE);
|
||||
|
@ -260,10 +261,67 @@ namespace blt::logging
|
|||
|
||||
void fmt_parser_t::parse_precision()
|
||||
{
|
||||
|
||||
if (!has_next())
|
||||
{
|
||||
std::cerr << "Missing token when parsing precision" << std::endl;
|
||||
std::exit(EXIT_FAILURE);
|
||||
}
|
||||
auto [_, value] = next();
|
||||
m_spec.precision = std::stoll(std::string(value));
|
||||
}
|
||||
|
||||
void fmt_parser_t::parse_type()
|
||||
{
|
||||
if (!has_next())
|
||||
{
|
||||
std::cerr << "Missing token when parsing type" << std::endl;
|
||||
std::exit(EXIT_FAILURE);
|
||||
}
|
||||
auto [_, value] = next();
|
||||
if (value.size() != 1)
|
||||
{
|
||||
std::cerr << "Type contains more than one character, we are not sure how to interpret this value '" << value << "'\n";
|
||||
std::exit(EXIT_FAILURE);
|
||||
}
|
||||
m_spec.uppercase = std::isupper(value.front());
|
||||
switch (value.front())
|
||||
{
|
||||
case 'b':
|
||||
case 'B':
|
||||
m_spec.type = fmt_type_t::BINARY;
|
||||
break;
|
||||
case 'c':
|
||||
m_spec.type = fmt_type_t::CHAR;
|
||||
break;
|
||||
case 'd':
|
||||
m_spec.type = fmt_type_t::DECIMAL;
|
||||
break;
|
||||
case 'o':
|
||||
m_spec.type = fmt_type_t::OCTAL;
|
||||
break;
|
||||
case 'x':
|
||||
case 'X':
|
||||
m_spec.type = fmt_type_t::HEX;
|
||||
break;
|
||||
case 'a':
|
||||
case 'A':
|
||||
m_spec.type = fmt_type_t::HEX_FLOAT;
|
||||
break;
|
||||
case 'e':
|
||||
case 'E':
|
||||
m_spec.type = fmt_type_t::EXPONENT;
|
||||
break;
|
||||
case 'f':
|
||||
case 'F':
|
||||
m_spec.type = fmt_type_t::FIXED_POINT;
|
||||
break;
|
||||
case 'g':
|
||||
case 'G':
|
||||
m_spec.type = fmt_type_t::GENERAL;
|
||||
break;
|
||||
default:
|
||||
std::cerr << "Invalid type " << value << std::endl;
|
||||
std::exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <blt/iterator/enumerate.h>
|
||||
#include <blt/logging/logging.h>
|
||||
|
@ -35,25 +36,63 @@ namespace blt::logging
|
|||
return str;
|
||||
}
|
||||
|
||||
void logger_t::handle_fmt(const std::string_view fmt)
|
||||
std::pair<i64, std::optional<fmt_type_t>> logger_t::handle_fmt(const std::string_view fmt)
|
||||
{
|
||||
std::cout << fmt << std::endl;
|
||||
const auto spec = m_parser.parse(fmt);
|
||||
if (spec.leading_zeros)
|
||||
m_stream << std::setfill('0');
|
||||
else
|
||||
m_stream << std::setfill(' ');
|
||||
if (spec.width > 0)
|
||||
m_stream << std::setw(static_cast<i32>(spec.width));
|
||||
else
|
||||
m_stream << std::setw(0);
|
||||
if (spec.precision > 0)
|
||||
m_stream << std::setprecision(static_cast<i32>(spec.precision));
|
||||
else
|
||||
m_stream << std::setprecision(2);
|
||||
if (spec.uppercase)
|
||||
m_stream << std::uppercase;
|
||||
else
|
||||
m_stream << std::nouppercase;
|
||||
std::optional<fmt_type_t> type;
|
||||
switch (spec.type)
|
||||
{
|
||||
case fmt_type_t::BINARY:
|
||||
case fmt_type_t::CHAR:
|
||||
case fmt_type_t::GENERAL:
|
||||
type = spec.type;
|
||||
break;
|
||||
case fmt_type_t::DECIMAL:
|
||||
m_stream << std::dec;
|
||||
break;
|
||||
case fmt_type_t::OCTAL:
|
||||
m_stream << std::oct;
|
||||
break;
|
||||
case fmt_type_t::HEX:
|
||||
m_stream << std::hex;
|
||||
break;
|
||||
case fmt_type_t::HEX_FLOAT:
|
||||
m_stream << std::hexfloat;
|
||||
break;
|
||||
case fmt_type_t::EXPONENT:
|
||||
m_stream << std::scientific;
|
||||
break;
|
||||
case fmt_type_t::FIXED_POINT:
|
||||
m_stream << std::fixed;
|
||||
break;
|
||||
}
|
||||
return {spec.arg_id, type};
|
||||
}
|
||||
|
||||
const std::string& logger_t::get(const size_t index)
|
||||
void logger_t::exponential()
|
||||
{
|
||||
if (index >= m_args_to_str.size())
|
||||
{
|
||||
std::cerr << "Insufficient number of arguments provided to format string '" << m_fmt << "' got ";
|
||||
for (const auto& [i, arg] : enumerate(std::as_const(m_args_to_str)))
|
||||
{
|
||||
std::cerr << '\'' << arg << "'";
|
||||
if (i != m_args_to_str.size() - 1)
|
||||
std::cerr << " ";
|
||||
}
|
||||
std::exit(EXIT_FAILURE);
|
||||
}
|
||||
return m_args_to_str[index];
|
||||
m_stream << std::scientific;
|
||||
}
|
||||
|
||||
void logger_t::fixed()
|
||||
{
|
||||
m_stream << std::fixed;
|
||||
}
|
||||
|
||||
void logger_t::compile(std::string fmt)
|
||||
|
@ -61,13 +100,15 @@ namespace blt::logging
|
|||
m_fmt = std::move(fmt);
|
||||
m_last_fmt_pos = 0;
|
||||
m_arg_pos = 0;
|
||||
m_stream.str("");
|
||||
m_stream.clear();
|
||||
}
|
||||
|
||||
bool logger_t::consume_until_fmt()
|
||||
std::optional<std::pair<size_t, size_t>> logger_t::consume_until_fmt()
|
||||
{
|
||||
const auto begin = m_fmt.find('{', m_last_fmt_pos);
|
||||
if (begin == std::string::npos)
|
||||
return false;
|
||||
return {};
|
||||
const auto end = m_fmt.find('}', begin);
|
||||
if (end == std::string::npos)
|
||||
{
|
||||
|
@ -75,25 +116,12 @@ namespace blt::logging
|
|||
std::exit(EXIT_FAILURE);
|
||||
}
|
||||
m_stream << std::string_view(m_fmt.data() + static_cast<ptrdiff_t>(m_last_fmt_pos), begin - m_last_fmt_pos);\
|
||||
if (end - begin > 1)
|
||||
handle_fmt(std::string_view(m_fmt.data() + static_cast<ptrdiff_t>(begin + 1), end - begin - 1));
|
||||
else
|
||||
{
|
||||
// no arguments, must consume from args
|
||||
m_stream << get(m_arg_pos++);
|
||||
}
|
||||
m_last_fmt_pos = end + 1;
|
||||
return true;
|
||||
return std::pair{begin, end};
|
||||
}
|
||||
|
||||
void logger_t::finish()
|
||||
{
|
||||
m_stream.str("");
|
||||
m_stream.clear();
|
||||
|
||||
while (consume_until_fmt())
|
||||
{}
|
||||
|
||||
m_stream << std::string_view(m_fmt.data() + static_cast<ptrdiff_t>(m_last_fmt_pos), m_fmt.size() - m_last_fmt_pos);
|
||||
m_last_fmt_pos = m_fmt.size();
|
||||
}
|
||||
|
|
|
@ -27,5 +27,10 @@ int main()
|
|||
blt::logging::println("This is a println with args '{}'", 42);
|
||||
blt::logging::println("This is a println with multiple args '{}' '{}' '{}'", 42, 32.34231233, "Hello World!");
|
||||
blt::logging::println("This is a '{1}' fmt string with positionals '{0}'", "I am a string!", "Well so am I except cooler :3");
|
||||
blt::logging::println("This is a println with a sign {:+}", 4120);
|
||||
blt::logging::println("This is a println with a space {: }", 4120);
|
||||
blt::logging::println("This is a println with a with {:3}", 4120);
|
||||
blt::logging::println("This is a println with a with leading zeros {:010}", 4120);
|
||||
blt::logging::println("This is a println with a precision {:.3}", 42.232342349);
|
||||
// blt::logging::println("This is println {}\twith a std::endl in the middle of it");
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue