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

Parses CSV formatted log files. More...

#include <CsvParser.hpp>

Inheritance diagram for parser::CsvParser:
Inheritance graph
Collaboration diagram for parser::CsvParser:
Collaboration graph

Public Member Functions

 CsvParser ()
 Constructs a new CSV parser with default settings.
 
virtual ~CsvParser () override=default
 Virtual destructor for proper cleanup.
 
void ParseData (const std::filesystem::path &filepath) override
 Parses CSV data from a file.
 
void ParseData (std::istream &input) override
 Parses CSV data from an input stream.
 
uint32_t GetCurrentProgress () const override
 Gets the current parsing progress.
 
uint32_t GetTotalProgress () const override
 Gets the total expected progress.
 
void SetDelimiter (char delimiter)
 Sets the field delimiter character.
 
void SetHasHeaders (bool hasHeaders)
 Sets whether the first line contains headers.
 
virtual std::vector< std::pair< std::string, std::string > > GetFileMetadata () const
 Ordered key → value pairs read from the file header during the last ParseData() call (see db::EventsContainer::SetFileMetadata).
 
void RegisterObserver (IDataParserObserver *observer)
 Registers an observer to receive parsing notifications.
 
void UnregisterObserver (IDataParserObserver *observer)
 
void NotifyNewEvent (db::LogEvent &&event)
 
void NotifyProgressUpdated ()
 
void NotifyNewEventBatch (std::vector< std::pair< int, db::LogEvent::EventItems > > &&eventBatch)
 

Private Member Functions

std::vector< std::string > ParseLine (const std::string &line)
 Parses a single CSV line into fields.
 
db::LogEvent::EventItems CreateEventFromFields (const std::vector< std::string > &fields, const std::vector< std::string > &headers, int eventId)
 Creates EventItems from parsed CSV fields.
 
util::Result< size_t, error::Error > FindHeaderIndex (const std::vector< std::string > &headers, const std::string &name)
 Finds the index of a header by name.
 
std::string Trim (std::string_view str)
 Trims whitespace from both ends of a string.
 

Private Attributes

char m_delimiter = ','
 Field delimiter character.
 
bool m_hasHeaders = true
 Whether first line contains headers.
 
uint32_t m_currentProgress = 0
 Current bytes processed.
 
uint32_t m_totalProgress = 0
 Total bytes to process.
 
std::vector< IDataParserObserver * > observers
 

Detailed Description

Parses CSV formatted log files.

This parser handles CSV (Comma-Separated Values) log files with the following format:

  • First line contains column headers (e.g., id, timestamp, level, info)
  • Subsequent lines contain log event data
  • Fields can be quoted to handle commas within values
  • Supports various delimiters (comma, semicolon, tab)
Expected CSV Format:
id,timestamp,level,info
1,2025-12-07 10:30:15,INFO,Application started
2,2025-12-07 10:30:16,DEBUG,Initializing modules
Thread Safety
This class is not thread-safe. Each parsing operation should use a separate instance or external synchronization.

Constructor & Destructor Documentation

◆ CsvParser()

parser::CsvParser::CsvParser ( )

Constructs a new CSV parser with default settings.

Here is the call graph for this function:

◆ ~CsvParser()

virtual parser::CsvParser::~CsvParser ( )
overridevirtualdefault

Virtual destructor for proper cleanup.

Member Function Documentation

◆ CreateEventFromFields()

db::LogEvent::EventItems parser::CsvParser::CreateEventFromFields ( const std::vector< std::string > &  fields,
const std::vector< std::string > &  headers,
int  eventId 
)
private

Creates EventItems from parsed CSV fields.

Parameters
fieldsVector of field values
headersVector of column headers
eventIdSequential event ID
Returns
EventItems containing the parsed fields
Here is the call graph for this function:
Here is the caller graph for this function:

◆ FindHeaderIndex()

util::Result< size_t, error::Error > parser::CsvParser::FindHeaderIndex ( const std::vector< std::string > &  headers,
const std::string &  name 
)
private

Finds the index of a header by name.

Parameters
headersVector of header names
nameHeader name to find
Returns
Ok(index) if found, Err if the column is absent
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetCurrentProgress()

uint32_t parser::CsvParser::GetCurrentProgress ( ) const
overridevirtual

Gets the current parsing progress.

Returns
Current number of bytes processed

Implements parser::IDataParser.

◆ GetFileMetadata()

virtual std::vector< std::pair< std::string, std::string > > parser::IDataParser::GetFileMetadata ( ) const
inlinevirtualinherited

Ordered key → value pairs read from the file header during the last ParseData() call (see db::EventsContainer::SetFileMetadata).

Returns
Empty for formats without a header (the default).

Reimplemented in parser::SapiLogParser.

◆ GetTotalProgress()

uint32_t parser::CsvParser::GetTotalProgress ( ) const
overridevirtual

Gets the total expected progress.

Returns
Total number of bytes to process (0 if unknown)

Implements parser::IDataParser.

◆ NotifyNewEvent()

void parser::IDataParser::NotifyNewEvent ( db::LogEvent &&  event)
inlineinherited

◆ NotifyNewEventBatch()

void parser::IDataParser::NotifyNewEventBatch ( std::vector< std::pair< int, db::LogEvent::EventItems > > &&  eventBatch)
inlineinherited
Here is the caller graph for this function:

◆ NotifyProgressUpdated()

void parser::IDataParser::NotifyProgressUpdated ( )
inlineinherited
Here is the caller graph for this function:

◆ ParseData() [1/2]

void parser::CsvParser::ParseData ( const std::filesystem::path &  filepath)
overridevirtual

Parses CSV data from a file.

Parameters
filepathPath to the CSV file to parse
Exceptions
error::Errorif the file cannot be opened or parsed

Implements parser::IDataParser.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ParseData() [2/2]

void parser::CsvParser::ParseData ( std::istream &  input)
overridevirtual

Parses CSV data from an input stream.

Parameters
inputThe input stream containing CSV data
Exceptions
error::Errorif the stream cannot be read or parsed

Implements parser::IDataParser.

Here is the call graph for this function:

◆ ParseLine()

std::vector< std::string > parser::CsvParser::ParseLine ( const std::string &  line)
private

Parses a single CSV line into fields.

Parameters
lineThe CSV line to parse
Returns
Vector of field values
Here is the call graph for this function:
Here is the caller graph for this function:

◆ RegisterObserver()

void parser::IDataParser::RegisterObserver ( IDataParserObserver *  observer)
inlineinherited

Registers an observer to receive parsing notifications.

Parameters
observerPointer to the observer object
Note
The observer must remain valid until parsing completes or it's unregistered
Duplicate observers are automatically prevented

◆ SetDelimiter()

void parser::CsvParser::SetDelimiter ( char  delimiter)
inline

Sets the field delimiter character.

Parameters
delimiterCharacter to use as field separator (default: ',')

◆ SetHasHeaders()

void parser::CsvParser::SetHasHeaders ( bool  hasHeaders)
inline

Sets whether the first line contains headers.

Parameters
hasHeadersTrue if first line contains column names

◆ Trim()

std::string parser::CsvParser::Trim ( std::string_view  str)
private

Trims whitespace from both ends of a string.

Parameters
strString to trim
Returns
Trimmed string
Here is the caller graph for this function:

◆ UnregisterObserver()

void parser::IDataParser::UnregisterObserver ( IDataParserObserver *  observer)
inlineinherited

Member Data Documentation

◆ m_currentProgress

uint32_t parser::CsvParser::m_currentProgress = 0
private

Current bytes processed.

◆ m_delimiter

char parser::CsvParser::m_delimiter = ','
private

Field delimiter character.

◆ m_hasHeaders

bool parser::CsvParser::m_hasHeaders = true
private

Whether first line contains headers.

◆ m_totalProgress

uint32_t parser::CsvParser::m_totalProgress = 0
private

Total bytes to process.

◆ observers

std::vector<IDataParserObserver*> parser::IDataParser::observers
privateinherited

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