-
Notifications
You must be signed in to change notification settings - Fork 41
/
summary_report_writer.hpp
123 lines (107 loc) · 3.85 KB
/
summary_report_writer.hpp
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* Copyright 2017 EMBL - European Bioinformatics Institute
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef VCF_SUMMARY_REPORT_WRITER_HPP
#define VCF_SUMMARY_REPORT_WRITER_HPP
#include <map>
#include "report_writer.hpp"
namespace ebi
{
namespace vcf
{
/**
* Stores the count and first line of occurrence of an error message
*/
struct ErrorSummary
{
size_t occurrences;
size_t first_occurrence_line;
};
/**
* Class that ensures similar kind of errors are reported only once.
*
* The intended algorithm is that errors and warnings will be printed only the first time they occur, and won't
* be printed again.
*
* The summary displays the count(number of times it occurs) of the error, and the line number of its first
* occurrence. We distinguish between different types of errors based on their simple error message (which contains
* no details). The `error_order` basically maintains the order in which these errors appear for the first time.
*/
class SummaryTracker
{
public:
std::map<std::string, ErrorSummary> error_summary_report;
std::vector<std::string> error_order;
void add_to_summary(std::string const & error_message, size_t error_line)
{
if (error_summary_report.find(error_message) == error_summary_report.end()) {
error_order.push_back(error_message);
error_summary_report[error_message] = ErrorSummary{1, error_line};
} else {
error_summary_report[error_message].occurrences++;
}
}
};
/**
* Implements a ReportWriter that writes to a file, but only the summary is written
*/
class SummaryReportWriter : public ReportWriter
{
public:
SummaryReportWriter(std::string filename) : file_name(filename)
{
file.open(filename, std::ios::out);
if(!file) {
throw std::runtime_error{"Unable to write output file " + file_name};
}
}
~SummaryReportWriter()
{
write_summary();
file.close();
}
virtual void write_error(Error &error) override
{
summary.add_to_summary("Error: " + error.message, error.line);
}
virtual void write_warning(Error &error) override
{
summary.add_to_summary("Warning: " + error.message, error.line);
}
virtual void write_message(const std::string &report_result) override
{
this->report_result = report_result;
}
virtual std::string get_report_message() override
{
return "Summary report written to : " + file_name;
}
private:
SummaryTracker summary;
std::string report_result;
std::string file_name;
std::ofstream file;
void write_summary()
{
file << report_result << std::endl;
for (auto & error_message : summary.error_order) {
file << error_message << ". This occurs " << summary.error_summary_report[error_message].occurrences
<< " time(s), first time in line " << summary.error_summary_report[error_message].first_occurrence_line << "." << std::endl;
}
}
};
}
}
#endif // VCF_SUMMARY_REPORT_WRITER_HPP