injectors
parent
fa0e70bda4
commit
2b1086ee15
|
@ -1,6 +1,6 @@
|
||||||
cmake_minimum_required(VERSION 3.20)
|
cmake_minimum_required(VERSION 3.20)
|
||||||
include(cmake/color.cmake)
|
include(cmake/color.cmake)
|
||||||
set(BLT_VERSION 5.2.8)
|
set(BLT_VERSION 5.2.9)
|
||||||
|
|
||||||
set(BLT_TARGET BLT)
|
set(BLT_TARGET BLT)
|
||||||
|
|
||||||
|
|
|
@ -30,6 +30,8 @@ namespace blt::logging
|
||||||
struct fmt_token_t;
|
struct fmt_token_t;
|
||||||
class fmt_tokenizer_t;
|
class fmt_tokenizer_t;
|
||||||
class fmt_parser_t;
|
class fmt_parser_t;
|
||||||
|
|
||||||
|
class injector_t;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif //BLT_LOGGING_FWDDECL_H
|
#endif //BLT_LOGGING_FWDDECL_H
|
||||||
|
|
|
@ -0,0 +1,41 @@
|
||||||
|
#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_INJECTOR_H
|
||||||
|
#define BLT_LOGGING_INJECTOR_H
|
||||||
|
|
||||||
|
namespace blt::logging
|
||||||
|
{
|
||||||
|
struct injector_output_t
|
||||||
|
{
|
||||||
|
std::string new_logging_output;
|
||||||
|
// should we continue processing the injector call chain?
|
||||||
|
bool should_continue;
|
||||||
|
// should we log the resulting string at the end of the injector call chain? If false for any injector, it becomes false for all injectors.
|
||||||
|
bool should_log;
|
||||||
|
};
|
||||||
|
|
||||||
|
class injector_t
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~injector_t() = default;
|
||||||
|
virtual injector_output_t inject(const std::string& input) = 0;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //BLT_LOGGING_INJECTOR_H
|
|
@ -20,6 +20,7 @@
|
||||||
#define BLT_LOGGING_LOGGING_CONFIG_H
|
#define BLT_LOGGING_LOGGING_CONFIG_H
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
|
#include <memory>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
@ -133,9 +134,15 @@ namespace blt::logging
|
||||||
|
|
||||||
void compile();
|
void compile();
|
||||||
|
|
||||||
logging_config_t& add_log_output(fs::writer_t* writer)
|
logging_config_t& add_log_output(fs::writer_t& writer)
|
||||||
{
|
{
|
||||||
m_log_outputs.push_back(writer);
|
m_log_outputs.push_back(&writer);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
logging_config_t& add_injector(injector_t* injector)
|
||||||
|
{
|
||||||
|
m_injectors.push_back(std::unique_ptr<injector_t>(injector));
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -200,8 +207,7 @@ namespace blt::logging
|
||||||
i32 line) const;
|
i32 line) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<std::string> m_log_tag_content;
|
std::vector<std::unique_ptr<injector_t>> m_injectors;
|
||||||
std::vector<tags::detail::log_tag_token_t> m_log_tag_tokens;
|
|
||||||
// wrappers for streams exist in blt/fs/stream_wrappers.h
|
// wrappers for streams exist in blt/fs/stream_wrappers.h
|
||||||
std::vector<fs::writer_t*> m_log_outputs = get_default_log_outputs();
|
std::vector<fs::writer_t*> m_log_outputs = get_default_log_outputs();
|
||||||
std::string m_log_format = get_default_log_format();
|
std::string m_log_format = get_default_log_format();
|
||||||
|
@ -219,6 +225,9 @@ namespace blt::logging
|
||||||
|
|
||||||
size_t m_longest_name_length = 0;
|
size_t m_longest_name_length = 0;
|
||||||
|
|
||||||
|
std::vector<std::string> m_log_tag_content;
|
||||||
|
std::vector<tags::detail::log_tag_token_t> m_log_tag_tokens;
|
||||||
|
|
||||||
static std::string get_default_log_format();
|
static std::string get_default_log_format();
|
||||||
static std::vector<fs::writer_t*> get_default_log_outputs();
|
static std::vector<fs::writer_t*> get_default_log_outputs();
|
||||||
static std::array<std::string, LOG_LEVEL_COUNT> get_default_log_level_colors();
|
static std::array<std::string, LOG_LEVEL_COUNT> get_default_log_level_colors();
|
||||||
|
|
|
@ -20,231 +20,231 @@
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <blt/iterator/enumerate.h>
|
#include <blt/logging/injector.h>
|
||||||
#include <blt/logging/logging.h>
|
#include <blt/logging/logging.h>
|
||||||
#include <blt/std/hashmap.h>
|
#include <blt/std/hashmap.h>
|
||||||
#include <blt/std/types.h>
|
#include <blt/std/types.h>
|
||||||
|
|
||||||
namespace blt::logging
|
namespace blt::logging
|
||||||
{
|
{
|
||||||
struct global_context_t
|
struct global_context_t
|
||||||
{
|
{
|
||||||
logging_config_t global_config;
|
logging_config_t global_config;
|
||||||
};
|
};
|
||||||
|
|
||||||
static global_context_t global_context;
|
static global_context_t global_context;
|
||||||
|
|
||||||
struct logging_thread_context_t
|
struct logging_thread_context_t
|
||||||
{
|
{
|
||||||
std::stringstream stream;
|
std::stringstream stream;
|
||||||
std::stringstream logging_stream;
|
std::stringstream logging_stream;
|
||||||
std::string thread_name;
|
std::string thread_name;
|
||||||
logger_t logger{stream};
|
logger_t logger{stream};
|
||||||
};
|
};
|
||||||
|
|
||||||
logging_thread_context_t& get_thread_context()
|
logging_thread_context_t& get_thread_context()
|
||||||
{
|
{
|
||||||
thread_local logging_thread_context_t context;
|
thread_local logging_thread_context_t context;
|
||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string logger_t::to_string() const
|
std::string logger_t::to_string() const
|
||||||
{
|
{
|
||||||
return dynamic_cast<std::stringstream&>(m_stream).str();
|
return dynamic_cast<std::stringstream&>(m_stream).str();
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t logger_t::find_ending_brace(size_t begin) const
|
size_t logger_t::find_ending_brace(size_t begin) const
|
||||||
{
|
{
|
||||||
size_t braces = 0;
|
size_t braces = 0;
|
||||||
for (; begin < m_fmt.size(); ++begin)
|
for (; begin < m_fmt.size(); ++begin)
|
||||||
{
|
{
|
||||||
if (m_fmt[begin] == '{')
|
if (m_fmt[begin] == '{')
|
||||||
++braces;
|
++braces;
|
||||||
else if (m_fmt[begin] == '}')
|
else if (m_fmt[begin] == '}')
|
||||||
--braces;
|
--braces;
|
||||||
if (braces == 0)
|
if (braces == 0)
|
||||||
return begin;
|
return begin;
|
||||||
}
|
}
|
||||||
return std::string::npos;
|
return std::string::npos;
|
||||||
}
|
}
|
||||||
|
|
||||||
void logger_t::setup_stream(const fmt_spec_t& spec) const
|
void logger_t::setup_stream(const fmt_spec_t& spec) const
|
||||||
{
|
{
|
||||||
if (spec.prefix_char)
|
if (spec.prefix_char)
|
||||||
m_stream << std::setfill(*spec.prefix_char);
|
m_stream << std::setfill(*spec.prefix_char);
|
||||||
else
|
else
|
||||||
m_stream << std::setfill(' ');
|
m_stream << std::setfill(' ');
|
||||||
switch (spec.alignment)
|
switch (spec.alignment)
|
||||||
{
|
{
|
||||||
case fmt_align_t::LEFT:
|
case fmt_align_t::LEFT:
|
||||||
m_stream << std::left;
|
m_stream << std::left;
|
||||||
break;
|
break;
|
||||||
case fmt_align_t::CENTER:
|
case fmt_align_t::CENTER:
|
||||||
// TODO?
|
// TODO?
|
||||||
break;
|
break;
|
||||||
case fmt_align_t::RIGHT:
|
case fmt_align_t::RIGHT:
|
||||||
m_stream << std::right;
|
m_stream << std::right;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (spec.width > 0)
|
if (spec.width > 0)
|
||||||
m_stream << std::setw(static_cast<i32>(spec.width));
|
m_stream << std::setw(static_cast<i32>(spec.width));
|
||||||
else
|
else
|
||||||
m_stream << std::setw(0);
|
m_stream << std::setw(0);
|
||||||
if (spec.precision > 0)
|
if (spec.precision > 0)
|
||||||
m_stream << std::setprecision(static_cast<i32>(spec.precision));
|
m_stream << std::setprecision(static_cast<i32>(spec.precision));
|
||||||
else
|
else
|
||||||
m_stream << std::setprecision(16);
|
m_stream << std::setprecision(16);
|
||||||
if (spec.alternate_form)
|
if (spec.alternate_form)
|
||||||
m_stream << std::showbase;
|
m_stream << std::showbase;
|
||||||
else
|
else
|
||||||
m_stream << std::noshowbase;
|
m_stream << std::noshowbase;
|
||||||
if (spec.uppercase)
|
if (spec.uppercase)
|
||||||
m_stream << std::uppercase;
|
m_stream << std::uppercase;
|
||||||
else
|
else
|
||||||
m_stream << std::nouppercase;
|
m_stream << std::nouppercase;
|
||||||
if (spec.sign == fmt_sign_t::PLUS)
|
if (spec.sign == fmt_sign_t::PLUS)
|
||||||
m_stream << std::showpos;
|
m_stream << std::showpos;
|
||||||
else
|
else
|
||||||
m_stream << std::noshowpos;
|
m_stream << std::noshowpos;
|
||||||
}
|
}
|
||||||
|
|
||||||
void logger_t::process_strings()
|
void logger_t::process_strings()
|
||||||
{
|
{
|
||||||
auto spec_it = m_fmt_specs.begin();
|
auto spec_it = m_fmt_specs.begin();
|
||||||
auto str_it = m_string_sections.begin();
|
auto str_it = m_string_sections.begin();
|
||||||
for (; spec_it != m_fmt_specs.end(); ++spec_it, ++str_it)
|
for (; spec_it != m_fmt_specs.end(); ++spec_it, ++str_it)
|
||||||
{
|
{
|
||||||
m_stream << *str_it;
|
m_stream << *str_it;
|
||||||
auto arg_pos = spec_it->arg_id;
|
auto arg_pos = spec_it->arg_id;
|
||||||
if (arg_pos == -1)
|
if (arg_pos == -1)
|
||||||
arg_pos = static_cast<i64>(m_arg_pos++);
|
arg_pos = static_cast<i64>(m_arg_pos++);
|
||||||
|
|
||||||
setup_stream(*spec_it);
|
setup_stream(*spec_it);
|
||||||
m_arg_print_funcs[arg_pos](m_stream, *spec_it);
|
m_arg_print_funcs[arg_pos](m_stream, *spec_it);
|
||||||
}
|
}
|
||||||
m_stream << *str_it;
|
m_stream << *str_it;
|
||||||
}
|
}
|
||||||
|
|
||||||
void logger_t::handle_type(std::ostream& stream, const fmt_spec_t& spec)
|
void logger_t::handle_type(std::ostream& stream, const fmt_spec_t& spec)
|
||||||
{
|
{
|
||||||
switch (spec.type)
|
switch (spec.type)
|
||||||
{
|
{
|
||||||
case fmt_type_t::DECIMAL:
|
case fmt_type_t::DECIMAL:
|
||||||
stream << std::noboolalpha;
|
stream << std::noboolalpha;
|
||||||
stream << std::dec;
|
stream << std::dec;
|
||||||
break;
|
break;
|
||||||
case fmt_type_t::OCTAL:
|
case fmt_type_t::OCTAL:
|
||||||
stream << std::oct;
|
stream << std::oct;
|
||||||
break;
|
break;
|
||||||
case fmt_type_t::HEX:
|
case fmt_type_t::HEX:
|
||||||
stream << std::hex;
|
stream << std::hex;
|
||||||
break;
|
break;
|
||||||
case fmt_type_t::HEX_FLOAT:
|
case fmt_type_t::HEX_FLOAT:
|
||||||
stream << std::hexfloat;
|
stream << std::hexfloat;
|
||||||
break;
|
break;
|
||||||
case fmt_type_t::EXPONENT:
|
case fmt_type_t::EXPONENT:
|
||||||
stream << std::scientific;
|
stream << std::scientific;
|
||||||
break;
|
break;
|
||||||
case fmt_type_t::FIXED_POINT:
|
case fmt_type_t::FIXED_POINT:
|
||||||
stream << std::fixed;
|
stream << std::fixed;
|
||||||
break;
|
break;
|
||||||
case fmt_type_t::GENERAL:
|
case fmt_type_t::GENERAL:
|
||||||
stream << std::defaultfloat;
|
stream << std::defaultfloat;
|
||||||
break;
|
break;
|
||||||
case fmt_type_t::UNSPECIFIED:
|
case fmt_type_t::UNSPECIFIED:
|
||||||
stream << std::boolalpha;
|
stream << std::boolalpha;
|
||||||
stream << std::dec;
|
stream << std::dec;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void logger_t::exponential(std::ostream& stream)
|
void logger_t::exponential(std::ostream& stream)
|
||||||
{
|
{
|
||||||
stream << std::scientific;
|
stream << std::scientific;
|
||||||
}
|
}
|
||||||
|
|
||||||
void logger_t::fixed(std::ostream& stream)
|
void logger_t::fixed(std::ostream& stream)
|
||||||
{
|
{
|
||||||
stream << std::fixed;
|
stream << std::fixed;
|
||||||
}
|
}
|
||||||
|
|
||||||
void logger_t::compile(std::string fmt)
|
void logger_t::compile(std::string fmt)
|
||||||
{
|
{
|
||||||
m_fmt = std::move(fmt);
|
m_fmt = std::move(fmt);
|
||||||
m_last_fmt_pos = 0;
|
m_last_fmt_pos = 0;
|
||||||
m_arg_pos = 0;
|
m_arg_pos = 0;
|
||||||
auto& ss = dynamic_cast<std::stringstream&>(m_stream);
|
auto& ss = dynamic_cast<std::stringstream&>(m_stream);
|
||||||
ss.str("");
|
ss.str("");
|
||||||
m_stream.clear();
|
m_stream.clear();
|
||||||
m_string_sections.clear();
|
m_string_sections.clear();
|
||||||
m_fmt_specs.clear();
|
m_fmt_specs.clear();
|
||||||
ptrdiff_t last_pos = 0;
|
ptrdiff_t last_pos = 0;
|
||||||
while (auto pair = consume_to_next_fmt())
|
while (auto pair = consume_to_next_fmt())
|
||||||
{
|
{
|
||||||
const auto [begin, end] = *pair;
|
const auto [begin, end] = *pair;
|
||||||
m_string_sections.emplace_back(m_fmt.data() + last_pos, begin - last_pos);
|
m_string_sections.emplace_back(m_fmt.data() + last_pos, begin - last_pos);
|
||||||
if (end - begin > 1)
|
if (end - begin > 1)
|
||||||
m_fmt_specs.push_back(m_parser.parse(std::string_view{m_fmt.data() + static_cast<ptrdiff_t>(begin) + 1, end - begin - 1}));
|
m_fmt_specs.push_back(m_parser.parse(std::string_view{m_fmt.data() + static_cast<ptrdiff_t>(begin) + 1, end - begin - 1}));
|
||||||
else
|
else
|
||||||
m_fmt_specs.emplace_back();
|
m_fmt_specs.emplace_back();
|
||||||
last_pos = static_cast<ptrdiff_t>(end) + 1;
|
last_pos = static_cast<ptrdiff_t>(end) + 1;
|
||||||
}
|
}
|
||||||
m_string_sections.emplace_back(m_fmt.data() + last_pos, m_fmt.size() - last_pos);
|
m_string_sections.emplace_back(m_fmt.data() + last_pos, m_fmt.size() - last_pos);
|
||||||
m_last_fmt_pos = 0;
|
m_last_fmt_pos = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::optional<std::pair<size_t, size_t>> logger_t::consume_to_next_fmt()
|
std::optional<std::pair<size_t, size_t>> logger_t::consume_to_next_fmt()
|
||||||
{
|
{
|
||||||
const auto begin = m_fmt.find('{', m_last_fmt_pos);
|
const auto begin = m_fmt.find('{', m_last_fmt_pos);
|
||||||
if (begin == std::string::npos)
|
if (begin == std::string::npos)
|
||||||
return {};
|
return {};
|
||||||
const auto end = find_ending_brace(begin);
|
const auto end = find_ending_brace(begin);
|
||||||
if (end == std::string::npos)
|
if (end == std::string::npos)
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << "Invalid format string, missing closing '}' near " << m_fmt.substr(std::min(static_cast<i64>(begin) - 5, 0l));
|
ss << "Invalid format string, missing closing '}' near " << m_fmt.substr(std::min(static_cast<i64>(begin) - 5, 0l));
|
||||||
throw std::runtime_error(ss.str());
|
throw std::runtime_error(ss.str());
|
||||||
}
|
}
|
||||||
m_last_fmt_pos = end + 1;
|
m_last_fmt_pos = end + 1;
|
||||||
return std::pair{begin, end};
|
return std::pair{begin, end};
|
||||||
}
|
}
|
||||||
|
|
||||||
logger_t& get_global_logger()
|
logger_t& get_global_logger()
|
||||||
{
|
{
|
||||||
return get_thread_context().logger;
|
return get_thread_context().logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
void print(const std::string& str)
|
void print(const std::string& str)
|
||||||
{
|
{
|
||||||
std::cout << str;
|
std::cout << str;
|
||||||
}
|
}
|
||||||
|
|
||||||
void newline()
|
void newline()
|
||||||
{
|
{
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
logging_config_t& get_global_config()
|
logging_config_t& get_global_config()
|
||||||
{
|
{
|
||||||
return global_context.global_config;
|
return global_context.global_config;
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_thread_name(const std::string& name)
|
void set_thread_name(const std::string& name)
|
||||||
{
|
{
|
||||||
get_thread_context().thread_name = name;
|
get_thread_context().thread_name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string& get_thread_name()
|
const std::string& get_thread_name()
|
||||||
{
|
{
|
||||||
return get_thread_context().thread_name;
|
return get_thread_context().thread_name;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::ostream& get_local_stream()
|
std::ostream& get_local_stream()
|
||||||
{
|
{
|
||||||
auto& context = get_thread_context();
|
auto& context = get_thread_context();
|
||||||
context.logging_stream.str("");
|
context.logging_stream.str("");
|
||||||
return context.logging_stream;
|
return context.logging_stream;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue