Hello All;

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

XY,1,1114,ABCDEF,d1744783-0681-4f0c-adee-83277f5e4b85:286,192.168.33.211\n

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 ??

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.
Tutorial:
https://solarianprogrammer.com/2011/10/12/cpp-11-regex-tutorial/
http://www.informit.com/articles/article.aspx?p=2079020

The simplest way to run code asynchronously, and retrieve the result of the asynchronous operation, is to use std::async().
Tutorial:
https://solarianprogrammer.com/2012/10/17/cpp-11-async-tutorial/
http://www.drdobbs.com/cpp/c11s-async-template/240001196

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:
http://coliru.stacked-crooked.com/a/dfd5aa2c6932d560
http://rextester.com/SHC70475

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.