/
logview.cpp
93 lines (69 loc) · 2.27 KB
/
logview.cpp
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
// File: main.cpp
//
// Version: 1.0
// Date:
// Author:
//
// Description: Main body for logview application
//
////////////////////////////////////////////////////////////
#include <iostream>
#include <fstream>
#include <vector>
#include <cstdlib>
#include "string.hpp"//added
#include "logentry.hpp"
////////////////////////////////////////////////////////////
void output_usage_and_exit(const char cmd[], const std::vector<String>& opts);
////////////////////////////////////////////////////////////
int main(int argc, char *argv[]) {
// Options
std::vector<String> opt(3);
opt[0] = "all";
opt[1] = "bytes";
opt[2] = "host";
// Error if there are not 3 things on the command line
if (argc != 3)
{ output_usage_and_exit(argv[0], opt); }
// Open file, quit if open fails
std::ifstream in(argv[2]);
if (!in)
{ std::cerr << "Couldn't open " << argv[2] << "\n"; exit(2); }
// Process the log file
std::vector<LogEntry> log_entries = parse(in);
// Close the in file
in.close();
// Handle the specified option
String option(argv[1]);
if (option == opt[0]) {
// Output everything
output_all(std::cout, log_entries);
}
else if (option == opt[1]) {
// Output total number of bytes
std::cout << "Total number of bytes sent: "
<< byte_count(log_entries) << std::endl;
}
else if (option == opt[2]) {
// Host names will be out-putted here
by_host(std::cout, log_entries);
}
else {
std::cerr << "Unrecognized option: " << option << std::endl;
std::cerr << "Recognized options: "
<< opt[0] << " "
<< opt[1] << " "
<< opt[2] << std::endl;
}
return 0;
}
////////////////////////////////////////////////////////////
void output_usage_and_exit(const char cmd[], const std::vector<String>& opt)
{
std::cerr << "Usage: " << cmd << " [";
for (std::vector<String>::size_type idx = 0; idx < opt.size() - 1; ++idx)
std::cerr << opt[idx] << " | ";
std::cerr << opt[opt.size() - 1] << "] log_file\n";
// Will exit if there is an error and display that error
exit(1);
}