Tomographer  v5.4
Tomographer C++ Framework Documentation
signal_status_report.h
Go to the documentation of this file.
1 /* This file is part of the Tomographer project, which is distributed under the
2  * terms of the MIT license.
3  *
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2016 ETH Zurich, Institute for Theoretical Physics, Philippe Faist
7  * Copyright (c) 2017 Caltech, Institute for Quantum Information and Matter, Philippe Faist
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a copy
10  * of this software and associated documentation files (the "Software"), to deal
11  * in the Software without restriction, including without limitation the rights
12  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13  * copies of the Software, and to permit persons to whom the Software is
14  * furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included in
17  * all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25  * SOFTWARE.
26  */
27 
28 
29 #ifndef TOMOGRAPHER_TOOLS_SIGNAL_STATUS_REPORT
30 #define TOMOGRAPHER_TOOLS_SIGNAL_STATUS_REPORT
31 
32 
41 #include <tomographer/multiproc.h>
42 
43 
44 namespace Tomographer
45 {
46 namespace Tools
47 {
48 
52 template<typename TaskDispatcher, typename Logger>
53 struct TOMOGRAPHER_EXPORT SigHandlerTaskDispatcherStatusReporter
54  : public SignalHandler
55 {
56  typedef typename TaskDispatcher::FullStatusReportType FullStatusReportType;
57 
58  SigHandlerTaskDispatcherStatusReporter(TaskDispatcher * tasks_, Logger & logger_)
59  : tasks(tasks_), logger(logger_)
60  {
61  logger.debug("Tomographer::Tools::SigHandlerTaskDispatcherStatusReporter",
62  "Setting up signal handler, with default callback") ;
63  tasks->setStatusReportHandler(intermediateProgressReport) ;
64  }
65 
66  template<typename CallbackFn>
67  SigHandlerTaskDispatcherStatusReporter(TaskDispatcher * tasks_, Logger & logger_, CallbackFn && fn)
68  : tasks(tasks_), logger(logger_)
69  {
70  logger.debug("Tomographer::Tools::SigHandlerTaskDispatcherStatusReporter",
71  "Setting up signal handler, with custom callback") ;
72  tasks->setStatusReportHandler(std::forward<CallbackFn>(fn)) ;
73  }
74 
75  TaskDispatcher * tasks;
76  Logger & logger;
77 
78  virtual void handleSignal(int /*sig*/)
79  {
80  //
81  // This handler must be signal-safe. Do not call any C/C++ standard library
82  // functions which are not listed as signal-safe -- see POSIX's list at
83  // http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_04_03_03
84  //
85  // requestStatusReport() is required to be signal-safe
86  tasks->requestStatusReport();
87  }
88 
92  static void intermediateProgressReport(const FullStatusReportType& report)
93  {
94  std::cerr << "\n" << report.getHumanReport() << "\n";
95  };
96 
97 };
98 
99 template<typename TaskDispatcher, typename LoggerT>
101 makeSigHandlerTaskDispatcherStatusReporter(TaskDispatcher * tasks, LoggerT & logger)
102 {
104 }
105 
106 
107 template<typename TaskDispatcher, typename LoggerT, typename CallbackFn>
109 makeSigHandlerTaskDispatcherStatusReporter(TaskDispatcher * tasks, LoggerT & logger, CallbackFn && fn)
110 {
112  tasks,
113  logger,
114  std::forward<CallbackFn>(fn)
115  );
116 }
117 
118 
119 
120 } // namespace Tools
121 } // namespace Tomographer
122 
123 
124 #endif
Base namespace for the Tomographer project.
Definition: densellh.h:45
An abstract signal handler (C++ interface)
Some common definitions for multiprocessing interfaces.
A generic handler which requests a status report from an TaskDispatcher Interface.
static void intermediateProgressReport(const FullStatusReportType &report)
Format a nice intermediate progress report.
Basic common code for intercepting a signal and basic interfacing to C++.