LogViewer
A modern log viewer with AI-assisted analysis (Qt 6)
Loading...
Searching...
No Matches
filters::Filter Class Reference

Composable log event filter with pluggable matching strategies and multi-condition support. More...

#include <Filter.hpp>

Collaboration diagram for filters::Filter:
Collaboration graph

Public Member Functions

 Filter (const std::string &name="", const std::string &columnName="", const std::string &pattern="", bool caseSensitive=false, bool inverted=false, bool parameterFilter=false, const std::string &paramKey="", int depth=0)
 
 Filter (const Filter &other)
 
Filter & operator= (const Filter &other)
 
 Filter (Filter &&)=default
 
Filter & operator= (Filter &&)=default
 
bool matches (const std::string &value) const
 
bool matches (const db::LogEvent &event) const
 
bool matchesParameter (const db::LogEvent &event) const
 
std::vector< unsigned long > applyToIndices (const std::vector< unsigned long > &inputIndices, const mvc::IModel &model) const
 
void compile ()
 
bool searchParameterRecursive (const std::vector< std::pair< std::string, std::string > > &items, const std::string &key, int currentDepth) const
 
void addCondition (const FilterCondition &condition)
 
void removeCondition (size_t index)
 
void clearConditions ()
 
const std::vector< FilterCondition > & getConditions () const
 
bool hasMultipleConditions () const
 
void setStrategy (std::unique_ptr< IFilterStrategy > strategy)
 Set matching strategy.
 
std::string getStrategyName () const
 Get current strategy name.
 
nlohmann::json toJson () const
 

Static Public Member Functions

static Filter fromJson (const nlohmann::json &j)
 

Public Attributes

std::string name
 
std::string columnName
 
std::string pattern
 
bool isEnabled = true
 
bool isInverted = false
 
bool isCaseSensitive = false
 
bool isParameterFilter
 
std::string parameterKey
 
int parameterDepth
 
std::vector< FilterCondition > conditions
 

Private Attributes

std::shared_ptr< std::regex > regex
 
std::unique_ptr< IFilterStrategy > m_strategy
 

Detailed Description

Composable log event filter with pluggable matching strategies and multi-condition support.

The Filter class implements a flexible filtering system for log events, supporting:

  • Single and Multiple Conditions: Conditions can be combined with AND logic
  • Strategy Pattern: Pluggable matching algorithms (regex, substring, etc.)
  • Complex Queries: Column matching, nested parameter search, regex patterns
  • Inversion: Supports NOT logic via inverted flag
  • Persistence: Filters can be serialized to JSON and restored
Design Patterns
  • Strategy Pattern: Uses IFilterStrategy for pluggable matching algorithms
  • Builder Pattern: Legacy fields (name, columnName, pattern) for simple cases
  • Composite Pattern: Multiple conditions combined with AND logic
Thread Safety
Filter instances should not be modified during concurrent matching. Strategy instances are immutable and thread-safe for concurrent reads. For thread-safe filter management, use FilterManager.
Performance Notes
  • Matching is O(k * n) where k = number of conditions, n = event fields
  • Evaluation short-circuits on first false condition
  • Compiled expressions are cached for efficiency
Usage Examples

Simple single-condition filter:

filters::Filter error_filter("Show Errors", "level", "ERROR");
error_filter.compile();
if (error_filter.matches(event)) {
// Event passes filter - display it
}
Composable log event filter with pluggable matching strategies and multi-condition support.
Definition Filter.hpp:182

Complex multi-condition filter:

filters::Filter complex_filter("Database Errors");
complex_filter.addCondition(
FilterCondition("database", "error", false, "", 0, true)
);
complex_filter.addCondition(
FilterCondition("status", "failed", false, "", 0, false)
);
complex_filter.compile();
Represents a single matching condition within a filter structure.
Definition Filter.hpp:64

Inverted filter:

filters::Filter not_debug("Exclude Debug");
not_debug.columnName = "level";
not_debug.pattern = "DEBUG";
not_debug.isInverted = true; // Show everything EXCEPT DEBUG
not_debug.compile();

Regex pattern matching:

filters::Filter regex_filter(
"IPv4 Addresses",
"ip_address",
R"(^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$)" // Regex pattern
);
regex_filter.compile();

Serialization:

// Save to JSON
nlohmann::json j = filter.toJson();
// Load from JSON
filters::Filter restored =
filters::Filter::fromJson(j); // Static method
static Filter fromJson(const nlohmann::json &j)
Definition Filter.cpp:427
See also
FilterManager for managing multiple filters
FilterCondition for individual condition details
IFilterStrategy for custom matching strategies

Constructor & Destructor Documentation

◆ Filter() [1/3]

filters::Filter::Filter ( const std::string &  name = "",
const std::string &  columnName = "",
const std::string &  pattern = "",
bool  caseSensitive = false,
bool  inverted = false,
bool  parameterFilter = false,
const std::string &  paramKey = "",
int  depth = 0 
)
Here is the call graph for this function:

◆ Filter() [2/3]

filters::Filter::Filter ( const Filter &  other)

◆ Filter() [3/3]

filters::Filter::Filter ( Filter &&  )
default

Member Function Documentation

◆ addCondition()

void filters::Filter::addCondition ( const FilterCondition &  condition)

◆ applyToIndices()

std::vector< unsigned long > filters::Filter::applyToIndices ( const std::vector< unsigned long > &  inputIndices,
const mvc::IModel &  model 
) const
Here is the call graph for this function:

◆ clearConditions()

void filters::Filter::clearConditions ( )

◆ compile()

void filters::Filter::compile ( )
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fromJson()

Filter filters::Filter::fromJson ( const nlohmann::json &  j)
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getConditions()

const std::vector< FilterCondition > & filters::Filter::getConditions ( ) const

◆ getStrategyName()

std::string filters::Filter::getStrategyName ( ) const

Get current strategy name.

Returns
Strategy identifier (e.g., "regex", "exact")

◆ hasMultipleConditions()

bool filters::Filter::hasMultipleConditions ( ) const
inline

◆ matches() [1/2]

bool filters::Filter::matches ( const db::LogEvent &  event) const
Here is the call graph for this function:

◆ matches() [2/2]

bool filters::Filter::matches ( const std::string &  value) const
Here is the caller graph for this function:

◆ matchesParameter()

bool filters::Filter::matchesParameter ( const db::LogEvent &  event) const
Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator=() [1/2]

Filter & filters::Filter::operator= ( const Filter &  other)

◆ operator=() [2/2]

Filter & filters::Filter::operator= ( Filter &&  )
default

◆ removeCondition()

void filters::Filter::removeCondition ( size_t  index)

◆ searchParameterRecursive()

bool filters::Filter::searchParameterRecursive ( const std::vector< std::pair< std::string, std::string > > &  items,
const std::string &  key,
int  currentDepth 
) const
Here is the call graph for this function:
Here is the caller graph for this function:

◆ setStrategy()

void filters::Filter::setStrategy ( std::unique_ptr< IFilterStrategy >  strategy)

Set matching strategy.

Parameters
strategyNew strategy to use (takes ownership)

Allows runtime switching between matching algorithms. Default is RegexFilterStrategy.

Here is the call graph for this function:

◆ toJson()

nlohmann::json filters::Filter::toJson ( ) const

Member Data Documentation

◆ columnName

std::string filters::Filter::columnName

◆ conditions

std::vector<FilterCondition> filters::Filter::conditions

◆ isCaseSensitive

bool filters::Filter::isCaseSensitive = false

◆ isEnabled

bool filters::Filter::isEnabled = true

◆ isInverted

bool filters::Filter::isInverted = false

◆ isParameterFilter

bool filters::Filter::isParameterFilter

◆ m_strategy

std::unique_ptr<IFilterStrategy> filters::Filter::m_strategy
private

◆ name

std::string filters::Filter::name

◆ parameterDepth

int filters::Filter::parameterDepth

◆ parameterKey

std::string filters::Filter::parameterKey

◆ pattern

std::string filters::Filter::pattern

◆ regex

std::shared_ptr<std::regex> filters::Filter::regex
private

The documentation for this class was generated from the following files: