Skip to content

Commit 583aa30

Browse files
committed
Avoid start multiple log watchdog threads
It is observed that `enableWatchDog` might be called after the manager activity is resumed without user's interaction, due to some unknown mechanism of preference refreshing.
1 parent e7a5835 commit 583aa30

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

daemon/src/main/jni/logcat.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class Logcat {
113113

114114
static size_t PrintLogLine(const AndroidLogEntry &entry, FILE *out);
115115

116-
void EnsureLogWatchDog();
116+
void StartLogWatchDog();
117117

118118
JNIEnv *env_;
119119
jobject thiz_;
@@ -243,9 +243,9 @@ void Logcat::ProcessBuffer(struct log_msg *buf) {
243243
} else if (msg == "!!refresh_verbose!!"sv) {
244244
RefreshFd(true);
245245
} else if (msg == "!!start_watchdog!!"sv) {
246+
if (!enable_watchdog) StartLogWatchDog();
246247
enable_watchdog = true;
247248
enable_watchdog.notify_one();
248-
EnsureLogWatchDog();
249249
} else if (msg == "!!stop_watchdog!!"sv) {
250250
enable_watchdog = false;
251251
enable_watchdog.notify_one();
@@ -260,20 +260,21 @@ void Logcat::ProcessBuffer(struct log_msg *buf) {
260260
}
261261
}
262262

263-
void Logcat::EnsureLogWatchDog() {
263+
void Logcat::StartLogWatchDog() {
264264
constexpr static auto kLogdSizeProp = "persist.logd.size"sv;
265265
constexpr static auto kLogdTagProp = "persist.log.tag"sv;
266266
constexpr static auto kLogdMainSizeProp = "persist.logd.size.main"sv;
267267
constexpr static auto kLogdCrashSizeProp = "persist.logd.size.crash"sv;
268268
constexpr static long kErr = -1;
269269
std::thread watchdog([this] {
270+
Log("[LogWatchDog started]\n");
270271
while (true) {
271272
enable_watchdog.wait(false); // Blocking current thread until enable_watchdog is true;
272273
auto logd_size = GetByteProp(kLogdSizeProp);
273274
auto logd_tag = GetStrProp(kLogdTagProp);
274275
auto logd_main_size = GetByteProp(kLogdMainSizeProp);
275276
auto logd_crash_size = GetByteProp(kLogdCrashSizeProp);
276-
Log("[LogWatchDog started] log.tag: " + logd_tag +
277+
Log("[LogWatchDog running] log.tag: " + logd_tag +
277278
"; logd.[default, main, crash].size: [" + std::to_string(logd_size) + "," +
278279
std::to_string(logd_main_size) + "," + std::to_string(logd_crash_size) + "]\n");
279280
if (!logd_tag.empty() ||

0 commit comments

Comments
 (0)