FACT++  1.0
template<class T , class S >
int Main::execute ( Configuration conf,
bool  dummy = false 
)

Definition at line 88 of file Main.h.

References DIM_VERSION_NUMBER, WindowLog::Display(), Configuration::Get(), Time::GetAsStr(), WindowLog::GetBacklog(), Configuration::GetName(), WindowLog::GetNullOutput(), Configuration::GetOptions(), Configuration::GetWildcardOptions(), Configuration::Has(), MessageImp::kError, MessageImp::kInfo, MessageImp::kMessage, StateMachineImp::kSM_Ready, MessageImp::kWarn, kYellow, WindowLog::OpenLogFile(), PACKAGE_BUGREPORT, PACKAGE_STRING, PACKAGE_URL, REVISION, WindowLog::SetBacklog(), WindowLog::SetNullOutput(), Dim::Setup(), str, t, Thread(), and Configuration::Vec().

Referenced by AutoScheduler< T >::Schedule().

89  {
90  Dim::Setup(conf.Get<string>("dns"), conf.Has("host")?conf.Get<string>("host"):"");
91 
92  // -----------------------------------------------------------------
93  const fs::path program(conf.GetName());
94 
95  // Split path to program into path and filename
96  const string prgpath = program.parent_path().string();
97 
98 #if BOOST_VERSION < 104600
99  const string prgname = program.filename();
100 #else
101  const string prgname = program.filename().string();
102 #endif
103 
104  fs::path path = conf.Has("logpath") ? conf.Get<string>("logpath") : "";
105 
106  // No explicit path given
107  if (path.empty())
108  {
109  path = prgpath.empty() ? "." : prgpath;
110 
111  // default path not accessible
112  if (access(prgpath.c_str(), W_OK))
113  {
114  path = ".";
115 
116  if (conf.Has("home"))
117  {
118  path = conf.Get<string>("home");
119  path /= ".fact++";
120  }
121  }
122  }
123 
124  // Create directories if necessary
125  fs::create_directories(path);
126 
127  // -----------------------------------------------------------------
128 
129  static T shell((path/prgname).string().c_str(),
130  conf.Has("console") ? conf.Get<int>("console")!=1 : conf.Get<bool>("null"));
131 
132  WindowLog &win = shell.GetStreamIn();
133  WindowLog &wout = shell.GetStreamOut();
134 
135  // Switching off buffering is not strictly necessary, since
136  // the destructor of shell should flush everything still buffered,
137  // nevertheless it helps to debug problems in the initialization
138  // sequence.
139  const bool backlog = wout.GetBacklog();
140  const bool null = wout.GetNullOutput();
141  if (conf.Has("console") || !conf.Get<bool>("null"))
142  {
143  wout.SetBacklog(false);
144  wout.SetNullOutput(false);
145  wout.Display(true);
146  }
147 
148  if (conf.Has("log") && !conf.Get<bool>("no-log"))
149  {
150 #if BOOST_VERSION < 104600
151  const fs::path file = fs::path(conf.Get<string>("log")).filename();
152 #else
153  const fs::path file = fs::path(conf.Get<string>("log")).filename();
154 #endif
155  if (!wout.OpenLogFile((path/file).string(), conf.Get<bool>("append-log")))
156  win << kYellow << "WARNING - Couldn't open log-file " << (path/file).string() << ": " << strerror(errno) << endl;
157  }
158 
159  S io_service(wout);
160 
161  const Time now;
162  io_service.Write(now, "/----------------------- Program ------------------------");
163  io_service.Write(now, "| Program: " PACKAGE_STRING " ("+prgname+":"+to_string(getpid())+")");
164  io_service.Write(now, "| CallPath: "+prgpath);
165  io_service.Write(now, "| Compiled: " __DATE__ " " __TIME__ );
166  io_service.Write(now, "| Revision: " REVISION);
167  io_service.Write(now, "| DIM: v"+to_string(DIM_VERSION_NUMBER/100)+"r"+to_string(DIM_VERSION_NUMBER%100)+" ("+io_service.GetName()+")");
168  io_service.Write(now, "| Contact: " PACKAGE_BUGREPORT);
169  io_service.Write(now, "| URL: " PACKAGE_URL);
170  io_service.Write(now, "| Start: "+now.GetAsStr("%c"));
171  io_service.Write(now, "\\----------------------- Options ------------------------");
172  const multimap<string,string> mmap = conf.GetOptions();
173  for (auto it=mmap.begin(); it!=mmap.end(); it++)
174  io_service.Write(now, ": "+it->first+(it->second.empty()?"":" = ")+it->second);
175 
176  const map<string,string> &args = conf.GetOptions<string>("arg:");
177  if (!args.empty())
178  {
179  io_service.Write(now, "------------------------ Arguments ----------------------", MessageImp::kMessage);
180 
181  for (auto it=args.begin(); it!=args.end(); it++)
182  {
183  ostringstream str;
184  str.setf(ios_base::left);
185  str << ": " << it->first << " = " << it->second;
186  io_service.Write(now, str.str(), MessageImp::kMessage);
187  }
188  }
189 
190  io_service.Write(now, "\\------------------- Evaluating options -----------------");
191  const int rc = io_service.EvalOptions(conf);
192  if (rc>=0)
193  {
194  ostringstream str;
195  str << "Exit triggered by EvalOptions with rc=" << rc;
196  io_service.Write(now, str.str(), rc==0?MessageImp::kInfo:MessageImp::kError);
197  return rc;
198  }
199 
200  const map<string,string> &wco = conf.GetWildcardOptions();
201  if (!wco.empty())
202  {
203  io_service.Write(now, "------------- Unrecognized wildcard options -------------", MessageImp::kWarn);
204 
205  size_t max = 0;
206  for (auto it=wco.begin(); it!=wco.end(); it++)
207  if (it->second.length()>max)
208  max = it->second.length();
209 
210  for (auto it=wco.begin(); it!=wco.end(); it++)
211  {
212  ostringstream str;
213  str.setf(ios_base::left);
214  str << setw(max+1) << it->second << " : " << it->first;
215  io_service.Write(now, str.str(), MessageImp::kWarn);
216  }
217  io_service.Write(now, "Unrecognized options found, will exit with rc=127", MessageImp::kError);
218  return 127;
219  }
220 
221  io_service.Message("==================== Starting main loop =================");
222 
223  if (conf.Has("console") || !conf.Get<bool>("null"))
224  {
225  wout.SetNullOutput(null);
226  wout.SetBacklog(backlog);
227  }
228 
229  shell.SetReceiver(io_service);
230 
231  // boost::thread t(boost::bind(&AutoScheduler<S>::Run, &io_service));
232  int ret = 0;
233  thread t(bind(Main::Thread, &io_service, dummy, ref(ret)));
234 
235  // Wait until state machine is ready (The only case I can imagine
236  // in which the state will never chane is when DIM triggers
237  // an exit before the state machine has been started at all.
238  // Hopefully checking the readline (see Threed) should fix
239  // that -- difficult to test.)
240  while ((io_service.GetCurrentState()<StateMachineImp::kSM_Ready ||
241  !io_service.MessageQueueEmpty()) && !shell.IsStopped())
242  usleep(1);
243 
244  // Execute command line commands
245  const vector<string> v1 = conf.Vec<string>("cmd");
246  for (vector<string>::const_iterator it=v1.begin(); it!=v1.end(); it++)
247  shell.ProcessLine(*it);
248 
249  const vector<string> v2 = conf.Vec<string>("exec");
250  for (vector<string>::const_iterator it=v2.begin(); it!=v2.end(); it++)
251  shell.Execute(*it, args);
252 
253  // Run the shell if no immediate exit was requested
254  if (!conf.Get<bool>("quit"))
255  shell.Run();
256 
257  io_service.Stop(); // Signal Loop-thread to stop
258  // io_service.Close(); // Obsolete, done by the destructor
259  // wout << "join: " << t.timed_join(boost::posix_time::milliseconds(0)) << endl;
260 
261  // Wait until the StateMachine has finished its thread
262  // before returning and destroying the dim objects which might
263  // still be in use.
264  t.join();
265 
266  return ret;
267  }
#define PACKAGE_BUGREPORT
#define PACKAGE_STRING
Mainloop running, state machine in operation.
A warning, things that somehow might result in unexpected or unwanted bahaviour.
Definition: MessageImp.h:18
void SetNullOutput(bool n=true)
Switch on or off any physical output to the screen (cout or fWindow)
Definition: WindowLog.h:137
Adds some functionality to boost::posix_time::ptime for our needs.
Definition: Time.h:30
char str[80]
Definition: test_client.c:7
A C++ ostream to an ncurses window supporting attributes and colors.
Definition: WindowLog.h:50
T Get(const std::string &var)
Set color Yellow.
Definition: WindowLog.h:19
const std::map< std::string, T > GetOptions(const std::string &opt)
std::vector< T > Vec(const std::string &var)
void Setup(const std::string &dns="", const std::string &host="")
Definition: DimSetup.cc:160
#define REVISION
Definition: ofits.h:16
An info telling something which can be interesting to know.
Definition: MessageImp.h:17
bool GetBacklog() const
Definition: WindowLog.h:142
Just a message, usually obsolete.
Definition: MessageImp.h:16
bool Has(const std::string &var)
#define PACKAGE_URL
void Thread(MainImp *io_service, bool dummy, int &rc)
Definition: Main.h:79
Error, something unexpected happened, but can still be handled by the program.
Definition: MessageImp.h:19
void Display(bool empty=false)
Display backlog.
Definition: WindowLog.cc:46
#define DIM_VERSION_NUMBER
Definition: dim.h:16
TT t
Definition: test_client.c:26
bool GetNullOutput() const
Definition: WindowLog.h:138
const std::map< std::string, std::string > & GetWildcardOptions() const
std::string GetAsStr(const char *fmt="%Y-%m-%d %H:%M:%S") const
Definition: Time.cc:240
bool OpenLogFile(const std::string &filename, bool append=false)
Open a log-file.
Definition: WindowLog.cc:111
const std::string & GetName() const
void SetBacklog(bool n=true)
Switch on or off any storage in the backlog.
Definition: WindowLog.h:141

+ Here is the call graph for this function:

+ Here is the caller graph for this function: