boost::posix_time::time_duration measure_running_duration(F const &action) { auto const clock = boost::date_time::microsec_clock<boost::posix_time::ptime>(); auto const start = clock.universal_time(); action(); auto const finish = clock.universal_time(); return (finish - start); }
fs::path JobDir::deliver( std::string const& contents, std::string const& tag ) { pt::ptime t = universal_time(); std::string file_name(to_iso_string(t)); assert( file_name.size() == 15 || file_name.size() == (unsigned)16 + pt::time_duration::num_fractional_digits() ); if (file_name.size() == 15) { // without fractional seconds file_name += '.'; file_name += std::string(pt::time_duration::num_fractional_digits(), '0'); } file_name += '_'; file_name += m_impl->id_str; if (!tag.empty()) { file_name += '_'; file_name += tag; } fs::path const file(file_name, fs::native); fs::path tmp_path; fs::path new_path; try { tmp_path = m_impl->tmp_dir / file; new_path = m_impl->new_dir / file; } catch (boost::filesystem::filesystem_error const& e) { throw JobDirError(e.what()); } fs::ofstream tmp_file(tmp_path); if (!tmp_file) { std::string msg("create failed for "); msg += tmp_path.string(); msg += " (" + boost::lexical_cast<std::string>(errno) + ')'; throw JobDirError(msg); } tmp_file << contents << std::flush; if (!tmp_file) { std::string msg("write failed for "); msg += tmp_path.string(); msg += " (" + boost::lexical_cast<std::string>(errno) + ')'; throw JobDirError(msg); } tmp_file.close(); // sync too? yes, rename doesn't sync (TODO) bool e = std::rename(tmp_path.string().c_str(), new_path.string().c_str()); if (e) { std::string msg("rename failed for "); msg += tmp_path.string(); msg += " (" + boost::lexical_cast<std::string>(errno) + ')'; throw JobDirError(msg); } return new_path; }