Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
AndroidSystemCore
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container registry
Model registry
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Werner Sembach
AndroidSystemCore
Commits
2b49abe4
Commit
2b49abe4
authored
Mar 8, 2018
by
Treehugger Robot
Committed by
Gerrit Code Review
Mar 8, 2018
Browse files
Options
Downloads
Plain Diff
Merge "Base: Add default tag manipulation"
parents
92ca281c
1923e768
Branches
o-mr1-iot-preview-7
Branches containing commit
Tags
android-o-mr1-iot-preview-7
Tags containing commit
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
base/include/android-base/logging.h
+3
-0
3 additions, 0 deletions
base/include/android-base/logging.h
base/logging.cpp
+31
-7
31 additions, 7 deletions
base/logging.cpp
base/logging_test.cpp
+26
-3
26 additions, 3 deletions
base/logging_test.cpp
with
60 additions
and
10 deletions
base/include/android-base/logging.h
+
3
−
0
View file @
2b49abe4
...
@@ -105,6 +105,9 @@ void StderrLogger(LogId, LogSeverity, const char*, const char*, unsigned int, co
...
@@ -105,6 +105,9 @@ void StderrLogger(LogId, LogSeverity, const char*, const char*, unsigned int, co
void
DefaultAborter
(
const
char
*
abort_message
);
void
DefaultAborter
(
const
char
*
abort_message
);
std
::
string
GetDefaultTag
();
void
SetDefaultTag
(
const
std
::
string
&
tag
);
#ifdef __ANDROID__
#ifdef __ANDROID__
// We expose this even though it is the default because a user that wants to
// We expose this even though it is the default because a user that wants to
// override the default log buffer will have to construct this themselves.
// override the default log buffer will have to construct this themselves.
...
...
This diff is collapsed.
Click to expand it.
base/logging.cpp
+
31
−
7
View file @
2b49abe4
...
@@ -139,9 +139,27 @@ static AbortFunction& Aborter() {
...
@@ -139,9 +139,27 @@ static AbortFunction& Aborter() {
return
aborter
;
return
aborter
;
}
}
static
std
::
string
&
ProgramInvocationName
()
{
static
std
::
recursive_mutex
&
TagLock
()
{
static
auto
&
programInvocationName
=
*
new
std
::
string
(
getprogname
());
static
auto
&
tag_lock
=
*
new
std
::
recursive_mutex
();
return
programInvocationName
;
return
tag_lock
;
}
static
std
::
string
*
gDefaultTag
;
std
::
string
GetDefaultTag
()
{
std
::
lock_guard
<
std
::
recursive_mutex
>
lock
(
TagLock
());
if
(
gDefaultTag
==
nullptr
)
{
return
""
;
}
return
*
gDefaultTag
;
}
void
SetDefaultTag
(
const
std
::
string
&
tag
)
{
std
::
lock_guard
<
std
::
recursive_mutex
>
lock
(
TagLock
());
if
(
gDefaultTag
!=
nullptr
)
{
delete
gDefaultTag
;
gDefaultTag
=
nullptr
;
}
if
(
!
tag
.
empty
())
{
gDefaultTag
=
new
std
::
string
(
tag
);
}
}
}
static
bool
gInitialized
=
false
;
static
bool
gInitialized
=
false
;
...
@@ -269,8 +287,7 @@ void InitLogging(char* argv[], LogFunction&& logger, AbortFunction&& aborter) {
...
@@ -269,8 +287,7 @@ void InitLogging(char* argv[], LogFunction&& logger, AbortFunction&& aborter) {
// Linux to recover this, but we don't have that luxury on the Mac/Windows,
// Linux to recover this, but we don't have that luxury on the Mac/Windows,
// and there are a couple of argv[0] variants that are commonly used.
// and there are a couple of argv[0] variants that are commonly used.
if
(
argv
!=
nullptr
)
{
if
(
argv
!=
nullptr
)
{
std
::
lock_guard
<
std
::
mutex
>
lock
(
LoggingLock
());
SetDefaultTag
(
basename
(
argv
[
0
]));
ProgramInvocationName
()
=
basename
(
argv
[
0
]);
}
}
const
char
*
tags
=
getenv
(
"ANDROID_LOG_TAGS"
);
const
char
*
tags
=
getenv
(
"ANDROID_LOG_TAGS"
);
...
@@ -448,9 +465,16 @@ std::ostream& LogMessage::stream() {
...
@@ -448,9 +465,16 @@ std::ostream& LogMessage::stream() {
void
LogMessage
::
LogLine
(
const
char
*
file
,
unsigned
int
line
,
LogId
id
,
LogSeverity
severity
,
void
LogMessage
::
LogLine
(
const
char
*
file
,
unsigned
int
line
,
LogId
id
,
LogSeverity
severity
,
const
char
*
tag
,
const
char
*
message
)
{
const
char
*
tag
,
const
char
*
message
)
{
if
(
tag
==
nullptr
)
tag
=
ProgramInvocationName
().
c_str
();
if
(
tag
==
nullptr
)
{
std
::
lock_guard
<
std
::
recursive_mutex
>
lock
(
TagLock
());
if
(
gDefaultTag
==
nullptr
)
{
gDefaultTag
=
new
std
::
string
(
getprogname
());
}
Logger
()(
id
,
severity
,
gDefaultTag
->
c_str
(),
file
,
line
,
message
);
}
else
{
Logger
()(
id
,
severity
,
tag
,
file
,
line
,
message
);
Logger
()(
id
,
severity
,
tag
,
file
,
line
,
message
);
}
}
}
void
LogMessage
::
LogLine
(
const
char
*
file
,
unsigned
int
line
,
LogId
id
,
LogSeverity
severity
,
void
LogMessage
::
LogLine
(
const
char
*
file
,
unsigned
int
line
,
LogId
id
,
LogSeverity
severity
,
const
char
*
message
)
{
const
char
*
message
)
{
...
...
This diff is collapsed.
Click to expand it.
base/logging_test.cpp
+
26
−
3
View file @
2b49abe4
...
@@ -206,8 +206,8 @@ static std::string make_log_pattern(android::base::LogSeverity severity,
...
@@ -206,8 +206,8 @@ static std::string make_log_pattern(android::base::LogSeverity severity,
}
}
#endif
#endif
static
void
CheckMessage
(
const
CapturedStderr
&
cap
,
static
void
CheckMessage
(
const
CapturedStderr
&
cap
,
android
::
base
::
LogSeverity
severity
,
android
::
base
::
LogSeverity
severity
,
const
char
*
expected
)
{
const
char
*
expected
,
const
char
*
expected
_tag
=
nullptr
)
{
std
::
string
output
;
std
::
string
output
;
ASSERT_EQ
(
0
,
lseek
(
cap
.
fd
(),
0
,
SEEK_SET
));
ASSERT_EQ
(
0
,
lseek
(
cap
.
fd
(),
0
,
SEEK_SET
));
android
::
base
::
ReadFdToString
(
cap
.
fd
(),
&
output
);
android
::
base
::
ReadFdToString
(
cap
.
fd
(),
&
output
);
...
@@ -217,9 +217,18 @@ static void CheckMessage(const CapturedStderr& cap,
...
@@ -217,9 +217,18 @@ static void CheckMessage(const CapturedStderr& cap,
// many characters are in the log message.
// many characters are in the log message.
ASSERT_GT
(
output
.
length
(),
strlen
(
expected
));
ASSERT_GT
(
output
.
length
(),
strlen
(
expected
));
ASSERT_NE
(
nullptr
,
strstr
(
output
.
c_str
(),
expected
))
<<
output
;
ASSERT_NE
(
nullptr
,
strstr
(
output
.
c_str
(),
expected
))
<<
output
;
if
(
expected_tag
!=
nullptr
)
{
ASSERT_NE
(
nullptr
,
strstr
(
output
.
c_str
(),
expected_tag
))
<<
output
;
}
#if !defined(_WIN32)
#if !defined(_WIN32)
std
::
regex
message_regex
(
make_log_pattern
(
severity
,
expected
));
std
::
string
regex_str
;
if
(
expected_tag
!=
nullptr
)
{
regex_str
.
append
(
expected_tag
);
regex_str
.
append
(
" "
);
}
regex_str
.
append
(
make_log_pattern
(
severity
,
expected
));
std
::
regex
message_regex
(
regex_str
);
ASSERT_TRUE
(
std
::
regex_search
(
output
,
message_regex
))
<<
output
;
ASSERT_TRUE
(
std
::
regex_search
(
output
,
message_regex
))
<<
output
;
#endif
#endif
}
}
...
@@ -600,3 +609,17 @@ TEST(logging, LOG_FATAL_ABORTER_MESSAGE) {
...
@@ -600,3 +609,17 @@ TEST(logging, LOG_FATAL_ABORTER_MESSAGE) {
__attribute__
((
constructor
))
void
TestLoggingInConstructor
()
{
__attribute__
((
constructor
))
void
TestLoggingInConstructor
()
{
LOG
(
ERROR
)
<<
"foobar"
;
LOG
(
ERROR
)
<<
"foobar"
;
}
}
TEST
(
logging
,
SetDefaultTag
)
{
constexpr
const
char
*
expected_tag
=
"test_tag"
;
constexpr
const
char
*
expected_msg
=
"foobar"
;
CapturedStderr
cap
;
{
std
::
string
old_default_tag
=
android
::
base
::
GetDefaultTag
();
android
::
base
::
SetDefaultTag
(
expected_tag
);
android
::
base
::
ScopedLogSeverity
sls
(
android
::
base
::
LogSeverity
::
INFO
);
LOG
(
INFO
)
<<
expected_msg
;
android
::
base
::
SetDefaultTag
(
old_default_tag
);
}
CheckMessage
(
cap
,
android
::
base
::
LogSeverity
::
INFO
,
expected_msg
,
expected_tag
);
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment