Fixposition SDK 0.0.0-heads/main-0-g90a51ff
Collection of c++ libraries and apps for use with Fixposition products
Loading...
Searching...
No Matches
app.hpp
Go to the documentation of this file.
1/**
2 * \verbatim
3 * ___ ___
4 * \ \ / /
5 * \ \/ / Copyright (c) Fixposition AG (www.fixposition.com) and contributors
6 * / /\ \ License: see the LICENSE file
7 * /__/ \__\
8 * \endverbatim
9 *
10 * @file
11 * @brief Fixposition SDK: Utilities for apps
12 *
13 * @page FPSDK_COMMON_APPS Utilities for apps
14 *
15 * **API**: fpsdk_common/app.hpp and fpsdk::common::app
16 *
17 */
18#ifndef __FPSDK_COMMON_APP_HPP__
19#define __FPSDK_COMMON_APP_HPP__
20
21/* LIBC/STL */
22#include <cstdint>
23
24/* EXTERNAL */
25
26/* PACKAGE */
27#include "logging.hpp"
28
29namespace fpsdk {
30namespace common {
31/**
32 * @brief Utilities for apps
33 */
34namespace app {
35/* ****************************************************************************************************************** */
36
37/**
38 * @brief Helper to catch SIGINT (CTRL-c)
39 *
40 * On construction this installs a handler for SIGINT. On destruction it sets the handler back to its previous state.
41 * Note that signal handlers are global and therefore you can only use one SigIntHelper in a app.
42 *
43 * Example:
44 *
45 * @code{.cpp}
46 * SigIntHelper sigint;
47 * while (!sigint.ShouldAbort()) {
48 * // do stuff..
49 * }
50 *
51 * if (sigint.ShouldAbort()) {
52 * INFO("We've been asked to stop");
53 * }
54 * @endcode
55 */
57{
58 public:
59 /**
60 * @brief Constructor
61 *
62 * @param[in] warn Print a WARNING() (true, default) or a DEBUG() (false)
63 */
64 SigIntHelper(const bool warn = true);
65
66 /**
67 * @brief Destructor
68 */
70
71 /**
72 * @brief Check if signal was raised and we should abort
73 *
74 * @returns true if signal was raised and we should abort, false otherwise
75 */
77
78 /**
79 * @brief Wait (block) until signal is raised and we should abort
80 *
81 * @param[in] millis Wait at most this long [ms], 0 = forever
82 *
83 * @returns true if the signal was raised, fals if timeout expired
84 */
85 bool WaitAbort(const uint32_t millis = 0);
86};
87
88/**
89 * @brief Helper to catch SIGPIPE
90 *
91 * On construction this installs a handler for SIGPIE. On destruction it sets the handler back to its previous state.
92 * Note that signal handlers are global and therefore you can only use one SigPipeHelper in a app.
93 */
95{
96 public:
97 /**
98 * @brief Constructor
99 *
100 * @param[in] warn Print a WARNING() (true) or a DEBUG() (false, default)
101 */
102 SigPipeHelper(const bool warn = false);
103
104 /**
105 * @brief Destructor
106 */
108
109 /**
110 * @brief Check if signal was raised
111 *
112 * @returns true if signal was raised, false otherwise
113 */
114 bool Raised();
115};
116
117/**
118 * @brief Helper to print a strack trace on SIGSEGV and SIGABRT
119 *
120 * On construction this installs a handler for SIGSEGV and SIGABRT, which prints a stack trace.
121 * Note that signal handlers are global and therefore you can only use one StacktraceHelper in a app.
122 * It is probably a good idea to only include this in non-Release builds.
123 *
124 * Example:
125 *
126 * @code{.cpp}
127 * int main(int, char**) {
128 * #ifndef NDEBUG
129 * StacktraceHelper stacktrace;
130 * #endif
131 *
132 * // Do stuff...
133 *
134 * return 0;
135 * }
136 * @endcode
137 */
139{
140 public:
143};
144
145/**
146 * @brief Prints a stacktrace to stderr
147 */
149
150/**
151 * @brief Program options
152 */
154{
155 public:
156 /**
157 * @brief A program option
158 */
159 struct Option
160 {
161 char flag; //!< The flag (reserved: 'h', 'V', 'v', 'q', '?', '*', ':')
162 bool has_argument; //!< True if flag requires an an argument, false if not
163 };
164
165 /**
166 * @brief Constructor
167 */
168 ProgramOptions(const std::string& app_name, const std::vector<Option>& options);
169
170 /**
171 * @brief Destructor
172 */
174
175 /**
176 * @brief Load arguments from argv[]
177 *
178 * @param[in,out] argc Number of arguments
179 * @param[in,out] argv Command-line arguments
180 * @return
181 */
182 bool LoadFromArgv(int argc, char** argv);
183
184 /**
185 * @brief Print the help screen and exit(0)
186 */
187 virtual void PrintHelp() = 0;
188
189 /**
190 * @brief Handle a command-line flag argument
191 *
192 * @param[in] option The option
193 * @param[in] argument Optional argument
194 *
195 * @returns true if option was accepted, false otherwise
196 */
197 virtual bool HandleOption(const Option& option, const std::string& argument) = 0;
198
199 /**
200 * @brief Check options, and handle non-flag arguments
201 *
202 * @param[in] args The non-flag arguments
203 *
204 * @returns true if options are good, false otherwise
205 */
206 virtual bool CheckOptions(const std::vector<std::string>& args)
207 {
208 (void)args;
209 return true;
210 }
211
212 //! Help screen for common options @hideinitializer
213 static constexpr const char* COMMON_FLAGS_HELP = /* clang-format off */
214 " -h -- Print program help screen, and exit\n"
215 " -V -- Print program, version and license information, and exit\n"
216 " -v / -q -- Increase / decrease logging verbosity, multiple flags accumulate\n"; // clang-format on
217
218 std::string app_name_; //!< App name
221 std::vector<std::string> argv_; //!< argv[] of program
222
223 private:
224 std::vector<Option> options_; //!< Program options
225 void PrintVersion();
226};
227
228/* ****************************************************************************************************************** */
229} // namespace app
230} // namespace common
231} // namespace fpsdk
232#endif // __FPSDK_COMMON_APP_HPP__
std::vector< std::string > argv_
argv[] of program
Definition app.hpp:221
logging::LoggingLevel logging_level_
Logging verbosity level.
Definition app.hpp:219
virtual void PrintHelp()=0
Print the help screen and exit(0)
std::string app_name_
App name.
Definition app.hpp:218
ProgramOptions(const std::string &app_name, const std::vector< Option > &options)
Constructor.
virtual bool CheckOptions(const std::vector< std::string > &args)
Check options, and handle non-flag arguments.
Definition app.hpp:206
bool LoadFromArgv(int argc, char **argv)
Load arguments from argv[].
static constexpr const char * COMMON_FLAGS_HELP
Help screen for common options.
Definition app.hpp:213
logging::LoggingTimestamps logging_timestamps_
Logging timestamps.
Definition app.hpp:220
virtual bool HandleOption(const Option &option, const std::string &argument)=0
Handle a command-line flag argument.
virtual ~ProgramOptions()
Destructor.
Helper to catch SIGINT (CTRL-c)
Definition app.hpp:57
bool ShouldAbort()
Check if signal was raised and we should abort.
SigIntHelper(const bool warn=true)
Constructor.
bool WaitAbort(const uint32_t millis=0)
Wait (block) until signal is raised and we should abort.
Helper to catch SIGPIPE.
Definition app.hpp:95
SigPipeHelper(const bool warn=false)
Constructor.
bool Raised()
Check if signal was raised.
Helper to print a strack trace on SIGSEGV and SIGABRT.
Definition app.hpp:139
Fixposition SDK: Logging.
void PrintStacktrace()
Prints a stacktrace to stderr.
LoggingTimestamps
Logging timestamps.
Definition logging.hpp:235
LoggingLevel
Logging verbosity levels, default is INFO.
Definition logging.hpp:178
@ INFO
[6/info] Interesting stuff, the default level (for apps)
Fixposition SDK.
bool has_argument
True if flag requires an an argument, false if not.
Definition app.hpp:162
char flag
The flag (reserved: 'h', 'V', 'v', 'q', '?', '*', ':')
Definition app.hpp:161