I have got a log file as follows

The log file has the following format : Date/Time, Severity, Module(the number after the process name is the process id) and finally the diagnostic message as an example :
 
dd/mm/yyyy HH:mm:ss.mmm  Severity               Module                                             Message
=======================   ========           ======                                              =======
17-11-2008 17:01:17.590                SUCCESS              wmlumberjack.exe:940 System Information.     CPU speed

17-11-2008 17:01:22.090                INFORMATION                winword.exe:3656 CWSWordAddIn::OnConnection.\WSWordAddIn.cpp(131) :  -> VDE:Start

I need to write a code in C++ that is able to filter and display the results based on the severity selected by the user. As an example if the user selects to see only WARNINGS, the application should only display the lines in the log file that contain the WARNING attribute.

Recommended Answers

All 5 Replies

Well, that's not too difficult. You could open the file, read it line-by-line, and print only those lines which contain the word "WARNING", for example. If you wanted to be more robust, you could separate out the column and compare that.

Some code that may help:

std::string text = "A message of some sort.";
if(text.find("message")) {
    std::cout << "The string contains the word 'message'\n";
}

And for columns, you could use stringstreams.

#include <sstream>  // for stringstreams

std::string line;  // this is read from a file
std::istringstream stream(line);
std::string col1, col2, col3;
stream >> col1 >> col2 >> col3;
if(col3 == "WARNING") /* ... */

[edit] By the way, don't say "URGENT HELP NEEDED IMMEDIATELY PLZ!!!!!111" in the title. It's not going to make people read it any faster. [/edit]

Thanks for your reply!! I am just a beginner....Can I know more in detail about the answer......

I've really told you all you need to know. I'll give you some more details, but you'll have to do some searching . . . .

Three steps.

  1. Open the file. You use ifstream to do this, it's not hard. http://www.cplusplus.com/reference/iostream/ifstream/ifstream/
  2. Read lines from the file, one at a time. Use getline() for this.
  3. If the line has "WARNING", print it. I showed you how to do this in the previous post.
commented: Nicely put +33

But how do I capture input from the user to select the severity???

the log file looks somewhat like below!!


dd/mm/yyyy HH:mm:ss.mmm  Severity               Module                                             Message
=======================   ========           ======                                              =======
17-11-2008 17:01:17.590                SUCCESS              wmlumberjack.exe:940 System Information.     CPU speed

17-11-2008 17:01:22.090                INFORMATION                winword.exe:3656 CWSWordAddIn::OnConnection.\WSWordAddIn.cpp(131) :  -> VDE:Start

17-11-2008 17:01:23.996                WARNING           winword.exe:3656 Workshare Failed to query Installation Validation value!

17-11-2008 17:01:45.903                ERROR                  winword.exe:664 WordCustomProperty      .\WordCustomProperty.cpp(81) :  -> GetCustomProperty

use a cout statement asking the user what type of severity they want like

//...
std::string choice;
cout << "Please enter the severity you want.\n";
cout << "Your choices are: warning, success, information, and error: ";
cin >> choice
//...
}
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.