Skip to content

Commit

Permalink
[log] add Android log system support (#2614)
Browse files Browse the repository at this point in the history
Developers can use the command `ot-ctl log level 5` to change the
default log level to DEBUG. Sometimes, the `ot-daemon` crashes before
developers can run the ot-ctl command on Android.

This commit add Android log system support, so that developers can
run the command `setprop log.tag.ot-daemon DEBUG` to change the log
level before the ot-daemon runs.
  • Loading branch information
zhanglongxia authored Nov 26, 2024
1 parent 414ee5b commit 094f9c1
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 10 deletions.
12 changes: 9 additions & 3 deletions src/agent/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
#include "ncp/thread_host.hpp"

#ifdef OTBR_ENABLE_PLATFORM_ANDROID
#include <log/log.h>
#ifndef __ANDROID__
#error "OTBR_ENABLE_PLATFORM_ANDROID can be enabled for only Android devices"
#endif
Expand Down Expand Up @@ -177,16 +178,21 @@ static void OnAllocateFailed(void)

static otbrLogLevel GetDefaultLogLevel(void)
{
otbrLogLevel level = OTBR_LOG_INFO;

#if OTBR_ENABLE_PLATFORM_ANDROID
char value[PROPERTY_VALUE_MAX];
// The log level is set to DEBUG by default, the final output log will be filtered by Android log system.
otbrLogLevel level = OTBR_LOG_DEBUG;
char value[PROPERTY_VALUE_MAX];

// Set the Android log level to INFO by default.
__android_log_set_minimum_priority(ANDROID_LOG_INFO);

property_get("ro.build.type", value, "user");
if (!strcmp(value, "user"))
{
level = OTBR_LOG_WARNING;
}
#else
otbrLogLevel level = OTBR_LOG_INFO;
#endif

return level;
Expand Down
64 changes: 57 additions & 7 deletions src/common/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
#include <sys/time.h>
#include <syslog.h>

#if OTBR_ENABLE_PLATFORM_ANDROID
#include <log/log.h>
#endif

#include <sstream>

#include "common/code_utils.hpp"
Expand Down Expand Up @@ -87,20 +91,25 @@ void otbrLogSyslogSetEnabled(bool aEnabled)
/** Initialize logging */
void otbrLogInit(const char *aProgramName, otbrLogLevel aLevel, bool aPrintStderr, bool aSyslogDisable)
{
const char *ident;

assert(aProgramName != nullptr);
assert(aLevel >= OTBR_LOG_EMERG && aLevel <= OTBR_LOG_DEBUG);

ident = strrchr(aProgramName, '/');
ident = (ident != nullptr) ? ident + 1 : aProgramName;

otbrLogSyslogSetEnabled(!aSyslogDisable);

#if OTBR_ENABLE_PLATFORM_ANDROID
OTBR_UNUSED_VARIABLE(aProgramName);
#else
assert(aProgramName != nullptr);

if (!sSyslogDisabled)
{
const char *ident;

ident = strrchr(aProgramName, '/');
ident = (ident != nullptr) ? ident + 1 : aProgramName;

openlog(ident, (LOG_CONS | LOG_PID) | (aPrintStderr ? LOG_PERROR : 0), OTBR_SYSLOG_FACILITY_ID);
}
#endif

sLevel = aLevel;
sDefaultLevel = sLevel;
}
Expand Down Expand Up @@ -130,6 +139,38 @@ static const char *GetPrefix(const char *aLogTag)
return prefix;
}

#if OTBR_ENABLE_PLATFORM_ANDROID
static android_LogPriority ConvertToAndroidLogPriority(otbrLogLevel aLevel)
{
android_LogPriority priority;

switch (aLevel)
{
case OTBR_LOG_EMERG:
case OTBR_LOG_ALERT:
case OTBR_LOG_CRIT:
priority = ANDROID_LOG_FATAL;
break;
case OTBR_LOG_ERR:
priority = ANDROID_LOG_ERROR;
break;
case OTBR_LOG_WARNING:
priority = ANDROID_LOG_WARN;
break;
case OTBR_LOG_NOTICE:
case OTBR_LOG_INFO:
priority = ANDROID_LOG_INFO;
break;
case OTBR_LOG_DEBUG:
default:
priority = ANDROID_LOG_DEBUG;
break;
}

return priority;
}
#endif

/** log to the syslog or standard out */
void otbrLog(otbrLogLevel aLevel, const char *aLogTag, const char *aFormat, ...)
{
Expand All @@ -147,7 +188,12 @@ void otbrLog(otbrLogLevel aLevel, const char *aLogTag, const char *aFormat, ...)
}
else
{
#if OTBR_ENABLE_PLATFORM_ANDROID
__android_log_print(ConvertToAndroidLogPriority(aLevel), LOG_TAG, "%s%s: %s", sLevelString[aLevel],
GetPrefix(aLogTag), buffer);
#else
syslog(static_cast<int>(aLevel), "%s%s: %s", sLevelString[aLevel], GetPrefix(aLogTag), buffer);
#endif
}
}

Expand Down Expand Up @@ -177,7 +223,11 @@ void otbrLogvNoFilter(otbrLogLevel aLevel, const char *aFormat, va_list aArgList
}
else
{
#if OTBR_ENABLE_PLATFORM_ANDROID
__android_log_vprint(ConvertToAndroidLogPriority(aLevel), LOG_TAG, aFormat, aArgList);
#else
vsyslog(static_cast<int>(aLevel), aFormat, aArgList);
#endif
}
}

Expand Down

0 comments on commit 094f9c1

Please sign in to comment.