powerd++  0.4.4
errors.hpp
Go to the documentation of this file.
1 
7 #include "utility.hpp"
8 
9 #ifndef _POWERDXX_ERRORS_HPP_
10 #define _POWERDXX_ERRORS_HPP_
11 
15 namespace errors {
16 
17 using namespace std::literals::string_literals;
18 
22 enum class Exit : int {
23  OK,
24  ECLARG,
25  EOUTOFRANGE,
26  ELOAD,
27  EFREQ,
28  EMODE,
29  EIVAL,
30  ESAMPLES,
31  ESYSCTL,
32  ENOFREQ,
33  ECONFLICT,
34  EPID,
35  EFORBIDDEN,
36  EDAEMON,
37  EWOPEN,
38  ESIGNAL,
39  ERANGEFMT,
40  ETEMPERATURE,
41  EEXCEPT,
42  EFILE,
43  EEXEC,
44  EDRIVER,
45  ESYSCTLNAME,
46  EFORMATFIELD,
47  LENGTH
48 };
49 
53 const char * const ExitStr[]{
54  "OK", "ECLARG", "EOUTOFRANGE", "ELOAD", "EFREQ", "EMODE", "EIVAL",
55  "ESAMPLES", "ESYSCTL", "ENOFREQ", "ECONFLICT", "EPID", "EFORBIDDEN",
56  "EDAEMON", "EWOPEN", "ESIGNAL", "ERANGEFMT", "ETEMPERATURE",
57  "EEXCEPT", "EFILE", "EEXEC", "EDRIVER", "ESYSCTLNAME", "EFORMATFIELD"
58 };
59 
60 static_assert(size_t{utility::to_value(Exit::LENGTH)} == utility::countof(ExitStr),
61  "Every Exit code must have a string representation");
62 
66 struct Exception {
71 
75  int err;
76 
80  std::string msg;
81 };
82 
93 [[noreturn]] inline void
94 fail(Exit const exitcode, int const err, std::string const & msg) {
95  throw Exception{exitcode, err, "("s +
96  ExitStr[utility::to_value(exitcode)] +
97  ") " + msg};
98 }
99 
100 } /* namespace errors */
101 
102 #endif /* _POWERDXX_ERRORS_HPP_ */
errors::Exit::EIVAL
@ EIVAL
The provided value is not a valid interval.
errors::Exit::EOUTOFRANGE
@ EOUTOFRANGE
A user provided value is out of range.
errors::Exit::ESYSCTLNAME
@ ESYSCTLNAME
User provided sysctl contains invalid characters.
errors::Exit::ESAMPLES
@ ESAMPLES
The provided value is not a valid sample count.
utility::countof
constexpr size_t countof(T(&)[Count])
Like sizeof(), but it returns the number of elements an array consists of instead of the number of by...
Definition: utility.hpp:32
errors::Exit::ENOFREQ
@ ENOFREQ
System does not support changing core frequencies.
errors::Exception::exitcode
Exit exitcode
The code to exit with.
Definition: errors.hpp:70
errors::Exit::EWOPEN
@ EWOPEN
Could not open file for writing.
errors::Exit::ELOAD
@ ELOAD
The provided value is not a valid load.
errors
Common error handling types and functions.
Definition: errors.hpp:15
errors::Exit::EDRIVER
@ EDRIVER
Frequency driver does not allow manual control.
errors::Exit::EFILE
@ EFILE
Not a valid file name.
utility::to_value
constexpr VT to_value(ET const op)
Casts an enum to its underlying value.
Definition: utility.hpp:85
errors::Exit::LENGTH
@ LENGTH
Enum length.
errors::Exit
Exit
Exit codes.
Definition: errors.hpp:22
errors::Exit::EPID
@ EPID
A pidfile could not be created.
errors::Exception
Exceptions bundle an exit code, errno value and message.
Definition: errors.hpp:66
errors::Exit::ETEMPERATURE
@ ETEMPERATURE
The provided value is not a valid temperature.
errors::Exit::OK
@ OK
Regular termination.
errors::Exit::ESIGNAL
@ ESIGNAL
Failed to install signal handler.
errors::Exit::EFREQ
@ EFREQ
The provided value is not a valid frequency.
errors::fail
void fail(Exit const exitcode, int const err, std::string const &msg)
Throws an Exception instance with the given message.
Definition: errors.hpp:94
errors::Exception::err
int err
The errno value at the time of creation.
Definition: errors.hpp:75
errors::Exit::ESYSCTL
@ ESYSCTL
A sysctl operation failed.
errors::Exit::ERANGEFMT
@ ERANGEFMT
A user provided range is missing the separator.
errors::Exception::msg
std::string msg
An error message.
Definition: errors.hpp:80
errors::Exit::EEXEC
@ EEXEC
Command execution failed.
errors::ExitStr
const char *const ExitStr[]
Printable strings for exit codes.
Definition: errors.hpp:53
errors::Exit::EMODE
@ EMODE
The provided value is not a valid mode.
errors::Exit::EFORBIDDEN
@ EFORBIDDEN
Insufficient privileges to change sysctl.
errors::Exit::EFORMATFIELD
@ EFORMATFIELD
Formatting string contains unexpected field.
errors::Exit::EDAEMON
@ EDAEMON
Unable to detach from terminal.
errors::Exit::ECLARG
@ ECLARG
Unexpected command line argument.
utility.hpp
Implements generally useful functions.
errors::Exit::ECONFLICT
@ ECONFLICT
Another frequency daemon instance is running.
errors::Exit::EEXCEPT
@ EEXCEPT
Untreated exception.