Use syslog for logging on posix. (#1203)

This commit is contained in:
Jonathan Hui
2017-01-24 23:23:08 -08:00
committed by GitHub
parent 22b35a5662
commit b2f70b213f
2 changed files with 20 additions and 8 deletions
+10 -8
View File
@@ -34,7 +34,10 @@
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <time.h>
#ifndef _WIN32
#include <syslog.h>
#endif
#include <platform/logging.h>
@@ -48,8 +51,6 @@
void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, ...)
{
struct timeval tv;
char timeString[40];
char logString[512];
unsigned int offset;
int charsWritten;
@@ -57,10 +58,7 @@ void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat
offset = 0;
gettimeofday(&tv, NULL);
strftime(timeString, sizeof(timeString), "%Y-%m-%d %H:%M:%S", localtime(&tv.tv_sec));
LOG_PRINTF("%s.%06d ", timeString, (uint32_t)tv.tv_usec);
LOG_PRINTF("[%d] ", NODE_ID);
va_start(args, aFormat);
charsWritten = vsnprintf(&logString[offset], sizeof(logString) - offset, aFormat, args);
@@ -69,7 +67,11 @@ void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat
VerifyOrExit(charsWritten >= 0, logString[offset] = 0);
exit:
fprintf(stderr, "%s\r\n", logString);
#ifndef _WIN32
syslog(LOG_CRIT, "%s", logString);
#else
printf("%s\r\n", logString);
#endif
(void)aLogLevel;
(void)aLogRegion;
+10
View File
@@ -41,6 +41,11 @@
#include <stdio.h>
#include <stdlib.h>
#ifndef _WIN32
#include <libgen.h>
#include <syslog.h>
#endif
#include <openthread.h>
#include <openthread-tasklet.h>
#include <platform/alarm.h>
@@ -58,6 +63,11 @@ void PlatformInit(int argc, char *argv[])
exit(EXIT_FAILURE);
}
#ifndef _WIN32
openlog(basename(argv[0]), LOG_PID, LOG_USER);
setlogmask(setlogmask(0) & LOG_UPTO(LOG_NOTICE));
#endif
NODE_ID = (uint32_t)strtol(argv[1], &endptr, 0);
if (*endptr != '\0')