Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
M
Magisk
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
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Magisk
Commits
c62dfc1b
Commit
c62dfc1b
authored
Dec 07, 2020
by
topjohnwu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make logging less error prone
parent
aabe2696
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
22 additions
and
13 deletions
+22
-13
daemon.cpp
native/jni/core/daemon.cpp
+22
-13
No files found.
native/jni/core/daemon.cpp
View file @
c62dfc1b
...
...
@@ -134,31 +134,32 @@ shortcut:
close
(
client
);
}
static
FILE
*
log_file
;
static
shared_ptr
<
FILE
>
log_file
;
bool
in_mem_log
=
true
;
atomic_flag
file_backed
=
ATOMIC_FLAG_INIT
;
static
char
*
log_buf
;
static
size_t
log_buf_len
;
void
setup_logfile
(
bool
reset
)
{
if
(
!
in_mem_log
)
if
(
file_backed
.
test_and_set
(
memory_order_relaxed
)
)
return
;
in_mem_log
=
false
;
if
(
reset
)
rename
(
LOGFILE
,
LOGFILE
".bak"
);
int
fd
=
xopen
(
LOGFILE
,
O_WRONLY
|
O_APPEND
|
O_CREAT
|
O_CLOEXEC
,
0644
);
if
(
fd
<
0
)
{
log_file
.
reset
();
return
;
}
// Dump all logs in memory (if exists)
if
(
log_buf
)
{
if
(
log_buf
)
write
(
fd
,
log_buf
,
log_buf_len
);
}
// Redirect to log file
log_file
=
fdopen
(
fd
,
"a"
);
setbuf
(
log_file
,
nullptr
);
free
(
log_buf
);
if
(
FILE
*
fp
=
fdopen
(
fd
,
"a"
))
{
setbuf
(
fp
,
nullptr
);
log_file
.
reset
(
fp
,
&
fclose
);
}
}
static
int
magisk_log
(
int
prio
,
const
char
*
fmt
,
va_list
ap
)
{
...
...
@@ -168,6 +169,10 @@ static int magisk_log(int prio, const char *fmt, va_list ap) {
// Log to logcat
__android_log_vprint
(
prio
,
"Magisk"
,
fmt
,
ap
);
auto
local_log_file
=
log_file
;
if
(
!
local_log_file
)
return
0
;
char
buf
[
4096
];
timeval
tv
;
tm
tm
;
...
...
@@ -192,11 +197,15 @@ static int magisk_log(int prio, const char *fmt, va_list ap) {
int
ms
=
tv
.
tv_usec
/
1000
;
len
+=
sprintf
(
buf
+
len
,
".%03d %c : "
,
ms
,
type
);
strcpy
(
buf
+
len
,
fmt
);
return
vfprintf
(
lo
g_file
,
buf
,
args
);
return
vfprintf
(
lo
cal_log_file
.
get
()
,
buf
,
args
);
}
static
void
android_logging
()
{
log_file
=
make_stream_fp
<
byte_stream
>
(
log_buf
,
log_buf_len
).
release
();
auto
in_mem_file
=
make_stream_fp
<
byte_stream
>
(
log_buf
,
log_buf_len
);
log_file
.
reset
(
in_mem_file
.
release
(),
[](
FILE
*
)
{
free
(
log_buf
);
log_buf
=
nullptr
;
});
log_cb
.
d
=
[](
auto
fmt
,
auto
ap
){
return
magisk_log
(
ANDROID_LOG_DEBUG
,
fmt
,
ap
);
};
log_cb
.
i
=
[](
auto
fmt
,
auto
ap
){
return
magisk_log
(
ANDROID_LOG_INFO
,
fmt
,
ap
);
};
log_cb
.
w
=
[](
auto
fmt
,
auto
ap
){
return
magisk_log
(
ANDROID_LOG_WARN
,
fmt
,
ap
);
};
...
...
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