Hello All;

I want to display logs in the following format as below:-


where:- XY is servername. ABCDE:- UserName. I want to display this specific log from the multiple logs of different server with different name. How to do that using threads ??

3 Years
Discussion Span
Last Post by vijayan121

Get log line.
Check first two letter.
If first two letters match the letters you're searching for, display the line.

Using threads could make this slightly more complicated, depending on how you're providing the log lines.


Sir, Can you please help me with some sort of code how to do that ??


You need to learn how to parse text data.

  1. Get field up to delimiter
  2. Go to #1 until you reach the end of the line.

Each field goes into a variable. Once you have reached the end of the line, you can store that data as appropriate for your application. This can be an in-memory structure/object, or a database record. FWIW, this is pretty standard stuff. Don't ask for code until you have made an attempt to solve the problem yourself.


To search for a pattern in a string, use the C++ regular expression library.

The simplest way to run code asynchronously, and retrieve the result of the asynchronous operation, is to use std::async().

Once you have read up on these two, writing the code is surprisingly easy.
Here's a snippet to get you started:

#include <iostream>
#include <string>
#include <regex>
#include <vector>
#include <fstream>
#include <future>

bool match( std::string line, std::string server_name, std::string user_name )
    static const std::string number = "\\d+" ;

    // see: http://en.cppreference.com/w/cpp/regex/ecmascript 
    // (beginning of line),server_name,number,number,user_name, ... (end of line)
    const std::regex pattern( '^' + server_name + ',' + number + ',' + number + ',' + user_name + ",.+$" ) ;

    // http://en.cppreference.com/w/cpp/regex/regex_match
    return std::regex_match( line, pattern ) ;

std::vector<std::string> filter_lines( std::string logfile_name, std::string server_name, std::string user_name )
    std::vector<std::string> result ;

    std::ifstream file(logfile_name) ;
    std::string line ;
    while( std::getline( file, line ) ) if( match( line, server_name, user_name ) ) result.push_back(line) ;

    return result ;

int main()
    const std::string server_name = "XY" ;
    const std::string user_name = "ABCDEF" ;

    // http://en.cppreference.com/w/cpp/thread/future
    using future_type = std::future< std::vector<std::string> > ;
    std::vector<future_type> futures ;

    for( std::string logfile_name : { "a.log", "b.log", "c.log", "d.log", "e.log" } )
        futures.push_back( std::async( std::launch::async, filter_lines, logfile_name, server_name, user_name ) ) ;
        // http://en.cppreference.com/w/cpp/thread/async

    std::vector<std::string> log_records ;
    for( auto& future : futures )
        // http://en.cppreference.com/w/cpp/thread/future/get
        auto vec = future.get() ; // note: get waits for async operation to complete

        log_records.insert( log_records.end(), vec.begin(), vec.end() ) ;

    // if needed, sort vector log_records
    // print contents of vector log_records

The same snippet as above, formatted in a sane, programmer-friendly manner:

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.