forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]>
- Loading branch information
Showing
5 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |