Last active
May 15, 2023 18:00
-
-
Save mrdomino/e81f77943e101d3de99f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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