CJCoding With Joseph
15free / daySign inCreate account

Log Reader Class

Create a class LogReader that loads a log file in its constructor. Each line of the file starts with a level word such as INFO, WARN or ERROR, followed by a space and a message:

INFO boot ok
ERROR disk full
INFO user login
ERROR net down

Implement:
- LogReader(const string& filename): opens the file and stores every line.
- bool loaded() const: returns true if the file was opened successfully, false otherwise.
- int count(const string& level) const: returns how many stored lines begin with the text level.
- string firstError() const: returns the message of the first stored line that begins with ERROR  (the text after ERROR ), or the string none if there is no such line.

The test prints loaded() as true or false, then count("ERROR"), then firstError(), each on its own line. For the file above (see Expected Output).

For the path missing_dir/log.txt the test prints false, 0 and none.

Do NOT write a main function.

Expected Output:

true
2
disk full
false
0
none
true
0
none

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
Streamsifstream
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.