Repository navigation
Expand file tree
/
Copy pathsys_log_parser.cpp
More file actions
101 lines (78 loc) · 2.43 KB
/
Copy pathsys_log_parser.cpp
File metadata and controls
101 lines (78 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Written By Paul Onyebuchi
// Title: System Log Processor
// Description: This program reads a log file, counts log lines with configured severity keywords and writes the result to a CSV file
#include <iostream>
#include <string>
#include <fstream>
//using namespace std;
// Global constants
const int SIZE = 3;
const std::string INPUT_FILE_NAME = "system_dump.txt",
OUTPUT_FILE_NAME = "error_report.csv";
const std::string ERROR_WORDS[SIZE] = { "ERROR", "WARNING", "CRITICAL" }; // Words to search for
// Function prototypes
bool processLogs(std::ifstream& fileStream, int [], const std::string []);
bool writeCSVReport(std::ofstream& outputStream, const int[], const std::string[], int);
int main() {
// Variables
int errorCounts[SIZE]{};
// Reads the file system_dump.txt
// - open file and associate with file name
std::ifstream inputFile(INPUT_FILE_NAME);
// Check if file opened
if (!inputFile.is_open())
{
std::cerr << "Failed to open file! \n";
return 1;
}
// Search for keywords: ERROR, WARNING, CRITICAL in the log file
// count number of log lines containing each keyword
if (!processLogs(inputFile, errorCounts, ERROR_WORDS))
{
std::cerr << "Failed while processing log file.\n";
return 2;
}
inputFile.close();
// Writing log-level counts to: error_report.csv
// Open output report
std::ofstream outputFile(OUTPUT_FILE_NAME);
if (!outputFile.is_open())
{
std::cerr << "Failed to open output File. \n";
return 1;
}
// Write report
if (!writeCSVReport(outputFile, errorCounts, ERROR_WORDS, SIZE))
{
std::cerr << "Failed to write report";
return 3;
}
outputFile.close();
return 0;
}
// Functions
//- This function reads the file and searches for log lines containing a keyword
bool processLogs(std::ifstream& fileStream, int counter[], const std::string errorWords[])
{
std::string logLevel{};
while (getline(fileStream, logLevel))
{
for (int index{}; index < SIZE; ++index)
{
if (logLevel.find(errorWords[index]) != std::string::npos)
// Increment the corresponding error counter array element
counter[index]++;
}
}
return true;
}
// This function creates a summary of reports from error log
bool writeCSVReport(std::ofstream& outputStream, const int counter[], const std::string errorWords[], int arrSize)
{
outputStream << "Log_Level,Count\n";
for (int index{}; index < arrSize; ++index)
{
outputStream << errorWords[index] << "," << counter[index] << " \n";
}
return true;
}