config works

main
Brett 2025-03-07 18:40:43 -05:00
parent 66efccf095
commit 07a11656fa
7 changed files with 343 additions and 171 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.20)
include(cmake/color.cmake)
set(BLT_VERSION 5.2.1)
set(BLT_VERSION 5.2.2)
set(BLT_TARGET BLT)

View File

@ -99,7 +99,7 @@ namespace blt::fs
void write(const T& t)
{
static_assert(std::is_trivially_copyable_v<T>);
m_writer->write(reinterpret_cast<char*>(&t), sizeof(T));
m_writer->write(reinterpret_cast<const char*>(&t), sizeof(T));
}
template <typename T>

View File

@ -25,20 +25,19 @@
#include <string_view>
#include <vector>
#include <blt/logging/fmt_tokenizer.h>
#include <blt/logging/logging_config.h>
#include <blt/meta/is_streamable.h>
#include <blt/std/utility.h>
namespace blt::logging
{
namespace detail
{
}
{}
struct logger_t
{
explicit logger_t(std::ostream& stream): m_stream(stream)
{
}
{}
template <typename... Args>
std::string log(std::string fmt, Args&&... args)
@ -64,8 +63,7 @@ namespace blt::logging
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)
{
m_arg_print_funcs[index] = [&t, this](std::ostream& stream, const fmt_spec_t& type) {
switch (type.sign)
{
case fmt_sign_t::SPACE:
@ -101,8 +99,7 @@ namespace blt::logging
if (type.sign == fmt_sign_t::SPACE && i != sizeof(T) - 1)
stream << ' ';
}
}
else
} else
{
if constexpr (blt::meta::is_streamable_v<T>)
stream << t;
@ -115,8 +112,7 @@ namespace blt::logging
if constexpr (std::is_arithmetic_v<T> || std::is_convertible_v<T, char>)
{
stream << static_cast<char>(t);
}
else
} else
{
if constexpr (blt::meta::is_streamable_v<T>)
stream << t;
@ -173,6 +169,10 @@ namespace blt::logging
logger_t& get_global_logger();
logging_config_t& get_global_config();
void set_thread_name(const std::string& name);
template <typename... Args>
void print(std::string fmt, Args&&... args)
{

View File

@ -22,9 +22,9 @@
#include <array>
#include <string>
#include <vector>
#include <blt/logging/logging.h>
#include <blt/std/types.h>
#include <blt/fs/fwddecl.h>
#include <blt/logging/fwddecl.h>
#include <blt/std/types.h>
namespace blt::logging
{
@ -121,6 +121,79 @@ namespace blt::logging
friend logger_t;
public:
logging_config_t()
{
compile();
}
void compile();
logging_config_t& add_log_output(fs::writer_t* writer)
{
log_outputs.push_back(writer);
return *this;
}
logging_config_t& set_log_format(std::string format)
{
log_format = std::move(format);
compile();
return *this;
}
logging_config_t& set_error_color(std::string color)
{
error_color = std::move(color);
compile();
return *this;
}
logging_config_t& set_log_level_colors(std::array<std::string, LOG_LEVEL_COUNT> colors)
{
log_level_colors = std::move(colors);
compile();
return *this;
}
logging_config_t& set_log_level_names(std::array<std::string, LOG_LEVEL_COUNT> names)
{
log_level_names = std::move(names);
return *this;
}
logging_config_t& set_level(const log_level_t level)
{
this->level = level;
return *this;
}
logging_config_t& set_use_color(const bool use_color)
{
this->use_color = use_color;
compile();
return *this;
}
logging_config_t& set_print_full_name(const bool print_full_name)
{
this->print_full_name = print_full_name;
return *this;
}
logging_config_t& set_ensure_alignment(const bool ensure_alignment)
{
this->ensure_alignment = ensure_alignment;
return *this;
}
[[nodiscard]] std::pair<const std::vector<tags::detail::log_tag_token_t>&, const std::vector<std::string>&> get_log_tag_tokens() const
{
return {log_tag_tokens, log_tag_content};
}
private:
std::vector<std::string> log_tag_content;
std::vector<tags::detail::log_tag_token_t> log_tag_tokens;
// wrappers for streams exist in blt/fs/stream_wrappers.h
std::vector<fs::writer_t*> log_outputs = get_default_log_outputs();
std::string log_format = get_default_log_format();
@ -135,7 +208,6 @@ namespace blt::logging
// This creates output where the user message always starts at the same column.
bool ensure_alignment = true;
private:
static std::string get_default_log_format();
static std::vector<fs::writer_t*> get_default_log_outputs();
static std::array<std::string, LOG_LEVEL_COUNT> get_default_log_level_colors();

View File

@ -17,19 +17,37 @@
*/
#include <iomanip>
#include <iostream>
#include <mutex>
#include <sstream>
#include <thread>
#include <blt/iterator/enumerate.h>
#include <blt/logging/logging.h>
#include <blt/std/hashmap.h>
#include <blt/std/types.h>
namespace blt::logging
{
struct global_context_t
{
logging_config_t global_config;
std::mutex thread_name_mutex;
hashmap_t<std::thread::id, std::string> thread_names;
};
static global_context_t global_context;
struct logging_thread_context_t
{
std::stringstream stream;
logger_t logger{stream};
};
logging_thread_context_t& get_thread_context()
{
thread_local logging_thread_context_t context;
return context;
}
std::string logger_t::to_string() const
{
return dynamic_cast<std::stringstream&>(m_stream).str();
@ -195,8 +213,7 @@ namespace blt::logging
logger_t& get_global_logger()
{
thread_local logging_thread_context_t context;
return context.logger;
return get_thread_context().logger;
}
void print(const std::string& str)
@ -208,4 +225,15 @@ namespace blt::logging
{
std::cout << std::endl;
}
logging_config_t& get_global_config()
{
return global_context.global_config;
}
void set_thread_name(const std::string& name)
{
std::scoped_lock lock{global_context.thread_name_mutex};
global_context.thread_names[std::this_thread::get_id()] = name;
}
}

View File

@ -52,7 +52,86 @@ namespace blt::logging
return map;
}
hashmap_t<std::string_view, log_tag_token_t> tag_map = make_map();
std::array<bool, static_cast<u8>(log_tag_token_t::CONTENT)> tag_known_values{
// year
false,
// month
false,
// day
false,
// hour
false,
// minute
false,
// second
false,
// ms
false,
// ns
false,
// unix
false,
// iso year
false,
// time
false,
// full_time
false,
// lc
true,
// ec
true,
// conditional error
false,
// reset
true,
// log level
false,
// thread_name
false,
// file
false,
// line
false,
// str
false
};
}
void logging_config_t::compile()
{
static hashmap_t<std::string_view, tags::detail::log_tag_token_t> tag_map = tags::detail::make_map();
log_tag_content.clear();
log_tag_tokens.clear();
size_t i = 0;
for (; i < log_format.size(); ++i)
{
size_t start = i;
while (i < log_format.size() && log_format[i] != '{')
++i;
if (i == log_format.size() || (i < log_format.size() && (i - start) > 0))
{
log_tag_content.emplace_back(std::string_view(log_format.data() + start, i - start));
log_tag_tokens.emplace_back(tags::detail::log_tag_token_t::CONTENT);
if (i == log_format.size())
break;
}
start = i;
while (i < log_format.size() && log_format[i] != '}')
++i;
const auto tag = std::string_view(log_format.data() + start, i - start + 1);
auto it = tag_map.find(tag);
if (it == tag_map.end())
throw std::runtime_error("Invalid log tag: " + std::string(tag));
log_tag_tokens.emplace_back(it->second);
}
if (i < log_format.size())
{
log_tag_content.emplace_back(std::string_view(log_format.data() + i, log_format.size() - i));
log_tag_tokens.emplace_back(tags::detail::log_tag_token_t::CONTENT);
}
}
std::string logging_config_t::get_default_log_format()
@ -90,14 +169,7 @@ namespace blt::logging
std::array<std::string, LOG_LEVEL_COUNT> logging_config_t::get_default_log_level_names()
{
return {
"TRACE",
"DEBUG",
"INFO",
"WARN",
"ERROR",
"FATAL",
};
return {"TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL",};
}
std::string logging_config_t::get_default_error_color()

View File

@ -160,7 +160,7 @@ int main()
std::ofstream os("test.txt");
blt::fs::fstream_writer_t wtr(os);
blt::fs::writer_wrapper_t writer(wtr);
blt::fs::writer_string_wrapper_t writer(wtr);
writer.write("This is a println with a stream\n");
writer.write("This is a mixed print ");