Skip to content

Commit

Permalink
New lambda function for logger/exception ; moving to C++14 to allow a…
Browse files Browse the repository at this point in the history
…uto arguments for this latter
  • Loading branch information
forthommel committed Apr 24, 2021
1 parent d079890 commit 56e9d32
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 30 deletions.
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ project(Hector)
set(PROJECT_VERSION 2)

set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR} ${CMAKE_SOURCE_DIR}/cmake)
if(CMAKE_VERSION VERSION_GREATER 3.1)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_C_STANDARD 11)
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11")
endif()


option(BUILD_PYTHON "Build the Python bindings" OFF)
option(BUILD_TESTS "Build the tests" ON)
Expand Down
7 changes: 7 additions & 0 deletions Hector/Exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ namespace hector {
return exc;
}

/// Lambda function handler
template <typename T>
inline Exception& log(T&& lam) {
lam(*this);
return *this;
}

/// Method/function that raised the exception
inline const std::string from() const { return from_; }
/// Error code associated to the exception
Expand Down
51 changes: 26 additions & 25 deletions src/IO/TwissHandler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,31 +94,32 @@ namespace hector {
}

void Twiss::printInfo() const {
std::ostringstream os;
os << "Twiss file successfully parsed. Metadata:";
if (header_str_.hasKey("title"))
os << "\n\tTitle: " << header_str_.get("title");
if (header_str_.hasKey("origin"))
os << "\n\tOrigin: " << trim(header_str_.get("origin"));
if (header_float_.hasKey("timestamp")) {
// C implementation for pre-gcc5 compilers
time_t time = (long)header_float_.get("timestamp");
std::tm tm;
char* time_chr = new char[100];
strftime(time_chr, 100, "%c", localtime_r(&time, &tm));
os << "\n\tExport date: " << time_chr;
delete[] time_chr;
} else if (header_str_.hasKey("date") || header_str_.hasKey("time"))
os << "\n\tExport date: " << trim(header_str_.get("date")) << " @ " << trim(header_str_.get("time"));
if (header_float_.hasKey("energy"))
os << "\n\tSimulated single beam energy: " << header_float_.get("energy") << " GeV";
if (header_str_.hasKey("sequence"))
os << "\n\tSequence: " << header_str_.get("sequence");
if (header_str_.hasKey("particle"))
os << "\n\tBeam particles: " << header_str_.get("particle");
if (header_float_.hasKey("length"))
os << "\n\tMaximal beamline length: " << header_float_.get("length") << " m";
H_INFO << os.str() << ".";
H_INFO.log([&](auto& log) {
log << "Twiss file successfully parsed. Metadata:";
if (header_str_.hasKey("title"))
log << "\n\tTitle: " << header_str_.get("title");
if (header_str_.hasKey("origin"))
log << "\n\tOrigin: " << trim(header_str_.get("origin"));
if (header_float_.hasKey("timestamp")) {
// C implementation for pre-gcc5 compilers
time_t time = (long)header_float_.get("timestamp");
std::tm tm;
char* time_chr = new char[100];
strftime(time_chr, 100, "%c", localtime_r(&time, &tm));
log << "\n\tExport date: " << time_chr;
delete[] time_chr;
} else if (header_str_.hasKey("date") || header_str_.hasKey("time"))
log << "\n\tExport date: " << trim(header_str_.get("date")) << " @ " << trim(header_str_.get("time"));
if (header_float_.hasKey("energy"))
log << "\n\tSimulated single beam energy: " << header_float_.get("energy") << " GeV";
if (header_str_.hasKey("sequence"))
log << "\n\tSequence: " << header_str_.get("sequence");
if (header_str_.hasKey("particle"))
log << "\n\tBeam particles: " << header_str_.get("particle");
if (header_float_.hasKey("length"))
log << "\n\tMaximal beamline length: " << header_float_.get("length") << " m";
log << ".";
});
}

std::map<std::string, std::string> Twiss::headerStrings() const { return header_str_.asMap(); }
Expand Down
11 changes: 6 additions & 5 deletions src/hector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,12 @@ int main(int argc, char* argv[]) {
}
}

ostringstream os;
os << "Summary\n\t-------";
for (const auto& el : stopping_elements)
os << hector::format("\n\t*) %.2f%% of particles stopped in %s", 100. * el.second / num_part, el.first.c_str());
H_INFO << os.str() << ".";
H_INFO.log([&](auto& log) {
log << "Summary\n\t-------";
for (const auto& el : stopping_elements)
log << hector::format(
"\n\t*) %.2f%% of particles stopped in %s", 100. * el.second / num_part, el.first.c_str());
});
}

return 0;
Expand Down

0 comments on commit 56e9d32

Please sign in to comment.