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

Represents a single, structured log event with key-value data. More...

#include <LogEvent.hpp>

Collaboration diagram for db::LogEvent:
Collaboration graph

Public Types

using EventItems = std::vector< std::pair< std::string, std::string > >
 A collection of key-value pairs representing event data (eventFieldName, data).
 
using EventItemsIterator = std::vector< std::pair< std::string, std::string > >::iterator
 Iterator type for traversing event items.
 
using EventItemsConstIterator = std::vector< std::pair< std::string, std::string > >::const_iterator
 Const iterator type for traversing event items.
 

Public Member Functions

template<typename... Args>
requires util::Constructible<EventItems, Args&&...>
 LogEvent (int id, Args &&... args)
 Template constructor with perfect forwarding.
 
 LogEvent (int id, std::initializer_list< std::pair< std::string, std::string > > items)
 Constructs a LogEvent from an initializer list.
 
int getId () const
 Gets the unique ID of the event.
 
const EventItems & getEventItems () const
 Gets a const reference to the event's data items.
 
const std::string findByKey (std::string_view key) const
 Finds a value by its key in the event's items.
 
const std::vector< std::string > findAllByKey (std::string_view key) const
 Finds all values by its key in the event's items.
 
EventItemsIterator findInEvent (const std::string &search)
 Finds an iterator to the first item matching the search term.
 
bool operator== (const LogEvent &other) const
 Equality comparison operator.
 
void SetOriginalId (int originalId)
 Sets the original ID before merge (stores as event data)
 
void SetId (int newId)
 Updates the event's ID (used during merge to reassign sequential IDs)
 
const std::string & GetSource () const
 Gets the source identifier for this event.
 
void SetSource (const std::string &source)
 Sets the source identifier for this event.
 

Private Attributes

int m_id
 Unique event identifier.
 
EventItems m_eventItems
 The structured data of the event.
 
std::string m_source
 Source identifier for multi-file merge.
 

Detailed Description

Represents a single, structured log event with key-value data.

A LogEvent encapsulates a log entry as a collection of key-value pairs, providing efficient access to structured log data. Each event has a unique identifier and contains metadata such as timestamps, severity levels, and custom attributes.

Design Philosophy
LogEvent is optimized for:
  • Memory efficiency through move semantics and perfect forwarding
  • Integration with filtering and display systems
  • Support for multi-source event merging with original ID tracking
Key Features
  • Key-value lookup: O(n) linear scan over a typically small field list — deliberately not a per-event hash index (see findByKey())
  • Multiple source tracking: Identifies events from different log files
  • Original ID preservation: Retains pre-merge IDs for traceability
  • Flexible construction: Supports initializer lists and perfect forwarding
Thread Safety
This class is not thread-safe. External synchronization is required when accessing LogEvent instances from multiple threads. For thread-safe collection management, use EventsContainer instead.
Memory Management
LogEvent uses move semantics and perfect forwarding where possible to minimize unnecessary copying of large data sets. Events are typically moved into EventsContainer for storage.
Usage Examples

Creating events from initializer list:

db::LogEvent event(42, {
{"timestamp", "2025-03-15T12:00:00Z"},
{"level", "ERROR"},
{"message", "Database connection failed"},
{"source", "db_engine"}
});
// Accessing data
std::string timestamp = event.findByKey("timestamp");
std::cout << "Level: " << event.findByKey("level") << std::endl;
Represents a single, structured log event with key-value data.
Definition LogEvent.hpp:97
const std::string findByKey(std::string_view key) const
Finds a value by its key in the event's items.
Definition LogEvent.cpp:25

Searching for values:

if (!event.findByKey("error_code").empty()) {
std::cout << "Error code: " << event.findByKey("error_code") << std::endl;
}

Iterating through event data:

for (const auto& [key, value] : event.getEventItems()) {
std::cout << key << " = " << value << std::endl;
}
const EventItems & getEventItems() const
Gets a const reference to the event's data items.
Definition LogEvent.cpp:20

Working with merged events:

db::LogEvent original_event(1, {{"msg", "warning"}});
original_event.SetOriginalId(1); // Track original ID
original_event.SetId(100); // Assign merged ID
original_event.SetSource("log_file_1.txt"); // Track source
void SetOriginalId(int originalId)
Sets the original ID before merge (stores as event data)
Definition LogEvent.cpp:72
See also
EventsContainer for thread-safe collection management
FilterManager for filtering events based on key-value data

Member Typedef Documentation

◆ EventItems

using db::LogEvent::EventItems = std::vector<std::pair<std::string, std::string> >

A collection of key-value pairs representing event data (eventFieldName, data).

◆ EventItemsConstIterator

using db::LogEvent::EventItemsConstIterator = std::vector<std::pair<std::string, std::string> >::const_iterator

Const iterator type for traversing event items.

◆ EventItemsIterator

using db::LogEvent::EventItemsIterator = std::vector<std::pair<std::string, std::string> >::iterator

Iterator type for traversing event items.

Constructor & Destructor Documentation

◆ LogEvent() [1/2]

template<typename... Args>
requires util::Constructible<EventItems, Args&&...>
db::LogEvent::LogEvent ( int  id,
Args &&...  args 
)
inline

Template constructor with perfect forwarding.

Constructs a LogEvent with an ID and forwards arguments to EventItems constructor. This enables efficient construction from various argument types without unnecessary copies.

Template Parameters
ArgsVariadic template arguments for EventItems construction
Parameters
idThe unique identifier for the event (typically assigned by parser)
argsArguments forwarded to EventItems constructor
Note
Uses C++20 concepts to constrain args that can construct an EventItems object
Usage
// From vector of pairs
std::vector<std::pair<std::string, std::string>> data = {...};
db::LogEvent event(123, data); // or std::move(data)
// From initializer list (handled by overload)
db::LogEvent event(456, {{"key", "value"}});
See also
LogEvent(int, std::initializer_list)

◆ LogEvent() [2/2]

db::LogEvent::LogEvent ( int  id,
std::initializer_list< std::pair< std::string, std::string > >  items 
)
inline

Constructs a LogEvent from an initializer list.

Convenient constructor for creating events with compile-time known data.

Parameters
idThe unique identifier for the event
itemsInitializer list of key-value pairs
Example
LogEvent event(1, {{"timestamp", "2025-01-01"}, {"type", "INFO"}});

Member Function Documentation

◆ findAllByKey()

const std::vector< std::string > db::LogEvent::findAllByKey ( std::string_view  key) const

Finds all values by its key in the event's items.

Performs a linear search through the event's key-value pairs to find all occurrences of the specified key.

Parameters
keyThe key to search for
Returns
const std::string The corresponding value, or empty string if not found
Note
Returns the first matching value if duplicate keys exist
Complexity
O(n) where n is the number of items in the event

◆ findByKey()

const std::string db::LogEvent::findByKey ( std::string_view  key) const

Finds a value by its key in the event's items.

Performs a linear scan to retrieve the value associated with the first occurrence of a key. If the key is not found, returns an empty string.

Parameters
keyThe key to search for (e.g., "timestamp", "level", "message")
Returns
const std::string The corresponding value, or empty string if not found
Note
Returns the first matching value if duplicate keys exist in event data
Example
db::LogEvent event(1, {
{"timestamp", "2025-03-15"},
{"level", "INFO"},
{"message", "User logged in"}
});
std::string timestamp = event.findByKey("timestamp"); // "2025-03-15"
std::string missing = event.findByKey("nonexistent"); // ""
See also
findAllByKey() for retrieving all values with the same key
findInEvent() for substring search in both keys and values
Complexity
O(n) where n is the number of fields in the event. Deliberately not backed by a per-event hash index: with typically a handful to a few dozen fields, a linear scan over a contiguous vector is both cheaper to build (no per-event heap allocations) and faster to query in practice than an unordered_map at this scale — and at 100M+ events, skipping that per-event hash table avoids the dominant memory cost.
Here is the caller graph for this function:

◆ findInEvent()

LogEvent::EventItemsIterator db::LogEvent::findInEvent ( const std::string &  search)

Finds an iterator to the first item matching the search term.

Searches through both keys and values for the specified search term.

Parameters
searchThe term to search for in keys and values
Returns
EventItemsIterator Iterator to the found item, or end() if not found
Note
This method performs a substring search, not exact matching
Here is the call graph for this function:

◆ getEventItems()

const LogEvent::EventItems & db::LogEvent::getEventItems ( ) const

Gets a const reference to the event's data items.

Returns
const EventItems& Reference to the internal items collection
Note
Use this method for read-only access to avoid unnecessary copying
Here is the caller graph for this function:

◆ getId()

int db::LogEvent::getId ( ) const

Gets the unique ID of the event.

Returns
int The event's unique identifier
Note
Event IDs should be unique within a single log file or session
Here is the caller graph for this function:

◆ GetSource()

const std::string & db::LogEvent::GetSource ( ) const
inline

Gets the source identifier for this event.

Returns
const std::string& The source identifier (empty for legacy events)
Here is the caller graph for this function:

◆ operator==()

bool db::LogEvent::operator== ( const LogEvent &  other) const
inline

Equality comparison operator.

Two LogEvent objects are considered equal if they have the same ID. The actual event data is not compared.

Parameters
otherThe LogEvent to compare against
Returns
bool true if both events have the same ID
Note
This comparison is based solely on ID, not event content
Here is the call graph for this function:

◆ SetId()

void db::LogEvent::SetId ( int  newId)

Updates the event's ID (used during merge to reassign sequential IDs)

After merging events, this method reassigns event IDs to be sequential based on their position in the merged list. The original ID is preserved separately via SetOriginalId().

Parameters
newIdThe new event ID to assign
Here is the call graph for this function:

◆ SetOriginalId()

void db::LogEvent::SetOriginalId ( int  originalId)

Sets the original ID before merge (stores as event data)

When merging events from different sources, the original event ID is preserved as a data field so users can see both the original ID and the new merged ID.

Parameters
originalIdThe original event ID before merge
Here is the call graph for this function:

◆ SetSource()

void db::LogEvent::SetSource ( const std::string &  source)
inline

Sets the source identifier for this event.

Parameters
sourceThe source identifier (e.g., file name or alias)

Member Data Documentation

◆ m_eventItems

EventItems db::LogEvent::m_eventItems
private

The structured data of the event.

◆ m_id

int db::LogEvent::m_id
private

Unique event identifier.

◆ m_source

std::string db::LogEvent::m_source
private

Source identifier for multi-file merge.


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