Merge "logd: add internal prdebug function"

This commit is contained in:
Mark Salyzyn 2016-02-09 16:01:24 +00:00 committed by Gerrit Code Review
commit 37adf180c7
2 changed files with 27 additions and 3 deletions

View File

@ -17,6 +17,7 @@
#ifndef _LOGD_LOG_UTILS_H__
#define _LOGD_LOG_UTILS_H__
#include <sys/cdefs.h>
#include <sys/types.h>
#include <log/log.h>
@ -29,6 +30,7 @@ namespace android {
// Furnished in main.cpp. Caller must own and free returned value
char *uidToName(uid_t uid);
void prdebug(const char *fmt, ...) __printflike(1, 2);
// Furnished in LogStatistics.cpp. Caller must own and free returned value
char *pidToName(pid_t pid);

View File

@ -211,10 +211,32 @@ bool property_get_bool(const char *key, int flag) {
return (flag & BOOL_DEFAULT_FLAG_TRUE_FALSE) != BOOL_DEFAULT_FALSE;
}
// Remove the static, and use this variable
// globally for debugging if necessary. eg:
// write(fdDmesg, "I am here\n", 10);
static int fdDmesg = -1;
void inline 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));
}
}
static sem_t uidName;
static uid_t uid;