Skip to content

Instantly share code, notes, and snippets.

@mrdomino
Last active May 15, 2023 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrdomino/e81f77943e101d3de99f to your computer and use it in GitHub Desktop.
Save mrdomino/e81f77943e101d3de99f to your computer and use it in GitHub Desktop.
#include <cstdlib>
#include <exception>
#include <iostream>
#include <optional>
#include <sstream>
#include <utility>
#define LOG_IF(TEST, LEVEL) \
if (!(TEST)) { \
} else if (::_::minLevel > (LEVEL)) { \
if ((LEVEL) == FATAL) abort(); \
} else ::_::Log(LEVEL)
// XX cheez
#define FATAL 5
namespace _ {
int minLevel = 0;
class DtorAbort {
// Kill the process when this goes out of scope
public:
inline ~DtorAbort() {
abort();
}
};
class Log {
public:
inline Log(const int level) {
if (level == FATAL) {
abort.emplace();
}
}
inline ~Log() {
std::cerr << ss.str() << std::endl;
}
template <typename T>
inline Log& operator<<(T&& t) {
ss << std::forward<T>(t);
return *this;
}
private:
std::ostringstream ss;
std::optional<DtorAbort> abort;
};
} // namespace _
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment