Commit aa8c3de4 authored by Kenton Varda's avatar Kenton Varda

kj::Log -> kj::_::Debug

parent 455781b6
...@@ -30,10 +30,10 @@ namespace _ { // private ...@@ -30,10 +30,10 @@ namespace _ { // private
void inlineRequireFailure(const char* file, int line, const char* expectation, void inlineRequireFailure(const char* file, int line, const char* expectation,
const char* macroArgs, const char* message) { const char* macroArgs, const char* message) {
if (message == nullptr) { if (message == nullptr) {
Log::Fault f(file, line, Exception::Nature::PRECONDITION, 0, expectation, macroArgs); Debug::Fault f(file, line, Exception::Nature::PRECONDITION, 0, expectation, macroArgs);
f.fatal(); f.fatal();
} else { } else {
Log::Fault f(file, line, Exception::Nature::PRECONDITION, 0, expectation, macroArgs, message); Debug::Fault f(file, line, Exception::Nature::PRECONDITION, 0, expectation, macroArgs, message);
f.fatal(); f.fatal();
} }
} }
......
...@@ -113,14 +113,14 @@ TEST(Logging, Log) { ...@@ -113,14 +113,14 @@ TEST(Logging, Log) {
mockCallback.text.clear(); mockCallback.text.clear();
// Enable it. // Enable it.
Log::setLogLevel(Log::Severity::INFO); Debug::setLogLevel(Debug::Severity::INFO);
KJ_LOG(INFO, "Some text."); line = __LINE__; KJ_LOG(INFO, "Some text."); line = __LINE__;
EXPECT_EQ("log message: " + fileLine(__FILE__, line) + ":+0: info: Some text.\n", EXPECT_EQ("log message: " + fileLine(__FILE__, line) + ":+0: info: Some text.\n",
mockCallback.text); mockCallback.text);
mockCallback.text.clear(); mockCallback.text.clear();
// Back to default. // Back to default.
Log::setLogLevel(Log::Severity::WARNING); Debug::setLogLevel(Debug::Severity::WARNING);
KJ_ASSERT(1 == 1); KJ_ASSERT(1 == 1);
EXPECT_THROW(KJ_ASSERT(1 == 2), MockException); line = __LINE__; EXPECT_THROW(KJ_ASSERT(1 == 2), MockException); line = __LINE__;
......
...@@ -28,10 +28,11 @@ ...@@ -28,10 +28,11 @@
#include <errno.h> #include <errno.h>
namespace kj { namespace kj {
namespace _ { // private
Log::Severity Log::minSeverity = Log::Severity::WARNING; Debug::Severity Debug::minSeverity = Debug::Severity::WARNING;
ArrayPtr<const char> KJ_STRINGIFY(Log::Severity severity) { ArrayPtr<const char> KJ_STRINGIFY(Debug::Severity severity) {
static const char* SEVERITY_STRINGS[] = { static const char* SEVERITY_STRINGS[] = {
"info", "info",
"warning", "warning",
...@@ -187,13 +188,13 @@ static String makeDescription(DescriptionStyle style, const char* code, int erro ...@@ -187,13 +188,13 @@ static String makeDescription(DescriptionStyle style, const char* code, int erro
} // namespace } // namespace
void Log::logInternal(const char* file, int line, Severity severity, const char* macroArgs, void Debug::logInternal(const char* file, int line, Severity severity, const char* macroArgs,
ArrayPtr<String> argValues) { ArrayPtr<String> argValues) {
getExceptionCallback().logMessage(file, line, 0, getExceptionCallback().logMessage(file, line, 0,
str(severity, ": ", makeDescription(LOG, nullptr, 0, macroArgs, argValues), '\n')); str(severity, ": ", makeDescription(LOG, nullptr, 0, macroArgs, argValues), '\n'));
} }
Log::Fault::~Fault() noexcept(false) { Debug::Fault::~Fault() noexcept(false) {
if (exception != nullptr) { if (exception != nullptr) {
Exception copy = mv(*exception); Exception copy = mv(*exception);
delete exception; delete exception;
...@@ -201,7 +202,7 @@ Log::Fault::~Fault() noexcept(false) { ...@@ -201,7 +202,7 @@ Log::Fault::~Fault() noexcept(false) {
} }
} }
void Log::Fault::fatal() { void Debug::Fault::fatal() {
Exception copy = mv(*exception); Exception copy = mv(*exception);
delete exception; delete exception;
exception = nullptr; exception = nullptr;
...@@ -209,7 +210,7 @@ void Log::Fault::fatal() { ...@@ -209,7 +210,7 @@ void Log::Fault::fatal() {
abort(); abort();
} }
void Log::Fault::init( void Debug::Fault::init(
const char* file, int line, Exception::Nature nature, int errorNumber, const char* file, int line, Exception::Nature nature, int errorNumber,
const char* condition, const char* macroArgs, ArrayPtr<String> argValues) { const char* condition, const char* macroArgs, ArrayPtr<String> argValues) {
exception = new Exception(nature, Exception::Durability::PERMANENT, file, line, exception = new Exception(nature, Exception::Durability::PERMANENT, file, line,
...@@ -217,19 +218,19 @@ void Log::Fault::init( ...@@ -217,19 +218,19 @@ void Log::Fault::init(
condition, errorNumber, macroArgs, argValues)); condition, errorNumber, macroArgs, argValues));
} }
String Log::makeContextDescriptionInternal(const char* macroArgs, ArrayPtr<String> argValues) { String Debug::makeContextDescriptionInternal(const char* macroArgs, ArrayPtr<String> argValues) {
return makeDescription(LOG, nullptr, 0, macroArgs, argValues); return makeDescription(LOG, nullptr, 0, macroArgs, argValues);
} }
int Log::getOsErrorNumber() { int Debug::getOsErrorNumber() {
int result = errno; int result = errno;
return result == EINTR ? -1 : result; return result == EINTR ? -1 : result;
} }
Log::Context::Context(): logged(false) {} Debug::Context::Context(): logged(false) {}
Log::Context::~Context() {} Debug::Context::~Context() {}
Log::Context::Value Log::Context::ensureInitialized() { Debug::Context::Value Debug::Context::ensureInitialized() {
KJ_IF_MAYBE(v, value) { KJ_IF_MAYBE(v, value) {
return Value(v->file, v->line, heapString(v->description)); return Value(v->file, v->line, heapString(v->description));
} else { } else {
...@@ -239,17 +240,17 @@ Log::Context::Value Log::Context::ensureInitialized() { ...@@ -239,17 +240,17 @@ Log::Context::Value Log::Context::ensureInitialized() {
} }
} }
void Log::Context::onRecoverableException(Exception&& exception) { void Debug::Context::onRecoverableException(Exception&& exception) {
Value v = ensureInitialized(); Value v = ensureInitialized();
exception.wrapContext(v.file, v.line, mv(v.description)); exception.wrapContext(v.file, v.line, mv(v.description));
next.onRecoverableException(kj::mv(exception)); next.onRecoverableException(kj::mv(exception));
} }
void Log::Context::onFatalException(Exception&& exception) { void Debug::Context::onFatalException(Exception&& exception) {
Value v = ensureInitialized(); Value v = ensureInitialized();
exception.wrapContext(v.file, v.line, mv(v.description)); exception.wrapContext(v.file, v.line, mv(v.description));
next.onFatalException(kj::mv(exception)); next.onFatalException(kj::mv(exception));
} }
void Log::Context::logMessage(const char* file, int line, int contextDepth, String&& text) { void Debug::Context::logMessage(const char* file, int line, int contextDepth, String&& text) {
if (!logged) { if (!logged) {
Value v = ensureInitialized(); Value v = ensureInitialized();
next.logMessage(v.file, v.line, 0, str("context: ", mv(v.description), '\n')); next.logMessage(v.file, v.line, 0, str("context: ", mv(v.description), '\n'));
...@@ -259,4 +260,5 @@ void Log::Context::logMessage(const char* file, int line, int contextDepth, Stri ...@@ -259,4 +260,5 @@ void Log::Context::logMessage(const char* file, int line, int contextDepth, Stri
next.logMessage(file, line, contextDepth + 1, mv(text)); next.logMessage(file, line, contextDepth + 1, mv(text));
} }
} // namespace _ (private)
} // namespace kj } // namespace kj
...@@ -106,10 +106,62 @@ ...@@ -106,10 +106,62 @@
namespace kj { namespace kj {
class Log { #define KJ_LOG(severity, ...) \
// Mostly-internal if (!::kj::_::Debug::shouldLog(::kj::_::Debug::Severity::severity)) {} else \
::kj::_::Debug::log(__FILE__, __LINE__, ::kj::_::Debug::Severity::severity, \
#__VA_ARGS__, __VA_ARGS__)
#define KJ_DBG(...) KJ_LOG(DEBUG, ##__VA_ARGS__)
#define _kJ_FAULT(nature, cond, ...) \
if (KJ_EXPECT_TRUE(cond)) {} else \
for (::kj::_::Debug::Fault f(__FILE__, __LINE__, ::kj::Exception::Nature::nature, 0, \
#cond, #__VA_ARGS__, ##__VA_ARGS__);; f.fatal())
#define _kJ_FAIL_FAULT(nature, ...) \
for (::kj::_::Debug::Fault f(__FILE__, __LINE__, ::kj::Exception::Nature::nature, 0, \
nullptr, #__VA_ARGS__, ##__VA_ARGS__);; f.fatal())
#define KJ_ASSERT(...) _kJ_FAULT(LOCAL_BUG, ##__VA_ARGS__)
#define KJ_REQUIRE(...) _kJ_FAULT(PRECONDITION, ##__VA_ARGS__)
#define KJ_FAIL_ASSERT(...) _kJ_FAIL_FAULT(LOCAL_BUG, ##__VA_ARGS__)
#define KJ_FAIL_REQUIRE(...) _kJ_FAIL_FAULT(PRECONDITION, ##__VA_ARGS__)
#define KJ_SYSCALL(call, ...) \
if (auto _kjSyscallResult = ::kj::_::Debug::syscall([&](){return (call);})) {} else \
for (::kj::_::Debug::Fault f( \
__FILE__, __LINE__, ::kj::Exception::Nature::OS_ERROR, \
_kjSyscallResult.getErrorNumber(), #call, #__VA_ARGS__, ##__VA_ARGS__);; f.fatal())
#define FAIL_SYSCALL(code, errorNumber, ...) \
for (::kj::_::Debug::Fault f( \
__FILE__, __LINE__, ::kj::Exception::Nature::OS_ERROR, \
errorNumber, code, #__VA_ARGS__, ##__VA_ARGS__);; f.fatal())
#define KJ_CONTEXT(...) \
auto _kjContextFunc = [&]() -> ::kj::_::Debug::Context::Value { \
return ::kj::_::Debug::Context::Value(__FILE__, __LINE__, \
::kj::_::Debug::makeContextDescription(#__VA_ARGS__, ##__VA_ARGS__)); \
}; \
::kj::_::Debug::ContextImpl<decltype(_kjContextFunc)> _kjContext(_kjContextFunc)
#ifdef NDEBUG
#define KJ_DLOG(...) do {} while (false)
#define KJ_DASSERT(...) do {} while (false)
#define KJ_DREQUIRE(...) do {} while (false)
#else
#define KJ_DLOG LOG
#define KJ_DASSERT KJ_ASSERT
#define KJ_DREQUIRE KJ_REQUIRE
#endif
namespace _ { // private
class Debug {
public: public:
Debug() = delete;
enum class Severity { enum class Severity {
INFO, // Information describing what the code is up to, which users may request to see INFO, // Information describing what the code is up to, which users may request to see
// with a flag like `--verbose`. Does not indicate a problem. Not printed by // with a flag like `--verbose`. Does not indicate a problem. Not printed by
...@@ -127,6 +179,8 @@ public: ...@@ -127,6 +179,8 @@ public:
static inline void setLogLevel(Severity severity) { minSeverity = severity; } static inline void setLogLevel(Severity severity) { minSeverity = severity; }
// Set the minimum message severity which will be logged. // Set the minimum message severity which will be logged.
//
// TODO(someday): Expose publicly.
template <typename... Params> template <typename... Params>
static void log(const char* file, int line, Severity severity, const char* macroArgs, static void log(const char* file, int line, Severity severity, const char* macroArgs,
...@@ -217,68 +271,18 @@ private: ...@@ -217,68 +271,18 @@ private:
// Get the error code of the last error (e.g. from errno). Returns -1 on EINTR. // Get the error code of the last error (e.g. from errno). Returns -1 on EINTR.
}; };
ArrayPtr<const char> KJ_STRINGIFY(Log::Severity severity); ArrayPtr<const char> KJ_STRINGIFY(Debug::Severity severity);
#define KJ_LOG(severity, ...) \
if (!::kj::Log::shouldLog(::kj::Log::Severity::severity)) {} else \
::kj::Log::log(__FILE__, __LINE__, ::kj::Log::Severity::severity, \
#__VA_ARGS__, __VA_ARGS__)
#define KJ_DBG(...) KJ_LOG(DEBUG, ##__VA_ARGS__)
#define _kJ_FAULT(nature, cond, ...) \
if (KJ_EXPECT_TRUE(cond)) {} else \
for (::kj::Log::Fault f(__FILE__, __LINE__, ::kj::Exception::Nature::nature, 0, \
#cond, #__VA_ARGS__, ##__VA_ARGS__);; f.fatal())
#define _kJ_FAIL_FAULT(nature, ...) \
for (::kj::Log::Fault f(__FILE__, __LINE__, ::kj::Exception::Nature::nature, 0, \
nullptr, #__VA_ARGS__, ##__VA_ARGS__);; f.fatal())
#define KJ_ASSERT(...) _kJ_FAULT(LOCAL_BUG, ##__VA_ARGS__)
#define KJ_REQUIRE(...) _kJ_FAULT(PRECONDITION, ##__VA_ARGS__)
#define KJ_FAIL_ASSERT(...) _kJ_FAIL_FAULT(LOCAL_BUG, ##__VA_ARGS__)
#define KJ_FAIL_REQUIRE(...) _kJ_FAIL_FAULT(PRECONDITION, ##__VA_ARGS__)
#define KJ_SYSCALL(call, ...) \
if (auto _kjSyscallResult = ::kj::Log::syscall([&](){return (call);})) {} else \
for (::kj::Log::Fault f( \
__FILE__, __LINE__, ::kj::Exception::Nature::OS_ERROR, \
_kjSyscallResult.getErrorNumber(), #call, #__VA_ARGS__, ##__VA_ARGS__);; f.fatal())
#define FAIL_SYSCALL(code, errorNumber, ...) \
for (::kj::Log::Fault f( \
__FILE__, __LINE__, ::kj::Exception::Nature::OS_ERROR, \
errorNumber, code, #__VA_ARGS__, ##__VA_ARGS__);; f.fatal())
#define KJ_CONTEXT(...) \
auto _kjContextFunc = [&]() -> ::kj::Log::Context::Value { \
return ::kj::Log::Context::Value( \
__FILE__, __LINE__, ::kj::Log::makeContextDescription(#__VA_ARGS__, ##__VA_ARGS__)); \
}; \
::kj::Log::ContextImpl<decltype(_kjContextFunc)> _kjContext(_kjContextFunc)
#ifdef NDEBUG
#define KJ_DLOG(...) do {} while (false)
#define KJ_DASSERT(...) do {} while (false)
#define KJ_DREQUIRE(...) do {} while (false)
#else
#define KJ_DLOG LOG
#define KJ_DASSERT KJ_ASSERT
#define KJ_DREQUIRE KJ_REQUIRE
#endif
template <typename... Params> template <typename... Params>
void Log::log(const char* file, int line, Severity severity, const char* macroArgs, void Debug::log(const char* file, int line, Severity severity, const char* macroArgs,
Params&&... params) { Params&&... params) {
String argValues[sizeof...(Params)] = {str(params)...}; String argValues[sizeof...(Params)] = {str(params)...};
logInternal(file, line, severity, macroArgs, arrayPtr(argValues, sizeof...(Params))); logInternal(file, line, severity, macroArgs, arrayPtr(argValues, sizeof...(Params)));
} }
template <typename... Params> template <typename... Params>
Log::Fault::Fault(const char* file, int line, Exception::Nature nature, int errorNumber, Debug::Fault::Fault(const char* file, int line, Exception::Nature nature, int errorNumber,
const char* condition, const char* macroArgs, Params&&... params) const char* condition, const char* macroArgs, Params&&... params)
: exception(nullptr) { : exception(nullptr) {
String argValues[sizeof...(Params)] = {str(params)...}; String argValues[sizeof...(Params)] = {str(params)...};
init(file, line, nature, errorNumber, condition, macroArgs, init(file, line, nature, errorNumber, condition, macroArgs,
...@@ -286,7 +290,7 @@ Log::Fault::Fault(const char* file, int line, Exception::Nature nature, int erro ...@@ -286,7 +290,7 @@ Log::Fault::Fault(const char* file, int line, Exception::Nature nature, int erro
} }
template <typename Call> template <typename Call>
Log::SyscallResult Log::syscall(Call&& call) { Debug::SyscallResult Debug::syscall(Call&& call) {
while (call() < 0) { while (call() < 0) {
int errorNum = getOsErrorNumber(); int errorNum = getOsErrorNumber();
// getOsErrorNumber() returns -1 to indicate EINTR // getOsErrorNumber() returns -1 to indicate EINTR
...@@ -298,11 +302,12 @@ Log::SyscallResult Log::syscall(Call&& call) { ...@@ -298,11 +302,12 @@ Log::SyscallResult Log::syscall(Call&& call) {
} }
template <typename... Params> template <typename... Params>
String Log::makeContextDescription(const char* macroArgs, Params&&... params) { String Debug::makeContextDescription(const char* macroArgs, Params&&... params) {
String argValues[sizeof...(Params)] = {str(params)...}; String argValues[sizeof...(Params)] = {str(params)...};
return makeContextDescriptionInternal(macroArgs, arrayPtr(argValues, sizeof...(Params))); return makeContextDescriptionInternal(macroArgs, arrayPtr(argValues, sizeof...(Params)));
} }
} // namespace _ (private)
} // namespace kj } // namespace kj
#endif // KJ_DEBUG_H_ #endif // KJ_DEBUG_H_
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment