mirror of
https://github.com/espressif/openthread.git
synced 2026-09-01 14:59:54 +00:00
[simul-plat] new option to write logs to a given file (#8516)
This commit adds a new option in simulation platform to allow us to specify a file name to write the logs into. This is applicable when `LOG_OUTPUT` is set to `PLATFORM_DEFINED`. The filename can be specified by `-l <name>` or `--log-file=<name>` options. If no log file is provided then logs are emitted to syslog.
This commit is contained in:
@@ -31,6 +31,7 @@
|
||||
#include <openthread/config.h>
|
||||
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdint.h>
|
||||
@@ -44,21 +45,82 @@
|
||||
#include "utils/code_utils.h"
|
||||
|
||||
#if (OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED)
|
||||
OT_TOOL_WEAK void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, ...)
|
||||
|
||||
static FILE *sLogFile = NULL;
|
||||
|
||||
void platformLoggingSetFileName(const char *aName)
|
||||
{
|
||||
if (sLogFile != NULL)
|
||||
{
|
||||
fclose(sLogFile);
|
||||
}
|
||||
|
||||
sLogFile = fopen(aName, "wt");
|
||||
|
||||
if (sLogFile == NULL)
|
||||
{
|
||||
fprintf(stderr, "Failed to open log file '%s': %s\r\n", aName, strerror(errno));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
void platformLoggingInit(const char *aName)
|
||||
{
|
||||
if (sLogFile == NULL)
|
||||
{
|
||||
openlog(aName, LOG_PID, LOG_USER);
|
||||
setlogmask(setlogmask(0) & LOG_UPTO(LOG_NOTICE));
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(sLogFile, "OpenThread logs\r\n");
|
||||
fprintf(sLogFile, "- Program: %s\r\n", aName);
|
||||
fprintf(sLogFile, "- Platform: simulation\r\n");
|
||||
fprintf(sLogFile, "- Node ID: %lu\r\n", (unsigned long)gNodeId);
|
||||
fprintf(sLogFile, "\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
void platformLoggingDeinit(void)
|
||||
{
|
||||
if (sLogFile != NULL)
|
||||
{
|
||||
fclose(sLogFile);
|
||||
sLogFile = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, ...)
|
||||
{
|
||||
OT_UNUSED_VARIABLE(aLogLevel);
|
||||
OT_UNUSED_VARIABLE(aLogRegion);
|
||||
|
||||
char logString[512];
|
||||
int offset;
|
||||
va_list args;
|
||||
|
||||
offset = snprintf(logString, sizeof(logString), "[%d]", gNodeId);
|
||||
|
||||
va_start(args, aFormat);
|
||||
vsnprintf(&logString[offset], sizeof(logString) - (uint16_t)offset, aFormat, args);
|
||||
va_end(args);
|
||||
|
||||
syslog(LOG_CRIT, "%s", logString);
|
||||
if (sLogFile == NULL)
|
||||
{
|
||||
char logString[512];
|
||||
int offset;
|
||||
|
||||
offset = snprintf(logString, sizeof(logString), "[%lu]", (unsigned long)gNodeId);
|
||||
|
||||
vsnprintf(&logString[offset], sizeof(logString) - (uint16_t)offset, aFormat, args);
|
||||
syslog(LOG_CRIT, "%s", logString);
|
||||
}
|
||||
else
|
||||
{
|
||||
vfprintf(sLogFile, aFormat, args);
|
||||
fprintf(sLogFile, "\r\n");
|
||||
}
|
||||
|
||||
va_end(args);
|
||||
}
|
||||
#endif
|
||||
|
||||
#else
|
||||
|
||||
void platformLoggingInit(const char *aName) { OT_UNUSED_VARIABLE(aName); }
|
||||
void platformLoggingDeinit(void) {}
|
||||
|
||||
#endif // (OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED)
|
||||
|
||||
@@ -185,6 +185,28 @@ void platformRadioProcess(otInstance *aInstance, const fd_set *aReadFdSet, const
|
||||
*/
|
||||
void platformRandomInit(void);
|
||||
|
||||
/**
|
||||
* This functions set the file name to use for logging.
|
||||
*
|
||||
* @param[in] aName The file name.
|
||||
*
|
||||
*/
|
||||
void platformLoggingSetFileName(const char *aName);
|
||||
|
||||
/**
|
||||
* This function initializes the platform logging service.
|
||||
*
|
||||
* @param[in] aName The log module name to set with syslog.
|
||||
*
|
||||
*/
|
||||
void platformLoggingInit(const char *aName);
|
||||
|
||||
/**
|
||||
* This function finalizes the platform logging service.
|
||||
*
|
||||
*/
|
||||
void platformLoggingDeinit(void);
|
||||
|
||||
/**
|
||||
* This function updates the file descriptor sets with file descriptors used by the UART driver.
|
||||
*
|
||||
|
||||
@@ -44,7 +44,6 @@
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <syslog.h>
|
||||
|
||||
#include <openthread/tasklet.h>
|
||||
#include <openthread/platform/alarm-milli.h>
|
||||
@@ -74,6 +73,7 @@ enum
|
||||
OT_SIM_OPT_ENABLE_ENERGY_SCAN = 'E',
|
||||
OT_SIM_OPT_SLEEP_TO_TX = 't',
|
||||
OT_SIM_OPT_TIME_SPEED = 's',
|
||||
OT_SIM_OPT_LOG_FILE = 'l',
|
||||
OT_SIM_OPT_UNKNOWN = '?',
|
||||
};
|
||||
|
||||
@@ -86,7 +86,11 @@ static void PrintUsage(const char *aProgramName, int aExitCode)
|
||||
" -h --help Display this usage information.\n"
|
||||
" -E --enable-energy-scan Enable energy scan capability.\n"
|
||||
" -t --sleep-to-tx Let radio support direct transition from sleep to TX with CSMA.\n"
|
||||
" -s --time-speed=val Speed up the time in simulation.\n",
|
||||
" -s --time-speed=val Speed up the time in simulation.\n"
|
||||
#if (OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED)
|
||||
" -l --log-file=name File name to write logs.\n"
|
||||
#endif
|
||||
,
|
||||
aProgramName);
|
||||
|
||||
exit(aExitCode);
|
||||
@@ -102,9 +106,18 @@ void otSysInit(int aArgCount, char *aArgVector[])
|
||||
{"enable-energy-scan", no_argument, 0, OT_SIM_OPT_ENABLE_ENERGY_SCAN},
|
||||
{"sleep-to-tx", no_argument, 0, OT_SIM_OPT_SLEEP_TO_TX},
|
||||
{"time-speed", required_argument, 0, OT_SIM_OPT_TIME_SPEED},
|
||||
#if (OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED)
|
||||
{"log-file", required_argument, 0, OT_SIM_OPT_LOG_FILE},
|
||||
#endif
|
||||
{0, 0, 0, 0},
|
||||
};
|
||||
|
||||
#if (OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED)
|
||||
static const char options[] = "Ehts:l:";
|
||||
#else
|
||||
static const char options[] = "Ehts:";
|
||||
#endif
|
||||
|
||||
if (gPlatformPseudoResetWasRequested)
|
||||
{
|
||||
gPlatformPseudoResetWasRequested = false;
|
||||
@@ -115,7 +128,7 @@ void otSysInit(int aArgCount, char *aArgVector[])
|
||||
|
||||
while (true)
|
||||
{
|
||||
int c = getopt_long(aArgCount, aArgVector, "Ehts:", long_options, NULL);
|
||||
int c = getopt_long(aArgCount, aArgVector, options, long_options, NULL);
|
||||
|
||||
if (c == -1)
|
||||
{
|
||||
@@ -144,6 +157,11 @@ void otSysInit(int aArgCount, char *aArgVector[])
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
break;
|
||||
#if OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED
|
||||
case OT_SIM_OPT_LOG_FILE:
|
||||
platformLoggingSetFileName(optarg);
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -162,12 +180,10 @@ void otSysInit(int aArgCount, char *aArgVector[])
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
openlog(basename(aArgVector[0]), LOG_PID, LOG_USER);
|
||||
setlogmask(setlogmask(0) & LOG_UPTO(LOG_NOTICE));
|
||||
|
||||
signal(SIGTERM, &handleSignal);
|
||||
signal(SIGHUP, &handleSignal);
|
||||
|
||||
platformLoggingInit(basename(aArgVector[0]));
|
||||
platformAlarmInit(speedUpFactor);
|
||||
platformRadioInit();
|
||||
#if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
|
||||
@@ -184,6 +200,7 @@ void otSysDeinit(void)
|
||||
#if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
|
||||
platformTrelDeinit();
|
||||
#endif
|
||||
platformLoggingDeinit();
|
||||
}
|
||||
|
||||
void otSysProcessDrivers(otInstance *aInstance)
|
||||
|
||||
Reference in New Issue
Block a user