Commit aa8c3de4 authored by Kenton Varda's avatar Kenton Varda

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

parent 455781b6
......@@ -30,10 +30,10 @@ namespace _ { // private
void inlineRequireFailure(const char* file, int line, const char* expectation,
const char* macroArgs, const char* message) {
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();
} 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();
}
}
......
......@@ -113,14 +113,14 @@ TEST(Logging, Log) {
mockCallback.text.clear();
// Enable it.
Log::setLogLevel(Log::Severity::INFO);
Debug::setLogLevel(Debug::Severity::INFO);
KJ_LOG(INFO, "Some text."); line = __LINE__;
EXPECT_EQ("log message: " + fileLine(__FILE__, line) + ":+0: info: Some text.\n",
mockCallback.text);
mockCallback.text.clear();
// Back to default.
Log::setLogLevel(Log::Severity::WARNING);
Debug::setLogLevel(Debug::Severity::WARNING);
KJ_ASSERT(1 == 1);
EXPECT_THROW(KJ_ASSERT(1 == 2), MockException); line = __LINE__;
......
......@@ -28,10 +28,11 @@
#include <errno.h>
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[] = {
"info",
"warning",
......@@ -187,13 +188,13 @@ static String makeDescription(DescriptionStyle style, const char* code, int erro
} // namespace
void Log::logInternal(const char* file, int line, Severity severity, const char* macroArgs,
ArrayPtr<String> argValues) {
void Debug::logInternal(const char* file, int line, Severity severity, const char* macroArgs,
ArrayPtr<String> argValues) {
getExceptionCallback().logMessage(file, line, 0,
str(severity, ": ", makeDescription(LOG, nullptr, 0, macroArgs, argValues), '\n'));
}
Log::Fault::~Fault() noexcept(false) {
Debug::Fault::~Fault() noexcept(false) {
if (exception != nullptr) {
Exception copy = mv(*exception);
delete exception;
......@@ -201,7 +202,7 @@ Log::Fault::~Fault() noexcept(false) {
}
}
void Log::Fault::fatal() {
void Debug::Fault::fatal() {
Exception copy = mv(*exception);
delete exception;
exception = nullptr;
......@@ -209,7 +210,7 @@ void Log::Fault::fatal() {
abort();
}
void Log::Fault::init(
void Debug::Fault::init(
const char* file, int line, Exception::Nature nature, int errorNumber,
const char* condition, const char* macroArgs, ArrayPtr<String> argValues) {
exception = new Exception(nature, Exception::Durability::PERMANENT, file, line,
......@@ -217,19 +218,19 @@ void Log::Fault::init(
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);
}
int Log::getOsErrorNumber() {
int Debug::getOsErrorNumber() {
int result = errno;
return result == EINTR ? -1 : result;
}
Log::Context::Context(): logged(false) {}
Log::Context::~Context() {}
Debug::Context::Context(): logged(false) {}
Debug::Context::~Context() {}
Log::Context::Value Log::Context::ensureInitialized() {
Debug::Context::Value Debug::Context::ensureInitialized() {
KJ_IF_MAYBE(v, value) {
return Value(v->file, v->line, heapString(v->description));
} else {
......@@ -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();
exception.wrapContext(v.file, v.line, mv(v.description));
next.onRecoverableException(kj::mv(exception));
}
void Log::Context::onFatalException(Exception&& exception) {
void Debug::Context::onFatalException(Exception&& exception) {
Value v = ensureInitialized();
exception.wrapContext(v.file, v.line, mv(v.description));
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) {
Value v = ensureInitialized();
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
next.logMessage(file, line, contextDepth + 1, mv(text));
}
} // namespace _ (private)
} // namespace kj
......@@ -106,10 +106,62 @@
namespace kj {
class Log {
// Mostly-internal
#define KJ_LOG(severity, ...) \
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:
Debug() = delete;
enum class Severity {
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
......@@ -127,6 +179,8 @@ public:
static inline void setLogLevel(Severity severity) { minSeverity = severity; }
// Set the minimum message severity which will be logged.
//
// TODO(someday): Expose publicly.
template <typename... Params>
static void log(const char* file, int line, Severity severity, const char* macroArgs,
......@@ -217,68 +271,18 @@ private:
// Get the error code of the last error (e.g. from errno). Returns -1 on EINTR.
};
ArrayPtr<const char> KJ_STRINGIFY(Log::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
ArrayPtr<const char> KJ_STRINGIFY(Debug::Severity severity);
template <typename... Params>
void Log::log(const char* file, int line, Severity severity, const char* macroArgs,
Params&&... params) {
void Debug::log(const char* file, int line, Severity severity, const char* macroArgs,
Params&&... params) {
String argValues[sizeof...(Params)] = {str(params)...};
logInternal(file, line, severity, macroArgs, arrayPtr(argValues, sizeof...(Params)));
}
template <typename... Params>
Log::Fault::Fault(const char* file, int line, Exception::Nature nature, int errorNumber,
const char* condition, const char* macroArgs, Params&&... params)
Debug::Fault::Fault(const char* file, int line, Exception::Nature nature, int errorNumber,
const char* condition, const char* macroArgs, Params&&... params)
: exception(nullptr) {
String argValues[sizeof...(Params)] = {str(params)...};
init(file, line, nature, errorNumber, condition, macroArgs,
......@@ -286,7 +290,7 @@ Log::Fault::Fault(const char* file, int line, Exception::Nature nature, int erro
}
template <typename Call>
Log::SyscallResult Log::syscall(Call&& call) {
Debug::SyscallResult Debug::syscall(Call&& call) {
while (call() < 0) {
int errorNum = getOsErrorNumber();
// getOsErrorNumber() returns -1 to indicate EINTR
......@@ -298,11 +302,12 @@ Log::SyscallResult Log::syscall(Call&& call) {
}
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)...};
return makeContextDescriptionInternal(macroArgs, arrayPtr(argValues, sizeof...(Params)));
}
} // namespace _ (private)
} // namespace kj
#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