logging library is done

main
Brett 2025-03-09 22:47:49 -04:00
parent 60536fd602
commit be46e8552b
8 changed files with 123 additions and 21 deletions

View File

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

View File

@ -28,6 +28,12 @@ namespace blt::fs
std::string base_name(const std::string& str);
std::string_view base_name_sv(std::string_view str);
std::string filename(const std::string& str);
std::string_view filename_sv(std::string_view str);
std::string extension(const std::string& str);
std::string_view extension_sv(std::string_view str);
}
#endif //BLT_FS_PATH_HELPER_H

View File

@ -168,6 +168,8 @@ namespace blt::logging
logging_config_t& get_global_config();
std::ostream& get_local_stream();
void set_thread_name(const std::string& name);
const std::string& get_thread_name();
@ -212,9 +214,62 @@ namespace blt::logging
}
namespace detail
{}
{
}
}
#if defined(__clang__) || defined(__llvm__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
#endif
#ifdef BLT_DISABLE_LOGGING
#define BLT_LOG(level, fmt, ...)
#else
#define BLT_LOG(level, fmt, ...) blt::logging::log(level, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
#ifdef BLT_DISABLE_TRACE
#define BLT_TRACE(format, ...)
#else
#define BLT_TRACE(format, ...) BLT_LOG(blt::logging::log_level_t::TRACE, format, ##__VA_ARGS__)
#endif
#ifdef BLT_DISABLE_DEBUG
#define BLT_DEBUG(format, ...)
#else
#define BLT_DEBUG(format, ...) BLT_LOG(blt::logging::log_level_t::DEBUG, format, ##__VA_ARGS__)
#endif
#ifdef BLT_DISABLE_INFO
#define BLT_INFO(format, ...)
#else
#define BLT_INFO(format, ...) BLT_LOG(blt::logging::log_level_t::INFO, format, ##__VA_ARGS__)
#endif
#ifdef BLT_DISABLE_WARN
#define BLT_WARN(format, ...)
#else
#define BLT_WARN(format, ...) BLT_LOG(blt::logging::log_level_t::WARN, format, ##__VA_ARGS__)
#endif
#ifdef BLT_DISABLE_ERROR
#define BLT_ERROR(format, ...)
#else
#define BLT_ERROR(format, ...) BLT_LOG(blt::logging::log_level_t::ERROR, format, ##__VA_ARGS__)
#endif
#ifdef BLT_DISABLE_FATAL
#define BLT_FATAL(format, ...)
#else
#define BLT_FATAL(format, ...) BLT_LOG(blt::logging::log_level_t::FATAL, format, ##__VA_ARGS__)
#endif
#endif
#if defined(__clang__) || defined(__llvm__)
#pragma clang diagnostic pop
#endif
#endif // BLT_LOGGING_LOGGING_H

View File

@ -216,6 +216,8 @@ namespace blt::logging
// This creates output where the user message always starts at the same column.
bool m_ensure_alignment = true;
size_t m_longest_name_length = 0;
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

@ -38,4 +38,30 @@ namespace blt::fs
const auto file_parts = string::split_sv(parts.back(), '.');
return file_parts.front();
}
std::string filename(const std::string& str)
{
return std::string(filename_sv(str));
}
std::string_view filename_sv(const std::string_view str)
{
const auto parts = string::split_sv(str, delim);
return parts.back();
}
std::string extension(const std::string& str)
{
return std::string(extension_sv(str));
}
std::string_view extension_sv(const std::string_view str)
{
const auto parts = string::split_sv(str, delim);
const auto file_parts = parts.back().find_first_of('.');
return parts.back().substr(std::min(file_parts + 1, parts.back().size()));
}
}

View File

@ -37,6 +37,7 @@ namespace blt::logging
struct logging_thread_context_t
{
std::stringstream stream;
std::stringstream logging_stream;
std::string thread_name;
logger_t logger{stream};
};
@ -239,4 +240,11 @@ namespace blt::logging
{
return get_thread_context().thread_name;
}
std::ostream& get_local_stream()
{
auto& context = get_thread_context();
context.logging_stream.str("");
return context.logging_stream;
}
}

View File

@ -17,6 +17,7 @@
*/
#include <ctime>
#include <iostream>
#include <blt/fs/path_helper.h>
#include <blt/fs/stream_wrappers.h>
#include <blt/logging/ansi.h>
#include <blt/logging/logging_config.h>
@ -91,6 +92,10 @@ namespace blt::logging
m_log_tag_content.emplace_back(std::string_view(m_log_format.data() + i, m_log_format.size() - i));
m_log_tag_tokens.emplace_back(tags::detail::log_tag_token_t::CONTENT);
}
m_longest_name_length = 0;
for (const auto& name : m_log_level_names)
m_longest_name_length = std::max(m_longest_name_length, name.size());
}
std::string add_year(const tm* current_time)
@ -187,10 +192,10 @@ namespace blt::logging
}
case tags::detail::log_tag_token_t::NS:
{
auto str = std::to_string(nano_time % 1000);
auto str = std::to_string(nano_time % 1000000000ul);
if (m_ensure_alignment)
{
for (size_t i = str.size(); i < 4; ++i)
for (size_t i = str.size(); i < 9; ++i)
str.insert(str.begin(), '0');
}
fmt += str;
@ -198,24 +203,12 @@ namespace blt::logging
}
case tags::detail::log_tag_token_t::UNIX:
{
auto str = std::to_string(millis_time % 1000);
if (m_ensure_alignment)
{
for (size_t i = str.size(); i < 4; ++i)
str.insert(str.begin(), '0');
}
fmt += str;
fmt += std::to_string(millis_time);
break;
}
case tags::detail::log_tag_token_t::UNIX_NANO:
{
auto str = std::to_string(nano_time);
if (m_ensure_alignment)
{
for (size_t i = str.size(); i < 4; ++i)
str.insert(str.begin(), '0');
}
fmt += str;
fmt += std::to_string(nano_time);
break;
}
case tags::detail::log_tag_token_t::ISO_YEAR:
@ -275,7 +268,10 @@ namespace blt::logging
fmt += thread_name;
break;
case tags::detail::log_tag_token_t::FILE:
if (m_print_full_name)
fmt += file;
else
fmt += fs::filename_sv(file);
break;
case tags::detail::log_tag_token_t::LINE:
fmt += std::to_string(line);
@ -294,7 +290,7 @@ namespace blt::logging
std::string logging_config_t::get_default_log_format()
{
return build(fg(ansi::color::color8_bright::CYAN)) + "[" + tags::FULL_TIME + "]" + tags::RESET + " " + tags::LOG_COLOR + "[" + tags::LOG_LEVEL
return build(fg(ansi::color::color8_bright::BLUE)) + "[" + tags::FULL_TIME + "]" + tags::RESET + " " + tags::LOG_COLOR + "[" + tags::LOG_LEVEL
+ "]" + tags::RESET + " " + build(fg(ansi::color::color8::MAGENTA)) + "(" + tags::FILE + ":" + tags::LINE + ")" + tags::RESET + " " +
tags::CONDITIONAL_ERROR_COLOR + tags::STR + tags::RESET + "\n";
}

View File

@ -173,6 +173,15 @@ int main()
blt::logging::println("Logged {} characters", charCount);
BLT_TRACE("Hello this is am empty trace!");
BLT_TRACE("This is a trace with data {} {} {}", "bad at code", 413, "boy");
BLT_DEBUG("This is complete? {}", "this is working!");
BLT_INFO("Hello there!");
BLT_WARN("This is a warning!");
BLT_ERROR("This is an error!");
BLT_FATAL("This is a fatal error!");
/*std::cout << "\033[2J";
constexpr int totalRows = 24;
// std::cout << "\033[1;" << (totalRows - 1) << "r";