28#include <fairlogger/Logger.h>
33std::string ltrim(
const std::string& s,
const std::string& what =
"\" ' ")
35 auto start =
s.find_first_not_of(what);
36 if (
start == std::string::npos) {
43std::string rtrim(
const std::string& s,
const std::string& what =
"\"' ")
45 auto end =
s.find_last_not_of(what);
46 return s.substr(0,
end + 1);
49std::string
trim(
const std::string& s,
const std::string& what =
"\"' ")
51 return rtrim(ltrim(s, what), what);
93 std::stringstream s(filenames);
95 while (std::getline(s,
f,
',')) {
122 LOG(info) <<
"Command line to execute: \"" << cmd <<
"\"";
126 LOG(fatal) <<
"Failed to fork process: " << std::strerror(errno);
133 execl(
"/bin/sh",
"sh",
"-c", cmd.c_str(), (
char*)
nullptr);
135 LOG(fatal) <<
"Failed to execute command: " << std::strerror(errno);
140 LOG(info) <<
"Child spawned process group is running with PID: " <<
mCmdPid;
148 LOG(info) <<
"No command is currently running";
156 constexpr unsigned int pollMillis = 10;
157 for (
unsigned int waited = 0; waited < graceMillis; waited += pollMillis) {
159 pid_t reaped = waitpid(
mCmdPid, &status, WNOHANG);
161 LOG(info) <<
"Command with process ID " <<
mCmdPid <<
" exited by itself";
168 std::this_thread::sleep_for(std::chrono::milliseconds(pollMillis));
171 LOG(info) <<
"Terminating process ID group " <<
mCmdPid;
172 if (kill(-
mCmdPid, SIGKILL) == -1) {
173 LOG(fatal) <<
"Failed to kill process: " << std::strerror(errno);
179 if (waitpid(
mCmdPid, &status, 0) == -1) {
180 LOG(fatal) <<
"Failed to wait for process termination: " << std::strerror(errno);
192 LOG(fatal) <<
"No file names to make temporary file from";
195 LOG(warning) <<
"More than one file name to make temporary file from";
196 LOG(warning) <<
"Using the first one: " <<
mFileNames.front();
197 LOG(warning) <<
"Removing all the others";
202 std::ofstream ofs(
mFileNames.front().c_str());
204 LOG(fatal) <<
"Failed to create temporary file: " <<
mFileNames.front();
211 char buf[] =
"generatorFifoXXXXXX";
212 auto fp = mkstemp(
buf);
214 LOG(fatal) <<
"Failed to make temporary file: "
215 << std::strerror(errno);
228 LOG(info) <<
"Temporary file name empty, nothing to remove";
236 if (not std::filesystem::exists(p)) {
237 LOG(info) <<
"Temporary file " << p <<
" does not exist";
243 std::filesystem::remove(p, ec);
245 LOG(warn) <<
"When removing " << p <<
": " << ec.message();
260 int ret = mkfifo(fileName.c_str(), 0600);
262 LOG(fatal) <<
"Failed to make fifo \"" << fileName <<
"\": "
263 << std::strerror(errno);
274 auto c = std::filesystem::canonical(std::filesystem::path(
f));
277 }
catch (std::exception& e) {
278 LOG(error) << e.what();
286 using namespace std::chrono_literals;
291 LOG(
debug) <<
"Waiting for data on " << p;
294 while (not std::filesystem::exists(p)) {
295 std::this_thread::sleep_for(
mWait * 1ms);
300 while (std::filesystem::file_size(p) <= 256) {
301 std::this_thread::sleep_for(
mWait * 1ms);
305 LOG(
debug) <<
"Got data in " << p <<
", sleeping for a while";
306 std::this_thread::sleep_for(
mWait * 2ms);
Utility functions for MC particles.
ULong_t getStartSeed() const
unsigned int getNEvents() const
GLenum GLuint GLenum GLsizei const GLchar * buf
std::string trim(std::string const &str)
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
void setCmdPid(const pid_t cmdPid)
virtual bool ensureFiles()
void setBackgroundSwitch(const std::string &opt)
void setSeedSwitch(const std::string &opt)
void setFileNames(const std::string &filenames)
void setNEvents(unsigned int nev)
void setup(const GeneratorFileOrCmdParam ¶m, const conf::SimConfig &config)
std::list< std::string > mFileNames
void setSeed(unsigned long seed)
void setNEventsSwitch(const std::string &opt)
void setOutputSwitch(const std::string &opt)
std::string mNEventsSwitch
virtual bool terminateCmd(unsigned int graceMillis=0)
void setCmd(const std::string &cmd)
virtual std::string makeCmdLine() const
std::string mOutputSwitch
virtual void waitForData(const std::string &filename) const
virtual bool removeTemp() const
virtual bool executeCmdLine(const std::string &cmd)
virtual bool makeTemp(const bool &)
std::string mBackgroundSwitch
void setBmaxSwitch(const std::string &opt)
virtual bool makeFifo() const
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"