-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathILog.cpp
More file actions
106 lines (90 loc) · 3.44 KB
/
ILog.cpp
File metadata and controls
106 lines (90 loc) · 3.44 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
102
103
104
105
106
#include "ILog.hpp"
#include <iostream>
#include <fstream>
#include <algorithm>
//////////////////////////////////////////////////////////////////////////////////////////
// Helper function declarations
bool sort_time_asc(const std::pair<std::string,double> &a,
const std::pair<std::string,double> &b);
bool sort_time_desc(const std::pair<std::string,double> &a,
const std::pair<std::string,double> &b);
//////////////////////////////////////////////////////////////////////////////////////////
// Console_log function definitions
void Console_log::results_log(const std::vector<std::pair<std::string, double>> &results) const {
std::cout << "**********************************************\n";
if (results.size() == 0) {
std::cout << "No Results\n";
return;
}
std::vector<std::pair<std::string, double>> sorted_results = results;
std::sort(sorted_results.begin(), sorted_results.end(), sort_time_asc);
std::cout << "Results (seconds):\n";
for (std::size_t i = 0; i < sorted_results.size(); i++) {
std::cout << sorted_results[i].first << ": ";
std::cout << sorted_results[i].second << std::endl;
}
std::cout << "**********************************************\n";
}
void Console_log::errors_log(const std::vector<std::pair<std::string, std::string>> &errors) const {
std::cout << "**********************************************\n";
if (errors.size() == 0) {
std::cout << "No Errors\n";
std::cout << "**********************************************\n";
return;
}
std::cout << "ERRORS:\n";
for (std::size_t i = 0; i < errors.size(); i++) {
std::cout << errors[i].first << ": ";
std::cout << errors[i].second << std::endl;
}
std::cout << "**********************************************\n";
}
//////////////////////////////////////////////////////////////////////////////////////////
// File_log function definitions
void File_log::results_log(const std::vector<std::pair<std::string, double>> &results) const {
std::ofstream out_file(_file_name);
if (out_file.is_open()) {
if (results.size() == 0) {
out_file << "No Results\n";
return;
}
out_file << "Results (seconds):\n";
for (std::size_t i = 0; i < results.size(); i++) {
out_file << results[i].first << ": ";
out_file << results[i].second << std::endl;
}
}
else std::cout << "Error writing to that file\n";
out_file.close();
}
void File_log::errors_log(const std::vector<std::pair<std::string, std::string>> &errors) const {
std::ofstream out_file(_file_name);
if (out_file.is_open()) {
if (errors.size() == 0) {
out_file << "No Errors";
return;
}
out_file << "ERRORS:\n";
for (std::size_t i = 0; i < errors.size(); i++) {
out_file << errors[i].first << ": ";
out_file << errors[i].second << std::endl;
}
}
else std::cout << "Error writing to that file\n";
out_file.close();
}
File_log::File_log(std::string file_name) {_file_name = file_name;}
void File_log::set_file_name(std::string file_name) {_file_name = file_name;}
//////////////////////////////////////////////////////////////////////////////////////////
// Helper function definitions
bool sort_time_desc(const std::pair<std::string,double> &a,
const std::pair<std::string,double> &b)
{
return (a.second > b.second);
}
bool sort_time_asc(const std::pair<std::string,double> &a,
const std::pair<std::string,double> &b)
{
return (a.second < b.second);
}
//////////////////////////////////////////////////////////////////////////////////////////