posix: syslog: add support for syslog

This is just a simple wrapper around Zephyr's native log api.

Note: the standard LOG_ERR syslog priority does conflict with
Zephyr's LOG_ERR() macro. This will need to be worked-around on
a case-by-case basis.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
This commit is contained in:
Christopher Friedt 2024-02-04 22:51:06 -05:00 committed by Chris Friedt
parent 4f34a410fa
commit 3593720080
5 changed files with 170 additions and 0 deletions

View file

@ -0,0 +1,65 @@
/*
* Copyright (c) 2024, Meta
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ZEPHYR_INCLUDE_POSIX_SYSLOG_H_
#define ZEPHYR_INCLUDE_POSIX_SYSLOG_H_
#include <stdarg.h>
/* option */
#define LOG_PID 1
#define LOG_CONS 2
#define LOG_NDELAY 4
#define LOG_ODELAY 8
#define LOG_NOWAIT 16
#define LOG_PERROR 32
/* facility */
#define LOG_KERN 0
#define LOG_USER 1
#define LOG_MAIL 2
#define LOG_NEWS 3
#define LOG_UUCP 4
#define LOG_DAEMON 5
#define LOG_AUTH 6
#define LOG_CRON 7
#define LOG_LPR 8
#define LOG_LOCAL0 9
#define LOG_LOCAL1 10
#define LOG_LOCAL2 11
#define LOG_LOCAL3 12
#define LOG_LOCAL4 13
#define LOG_LOCAL5 14
#define LOG_LOCAL6 15
#define LOG_LOCAL7 16
/* priority */
#define LOG_EMERG 0
#define LOG_ALERT 1
#define LOG_CRIT 2
#define LOG_ERR 3
#define LOG_WARNING 4
#define LOG_NOTICE 5
#define LOG_INFO 6
#define LOG_DEBUG 7
/* generate a valid log mask */
#define LOG_MASK(mask) ((mask) & BIT_MASK(LOG_DEBUG + 1))
#ifdef __cplusplus
extern "C" {
#endif
void closelog(void);
void openlog(const char *ident, int logopt, int facility);
int setlogmask(int maskpri);
void syslog(int priority, const char *message, ...);
void vsyslog(int priority, const char *format, va_list ap);
#ifdef __cplusplus
}
#endif
#endif /* ZEPHYR_INCLUDE_POSIX_SYSLOG_H_ */

View file

@ -44,6 +44,7 @@ zephyr_library_sources_ifdef(CONFIG_POSIX_FS fs.c)
zephyr_library_sources_ifdef(CONFIG_POSIX_MQUEUE mqueue.c)
zephyr_library_sources_ifdef(CONFIG_POSIX_PUTMSG stropts.c)
zephyr_library_sources_ifdef(CONFIG_POSIX_SIGNAL signal.c ${STRSIGNAL_TABLE_H})
zephyr_library_sources_ifdef(CONFIG_POSIX_SYSLOG syslog.c)
zephyr_library_sources_ifdef(CONFIG_POSIX_UNAME uname.c)
zephyr_library_sources_ifdef(CONFIG_PTHREAD_IPC _common.c)
zephyr_library_sources_ifdef(CONFIG_PTHREAD_BARRIER barrier.c)

View file

@ -43,6 +43,7 @@ rsource "Kconfig.signal"
rsource "Kconfig.spinlock"
rsource "Kconfig.stropts"
rsource "Kconfig.sysconf"
rsource "Kconfig.syslog"
rsource "Kconfig.timer"
rsource "Kconfig.uname"

View file

@ -0,0 +1,10 @@
# Copyright (c) 2024, Meta
#
# SPDX-License-Identifier: Apache-2.0
config POSIX_SYSLOG
bool "Support for syslog()"
default y if POSIX_API
help
This option provides support for closelog(), openlog(), syslog(),
setlogmask(), and vsyslog().

View file

@ -0,0 +1,93 @@
/*
* Copyright (c) 2024, Meta
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <errno.h>
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#undef LOG_ERR
#include <zephyr/posix/syslog.h>
#include <zephyr/sys/printk.h>
static struct k_spinlock syslog_lock;
static uint8_t syslog_mask;
static int syslog_priority_to_zephyr_log_level(int priority)
{
switch (priority) {
case LOG_EMERG:
case LOG_ALERT:
case LOG_CRIT:
case LOG_ERR:
return LOG_LEVEL_ERR;
case LOG_WARNING:
return LOG_LEVEL_WRN;
case LOG_NOTICE:
case LOG_INFO:
return LOG_LEVEL_INF;
case LOG_DEBUG:
return LOG_LEVEL_DBG;
default:
return -EINVAL;
}
}
void closelog(void)
{
}
void openlog(const char *ident, int option, int facility)
{
ARG_UNUSED(ident);
ARG_UNUSED(option);
ARG_UNUSED(facility);
}
void syslog(int priority, const char *format, ...)
{
va_list ap;
va_start(ap, format);
vsyslog(priority, format, ap);
va_end(ap);
}
int setlogmask(int maskpri)
{
int oldpri;
K_SPINLOCK(&syslog_lock) {
oldpri = syslog_mask;
syslog_mask = maskpri;
}
return oldpri;
}
void vsyslog(int priority, const char *format, va_list ap)
{
uint8_t mask;
int level = syslog_priority_to_zephyr_log_level(priority);
if (level < 0) {
/* invalid priority */
return;
}
K_SPINLOCK(&syslog_lock) {
mask = syslog_mask;
}
if ((BIT(level) & mask) == 0) {
/* masked */
return;
}
#if !defined(CONFIG_LOG) || defined(CONFIG_LOG_MODE_MINIMAL)
vprintk(format, ap);
#else
log_generic(level, format, ap);
#endif
}