Skip to content

Commit

Permalink
C4LogSystem: Add markup stripping
Browse files Browse the repository at this point in the history
  • Loading branch information
Fulgen301 committed Nov 18, 2024
1 parent 0160e67 commit 51d2516
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/C4Log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,41 @@ class LoggerNameIfExistsFormatterFlag : public spdlog::custom_flag_formatter
}
};

class StrippedTextFormatterFlag : public spdlog::custom_flag_formatter
{
public:
void format(const spdlog::details::log_msg &msg, const tm &, std::string &dest) override
{
if (msg.payload.empty())
{
return;
}

dest.resize_and_overwrite(dest.size() + msg.payload.size(), [&msg, oldSize{dest.size()}](char *const ptr, const std::size_t size)
{
std::fill_n(ptr + oldSize, size - oldSize, 0xAB);
auto oldSize2 = oldSize;
auto &payload = msg.payload;
auto copied = msg.payload.copy(ptr + oldSize, msg.payload.size());
ptr[oldSize + copied] = '\0';

CMarkup markup{false};
if (markup.StripMarkup(ptr + oldSize))
{
return std::strlen(ptr);
}
else
{
return size;
}
});
}

std::unique_ptr<custom_flag_formatter> clone() const override
{
return std::make_unique<StrippedTextFormatterFlag>();
}
};
}

C4LogSystem::LogSink::LogSink(std::unique_ptr<spdlog::formatter> formatter)
Expand Down Expand Up @@ -156,6 +191,7 @@ C4LogSystem::GuiSink::GuiSink(const spdlog::level::level_enum level, const bool

auto guiFormatter = std::make_unique<spdlog::pattern_formatter>();
guiFormatter->add_flag<LogLevelPrefixFormatterFlag>('*');
guiFormatter->add_flag<StrippedTextFormatterFlag>('v');

if (showLoggerNameInGui)
{
Expand Down Expand Up @@ -265,6 +301,7 @@ C4LogSystem::C4LogSystem()

defaultPatternFormatter = std::make_unique<spdlog::pattern_formatter>();
defaultPatternFormatter->add_flag<LoggerNameIfExistsFormatterFlag>('~');
defaultPatternFormatter->add_flag<StrippedTextFormatterFlag>('v');
defaultPatternFormatter->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%^%l%$] %~%v");

loggerSilentGuiSink = std::make_shared<GuiSink>(spdlog::level::warn, true);
Expand Down

0 comments on commit 51d2516

Please sign in to comment.