logd: use libbase logging
We can use libbase logging to output to the kernel log instead of the
'prdebug' function, so use that instead.
Bonus #1: we can now use CHECK().
Bonus #2: logging unit tests automatically output to stderr.
Bonus #3: We see dependent library's logs instead of losing them to
the void.
Test: logging unit tests
Test: logs show appropriately in dmesg / stderr
Test: CHECK() works
Change-Id: I92f8056b4820dc4998996cf46460568085299700
diff --git a/logd/main.cpp b/logd/main.cpp
index c2b5a1d..773ffb8 100644
--- a/logd/main.cpp
+++ b/logd/main.cpp
@@ -36,7 +36,9 @@
#include <memory>
+#include <android-base/logging.h>
#include <android-base/macros.h>
+#include <android-base/stringprintf.h>
#include <cutils/android_get_control_file.h>
#include <cutils/sockets.h>
#include <log/event_tag_map.h>
@@ -70,19 +72,18 @@
sched_param param = {};
if (set_sched_policy(0, SP_BACKGROUND) < 0) {
- android::prdebug("failed to set background scheduling policy");
+ PLOG(ERROR) << "failed to set background scheduling policy";
return -1;
}
if (sched_setscheduler((pid_t)0, SCHED_BATCH, ¶m) < 0) {
- android::prdebug("failed to set batch scheduler");
+ PLOG(ERROR) << "failed to set batch scheduler";
return -1;
}
- if (!__android_logger_property_get_bool("ro.debuggable",
- BOOL_DEFAULT_FALSE) &&
+ if (!__android_logger_property_get_bool("ro.debuggable", BOOL_DEFAULT_FALSE) &&
prctl(PR_SET_DUMPABLE, 0) == -1) {
- android::prdebug("failed to clear PR_SET_DUMPABLE");
+ PLOG(ERROR) << "failed to clear PR_SET_DUMPABLE";
return -1;
}
@@ -105,40 +106,13 @@
return -1;
}
if (cap_set_proc(caps.get()) < 0) {
- android::prdebug("failed to set CAP_SYSLOG or CAP_AUDIT_CONTROL (%d)", errno);
+ PLOG(ERROR) << "failed to set CAP_SYSLOG or CAP_AUDIT_CONTROL";
return -1;
}
return 0;
}
-static int fdDmesg = -1;
-void android::prdebug(const char* fmt, ...) {
- if (fdDmesg < 0) {
- return;
- }
-
- static const char message[] = {
- KMSG_PRIORITY(LOG_DEBUG), 'l', 'o', 'g', 'd', ':', ' '
- };
- char buffer[256];
- memcpy(buffer, message, sizeof(message));
-
- va_list ap;
- va_start(ap, fmt);
- int n = vsnprintf(buffer + sizeof(message),
- sizeof(buffer) - sizeof(message), fmt, ap);
- va_end(ap);
- if (n > 0) {
- buffer[sizeof(buffer) - 1] = '\0';
- if (!strchr(buffer, '\n')) {
- buffer[sizeof(buffer) - 2] = '\0';
- strlcat(buffer, "\n", sizeof(buffer));
- }
- write(fdDmesg, buffer, strlen(buffer));
- }
-}
-
char* android::uidToName(uid_t u) {
struct Userdata {
uid_t uid;
@@ -246,8 +220,20 @@
return issueReinit();
}
+ android::base::InitLogging(
+ argv, [](android::base::LogId log_id, android::base::LogSeverity severity,
+ const char* tag, const char* file, unsigned int line, const char* message) {
+ if (tag && strcmp(tag, "logd") != 0) {
+ auto prefixed_message = android::base::StringPrintf("%s: %s", tag, message);
+ android::base::KernelLogger(log_id, severity, "logd", file, line,
+ prefixed_message.c_str());
+ } else {
+ android::base::KernelLogger(log_id, severity, "logd", file, line, message);
+ }
+ });
+
static const char dev_kmsg[] = "/dev/kmsg";
- fdDmesg = android_get_control_file(dev_kmsg);
+ int fdDmesg = android_get_control_file(dev_kmsg);
if (fdDmesg < 0) {
fdDmesg = TEMP_FAILURE_RETRY(open(dev_kmsg, O_WRONLY | O_CLOEXEC));
}
@@ -263,7 +249,7 @@
fdPmesg = TEMP_FAILURE_RETRY(
open(proc_kmsg, O_RDONLY | O_NDELAY | O_CLOEXEC));
}
- if (fdPmesg < 0) android::prdebug("Failed to open %s\n", proc_kmsg);
+ if (fdPmesg < 0) PLOG(ERROR) << "Failed to open " << proc_kmsg;
}
bool auditd = __android_logger_property_get_bool("ro.logd.auditd", BOOL_DEFAULT_TRUE);