2023-07-03 13:10:16 +02:00
|
|
|
#include "utils/Logs.hpp"
|
2023-07-06 18:16:25 +02:00
|
|
|
#include <cstdarg>
|
|
|
|
|
#include <cstdint>
|
2023-07-03 13:10:16 +02:00
|
|
|
#include <sstream>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <string_view>
|
2023-07-10 03:58:21 +02:00
|
|
|
#include <unistd.h>
|
2023-07-03 13:10:16 +02:00
|
|
|
#include <vector>
|
|
|
|
|
|
2023-07-14 14:01:36 +02:00
|
|
|
static void append_hex(std::string &out, std::unsigned_integral auto value) {
|
2023-07-03 13:10:16 +02:00
|
|
|
std::ostringstream buf;
|
2023-07-14 14:01:36 +02:00
|
|
|
if (value < 10)
|
|
|
|
|
buf << value;
|
|
|
|
|
else if (value >= decltype(value)(UINTMAX_MAX) - 1)
|
2023-07-15 16:24:00 +02:00
|
|
|
buf << "-" << std::uintmax_t(decltype(value)(-value));
|
2023-07-14 14:01:36 +02:00
|
|
|
else
|
2023-07-15 16:24:00 +02:00
|
|
|
buf << "0x" << std::hex << std::uintmax_t(value);
|
2023-07-03 13:10:16 +02:00
|
|
|
out += buf.str();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace orbis::logs {
|
2023-07-07 01:03:30 +02:00
|
|
|
void log_class_string<void *>::format(std::string &out, const void *arg) {
|
2023-07-03 13:10:16 +02:00
|
|
|
const void *ptr = *reinterpret_cast<const void *const *>(arg);
|
|
|
|
|
append_hex(out, reinterpret_cast<std::uintptr_t>(ptr));
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-08 18:09:31 +02:00
|
|
|
void log_class_string<char *>::format_n(std::string &out, const void *arg,
|
|
|
|
|
std::size_t n) {
|
|
|
|
|
const char *ptr = reinterpret_cast<const char *>(arg);
|
|
|
|
|
const auto addr = reinterpret_cast<std::uintptr_t>(ptr);
|
|
|
|
|
const auto _end = n ? addr + n - 1 : addr;
|
|
|
|
|
if (addr < 0x10000 || std::max(n, _end) > 0x7fff'ffff'ffff) {
|
|
|
|
|
out += "{{{{{BAD_ADDR:";
|
|
|
|
|
append_hex(out, addr);
|
|
|
|
|
out += "}}}}}";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
while (n--) {
|
|
|
|
|
const char c = *ptr++;
|
|
|
|
|
if (!c)
|
|
|
|
|
break;
|
|
|
|
|
out += c;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-07 01:03:30 +02:00
|
|
|
void log_class_string<char *>::format(std::string &out, const void *arg) {
|
2023-07-08 18:09:31 +02:00
|
|
|
const char *ptr = *reinterpret_cast<const char *const *>(arg);
|
2023-07-08 16:39:59 +02:00
|
|
|
const auto addr = reinterpret_cast<std::uintptr_t>(ptr);
|
|
|
|
|
if (addr < 0x10000 || addr > 0x7fff'ffff'ffff) {
|
|
|
|
|
out += "{{{{{BAD_ADDR:";
|
|
|
|
|
append_hex(out, addr);
|
|
|
|
|
out += "}}}}}";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
out += ptr;
|
2023-07-03 13:10:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
|
void log_class_string<std::string>::format(std::string &out, const void *arg) {
|
|
|
|
|
out += get_object(arg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
|
void log_class_string<std::string_view>::format(std::string &out,
|
|
|
|
|
const void *arg) {
|
|
|
|
|
out += get_object(arg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
|
void log_class_string<std::vector<char>>::format(std::string &out,
|
|
|
|
|
const void *arg) {
|
|
|
|
|
const std::vector<char> &obj = get_object(arg);
|
|
|
|
|
out.append(obj.cbegin(), obj.cend());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
|
void log_class_string<std::u8string>::format(std::string &out,
|
|
|
|
|
const void *arg) {
|
|
|
|
|
const std::u8string &obj = get_object(arg);
|
|
|
|
|
out.append(obj.cbegin(), obj.cend());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
|
void log_class_string<std::u8string_view>::format(std::string &out,
|
|
|
|
|
const void *arg) {
|
|
|
|
|
const std::u8string_view &obj = get_object(arg);
|
|
|
|
|
out.append(obj.cbegin(), obj.cend());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
|
void log_class_string<std::vector<char8_t>>::format(std::string &out,
|
|
|
|
|
const void *arg) {
|
|
|
|
|
const std::vector<char8_t> &obj = get_object(arg);
|
|
|
|
|
out.append(obj.cbegin(), obj.cend());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
|
void log_class_string<char>::format(std::string &out, const void *arg) {
|
|
|
|
|
append_hex(out, static_cast<unsigned char>(get_object(arg)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
|
void log_class_string<unsigned char>::format(std::string &out,
|
|
|
|
|
const void *arg) {
|
|
|
|
|
append_hex(out, get_object(arg));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
|
void log_class_string<signed char>::format(std::string &out, const void *arg) {
|
|
|
|
|
append_hex(out, static_cast<unsigned char>(get_object(arg)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
|
void log_class_string<short>::format(std::string &out, const void *arg) {
|
|
|
|
|
append_hex(out, static_cast<unsigned short>(get_object(arg)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
|
void log_class_string<ushort>::format(std::string &out, const void *arg) {
|
|
|
|
|
append_hex(out, get_object(arg));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
|
void log_class_string<int>::format(std::string &out, const void *arg) {
|
|
|
|
|
append_hex(out, static_cast<uint>(get_object(arg)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
|
void log_class_string<uint>::format(std::string &out, const void *arg) {
|
|
|
|
|
append_hex(out, get_object(arg));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
|
void log_class_string<long>::format(std::string &out, const void *arg) {
|
|
|
|
|
append_hex(out, static_cast<unsigned long>(get_object(arg)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
|
void log_class_string<ulong>::format(std::string &out, const void *arg) {
|
|
|
|
|
append_hex(out, get_object(arg));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
|
void log_class_string<long long>::format(std::string &out, const void *arg) {
|
|
|
|
|
append_hex(out, static_cast<unsigned long long>(get_object(arg)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
|
void log_class_string<unsigned long long>::format(std::string &out,
|
|
|
|
|
const void *arg) {
|
|
|
|
|
append_hex(out, get_object(arg));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
|
void log_class_string<float>::format(std::string &out, const void *arg) {
|
|
|
|
|
std::ostringstream buf(out, std::ios_base::ate);
|
|
|
|
|
buf << get_object(arg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
|
void log_class_string<double>::format(std::string &out, const void *arg) {
|
|
|
|
|
std::ostringstream buf(out, std::ios_base::ate);
|
|
|
|
|
buf << get_object(arg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
|
void log_class_string<bool>::format(std::string &out, const void *arg) {
|
|
|
|
|
out += get_object(arg) ? "1" : "0";
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-15 08:25:22 +02:00
|
|
|
void _orbis_log_print(LogLevel lvl, std::string_view msg,
|
|
|
|
|
std::string_view names, const log_type_info *sup, ...) {
|
2023-07-07 01:03:30 +02:00
|
|
|
if (lvl > logs_level.load(std::memory_order::relaxed)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-07-03 13:10:16 +02:00
|
|
|
|
|
|
|
|
/*constinit thread_local*/ std::string text;
|
|
|
|
|
/*constinit thread_local*/ std::vector<const void *> args;
|
|
|
|
|
|
|
|
|
|
std::size_t args_count = 0;
|
|
|
|
|
for (auto v = sup; v && v->log_string; v++)
|
|
|
|
|
args_count++;
|
|
|
|
|
|
|
|
|
|
text.reserve(50000);
|
|
|
|
|
args.resize(args_count);
|
|
|
|
|
|
|
|
|
|
va_list c_args;
|
|
|
|
|
va_start(c_args, sup);
|
|
|
|
|
for (const void *&arg : args)
|
|
|
|
|
arg = va_arg(c_args, const void *);
|
|
|
|
|
va_end(c_args);
|
|
|
|
|
|
|
|
|
|
text += msg;
|
2023-07-12 20:41:14 +02:00
|
|
|
if (args_count)
|
|
|
|
|
text += "(";
|
2023-07-03 13:10:16 +02:00
|
|
|
for (std::size_t i = 0; i < args_count; i++) {
|
|
|
|
|
if (i)
|
|
|
|
|
text += ", ";
|
|
|
|
|
names.remove_prefix(names.find_first_not_of(" \t\n\r"));
|
|
|
|
|
std::string_view name = names.substr(0, names.find_first_of(","));
|
|
|
|
|
names.remove_prefix(name.size() + 1);
|
|
|
|
|
text += name;
|
|
|
|
|
text += "=";
|
|
|
|
|
sup[i].log_string(text, args[i]);
|
|
|
|
|
}
|
2023-07-12 20:41:14 +02:00
|
|
|
if (args_count)
|
|
|
|
|
text += ")";
|
2023-07-03 13:10:16 +02:00
|
|
|
|
2023-07-09 11:07:39 +02:00
|
|
|
const char *color = "";
|
|
|
|
|
switch (lvl) {
|
|
|
|
|
case LogLevel::Always:
|
|
|
|
|
color = "\e[36;1m";
|
|
|
|
|
break;
|
|
|
|
|
case LogLevel::Fatal:
|
|
|
|
|
color = "\e[35;1m";
|
|
|
|
|
break;
|
|
|
|
|
case LogLevel::Error:
|
|
|
|
|
color = "\e[0;31m";
|
|
|
|
|
break;
|
|
|
|
|
case LogLevel::Todo:
|
|
|
|
|
color = "\e[1;33m";
|
|
|
|
|
break;
|
|
|
|
|
case LogLevel::Success:
|
|
|
|
|
color = "\e[1;32m";
|
|
|
|
|
break;
|
|
|
|
|
case LogLevel::Warning:
|
|
|
|
|
color = "\e[0;33m";
|
|
|
|
|
break;
|
|
|
|
|
case LogLevel::Notice:
|
|
|
|
|
color = "";
|
|
|
|
|
break;
|
|
|
|
|
case LogLevel::Trace:
|
|
|
|
|
color = "";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-10 03:58:21 +02:00
|
|
|
static const bool istty = isatty(fileno(stderr));
|
|
|
|
|
if (istty) {
|
|
|
|
|
std::fprintf(stderr, "%s%s\e[0m\n", color, text.c_str());
|
|
|
|
|
} else {
|
|
|
|
|
std::fprintf(stderr, "%s\n", text.c_str());
|
|
|
|
|
}
|
2023-07-03 13:10:16 +02:00
|
|
|
}
|
|
|
|
|
} // namespace orbis::logs
|