Skip to content

Commit 32a54a9

Browse files
Nacho ValladaresDigitalInBlue
Nacho Valladares
authored andcommitted
Forcing the output file (if any) to close when finishing the bencnhmarks
1 parent d3a7a9b commit 32a54a9

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

include/celero/ResultTable.h

+4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ namespace celero
4545
///
4646
void setFileName(const std::string& x);
4747

48+
///
49+
/// Force the output file (if any) to close
50+
///
51+
void closeFile();
4852
///
4953
/// Add a new result to the result table.
5054
///

src/Celero.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,15 @@ void celero::Run(int argc, char** argv)
139139
}
140140

141141
// Has a result output file been specified?
142+
bool must_close_file = false;
142143
auto argument = args.get<std::string>("outputTable");
143144
if(argument.empty() == false)
144145
{
145146
std::cout << "Writing results to: " << argument << std::endl;
146147
celero::ResultTable::Instance().setFileName(argument);
147148

148149
celero::AddExperimentResultCompleteFunction([](std::shared_ptr<celero::Result> p) { celero::ResultTable::Instance().add(p); });
150+
must_close_file = true;
149151
}
150152

151153
// Has a result output file been specified?
@@ -185,6 +187,9 @@ void celero::Run(int argc, char** argv)
185187
executor::RunAll();
186188
}
187189

190+
if (must_close_file) {
191+
celero::ResultTable::Instance().closeFile();
192+
}
188193
// Final output.
189194
std::cout << "Complete.\n";
190195
}

src/ResultTable.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ class celero::ResultTable::Impl
3838
{
3939
}
4040

41+
~Impl() {
42+
closeFile();
43+
}
44+
45+
void closeFile() {
46+
if (this->ofs.is_open() == true) {
47+
this->ofs.close();
48+
}
49+
}
50+
4151
void setFileName(const std::string& x)
4252
{
4353
if(this->ofs.is_open() == true)
@@ -88,6 +98,10 @@ void ResultTable::setFileName(const std::string& x)
8898
this->pimpl->setFileName(x);
8999
}
90100

101+
void ResultTable::closeFile() {
102+
this->pimpl->closeFile();
103+
}
104+
91105
void ResultTable::add(std::shared_ptr<Result> x)
92106
{
93107
if(this->pimpl->ofs.is_open() == true)

0 commit comments

Comments
 (0)