Tracer_service.cc
Go to the documentation of this file.
1 // vim: set sw=2 expandtab :
2 
14 #include "art/Utilities/Globals.h"
21 #include "fhiclcpp/types/Atom.h"
22 #include "fhiclcpp/types/Name.h"
24 
25 #include <atomic>
26 #include <iostream>
27 #include <string>
28 
29 using namespace std;
30 using namespace std::string_literals;
31 
32 namespace art {
33 
34  class Tracer {
35  public:
36  struct Config {
37  fhicl::Atom<string> indentation{fhicl::Name{"indentation"}, "++"};
38  };
41 
42  private:
43  void log_with_indent(unsigned n, std::string const& message) const;
44 
45  void postBeginJob();
46  void postEndJob();
47 
48  void preBeginRun(Run const& run);
49  void postBeginRun(Run const& run);
50 
51  void preBeginSubRun(SubRun const& subRun);
52  void postBeginSubRun(SubRun const& subRun);
53 
54  void preEvent(Event const& ev, ScheduleContext);
55  void postEvent(Event const& ev, ScheduleContext);
56 
57  void preEndSubRun(SubRunID const& id, Timestamp const& ts);
58  void postEndSubRun(SubRun const& run);
59 
60  void preEndRun(RunID const& id, Timestamp const& ts);
61  void postEndRun(Run const& run);
62 
63  void preModuleConstruction(ModuleDescription const& md);
64  void postModuleConstruction(ModuleDescription const& md);
65 
66  void preModuleBeginJob(ModuleDescription const& md);
67  void postModuleBeginJob(ModuleDescription const& md);
68 
69  void preModuleBeginRun(ModuleContext const& mc);
70  void postModuleBeginRun(ModuleContext const& mc);
71 
72  void preModuleBeginSubRun(ModuleContext const& mc);
73  void postModuleBeginSubRun(ModuleContext const& mc);
74 
75  void preModuleEvent(ModuleContext const& mc);
76  void postModuleEvent(ModuleContext const& mc);
77 
78  void preModuleEndSubRun(ModuleContext const& mc);
79  void postModuleEndSubRun(ModuleContext const& mc);
80 
81  void preModuleEndRun(ModuleContext const& mc);
82  void postModuleEndRun(ModuleContext const& mc);
83 
84  void preModuleEndJob(ModuleDescription const& md);
85  void postModuleEndJob(ModuleDescription const& md);
86 
87  void preSourceEvent(ScheduleContext);
88  void postSourceEvent(Event const&, ScheduleContext);
89 
90  void preSourceSubRun();
91  void postSourceSubRun(SubRun const&);
92 
93  void preSourceRun();
94  void postSourceRun(Run const&);
95 
96  void preOpenFile();
97  void postOpenFile(string const& fn);
98 
99  void preCloseFile();
100  void postCloseFile();
101 
102  void postOpenOutputFile(string const& label);
103  void preCloseOutputFile(string const& label);
104  void postCloseOutputFile(OutputFileInfo const& info);
105 
106  void prePathBeginRun(string const& s);
107  void postPathBeginRun(string const& s, HLTPathStatus const& hlt);
108 
109  void prePathBeginSubRun(string const& s);
110  void postPathBeginSubRun(string const& s, HLTPathStatus const& hlt);
111 
112  void prePathEvent(PathContext const& pc);
113  void postPathEvent(PathContext const& pc, HLTPathStatus const& hlt);
114 
115  void prePathEndSubRun(string const& s);
116  void postPathEndSubRun(string const& s, HLTPathStatus const& hlt);
117 
118  void prePathEndRun(string const& s);
119  void postPathEndRun(string const& s, HLTPathStatus const& hlt);
120 
121  // Member Data
122  private:
123  string const indentation_;
124  std::atomic<unsigned int> depth_{};
125  };
126 
128  : indentation_{config().indentation()}
129  {
130  if (auto const nthreads = Globals::instance()->nthreads(); nthreads != 1) {
131  mf::LogWarning("Tracer") << "Because " << nthreads
132  << " threads have been configured, the tracing "
133  "messages will be interleaved.\n"
134  << "Please configure your job to use one thread "
135  "for a predictable output.";
136  }
137 
138  iRegistry.sPostBeginJob.watch(this, &Tracer::postBeginJob);
139  iRegistry.sPostEndJob.watch(this, &Tracer::postEndJob);
140  iRegistry.sPreModule.watch(this, &Tracer::preModuleEvent);
141  iRegistry.sPostModule.watch(this, &Tracer::postModuleEvent);
142  iRegistry.sPreModuleConstruction.watch(this,
144  iRegistry.sPostModuleConstruction.watch(this,
146  iRegistry.sPreModuleBeginJob.watch(this, &Tracer::preModuleBeginJob);
147  iRegistry.sPostModuleBeginJob.watch(this, &Tracer::postModuleBeginJob);
148  iRegistry.sPreModuleEndJob.watch(this, &Tracer::preModuleEndJob);
149  iRegistry.sPostModuleEndJob.watch(this, &Tracer::postModuleEndJob);
150  iRegistry.sPreModuleBeginRun.watch(this, &Tracer::preModuleBeginRun);
151  iRegistry.sPostModuleBeginRun.watch(this, &Tracer::postModuleBeginRun);
152  iRegistry.sPreModuleEndRun.watch(this, &Tracer::preModuleEndRun);
153  iRegistry.sPostModuleEndRun.watch(this, &Tracer::postModuleEndRun);
154  iRegistry.sPreModuleBeginSubRun.watch(this, &Tracer::preModuleBeginSubRun);
155  iRegistry.sPostModuleBeginSubRun.watch(this,
157  iRegistry.sPreModuleEndSubRun.watch(this, &Tracer::preModuleEndSubRun);
158  iRegistry.sPostModuleEndSubRun.watch(this, &Tracer::postModuleEndSubRun);
159  iRegistry.sPreProcessPath.watch(this, &Tracer::prePathEvent);
160  iRegistry.sPostProcessPath.watch(this, &Tracer::postPathEvent);
161  iRegistry.sPrePathBeginRun.watch(this, &Tracer::prePathBeginRun);
162  iRegistry.sPostPathBeginRun.watch(this, &Tracer::postPathBeginRun);
163  iRegistry.sPrePathEndRun.watch(this, &Tracer::prePathEndRun);
164  iRegistry.sPostPathEndRun.watch(this, &Tracer::postPathEndRun);
165  iRegistry.sPrePathBeginSubRun.watch(this, &Tracer::prePathBeginSubRun);
166  iRegistry.sPostPathBeginSubRun.watch(this, &Tracer::postPathBeginSubRun);
167  iRegistry.sPrePathEndSubRun.watch(this, &Tracer::prePathEndSubRun);
168  iRegistry.sPostPathEndSubRun.watch(this, &Tracer::postPathEndSubRun);
169  iRegistry.sPreProcessEvent.watch(this, &Tracer::preEvent);
170  iRegistry.sPostProcessEvent.watch(this, &Tracer::postEvent);
171  iRegistry.sPreBeginRun.watch(this, &Tracer::preBeginRun);
172  iRegistry.sPostBeginRun.watch(this, &Tracer::postBeginRun);
173  iRegistry.sPreEndRun.watch(this, &Tracer::preEndRun);
174  iRegistry.sPostEndRun.watch(this, &Tracer::postEndRun);
175  iRegistry.sPreBeginSubRun.watch(this, &Tracer::preBeginSubRun);
176  iRegistry.sPostBeginSubRun.watch(this, &Tracer::postBeginSubRun);
177  iRegistry.sPreEndSubRun.watch(this, &Tracer::preEndSubRun);
178  iRegistry.sPostEndSubRun.watch(this, &Tracer::postEndSubRun);
179  iRegistry.sPreSourceEvent.watch(this, &Tracer::preSourceEvent);
180  iRegistry.sPostSourceEvent.watch(this, &Tracer::postSourceEvent);
181  iRegistry.sPreOpenFile.watch(this, &Tracer::preOpenFile);
182  iRegistry.sPostOpenFile.watch(this, &Tracer::postOpenFile);
183  iRegistry.sPreCloseFile.watch(this, &Tracer::preCloseFile);
184  iRegistry.sPostCloseFile.watch(this, &Tracer::postCloseFile);
185  iRegistry.sPostOpenOutputFile.watch(this, &Tracer::postOpenOutputFile);
186  iRegistry.sPreCloseOutputFile.watch(this, &Tracer::preCloseOutputFile);
187  iRegistry.sPostCloseOutputFile.watch(this, &Tracer::postCloseOutputFile);
188  iRegistry.sPreSourceRun.watch(this, &Tracer::preSourceRun);
189  iRegistry.sPostSourceRun.watch(this, &Tracer::postSourceRun);
190  iRegistry.sPreSourceSubRun.watch(this, &Tracer::preSourceSubRun);
191  iRegistry.sPostSourceSubRun.watch(this, &Tracer::postSourceSubRun);
192  }
193 
194  void
196  {
197  std::string printout;
198  for (; n != 0u; --n) {
199  printout.append(indentation_);
200  }
201  printout += ' ';
202  printout += message;
203  printout += '\n';
204  std::cout << printout;
205  }
206 
207  void
209  {
210  log_with_indent(1, "Job started");
211  }
212 
213  void
215  {
216  log_with_indent(1, "Job ended");
217  }
218 
220  {
221  log_with_indent(2, "source event");
222  }
223 
224  void
226  {
227  log_with_indent(2, "finished source event");
228  }
229 
230  void
232  {
233  log_with_indent(2, "source subRun");
234  }
235 
236  void
238  {
239  log_with_indent(2, "finished source subRun");
240  }
241 
242  void
244  {
245  log_with_indent(2, "source run");
246  }
247 
248  void
250  {
251  log_with_indent(2, "finished source run");
252  }
253 
254  void
256  {
257  log_with_indent(2, "open input file");
258  }
259 
260  void
261  Tracer::postOpenFile(string const& fn)
262  {
263  string const displayed_fn{fn.empty() ? "<none>"s : fn};
264  log_with_indent(2, "finished open input file: " + displayed_fn);
265  }
266 
267  void
269  {
270  log_with_indent(2, "close input file");
271  }
272 
273  void
275  {
276  log_with_indent(2, "finished close input file");
277  }
278 
279  void
281  {
282  log_with_indent(2, "opened output file from " + label);
283  }
284 
285  void
287  {
288  log_with_indent(2, "close output file from " + label);
289  }
290 
291  void
293  {
294  string const fn{info.fileName().empty() ? "<none>"s : info.fileName()};
296  2, "finished close output file " + fn + " from " + info.moduleLabel());
297  }
298 
299  void
301  {
302  depth_ = 0;
303  std::ostringstream msg;
304  msg << "processing event: " << ev.id() << " time: " << ev.time().value();
305  log_with_indent(2, msg.str());
306  }
307 
308  void
310  {
311  log_with_indent(2, "finished event");
312  }
313 
314  void
316  {
317  log_with_indent(3, "processing path for event: " + pc.pathName());
318  }
319 
320  void
322  {
323  log_with_indent(3, "finished path for event: " + pc.pathName());
324  }
325 
326  void
328  {
329  ++depth_;
330  log_with_indent(3 + depth_, "module for event: " + mc.moduleLabel());
331  }
332 
333  void
335  {
336  --depth_;
337  log_with_indent(4 + depth_, "finished for event: " + mc.moduleLabel());
338  }
339 
340  void
342  {
343  depth_ = 0;
344  std::ostringstream msg;
345  msg << "processing begin run: " << run.id()
346  << " time: " << run.beginTime().value();
347  log_with_indent(2, msg.str());
348  }
349 
350  void
352  {
353  log_with_indent(2, "finished begin run");
354  }
355 
356  void
357  Tracer::prePathBeginRun(string const& iName)
358  {
359  log_with_indent(3, "processing path for begin run: " + iName);
360  }
361 
362  void
363  Tracer::postPathBeginRun(string const& /*iName*/, HLTPathStatus const&)
364  {
365  log_with_indent(3, "finished path for begin run");
366  }
367 
368  void
370  {
371  ++depth_;
372  log_with_indent(3 + depth_, "module for begin run: " + mc.moduleLabel());
373  }
374 
375  void
377  {
378  --depth_;
379  log_with_indent(4 + depth_, "finished for begin run: " + mc.moduleLabel());
380  }
381 
382  void
383  art::Tracer::preEndRun(RunID const& iID, Timestamp const& iTime)
384  {
385  depth_ = 0;
386  std::ostringstream msg;
387  msg << "processing end run: " << iID << " time: " << iTime.value();
388  log_with_indent(2, msg.str());
389  }
390 
391  void
393  {
394  log_with_indent(2, "finished end run");
395  }
396 
397  void
398  Tracer::prePathEndRun(string const& iName)
399  {
400  log_with_indent(3, "processing path for end run: " + iName);
401  }
402 
403  void
404  Tracer::postPathEndRun(string const& /*iName*/, HLTPathStatus const&)
405  {
406  log_with_indent(3, "finished path for end run");
407  }
408 
409  void
411  {
412  ++depth_;
413  log_with_indent(3 + depth_, "module for end run: " + mc.moduleLabel());
414  }
415 
416  void
418  {
419  --depth_;
420  log_with_indent(4 + depth_, "finished for end run: " + mc.moduleLabel());
421  }
422 
423  void
425  {
426  depth_ = 0;
427  std::ostringstream msg;
428  msg << "processing begin subRun: " << subRun.id()
429  << " time: " << subRun.beginTime().value();
430  log_with_indent(2, msg.str());
431  }
432 
433  void
435  {
436  log_with_indent(2, "finished begin subRun");
437  }
438 
439  void
440  Tracer::prePathBeginSubRun(string const& iName)
441  {
442  log_with_indent(3, "processing path for begin subRun: " + iName);
443  }
444 
445  void
446  Tracer::postPathBeginSubRun(string const& /*iName*/, HLTPathStatus const&)
447  {
448  log_with_indent(3, "finished path for begin subRun");
449  }
450 
451  void
453  {
454  ++depth_;
455  log_with_indent(3 + depth_, "module for begin subRun: " + mc.moduleLabel());
456  }
457 
458  void
460  {
461  --depth_;
462  log_with_indent(4, "finished for begin subRun: " + mc.moduleLabel());
463  }
464 
465  void
466  art::Tracer::preEndSubRun(SubRunID const& iID, Timestamp const& iTime)
467  {
468  depth_ = 0;
469  std::ostringstream msg;
470  msg << "processing end subRun: " << iID << " time: " << iTime.value();
471  log_with_indent(2, msg.str());
472  }
473 
474  void
476  {
477  log_with_indent(2, "finished end subRun");
478  }
479 
480  void
481  Tracer::prePathEndSubRun(string const& iName)
482  {
483  log_with_indent(3, "processing path for end subRun: " + iName);
484  }
485 
486  void
487  Tracer::postPathEndSubRun(string const& /*iName*/, HLTPathStatus const&)
488  {
489  log_with_indent(3, "finished path for end subRun");
490  }
491 
492  void
494  {
495  ++depth_;
496  log_with_indent(3 + depth_, "module for end subRun: " + mc.moduleLabel());
497  }
498 
499  void
501  {
502  --depth_;
503  log_with_indent(4 + depth_, "finished for end subRun: " + mc.moduleLabel());
504  }
505 
506  void
508  {
509  log_with_indent(1, "constructing module: " + md.moduleLabel());
510  }
511 
512  void
514  {
515  log_with_indent(1, "construction finished: " + md.moduleLabel());
516  }
517 
518  void
520  {
521  log_with_indent(1, "beginJob module: " + md.moduleLabel());
522  }
523 
524  void
526  {
527  log_with_indent(1, "beginJob finished: " + md.moduleLabel());
528  }
529 
530  void
532  {
533  log_with_indent(1, "endJob module: " + md.moduleLabel());
534  }
535 
536  void
538  {
539  log_with_indent(1, "endJob finished: " + md.moduleLabel());
540  }
541 
542 } // namespace art
543 
void preModuleEndSubRun(ModuleContext const &mc)
void preModuleEndRun(ModuleContext const &mc)
void log_with_indent(unsigned n, std::string const &message) const
void postSourceSubRun(SubRun const &)
void postSourceRun(Run const &)
void preCloseOutputFile(string const &label)
void preModuleConstruction(ModuleDescription const &md)
RunID id() const
Definition: Run.cc:17
void postModuleConstruction(ModuleDescription const &md)
void msg(const char *fmt,...)
Definition: message.cpp:107
std::string string
Definition: nybbler.cc:12
void preModuleBeginSubRun(ModuleContext const &mc)
void postPathBeginSubRun(string const &s, HLTPathStatus const &hlt)
std::string const & moduleLabel() const
void preEvent(Event const &ev, ScheduleContext)
void preEndRun(RunID const &id, Timestamp const &ts)
void postPathEndSubRun(string const &s, HLTPathStatus const &hlt)
STL namespace.
void postModuleBeginJob(ModuleDescription const &md)
void postModuleBeginRun(ModuleContext const &mc)
void postModuleBeginSubRun(ModuleContext const &mc)
void preBeginRun(Run const &run)
std::atomic< unsigned int > depth_
std::string const & moduleLabel() const
constexpr TimeValue_t value() const
Definition: Timestamp.h:23
Definition: Run.h:17
void preSourceEvent(ScheduleContext)
fhicl::Atom< string > indentation
void preBeginSubRun(SubRun const &subRun)
void preBeginRun(const ::art::Run &run)
void postEndSubRun(SubRun const &run)
Tracer(Parameters const &, ActivityRegistry &)
void preModuleBeginRun(ModuleContext const &mc)
QAsciiDict< Entry > fn
auto const & pathName() const
Definition: PathContext.h:63
std::string const & fileName() const
Timestamp time() const
void preModuleEvent(ModuleContext const &mc)
void postModuleEndRun(ModuleContext const &mc)
#define DECLARE_ART_SERVICE(svc, scope)
static Config * config
Definition: config.cpp:1054
void postBeginRun(Run const &run)
void preModuleEndJob(ModuleDescription const &md)
void postModuleEvent(ModuleContext const &mc)
void preCloseFile()
std::void_t< T > n
void postOpenFile(string const &fn)
void postEndRun(Run const &run)
void postEvent(Event const &ev, ScheduleContext)
void prePathEndSubRun(string const &s)
void postModuleEndSubRun(ModuleContext const &mc)
void prePathBeginRun(string const &s)
void prePathEndRun(string const &s)
void postPathEndRun(string const &s, HLTPathStatus const &hlt)
#define DEFINE_ART_SERVICE(svc)
void postOpenOutputFile(string const &label)
void postModuleEndJob(ModuleDescription const &md)
void preModuleBeginJob(ModuleDescription const &md)
void postBeginJob()
void preSourceRun()
SubRunID id() const
Definition: SubRun.cc:21
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
void preSourceSubRun()
auto const & moduleLabel() const
Definition: ModuleContext.h:43
void postCloseFile()
static Globals * instance()
Definition: Globals.cc:17
void preEndSubRun(SubRunID const &id, Timestamp const &ts)
void prePathEvent(PathContext const &pc)
void prePathBeginSubRun(string const &s)
void preOpenFile()
void postCloseOutputFile(OutputFileInfo const &info)
static QCString * s
Definition: config.cpp:1042
EventID id() const
Definition: Event.cc:34
void postPathEvent(PathContext const &pc, HLTPathStatus const &hlt)
Timestamp const & beginTime() const
Definition: DataViewImpl.cc:92
string const indentation_
void postBeginSubRun(SubRun const &subRun)
void postSourceEvent(Event const &, ScheduleContext)
void postPathBeginRun(string const &s, HLTPathStatus const &hlt)