1 /*
2 * Copyright (c) 2024, Meta
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6 #include <errno.h>
7
8 #include <zephyr/kernel.h>
9 #include <zephyr/logging/log.h>
10 #undef LOG_ERR
11 #include <zephyr/posix/syslog.h>
12 #include <zephyr/sys/printk.h>
13
14 static struct k_spinlock syslog_lock;
15 static uint8_t syslog_mask;
16
syslog_priority_to_zephyr_log_level(int priority)17 static int syslog_priority_to_zephyr_log_level(int priority)
18 {
19 switch (priority) {
20 case LOG_EMERG:
21 case LOG_ALERT:
22 case LOG_CRIT:
23 case LOG_ERR:
24 return LOG_LEVEL_ERR;
25 case LOG_WARNING:
26 return LOG_LEVEL_WRN;
27 case LOG_NOTICE:
28 case LOG_INFO:
29 return LOG_LEVEL_INF;
30 case LOG_DEBUG:
31 return LOG_LEVEL_DBG;
32 default:
33 return -EINVAL;
34 }
35 }
36
closelog(void)37 void closelog(void)
38 {
39 }
40
openlog(const char * ident,int option,int facility)41 void openlog(const char *ident, int option, int facility)
42 {
43 ARG_UNUSED(ident);
44 ARG_UNUSED(option);
45 ARG_UNUSED(facility);
46 }
47
syslog(int priority,const char * format,...)48 void syslog(int priority, const char *format, ...)
49 {
50 va_list ap;
51
52 va_start(ap, format);
53 vsyslog(priority, format, ap);
54 va_end(ap);
55 }
56
setlogmask(int maskpri)57 int setlogmask(int maskpri)
58 {
59 int oldpri = -1;
60
61 K_SPINLOCK(&syslog_lock) {
62 oldpri = syslog_mask;
63 syslog_mask = maskpri;
64 }
65
66 return oldpri;
67 }
68
vsyslog(int priority,const char * format,va_list ap)69 void vsyslog(int priority, const char *format, va_list ap)
70 {
71 uint8_t mask = 0;
72 int level = syslog_priority_to_zephyr_log_level(priority);
73
74 if (level < 0) {
75 /* invalid priority */
76 return;
77 }
78
79 K_SPINLOCK(&syslog_lock) {
80 mask = syslog_mask;
81 }
82
83 if ((BIT(level) & mask) == 0) {
84 /* masked */
85 return;
86 }
87
88 #if !defined(CONFIG_LOG) || defined(CONFIG_LOG_MODE_MINIMAL)
89 vprintk(format, ap);
90 #else
91 log_generic(level, format, ap);
92 #endif
93 }
94