Belle II Software  release-08-01-10
Executor.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 #include "daq/slc/system/Executor.h"
9 #include "daq/slc/system/LogFile.h"
10 
11 #include <iostream>
12 #include <cstdarg>
13 #include <cstdio>
14 #include <unistd.h>
15 
16 using namespace Belle2;
17 
18 void Executor::setExecutable(const char* format, ...)
19 {
20  va_list ap;
21  static char ss[1024];
22  va_start(ap, format);
23  vsprintf(ss, format, ap);
24  va_end(ap);
25  m_path = ss;
26 }
27 
28 void Executor::setExecutable(const std::string& path)
29 {
30  m_path = path;
31 }
32 
33 void Executor::addArg(const char* format, ...)
34 {
35  va_list ap;
36  static char ss[1024];
37  va_start(ap, format);
38  vsprintf(ss, format, ap);
39  va_end(ap);
40  m_arg_v.push_back(ss);
41 }
42 
43 void Executor::addArg(const std::string& arg)
44 {
45  m_arg_v.push_back(arg);
46 }
47 
48 bool Executor::execute(bool isdaemon)
49 {
50  char* argv[256];
51  argv[0] = (char*)m_path.c_str();
52  std::cerr << "" << argv[0] << " ";
53  for (size_t i = 0; i < m_arg_v.size(); i++) {
54  argv[i + 1] = (char*)m_arg_v[i].c_str();
55  std::cerr << "" << argv[i + 1] << " ";
56  }
57  std::cerr << std::endl;
58  argv[m_arg_v.size() + 1] = NULL;
59  if (isdaemon) daemon(0, 0);
60  if (execvp(m_path.c_str(), argv) == -1) {
61  LogFile::error("Faield to start %s", argv[0]);
62  return false;
63  }
64 
65  return true;
66 }
Abstract base class for different kinds of events.