Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
C
capnproto
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
capnproto
Commits
aa8c3de4
Commit
aa8c3de4
authored
Jun 06, 2013
by
Kenton Varda
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
kj::Log -> kj::_::Debug
parent
455781b6
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
85 additions
and
78 deletions
+85
-78
common.c++
c++/src/kj/common.c++
+2
-2
debug-test.c++
c++/src/kj/debug-test.c++
+2
-2
debug.c++
c++/src/kj/debug.c++
+17
-15
debug.h
c++/src/kj/debug.h
+64
-59
No files found.
c++/src/kj/common.c++
View file @
aa8c3de4
...
...
@@ -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
)
{
Lo
g
::
Fault
f
(
file
,
line
,
Exception
::
Nature
::
PRECONDITION
,
0
,
expectation
,
macroArgs
);
Debu
g
::
Fault
f
(
file
,
line
,
Exception
::
Nature
::
PRECONDITION
,
0
,
expectation
,
macroArgs
);
f
.
fatal
();
}
else
{
Lo
g
::
Fault
f
(
file
,
line
,
Exception
::
Nature
::
PRECONDITION
,
0
,
expectation
,
macroArgs
,
message
);
Debu
g
::
Fault
f
(
file
,
line
,
Exception
::
Nature
::
PRECONDITION
,
0
,
expectation
,
macroArgs
,
message
);
f
.
fatal
();
}
}
...
...
c++/src/kj/debug-test.c++
View file @
aa8c3de4
...
...
@@ -113,14 +113,14 @@ TEST(Logging, Log) {
mockCallback
.
text
.
clear
();
// Enable it.
Log
::
setLogLevel
(
Lo
g
::
Severity
::
INFO
);
Debug
::
setLogLevel
(
Debu
g
::
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
(
Lo
g
::
Severity
::
WARNING
);
Debug
::
setLogLevel
(
Debu
g
::
Severity
::
WARNING
);
KJ_ASSERT
(
1
==
1
);
EXPECT_THROW
(
KJ_ASSERT
(
1
==
2
),
MockException
);
line
=
__LINE__
;
...
...
c++/src/kj/debug.c++
View file @
aa8c3de4
...
...
@@ -28,10 +28,11 @@
#include <errno.h>
namespace
kj
{
namespace
_
{
// private
Log
::
Severity
Log
::
minSeverity
=
Lo
g
::
Severity
::
WARNING
;
Debug
::
Severity
Debug
::
minSeverity
=
Debu
g
::
Severity
::
WARNING
;
ArrayPtr
<
const
char
>
KJ_STRINGIFY
(
Lo
g
::
Severity
severity
)
{
ArrayPtr
<
const
char
>
KJ_STRINGIFY
(
Debu
g
::
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
Lo
g
::
logInternal
(
const
char
*
file
,
int
line
,
Severity
severity
,
const
char
*
macroArgs
,
ArrayPtr
<
String
>
argValues
)
{
void
Debu
g
::
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'
));
}
Lo
g
::
Fault
::~
Fault
()
noexcept
(
false
)
{
Debu
g
::
Fault
::~
Fault
()
noexcept
(
false
)
{
if
(
exception
!=
nullptr
)
{
Exception
copy
=
mv
(
*
exception
);
delete
exception
;
...
...
@@ -201,7 +202,7 @@ Log::Fault::~Fault() noexcept(false) {
}
}
void
Lo
g
::
Fault
::
fatal
()
{
void
Debu
g
::
Fault
::
fatal
()
{
Exception
copy
=
mv
(
*
exception
);
delete
exception
;
exception
=
nullptr
;
...
...
@@ -209,7 +210,7 @@ void Log::Fault::fatal() {
abort
();
}
void
Lo
g
::
Fault
::
init
(
void
Debu
g
::
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
Lo
g
::
makeContextDescriptionInternal
(
const
char
*
macroArgs
,
ArrayPtr
<
String
>
argValues
)
{
String
Debu
g
::
makeContextDescriptionInternal
(
const
char
*
macroArgs
,
ArrayPtr
<
String
>
argValues
)
{
return
makeDescription
(
LOG
,
nullptr
,
0
,
macroArgs
,
argValues
);
}
int
Lo
g
::
getOsErrorNumber
()
{
int
Debu
g
::
getOsErrorNumber
()
{
int
result
=
errno
;
return
result
==
EINTR
?
-
1
:
result
;
}
Lo
g
::
Context
::
Context
()
:
logged
(
false
)
{}
Lo
g
::
Context
::~
Context
()
{}
Debu
g
::
Context
::
Context
()
:
logged
(
false
)
{}
Debu
g
::
Context
::~
Context
()
{}
Log
::
Context
::
Value
Lo
g
::
Context
::
ensureInitialized
()
{
Debug
::
Context
::
Value
Debu
g
::
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
Lo
g
::
Context
::
onRecoverableException
(
Exception
&&
exception
)
{
void
Debu
g
::
Context
::
onRecoverableException
(
Exception
&&
exception
)
{
Value
v
=
ensureInitialized
();
exception
.
wrapContext
(
v
.
file
,
v
.
line
,
mv
(
v
.
description
));
next
.
onRecoverableException
(
kj
::
mv
(
exception
));
}
void
Lo
g
::
Context
::
onFatalException
(
Exception
&&
exception
)
{
void
Debu
g
::
Context
::
onFatalException
(
Exception
&&
exception
)
{
Value
v
=
ensureInitialized
();
exception
.
wrapContext
(
v
.
file
,
v
.
line
,
mv
(
v
.
description
));
next
.
onFatalException
(
kj
::
mv
(
exception
));
}
void
Lo
g
::
Context
::
logMessage
(
const
char
*
file
,
int
line
,
int
contextDepth
,
String
&&
text
)
{
void
Debu
g
::
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
c++/src/kj/debug.h
View file @
aa8c3de4
...
...
@@ -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
Lo
g
::
log
(
const
char
*
file
,
int
line
,
Severity
severity
,
const
char
*
macroArgs
,
Params
&&
...
params
)
{
void
Debu
g
::
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
>
Lo
g
::
Fault
::
Fault
(
const
char
*
file
,
int
line
,
Exception
::
Nature
nature
,
int
errorNumber
,
const
char
*
condition
,
const
char
*
macroArgs
,
Params
&&
...
params
)
Debu
g
::
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
Lo
g
::
syscall
(
Call
&&
call
)
{
Debug
::
SyscallResult
Debu
g
::
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
Lo
g
::
makeContextDescription
(
const
char
*
macroArgs
,
Params
&&
...
params
)
{
String
Debu
g
::
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_
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment