Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
孤小逸
Mace
提交
edca6b50
Mace
项目概览
孤小逸
/
Mace
与 Fork 源项目一致
Fork自
Xiaomi / Mace
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Mace
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
edca6b50
编写于
5月 24, 2018
作者:
W
wuchenghui
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
modify logging
上级
bf62c769
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
48 addition
and
124 deletion
+48
-124
mace/core/registry.h
mace/core/registry.h
+2
-32
mace/utils/logging.cc
mace/utils/logging.cc
+26
-48
mace/utils/logging.h
mace/utils/logging.h
+20
-44
未找到文件。
mace/core/registry.h
浏览文件 @
edca6b50
...
@@ -40,10 +40,6 @@ class Registry {
...
@@ -40,10 +40,6 @@ class Registry {
registry_
[
key
]
=
creator
;
registry_
[
key
]
=
creator
;
}
}
inline
bool
Has
(
const
SrcType
&
key
)
const
{
return
registry_
.
count
(
key
)
!=
0
;
}
std
::
unique_ptr
<
ObjectType
>
Create
(
const
SrcType
&
key
,
Args
...
args
)
const
{
std
::
unique_ptr
<
ObjectType
>
Create
(
const
SrcType
&
key
,
Args
...
args
)
const
{
if
(
registry_
.
count
(
key
)
==
0
)
{
if
(
registry_
.
count
(
key
)
==
0
)
{
LOG
(
FATAL
)
<<
"Key not registered: "
<<
key
;
LOG
(
FATAL
)
<<
"Key not registered: "
<<
key
;
...
@@ -51,17 +47,6 @@ class Registry {
...
@@ -51,17 +47,6 @@ class Registry {
return
registry_
.
at
(
key
)(
args
...);
return
registry_
.
at
(
key
)(
args
...);
}
}
/**
* Returns the keys currently registered as a vector.
*/
std
::
vector
<
SrcType
>
Keys
()
const
{
std
::
vector
<
SrcType
>
keys
;
for
(
const
auto
&
it
:
registry_
)
{
keys
.
push_back
(
it
.
first
);
}
return
keys
;
}
private:
private:
std
::
map
<
SrcType
,
Creator
>
registry_
;
std
::
map
<
SrcType
,
Creator
>
registry_
;
std
::
mutex
register_mutex_
;
std
::
mutex
register_mutex_
;
...
@@ -97,27 +82,12 @@ class Registerer {
...
@@ -97,27 +82,12 @@ class Registerer {
typedef Registerer<SrcType, ObjectType, ##__VA_ARGS__> \
typedef Registerer<SrcType, ObjectType, ##__VA_ARGS__> \
Registerer##RegistryName;
Registerer##RegistryName;
/*
#define MACE_DEFINE_TYPED_REGISTRY(RegistryName, SrcType, ObjectType, ...) \
Registry<SrcType, ObjectType, ##__VA_ARGS__> *RegistryName() { \
static Registry<SrcType, ObjectType, ##__VA_ARGS__> *registry = \
new Registry<SrcType, ObjectType, ##__VA_ARGS__>(); \
return registry; \
}
*/
#define MACE_DECLARE_REGISTRY(RegistryName, ObjectType, ...) \
#define MACE_DECLARE_REGISTRY(RegistryName, ObjectType, ...) \
MACE_DECLARE_TYPED_REGISTRY(RegistryName, std::string, ObjectType, \
MACE_DECLARE_TYPED_REGISTRY(RegistryName, std::string, ObjectType, \
##__VA_ARGS__)
##__VA_ARGS__)
/*
#define MACE_REGISTER_TYPED_CLASS(RegistryName, registry, key, ...) \
#define MACE_DEFINE_REGISTRY(RegistryName, ObjectType, ...) \
Registerer##RegistryName MACE_ANONYMOUS_VARIABLE(RegistryName)( \
MACE_DEFINE_TYPED_REGISTRY(RegistryName, std::string, ObjectType, \
##__VA_ARGS__)
*/
#define MACE_REGISTER_TYPED_CLASS(RegistryName, registry, key, ...) \
Registerer##RegistryName MACE_ANONYMOUS_VARIABLE(l_##RegistryName)( \
key, registry, Registerer##RegistryName::DefaultCreator<__VA_ARGS__>);
key, registry, Registerer##RegistryName::DefaultCreator<__VA_ARGS__>);
#define MACE_REGISTER_CLASS(RegistryName, registry, key, ...) \
#define MACE_REGISTER_CLASS(RegistryName, registry, key, ...) \
...
...
mace/utils/logging.cc
浏览文件 @
edca6b50
...
@@ -16,10 +16,10 @@
...
@@ -16,10 +16,10 @@
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
#include <sstream>
#if defined(ANDROID) || defined(__ANDROID__)
#if defined(ANDROID) || defined(__ANDROID__)
#include <android/log.h>
#include <android/log.h>
#include <iostream>
#include <iostream>
#include <sstream>
#endif
#endif
namespace
mace
{
namespace
mace
{
...
@@ -28,8 +28,13 @@ namespace logging {
...
@@ -28,8 +28,13 @@ namespace logging {
LogMessage
::
LogMessage
(
const
char
*
fname
,
int
line
,
int
severity
)
LogMessage
::
LogMessage
(
const
char
*
fname
,
int
line
,
int
severity
)
:
fname_
(
fname
),
line_
(
line
),
severity_
(
severity
)
{}
:
fname_
(
fname
),
line_
(
line
),
severity_
(
severity
)
{}
#if defined(ANDROID) || defined(__ANDROID__)
void
LogMessage
::
DealWithFatal
()
{
// When there is a fatal log, now we simply abort.
abort
();
}
void
LogMessage
::
GenerateLogMessage
()
{
void
LogMessage
::
GenerateLogMessage
()
{
#if defined(ANDROID) || defined(__ANDROID__)
int
android_log_level
;
int
android_log_level
;
switch
(
severity_
)
{
switch
(
severity_
)
{
case
INFO
:
case
INFO
:
...
@@ -61,74 +66,47 @@ void LogMessage::GenerateLogMessage() {
...
@@ -61,74 +66,47 @@ void LogMessage::GenerateLogMessage() {
// Also log to stderr (for standalone Android apps).
// Also log to stderr (for standalone Android apps).
std
::
cerr
<<
"IWEF"
[
severity_
]
<<
" "
<<
ss
.
str
()
<<
std
::
endl
;
std
::
cerr
<<
"IWEF"
[
severity_
]
<<
" "
<<
ss
.
str
()
<<
std
::
endl
;
// Android logging at level FATAL does not terminate execution, so abort()
// is still required to stop the program.
if
(
severity_
==
FATAL
)
{
abort
();
}
}
#else
#else
void
LogMessage
::
GenerateLogMessage
()
{
fprintf
(
stderr
,
"%c %s:%d] %s
\n
"
,
"IWEF"
[
severity_
],
fname_
,
line_
,
fprintf
(
stderr
,
"%c %s:%d] %s
\n
"
,
"IWEF"
[
severity_
],
fname_
,
line_
,
str
().
c_str
());
str
().
c_str
());
}
#endif
#endif
// When there is a fatal log, terminate execution
if
(
severity_
==
FATAL
)
{
DealWithFatal
();
}
}
namespace
{
namespace
{
// Parse log level (int64_t) from environment variable (char*)
int
LogLevelStrToInt
(
const
char
*
mace_env_var_val
)
{
int64_t
LogLevelStrToInt
(
const
char
*
mace_env_var_val
)
{
if
(
mace_env_var_val
==
nullptr
)
{
if
(
mace_env_var_val
==
nullptr
)
{
return
0
;
return
0
;
}
}
// Simply use atoi here. Return 0 if convert unsuccessfully.
// Ideally we would use env_var / safe_strto64, but it is
return
atoi
(
mace_env_var_val
);
// hard to use here without pulling in a lot of dependencies,
// so we use std:istringstream instead
std
::
string
min_log_level
(
mace_env_var_val
);
std
::
istringstream
ss
(
min_log_level
);
int64_t
level
;
if
(
!
(
ss
>>
level
))
{
// Invalid vlog level setting, set level to default (0)
level
=
0
;
}
return
level
;
}
}
int64_t
MinLogLevelFromEnv
()
{
int
MinLogLevelFromEnv
()
{
const
char
*
mace_env_var_val
=
getenv
(
"MACE_CPP_MIN_LOG_LEVEL"
);
// Read the min log level from env once during the first call to logging.
return
LogLevelStrToInt
(
mace_env_var_val
);
static
int
log_level
=
LogLevelStrToInt
(
getenv
(
"MACE_CPP_MIN_LOG_LEVEL"
));
return
log_level
;
}
}
int64_t
MinVLogLevelFromEnv
()
{
int
MinVLogLevelFromEnv
()
{
const
char
*
mace_env_var_val
=
getenv
(
"MACE_CPP_MIN_VLOG_LEVEL"
);
// Read the min vlog level from env once during the first call to logging.
return
LogLevelStrToInt
(
mace_env_var_val
);
static
int
vlog_level
=
LogLevelStrToInt
(
getenv
(
"MACE_CPP_MIN_VLOG_LEVEL"
));
return
vlog_level
;
}
}
}
// namespace
}
// namespace
LogMessage
::~
LogMessage
()
{
LogMessage
::~
LogMessage
()
{
// Read the min log level once during the first call to logging.
int
min_log_level
=
MinLogLevelFromEnv
();
static
int64_t
min_log_level
=
MinLogLevelFromEnv
();
if
(
severity_
>=
min_log_level
)
GenerateLogMessage
();
if
(
severity_
>=
min_log_level
)
GenerateLogMessage
();
}
}
int64_t
LogMessage
::
MinVLogLevel
()
{
int
LogMessage
::
MinVLogLevel
()
{
static
int64_t
min_vlog_level
=
MinVLogLevelFromEnv
();
return
MinVLogLevelFromEnv
();
return
min_vlog_level
;
}
LogMessageFatal
::
LogMessageFatal
(
const
char
*
file
,
int
line
)
:
LogMessage
(
file
,
line
,
FATAL
)
{}
LogMessageFatal
::~
LogMessageFatal
()
{
// abort() ensures we don't return (we promised we would not via
// ATTRIBUTE_NORETURN).
GenerateLogMessage
();
abort
();
}
}
}
// namespace logging
}
// namespace logging
...
...
mace/utils/logging.h
浏览文件 @
edca6b50
...
@@ -30,61 +30,37 @@
...
@@ -30,61 +30,37 @@
namespace
mace
{
namespace
mace
{
const
int
INFO
=
0
;
// base_logging::INFO;
// Log severity level constants.
const
int
WARNING
=
1
;
// base_logging::WARNING
;
const
int
INFO
=
0
;
const
int
ERROR
=
2
;
// base_logging::ERROR
;
const
int
WARNING
=
1
;
const
int
FATAL
=
3
;
// base_logging::FATAL
;
const
int
ERROR
=
2
;
const
int
NUM_SEVERITIES
=
4
;
// base_logging::NUM_SEVERITIES
;
const
int
FATAL
=
3
;
namespace
logging
{
namespace
logging
{
class
LogMessage
:
public
std
::
basic_ostringstream
<
char
>
{
class
LogMessage
:
public
std
::
ostringstream
{
public:
public:
LogMessage
(
const
char
*
fname
,
int
line
,
int
severity
);
LogMessage
(
const
char
*
fname
,
int
line
,
int
severity
);
~
LogMessage
();
~
LogMessage
();
// Returns the minimum log level for VLOG statements.
static
int
MinVLogLevel
();
// E.g., if MinVLogLevel() is 2, then VLOG(2) statements will produce output,
// but VLOG(3) will not. Defaults to 0.
static
int64_t
MinVLogLevel
();
pr
otected
:
pr
ivate
:
void
GenerateLogMessage
();
void
GenerateLogMessage
();
void
DealWithFatal
();
private:
const
char
*
fname_
;
const
char
*
fname_
;
int
line_
;
int
line_
;
int
severity_
;
int
severity_
;
};
};
// LogMessageFatal ensures the process will exit in failure after
#define LOG(severity) \
// logging this message.
::mace::logging::LogMessage(__FILE__, __LINE__, mace::severity)
class
LogMessageFatal
:
public
LogMessage
{
public:
LogMessageFatal
(
const
char
*
file
,
int
line
);
~
LogMessageFatal
();
};
#define _MACE_LOG_INFO \
::mace::logging::LogMessage(__FILE__, __LINE__, mace::INFO)
#define _MACE_LOG_WARNING \
::mace::logging::LogMessage(__FILE__, __LINE__, mace::WARNING)
#define _MACE_LOG_ERROR \
::mace::logging::LogMessage(__FILE__, __LINE__, mace::ERROR)
#define _MACE_LOG_FATAL ::mace::logging::LogMessageFatal(__FILE__, __LINE__)
#define _MACE_LOG_QFATAL _MACE_LOG_FATAL
#define LOG(severity) _MACE_LOG_##severity
// Set MACE_CPP_MIN_VLOG_LEVEL environment to update minimum log
// level
// of VLOG
#define VLOG_IS_ON(lvl) ((lvl) <= ::mace::logging::LogMessage::MinVLogLevel())
#define VLOG(lvl) \
// Set MACE_CPP_MIN_VLOG_LEVEL environment to update minimum log level of VLOG.
if (VLOG_IS_ON(lvl)) \
// Only when vlog_level <= MinVLogLevel(), it will produce output.
::mace::logging::LogMessage(__FILE__, __LINE__, mace::INFO)
#define VLOG_IS_ON(vll) ((vll) <= ::mace::logging::LogMessage::MinVLogLevel())
#define VLOG(vll) if (VLOG_IS_ON(vll)) LOG(INFO)
// MACE_CHECK/MACE_ASSERT dies with a fatal error if condition is not true.
// MACE_CHECK/MACE_ASSERT dies with a fatal error if condition is not true.
// MACE_ASSERT is controlled by NDEBUG ('-c opt' for bazel) while MACE_CHECK
// MACE_ASSERT is controlled by NDEBUG ('-c opt' for bazel) while MACE_CHECK
...
@@ -92,14 +68,14 @@ class LogMessageFatal : public LogMessage {
...
@@ -92,14 +68,14 @@ class LogMessageFatal : public LogMessage {
// Therefore, it is safe to do things like:
// Therefore, it is safe to do things like:
// MACE_CHECK(fp->Write(x) == 4)
// MACE_CHECK(fp->Write(x) == 4)
// MACE_CHECK(fp->Write(x) == 4, "Write failed")
// MACE_CHECK(fp->Write(x) == 4, "Write failed")
// which are not
correct
for MACE_ASSERT.
// which are not
safe
for MACE_ASSERT.
#define MACE_CHECK(condition, ...) \
#define MACE_CHECK(condition, ...) \
if (!(condition))
\
if (!(condition)) \
LOG(FATAL) << "Check failed: " #condition " " << MakeString(__VA_ARGS__)
LOG(FATAL) << "Check failed: " #condition " " << MakeString(__VA_ARGS__)
#ifndef NDEBUG
#ifndef NDEBUG
#define MACE_ASSERT(condition, ...) \
#define MACE_ASSERT(condition, ...) \
if (!(condition))
\
if (!(condition)) \
LOG(FATAL) << "Assert failed: " #condition " " << MakeString(__VA_ARGS__)
LOG(FATAL) << "Assert failed: " #condition " " << MakeString(__VA_ARGS__)
#else
#else
#define MACE_ASSERT(condition, ...) ((void)0)
#define MACE_ASSERT(condition, ...) ((void)0)
...
@@ -108,12 +84,12 @@ class LogMessageFatal : public LogMessage {
...
@@ -108,12 +84,12 @@ class LogMessageFatal : public LogMessage {
template
<
typename
T
>
template
<
typename
T
>
T
&&
CheckNotNull
(
const
char
*
file
,
int
line
,
const
char
*
exprtext
,
T
&&
t
)
{
T
&&
CheckNotNull
(
const
char
*
file
,
int
line
,
const
char
*
exprtext
,
T
&&
t
)
{
if
(
t
==
nullptr
)
{
if
(
t
==
nullptr
)
{
LogMessageFatal
(
file
,
line
)
<<
std
::
string
(
exprtext
);
::
mace
::
logging
::
LogMessage
(
file
,
line
,
FATAL
)
<<
std
::
string
(
exprtext
);
}
}
return
std
::
forward
<
T
>
(
t
);
return
std
::
forward
<
T
>
(
t
);
}
}
#define MACE_CHECK_NOTNULL(val)
\
#define MACE_CHECK_NOTNULL(val) \
::mace::logging::CheckNotNull(__FILE__, __LINE__, \
::mace::logging::CheckNotNull(__FILE__, __LINE__, \
"'" #val "' Must be non NULL", (val))
"'" #val "' Must be non NULL", (val))
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录