REDAC HybridController
Firmware for LUCIDAC/REDAC Teensy
Loading...
Searching...
No Matches
error.h
Go to the documentation of this file.
1// Copyright (c) 2023 anabrid GmbH
2// Contact: https://www.anabrid.com/licensing/
3// SPDX-License-Identifier: MIT OR GPL-2.0-or-later
4
5#pragma once
6
7#include "utils/logging.h"
8#include <Arduino.h>
9#include <cstdarg>
10#include <string>
11
12#define RETURN_IF_FAILED(status) \
13 { auto _status = (status); if (!_status.is_ok()) return _status; }
14
15namespace utils {
16constexpr unsigned int bufsize = 1000;
17
18// extern char buf[bufsize];
19
20template <typename... Args> FLASHMEM std::string small_sprintf(const char *format, Args... args) {
21 char buf[bufsize];
22 snprintf(buf, bufsize, format, args...);
23 std::string ret = buf;
24 return ret;
25}
26
35class status {
36public:
37 int code;
38 std::string msg;
39
40 status(int code, std::string msg) : code(code), msg(msg) {
41 if (code != 0) {
42 LOG4("utils::status error code=", code, " message=", msg.c_str());
43 if (msg.length() == 0) {
44 LOG_ALWAYS("Misuse of utils::status! Probably a casting problem.");
45 }
46 }
47 }
48
50 status() : status(0, "success") {}
51
53 explicit status(int code) : status(code, "No error message provided") {}
54
56 explicit status(bool code) : status(code ? 0 : 1) {}
57
59 explicit status(std::string msg) : status(-1, msg) {}
60
61 // This constructor is neccessary to distinguish it from the status(*format, ...) constructor.
62 explicit status(const char *msg) : status(-2, msg) {}
63
65 template <typename... Args>
66 status(const char *format, Args... args) : status(small_sprintf(format, args...)) {}
67
68 template <typename... Args>
69 status(int code, const char *format, Args... args) : status(code, small_sprintf(format, args...)) {}
70
72 status &attach(const status &other, const char *description = "") {
73 if (!other.is_ok() || !is_ok()) {
74 code += other.code * 128;
75 msg += other.msg + description;
76 }
77 return *this; // chainable
78 }
79
81 status &attach_to(status &other, const char *description = "") {
82 other.attach(*this, description);
83 return *this;
84 }
85
87 status &attach(int _code, const char *_msg) {
88 code += _code * 128;
89 msg += _msg;
90 return *this;
91 }
92
93 status &attach(const char *_msg) {
94 msg += _msg;
95 return *this;
96 }
97
98 bool is_ok() const { return code == 0; }
99
101 operator bool() const { return is_ok(); }
102
104 static status success() { return status(0); }
105
107 static status failure() { return status(-1); }
108};
109} // namespace utils
A recoverable error, inspired from https://abseil.io/docs/cpp/guides/status and https://github....
Definition error.h:35
int code
Status code. 0 means success.
Definition error.h:37
status(int code)
0 means success, anything else not success.
Definition error.h:53
bool is_ok() const
Definition error.h:98
static status failure()
Syntactic sugar for failure.
Definition error.h:107
status(bool code)
True means success, false not success.
Definition error.h:56
static status success()
Syntactic sugar for success.
Definition error.h:104
status & attach(const char *_msg)
Definition error.h:93
status(int code, std::string msg)
Definition error.h:40
status & attach_to(status &other, const char *description="")
Attach this error message to another one. Is chainable, returns self.
Definition error.h:81
std::string msg
Human readable error string.
Definition error.h:38
status(const char *format, Args... args)
Usage like status("Foo %d bar %s baz", 3, "bling");.
Definition error.h:66
status & attach(const status &other, const char *description="")
Attach another error message to this one. Is chainable, returns self.
Definition error.h:72
status & attach(int _code, const char *_msg)
Attach a "raw" message to this one.
Definition error.h:87
status(const char *msg)
Definition error.h:62
status()
Empty means success. See also success() method for more verbosity in code.
Definition error.h:50
status(std::string msg)
Generic failure (without code)
Definition error.h:59
status(int code, const char *format, Args... args)
Definition error.h:69
#define LOG4(a, b, c, d)
Definition logging.h:110
#define LOG_ALWAYS(message)
Definition logging.h:38
Definition carrier.h:12
FLASHMEM std::string small_sprintf(const char *format, Args... args)
Definition error.h:20
constexpr unsigned int bufsize
Definition error.h:16