Skip to content

Commit

Permalink
[logging] use program name for log id (#1953)
Browse files Browse the repository at this point in the history
On Android, we use the "ot-daemon" name for the runnable program
and we want the syslog id be updated to "ot-daemon" as well.
Instead of hardcoding the syslog id, this commit derive the id
from the program name so it works for both linux and Android.
  • Loading branch information
wgtdkp authored Aug 1, 2023
1 parent 0a89ec6 commit 0de4d53
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
3 changes: 1 addition & 2 deletions src/agent/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
#include "common/types.hpp"
#include "ncp/ncp_openthread.hpp"

static const char kSyslogIdent[] = "otbr-agent";
static const char kDefaultInterfaceName[] = "wpan0";

// Port number used by Rest server.
Expand Down Expand Up @@ -268,7 +267,7 @@ static int realmain(int argc, char *argv[])
}
}

otbrLogInit(kSyslogIdent, logLevel, verbose);
otbrLogInit(argv[0], logLevel, verbose);
otbrLogNotice("Running %s", OTBR_PACKAGE_VERSION);
otbrLogNotice("Thread version: %s", otbr::Ncp::ControllerOpenThread::GetThreadVersion());
otbrLogNotice("Thread interface: %s", interfaceName);
Expand Down
11 changes: 8 additions & 3 deletions src/common/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,17 @@ void otbrLogSetLevel(otbrLogLevel aLevel)
}

/** Initialize logging */
void otbrLogInit(const char *aIdent, otbrLogLevel aLevel, bool aPrintStderr)
void otbrLogInit(const char *aProgramName, otbrLogLevel aLevel, bool aPrintStderr)
{
assert(aIdent);
const char *ident;

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

openlog(aIdent, (LOG_CONS | LOG_PID) | (aPrintStderr ? LOG_PERROR : 0), OTBR_SYSLOG_FACILITY_ID);
ident = strrchr(aProgramName, '/');
ident = (ident != nullptr) ? ident + 1 : aProgramName;

openlog(ident, (LOG_CONS | LOG_PID) | (aPrintStderr ? LOG_PERROR : 0), OTBR_SYSLOG_FACILITY_ID);
sLevel = aLevel;
sDefaultLevel = sLevel;
}
Expand Down
4 changes: 2 additions & 2 deletions src/common/logging.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ void otbrLogEnableSyslog(bool aEnabled);
/**
* This function initialize the logging service.
*
* @param[in] aIdent Identity of the logger.
* @param[in] aProgramName The name of this runnable program.
* @param[in] aLevel Log level of the logger.
* @param[in] aPrintStderr Whether to log to stderr.
*
*/
void otbrLogInit(const char *aIdent, otbrLogLevel aLevel, bool aPrintStderr);
void otbrLogInit(const char *aProgramName, otbrLogLevel aLevel, bool aPrintStderr);

/**
* This function log at level @p aLevel.
Expand Down
3 changes: 1 addition & 2 deletions src/web/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
#include "common/logging.hpp"
#include "web/web-service/web_server.hpp"

static const char kSyslogIdent[] = "otbr-web";
static const char kDefaultInterfaceName[] = "wpan0";
static const char kDefaultListenAddr[] = "::";

Expand Down Expand Up @@ -112,7 +111,7 @@ int main(int argc, char **argv)
}
}

otbrLogInit(kSyslogIdent, logLevel, true);
otbrLogInit(argv[0], logLevel, true);
otbrLogInfo("Running %s", OTBR_PACKAGE_VERSION);

if (interfaceName == nullptr)
Expand Down

0 comments on commit 0de4d53

Please sign in to comment.