3 ms·
> Link ? I'm interested. The examples I'm thinking of are all closed source, but here's a trivial implementation on the simpler end of the scale in my favorite
by MaulingMonkey 9y ago
> Link ? I'm interested.
The examples I'm thinking of are all closed source, but here's a trivial implementation on the simpler end of the scale in my favorite style/variation of the idea:
#include <iostream>
#include <sstream>
namespace xyz { constexpr bool logFails = true; }
#define XYZ_CHECK_MSG(cond, msg, expr) if (!(cond)) { \
if (::xyz::logFails) { ::std::cerr << msg << ::std::endl; } \
expr; \
} else ((void)0) /* ; */
#define XYZ_CHECK(cond, expr) XYZ_CHECK_MSG(cond, "XYZ_CHECK(" #cond ", ...) failed", expr);
void increment (long * i) {
XYZ_CHECK(i, return); // nonfatal
++*i;
}
int main(int argc, char ** argv) {
XYZ_CHECK_MSG(argc > 1, "Usage: " << argv[0] << " [number] [number]", std::exit(1)); // super fatal
for (int iArg = 1; iArg < argc; ++iArg) {
const char * const arg = argv[argc];
char * argEnd = nullptr;
long n = std::strtol(argv[iArg], &argEnd, 10);
// doesn't even end the function - "continue" could be replaced with "n=0" as well!
XYZ_CHECK_MSG(arg != argEnd && *argEnd == '\0', "Argument " << iArg << " was not a number", continue);
increment(&n);
std::cout << n << "\n";
}
}
Technically, if you don't want an error handling case, you don't have to provide one, but that's a bit explicit - you won't forget to at least think about it unless you really work at establishing such a bad habit:
XYZ_CHECK_MSG(condition, "Something bad happened, but let's just continue on our way.", );
More complicated versions of this might define additional macro params to:
1) Define a name and default severity - such that you can squelch false positives that snuck into the build before you can create and deploy a new one, or enable slower debug checks that might be skipped by default.
2) Configure where the logs go or what categories the messages will be filtered into.
3) Tag JIRA tasks or bug numbers to automatically reopen, or specific authors to alert.
Although these can also be handled in a more orthogonal manner.