Add OwnFilteringChannel[.h, .cpp]

This commit is contained in:
Peter Nguyen 2024-09-16 23:35:07 -07:00
parent 56b39c37a1
commit 25475ceda2
2 changed files with 116 additions and 0 deletions

View File

@ -0,0 +1,55 @@
#include "OwnFilteringChannel.h"
#include <Poco/RegularExpression.h>
// #include <iostream> // TODO
namespace DB
{
namespace ErrorCodes
{
extern const int TYPE_MISMATCH;
}
void OwnFilteringChannel::log(const Poco::Message & msg)
{
std::string formatted_text;
// Apply formatting to the text
if (pFormatter)
{
pFormatter->formatExtended(ExtendedLogMessage::getFrom(msg), formatted_text);
}
else {
formatted_text = msg.getText();
}
if (!regexpFilteredOut(formatted_text))
pChannel->log(msg);
}
bool OwnFilteringChannel::regexpFilteredOut(std::string text) const
{
if (positive_pattern != "")
{
Poco::RegularExpression positive_regexp(positive_pattern);
if (!positive_regexp.match(text))
{
// std::cout << "Skipping Message: " << text << "| due to positive regexp: " << positive_pattern << std::endl;
return true;
}
}
if (negative_pattern != "")
{
Poco::RegularExpression negative_regexp(negative_pattern);
if (negative_regexp.match(text))
{
// std::cout << "Skipping Message: " << text << "| due to negative regexp: " << negative_pattern << std::endl;
return true;
}
}
// std::cout << "THE FOLLOWING MESSAGE PASSED using positive: " << positive_pattern << " and negative: " << negative_pattern << std::endl;
return false;
}
}

View File

@ -0,0 +1,61 @@
#pragma once
#include <Poco/AutoPtr.h>
#include <Poco/Channel.h>
#include <Poco/Message.h>
#include <Poco/Util/AbstractConfiguration.h>
#include "OwnPatternFormatter.h"
namespace DB
{
// Filters the logs based on regular expressions. Should be processed after formatting channel to read entire formatted text
class OwnFilteringChannel : public Poco::Channel
{
public:
explicit OwnFilteringChannel(Poco::AutoPtr<Poco::Channel> pChannel_, Poco::AutoPtr<OwnPatternFormatter> pf,
std::string positive_pattern_, std::string negative_pattern_)
: positive_pattern(positive_pattern_), negative_pattern(negative_pattern_), pChannel(pChannel_), pFormatter(pf)
{
}
// Only log if pass both positive and negative regexp checks.
// Checks the regexps on the formatted text (without color), but then passes the raw text
// to the split channel to handle formatting for individual channels (e.g apply color)
void log(const Poco::Message & msg) override;
// Sets the regex patterns to use for filtering. Specifying an empty string pattern "" indicates no filtering
void setRegexpPatterns(std::string positive_pattern_, std::string negative_pattern_)
{
positive_pattern = positive_pattern_;
negative_pattern = negative_pattern_;
}
void open() override
{
if (pChannel)
pChannel->open();
}
void close() override
{
if (pChannel)
pChannel->close();
}
void setProperty(const std::string& name, const std::string& value) override
{
if (pChannel)
pChannel->setProperty(name, value);
}
private:
bool regexpFilteredOut(std::string text) const;
std::string positive_pattern;
std::string negative_pattern;
Poco::AutoPtr<Poco::Channel> pChannel;
Poco::AutoPtr<OwnPatternFormatter> pFormatter;
};
}