mCRL2
Loading...
Searching...
No Matches
tool.h
Go to the documentation of this file.
1// Author(s): Wieger Wesselink
2// Copyright: see the accompanying file COPYING or copy at
3// https://github.com/mCRL2org/mCRL2/blob/master/COPYING
4//
5// Distributed under the Boost Software License, Version 1.0.
6// (See accompanying file LICENSE_1_0.txt or copy at
7// http://www.boost.org/LICENSE_1_0.txt)
8//
11
12#ifndef MCRL2_UTILITIES_TOOL_H
13#define MCRL2_UTILITIES_TOOL_H
14
16
20
21#ifdef MCRL2_PLATFORM_WINDOWS
22 #include <io.h>
23 #include <fcntl.h>
24#endif
25
26namespace mcrl2
27{
28
29namespace utilities
30{
31
33namespace tools
34{
35
38class tool
39{
40 protected:
42 std::string m_name;
43
45 std::string m_author;
46
48 std::string m_what_is;
49
51 std::string m_tool_description;
52
54 std::string m_known_issues;
55
57 std::string m_timing_filename;
58
61
64
67 virtual void add_options(interface_description& desc)
68 {
69 desc.add_option("timings", make_optional_argument<std::string>("FILE", ""),
70 "append timing measurements to FILE. Measurements are written to "
71 "standard error if no FILE is provided");
72 }
73
76 virtual void parse_options(const command_line_parser& parser)
77 {
78 if (parser.options.count("timings") > 0)
79 {
80 m_timing_enabled = true;
82 m_timing_filename = parser.option_argument("timings");
83 }
84 }
85
88 virtual bool pre_run(int& /*argc*/, char** /*argv*/)
89 {
90 return true;
91 }
92
95 virtual void check_standard_options(const command_line_parser& parser)
96 {
97 if (parser.options.count("quiet"))
98 {
99 if (parser.options.count("debug"))
100 {
101 parser.error("options -q/--quiet and -d/--debug cannot be used together\n");
102 }
103 if (parser.options.count("verbose"))
104 {
105 parser.error("options -q/--quiet and -v/--verbose cannot be used together\n");
106 }
107 }
108#ifndef MCRL2_TOOL_CLASSES_NO_CORE
109 if (parser.options.count("quiet"))
110 {
112 }
113 if (parser.options.count("verbose"))
114 {
116 }
117 if (parser.options.count("debug"))
118 {
120 }
121 if (parser.options.count("log-level"))
122 {
123 log::logger::set_reporting_level(log::log_level_from_string(parser.option_argument("log-level")));
124 }
125#endif
126 }
127
132 virtual void check_positional_options(const command_line_parser& parser)
133 {
134 parser.check_no_duplicate_arguments();
135 }
136
139 virtual std::string synopsis() const
140 {
141 return "[OPTION]...\n";
142 }
143
144 public:
146 tool(const std::string& name,
147 const std::string& author,
148 const std::string& what_is,
149 const std::string& tool_description,
150 std::string known_issues = ""
151 )
152 : m_name(name),
153 m_author(author),
154 m_what_is(what_is),
155 m_tool_description(tool_description),
156 m_known_issues(known_issues),
158 m_timer(name),
159 m_timing_enabled(false)
160 {}
161
163 virtual ~tool()
164 {}
165
168 virtual bool run() = 0;
169
171 const std::string& timing_filename() const
172 {
173 return m_timing_filename;
174 }
175
178 {
179 return m_timer;
180 }
181
187 int execute(int argc, char* argv[])
188 {
189#ifdef MCRL2_PLATFORM_WINDOWS
190 // All tools expect their std::cin to be binary streams. In Windows,
191 // this is the way to guarantee this. In applications that do not have
192 // a standard input attached (Windows-mode applications for instance),
193 // _fileno(stdin) returns a value less than 0.
194 if (_fileno(stdin) >= 0 && _setmode(_fileno(stdin), _O_BINARY) == -1)
195 {
196 throw std::runtime_error("Cannot set stdin to binary mode");
197 }
198#endif
199 try
200 {
201 interface_description clinterface(argv[0], m_name, m_author, m_what_is, synopsis(), m_tool_description, m_known_issues);
202 add_options(clinterface);
203 command_line_parser parser(clinterface, argc, argv);
205
206 if (parser.continue_execution())
207 {
209 parse_options(parser);
210
211 // If pre_run succeeds, then do the actual running.
212 bool result = pre_run(argc, argv);
213 if (result)
214 {
215 // Create timer, and by default measure running time of run()
216 // method.
218
219 timer().start("total");
220 result = run();
221 timer().finish("total");
222
224 {
225 timer().report();
226 }
227 }
228
229 // Either pre_run or run failed.
230 if (!result)
231 {
232 return EXIT_FAILURE;
233 }
234 }
235
236 return EXIT_SUCCESS;
237 }
238 catch (mcrl2::runtime_error& e)
239 {
240 mCRL2log(mcrl2::log::error) << e.what() << std::endl;
241 }
242 catch (std::exception& e)
243 {
244 mCRL2log(mcrl2::log::error) << e.what() << std::endl;
245 }
246 return EXIT_FAILURE;
247 }
248};
249
250} // namespace tools
251
252} // namespace utilities
253
254} // namespace mcrl2
255
256#endif // MCRL2_UTILITIES_TOOL_H
static void set_report_time_info()
Indicate that timing information should be printed.
Definition logger.h:223
static void set_reporting_level(const log_level_t level)
Set reporting level.
Definition logger.h:210
Standard exception class for reporting runtime errors.
Definition exception.h:27
Simple timer to time the CPU time used by a piece of code.
void start(const std::string &timing_name)
Start measurement with a hint.
void finish(const std::string &timing_name)
Finish a measurement with a hint.
void report()
Write all timing information that has been recorded.
Base class for command line tools. as result.
Definition tool.h:39
const std::string & timing_filename() const
Return the filename in which timings must be saved.
Definition tool.h:171
std::string m_timing_filename
The filename to which timings must be written.
Definition tool.h:57
virtual void parse_options(const command_line_parser &parser)
Parse non-standard options.
Definition tool.h:76
virtual bool pre_run(int &, char **)
Executed only if run would be executed and invoked before run.
Definition tool.h:88
virtual void add_options(interface_description &desc)
Add options to an interface description.
Definition tool.h:67
std::string m_name
The name of the tool.
Definition tool.h:42
tool(const std::string &name, const std::string &author, const std::string &what_is, const std::string &tool_description, std::string known_issues="")
Constructor.
Definition tool.h:146
virtual bool run()=0
Run the tool. The options must be set manually.
std::string m_tool_description
The description of the tool.
Definition tool.h:51
bool m_timing_enabled
Determines whether timing output should be written.
Definition tool.h:63
execution_timer m_timer
The timer which can be used by the tools.
Definition tool.h:60
execution_timer & timer()
Return reference to the timer that can be used.
Definition tool.h:177
virtual void check_standard_options(const command_line_parser &parser)
Parse standard options.
Definition tool.h:95
virtual ~tool()
Destructor.
Definition tool.h:163
int execute(int argc, char *argv[])
Run the tool with the given command line options.
Definition tool.h:187
std::string m_known_issues
Known issues of the tool.
Definition tool.h:54
std::string m_what_is
One-line "what is" description of the tool.
Definition tool.h:48
virtual std::string synopsis() const
Returns the synopsis of the tool.
Definition tool.h:139
virtual void check_positional_options(const command_line_parser &parser)
Checks if the number of positional options is OK. By default this function handles standard options: ...
Definition tool.h:132
std::string m_author
The name of the developer(s)
Definition tool.h:45
Components for command line interfaces of mCRL2 tools.
Class to obtain running times of code.
#define mCRL2log(LEVEL)
mCRL2log(LEVEL) provides the stream used to log.
Definition logger.h:391
@ verbose
Definition logger.h:37
log_level_t log_level_from_string(const std::string &s)
Convert string to log level.
Definition logger.h:54
A class that takes a linear process specification and checks all tau-summands of that LPS for conflue...
Definition indexed_set.h:72