-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogview.cpp
More file actions
executable file
·61 lines (55 loc) · 1.65 KB
/
logview.cpp
File metadata and controls
executable file
·61 lines (55 loc) · 1.65 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
// File: main.cpp
//
// Version: 1.1
// Date:
// Author:
//
// Description: Main body for logview application
//
#include "string.hpp"
#include "logentry.hpp"
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <vector>
int main(int argc, char *argv[]) {
// Options
std::vector<String> option(3);
option[0] = "all";
option[1] = "bytes";
option[2] = "host";
if (argc != 3) { // Error if there are not 3 things on the command line
std::cerr << "Error" << std::endl;
std::cerr << "Usage: " << argv[0] << " [ "
<< option[0] << " | "
<< option[1] << " | "
<< option[2] << " ] log_file" << std::endl;
exit(1); // Exit with error
}
std::ifstream in(argv[2]); // Open file, quit if open fails
if (!in) {
std::cerr << "Error: Can not open " << argv[2] << std::endl;
exit(2);
}
std::vector<LogEntry> log_entries = parse(in); // Process the log file
in.close();
if (argv[1] == option[0]) { // Handle the specified option
output_all(std::cout, log_entries);
}
else if (argv[1] == option[1]) {
std::cout << "Total number of bytes sent: "
<< byte_count(log_entries) << std::endl;
}
else if (argv[1] == option[2]) {
by_host(std::cout, log_entries);
}
else { //Error, bad option
std::cerr << "Unrecognized option: " << argv[1] << std::endl;
std::cerr << "Recognized options: "
<< option[0] << " "
<< option[1] << " "
<< option[2] << std::endl;
exit(3);
}
return 0;
}