ctools  2.0.0
 All Classes Namespaces Files Functions Variables Macros Pages
support.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * support - support functions *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2016-2019 by Juergen Knoedlseder *
5  * ----------------------------------------------------------------------- *
6  * *
7  * This program is free software: you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation, either version 3 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19  * *
20  ***************************************************************************/
21 /**
22  * @file support.cpp
23  * @brief Support function implementation
24  * @author Juergen Knoedlseder
25  */
26 
27 /* __ Includes ___________________________________________________________ */
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 #include "GammaLib.hpp"
32 #include "GCTALib.hpp"
33 #include "support.hpp"
34 
35 /* __ Method name definitions ____________________________________________ */
36 
37 /* __ Debug definitions __________________________________________________ */
38 
39 /* __ Coding definitions _________________________________________________ */
40 
41 
42 /*==========================================================================
43  = =
44  = Support functions =
45  = =
46  ==========================================================================*/
47 
48 /***********************************************************************//**
49  * @brief Execute ctool
50  *
51  * @param[in] tool Pointer to ctool.
52  * @return Return code
53  *
54  * Executes a ctool. The execution is enclosed in a try-catch statement so
55  * that any exceptions that occur during execution will be properly logged
56  * and written into the standard output.
57  ***************************************************************************/
58 int execute_ctool(ctool* tool)
59 {
60  // Initialise return code
61  int rc = 1;
62 
63  // Try executing tool. This catches any exceptions that occur during
64  // the execution.
65  try {
66 
67  // Execute ctool
68  tool->execute();
69 
70  // Signal success
71  rc = 0;
72 
73  } // endtry
74 
75  // Handle any exceptions that have occured
76  catch (std::exception &e) {
77 
78  // Extract error message
79  std::string message = e.what();
80  std::string signal = "*** ERROR encounterted in the execution of "+
81  tool->name()+". Run aborted ...";
82 
83  // Write error in logger
84  tool->log << signal << std::endl;
85  tool->log << message << std::endl;
86 
87  // Write error in standard output
88  std::cout << signal << std::endl;
89  std::cout << message << std::endl;
90 
91  } // endcatch: catched any exception
92 
93  // Return return code
94  return rc;
95 }
96 
97 
98 /***********************************************************************//**
99  * @brief Report ctool failure
100  *
101  * @param[in] name Name of ctool.
102  * @param[in] message Exception message.
103  *
104  * Reports a ctools failure.
105  ***************************************************************************/
106 void report_ctool_failure(const std::string& name, const std::string& message)
107 {
108  // Set signal string
109  std::string signal = "*** ERROR encounterted in the execution of "+
110  name+". Run aborted ...";
111 
112  // Write error in standard output
113  std::cout << signal << std::endl;
114  std::cout << message << std::endl;
115 
116  // Return
117  return;
118 }
Support function definitions.
Base class for ctools.
Definition: ctool.hpp:50
void report_ctool_failure(const std::string &name, const std::string &message)
Report ctool failure.
Definition: support.cpp:106
virtual void execute(void)
Execute ctool.
Definition: ctool.cpp:285
int execute_ctool(ctool *tool)
Execute ctool.
Definition: support.cpp:58