[libc-commits] [libc] 8919899 - [libc] Implement syslog functions (#244029)
via libc-commits
libc-commits at lists.llvm.org
Fri Nov 20 11:04:02 PST 2026
Author: Michael Jones
Date: 2026-11-20T11:03:56-08:00
New Revision: 8919899b7733bffea72dee18d0f7c7bd5b09cda2
URL: https://github.com/llvm/llvm-project/commit/8919899b7733bffea72dee18d0f7c7bd5b09cda2
DIFF: https://github.com/llvm/llvm-project/commit/8919899b7733bffea72dee18d0f7c7bd5b09cda2.diff
LOG: [libc] Implement syslog functions (#244029)
These functions write directly to a system log with a timestamp prefix
and printf semantixs after. This PR adds them with an internal core that
makes it easier to test, and appropriate tests.
This does not cover the more exotic logging facilities, such as sockets.
That can be covered in followup PRs as needed.
Assisted-by: Automated tooling, human reviewed.
Added:
libc/hdr/syslog_macros.h
libc/src/syslog/CMakeLists.txt
libc/src/syslog/closelog.h
libc/src/syslog/linux/CMakeLists.txt
libc/src/syslog/linux/closelog.cpp
libc/src/syslog/linux/openlog.cpp
libc/src/syslog/linux/setlogmask.cpp
libc/src/syslog/linux/syslog.cpp
libc/src/syslog/linux/syslog_internal.cpp
libc/src/syslog/linux/syslog_internal.h
libc/src/syslog/linux/vsyslog.cpp
libc/src/syslog/openlog.h
libc/src/syslog/setlogmask.h
libc/src/syslog/syslog.h
libc/src/syslog/vsyslog.h
libc/test/src/syslog/CMakeLists.txt
libc/test/src/syslog/linux/CMakeLists.txt
libc/test/src/syslog/linux/openlog_syslog_closelog_test.cpp
libc/test/src/syslog/linux/setlogmask_test.cpp
libc/test/src/syslog/linux/syslog_internal_test.cpp
libc/test/src/syslog/linux/vsyslog_test.cpp
Modified:
libc/config/linux/aarch64/entrypoints.txt
libc/config/linux/riscv/entrypoints.txt
libc/config/linux/x86_64/entrypoints.txt
libc/hdr/CMakeLists.txt
libc/include/llvm-libc-macros/syslog-macros.h
libc/include/syslog.yaml
libc/src/CMakeLists.txt
libc/test/src/CMakeLists.txt
Removed:
################################################################################
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 4dbf3fab75ff4..99678f528cda2 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -1574,6 +1574,13 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.sys.select.pselect
libc.src.sys.select.select
+ # syslog.h entrypoints
+ libc.src.syslog.closelog
+ libc.src.syslog.openlog
+ libc.src.syslog.setlogmask
+ libc.src.syslog.syslog
+ libc.src.syslog.vsyslog
+
# langinfo.h entrypoints
libc.src.langinfo.nl_langinfo
libc.src.langinfo.nl_langinfo_l
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index be15b84a24d7b..abe25295b90e6 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -1807,6 +1807,13 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.sys.select.pselect
libc.src.sys.select.select
+ # syslog.h entrypoints
+ libc.src.syslog.closelog
+ libc.src.syslog.openlog
+ libc.src.syslog.setlogmask
+ libc.src.syslog.syslog
+ libc.src.syslog.vsyslog
+
# wchar.h entrypoints
libc.src.wchar.mbrlen
libc.src.wchar.mbsinit
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 4b7891ccb1691..2ab819aaa860f 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1811,6 +1811,13 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.sys.select.pselect
libc.src.sys.select.select
+ # syslog.h entrypoints
+ libc.src.syslog.closelog
+ libc.src.syslog.openlog
+ libc.src.syslog.setlogmask
+ libc.src.syslog.syslog
+ libc.src.syslog.vsyslog
+
# wchar.h entrypoints
libc.src.wchar.mbrlen
libc.src.wchar.mbsinit
diff --git a/libc/hdr/CMakeLists.txt b/libc/hdr/CMakeLists.txt
index bc873756ec836..ec2a47ad9694f 100644
--- a/libc/hdr/CMakeLists.txt
+++ b/libc/hdr/CMakeLists.txt
@@ -296,6 +296,14 @@ add_proxy_header_library(
libc.include.llvm-libc-macros.sys_stat_macros
)
+add_proxy_header_library(
+ syslog_macros
+ HDRS
+ syslog_macros.h
+ FULL_BUILD_DEPENDS
+ libc.include.llvm-libc-macros.syslog_macros
+)
+
add_proxy_header_library(
sys_mman_macros
HDRS
diff --git a/libc/hdr/syslog_macros.h b/libc/hdr/syslog_macros.h
new file mode 100644
index 0000000000000..7fbb3851463ba
--- /dev/null
+++ b/libc/hdr/syslog_macros.h
@@ -0,0 +1,27 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Proxy header for macros from syslog.h.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_HDR_SYSLOG_MACROS_H
+#define LLVM_LIBC_HDR_SYSLOG_MACROS_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-macros/syslog-macros.h"
+
+#else // Overlay mode
+
+#include <syslog.h>
+
+#endif // LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_SYSLOG_MACROS_H
diff --git a/libc/include/llvm-libc-macros/syslog-macros.h b/libc/include/llvm-libc-macros/syslog-macros.h
index 688127a67eae1..b917c6c35bc0e 100644
--- a/libc/include/llvm-libc-macros/syslog-macros.h
+++ b/libc/include/llvm-libc-macros/syslog-macros.h
@@ -50,4 +50,8 @@
#define LOG_INFO 6
#define LOG_DEBUG 7
+// Priority masks for setlogmask
+#define LOG_MASK(pri) (1 << (pri))
+#define LOG_UPTO(pri) ((1 << ((pri) + 1)) - 1)
+
#endif // LLVM_LIBC_MACROS_SYSLOG_MACROS_H
diff --git a/libc/include/syslog.yaml b/libc/include/syslog.yaml
index 2452b22ad506b..aca6f7d9c440e 100644
--- a/libc/include/syslog.yaml
+++ b/libc/include/syslog.yaml
@@ -62,7 +62,48 @@ macros:
macro_header: syslog-macros.h
- macro_name: LOG_DEBUG
macro_header: syslog-macros.h
-types: []
+ - macro_name: LOG_MASK
+ macro_header: syslog-macros.h
+ - macro_name: LOG_UPTO
+ macro_header: syslog-macros.h
+types:
+ - type_name: va_list
enums: []
objects: []
-functions: []
+functions:
+ - name: closelog
+ standards:
+ - posix
+ return_type: void
+ arguments:
+ - type: void
+ - name: openlog
+ standards:
+ - posix
+ return_type: void
+ arguments:
+ - type: const char *
+ - type: int
+ - type: int
+ - name: setlogmask
+ standards:
+ - posix
+ return_type: int
+ arguments:
+ - type: int
+ - name: syslog
+ standards:
+ - posix
+ return_type: void
+ arguments:
+ - type: int
+ - type: const char *
+ - type: '...'
+ - name: vsyslog
+ standards:
+ - bsd
+ return_type: void
+ arguments:
+ - type: int
+ - type: const char *
+ - type: va_list
diff --git a/libc/src/CMakeLists.txt b/libc/src/CMakeLists.txt
index 8805fc196af4a..08a23e715ca47 100644
--- a/libc/src/CMakeLists.txt
+++ b/libc/src/CMakeLists.txt
@@ -64,3 +64,7 @@ add_subdirectory(signal)
add_subdirectory(spawn)
add_subdirectory(threads)
add_subdirectory(ucontext)
+
+if(${LIBC_TARGET_OS} STREQUAL "linux")
+ add_subdirectory(syslog)
+endif()
diff --git a/libc/src/syslog/CMakeLists.txt b/libc/src/syslog/CMakeLists.txt
new file mode 100644
index 0000000000000..fc96908fd4589
--- /dev/null
+++ b/libc/src/syslog/CMakeLists.txt
@@ -0,0 +1,38 @@
+if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+ add_subdirectory(${LIBC_TARGET_OS})
+endif()
+
+add_entrypoint_object(
+ closelog
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.closelog
+)
+
+add_entrypoint_object(
+ openlog
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.openlog
+)
+
+add_entrypoint_object(
+ setlogmask
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.setlogmask
+)
+
+add_entrypoint_object(
+ syslog
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.syslog
+)
+
+add_entrypoint_object(
+ vsyslog
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.vsyslog
+)
diff --git a/libc/src/syslog/closelog.h b/libc/src/syslog/closelog.h
new file mode 100644
index 0000000000000..457550531e7dc
--- /dev/null
+++ b/libc/src/syslog/closelog.h
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header for closelog.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SYSLOG_CLOSELOG_H
+#define LLVM_LIBC_SRC_SYSLOG_CLOSELOG_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+void closelog();
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SYSLOG_CLOSELOG_H
diff --git a/libc/src/syslog/linux/CMakeLists.txt b/libc/src/syslog/linux/CMakeLists.txt
new file mode 100644
index 0000000000000..0651a4a161433
--- /dev/null
+++ b/libc/src/syslog/linux/CMakeLists.txt
@@ -0,0 +1,93 @@
+add_object_library(
+ syslog_internal
+ SRCS
+ syslog_internal.cpp
+ HDRS
+ syslog_internal.h
+ DEPENDS
+ libc.hdr.syslog_macros
+ libc.hdr.time_macros
+ libc.hdr.types.pid_t
+ libc.hdr.types.size_t
+ libc.hdr.types.struct_timespec
+ libc.hdr.types.struct_tm
+ libc.src.__support.CPP.string_view
+ libc.src.__support.OSUtil.osutil
+ libc.src.__support.OSUtil.linux.syscall_wrappers.getpid
+ libc.src.__support.arg_list
+ libc.src.__support.integer_to_string
+ libc.src.__support.macros.attributes
+ libc.src.__support.macros.config
+ libc.src.__support.printf_core.core_structs
+ libc.src.__support.printf_core.printf_main
+ libc.src.__support.printf_core.writer
+ libc.src.__support.threads.mutex
+ libc.src.__support.time.clock_gettime
+ libc.src.errno.program_invocation_short_name
+ libc.src.time.strftime_core.strftime_main
+ libc.src.time.time_utils
+)
+
+add_entrypoint_object(
+ closelog
+ SRCS
+ closelog.cpp
+ HDRS
+ ../closelog.h
+ DEPENDS
+ .syslog_internal
+ libc.src.__support.common
+ libc.src.__support.macros.config
+)
+
+add_entrypoint_object(
+ openlog
+ SRCS
+ openlog.cpp
+ HDRS
+ ../openlog.h
+ DEPENDS
+ .syslog_internal
+ libc.src.__support.common
+ libc.src.__support.macros.config
+)
+
+add_entrypoint_object(
+ setlogmask
+ SRCS
+ setlogmask.cpp
+ HDRS
+ ../setlogmask.h
+ DEPENDS
+ .syslog_internal
+ libc.src.__support.common
+ libc.src.__support.macros.config
+)
+
+add_entrypoint_object(
+ syslog
+ SRCS
+ syslog.cpp
+ HDRS
+ ../syslog.h
+ DEPENDS
+ .syslog_internal
+ libc.src.__support.arg_list
+ libc.src.__support.common
+ libc.src.__support.macros.config
+ libc.src.__support.macros.null_check
+)
+
+add_entrypoint_object(
+ vsyslog
+ SRCS
+ vsyslog.cpp
+ HDRS
+ ../vsyslog.h
+ DEPENDS
+ .syslog_internal
+ libc.src.__support.arg_list
+ libc.src.__support.common
+ libc.src.__support.macros.config
+ libc.src.__support.macros.null_check
+)
diff --git a/libc/src/syslog/linux/closelog.cpp b/libc/src/syslog/linux/closelog.cpp
new file mode 100644
index 0000000000000..6b584a16f4c90
--- /dev/null
+++ b/libc/src/syslog/linux/closelog.cpp
@@ -0,0 +1,23 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation of closelog.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/syslog/closelog.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/syslog/linux/syslog_internal.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(void, closelog, ()) { syslog_internal::close_log(); }
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/syslog/linux/openlog.cpp b/libc/src/syslog/linux/openlog.cpp
new file mode 100644
index 0000000000000..ee823344dc7b7
--- /dev/null
+++ b/libc/src/syslog/linux/openlog.cpp
@@ -0,0 +1,26 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation of openlog.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/syslog/openlog.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/syslog/linux/syslog_internal.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(void, openlog,
+ (const char *ident, int option, int facility)) {
+ syslog_internal::open_log(ident, option, facility);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/syslog/linux/setlogmask.cpp b/libc/src/syslog/linux/setlogmask.cpp
new file mode 100644
index 0000000000000..8e5cac6dfe956
--- /dev/null
+++ b/libc/src/syslog/linux/setlogmask.cpp
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation of setlogmask.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/syslog/setlogmask.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/syslog/linux/syslog_internal.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, setlogmask, (int mask)) {
+ return syslog_internal::set_log_mask(mask);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/syslog/linux/syslog.cpp b/libc/src/syslog/linux/syslog.cpp
new file mode 100644
index 0000000000000..3948a53766057
--- /dev/null
+++ b/libc/src/syslog/linux/syslog.cpp
@@ -0,0 +1,34 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation of syslog.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/syslog/syslog.h"
+#include "src/__support/arg_list.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/null_check.h"
+#include "src/syslog/linux/syslog_internal.h"
+
+#include <stdarg.h>
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(void, syslog, (int priority, const char *format, ...)) {
+ LIBC_CRASH_ON_NULLPTR(format);
+ va_list vlist;
+ va_start(vlist, format);
+ internal::ArgList args(vlist);
+ va_end(vlist);
+ syslog_internal::log_message(priority, format, args);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/syslog/linux/syslog_internal.cpp b/libc/src/syslog/linux/syslog_internal.cpp
new file mode 100644
index 0000000000000..83382e3356f88
--- /dev/null
+++ b/libc/src/syslog/linux/syslog_internal.cpp
@@ -0,0 +1,149 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Linux implementation of internal state and formatting for syslog.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/syslog/linux/syslog_internal.h"
+
+#include "hdr/syslog_macros.h"
+#include "hdr/time_macros.h"
+#include "hdr/types/pid_t.h"
+#include "hdr/types/size_t.h"
+#include "hdr/types/struct_timespec.h"
+#include "hdr/types/struct_tm.h"
+#include "src/__support/CPP/string_view.h"
+#include "src/__support/OSUtil/io.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/getpid.h"
+#include "src/__support/arg_list.h"
+#include "src/__support/integer_to_string.h"
+#include "src/__support/macros/attributes.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/printf_core/core_structs.h"
+#include "src/__support/printf_core/printf_main.h"
+#include "src/__support/printf_core/writer.h"
+#include "src/__support/threads/mutex.h"
+#include "src/__support/time/clock_gettime.h"
+#include "src/errno/program_invocation_short_name.h"
+#include "src/time/strftime_core/strftime_main.h"
+#include "src/time/time_utils.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace syslog_internal {
+
+namespace {
+
+constexpr size_t BUFFER_SIZE = 1024;
+
+Mutex log_mutex(/*is_priority_inherit=*/false, /*is_recursive=*/false,
+ /*is_robust=*/false, /*is_pshared=*/false);
+LIBC_CONSTINIT detail::LogState log_state;
+
+LIBC_INLINE int stderr_write_hook(cpp::string_view str, void *) {
+ write_to_stderr(str);
+ return printf_core::WRITE_OK;
+}
+
+} // namespace
+
+namespace detail {
+
+bool write_log_message(
+ printf_core::Writer<printf_core::OverflowMode::CALLBACK> &writer,
+ const LogState &state, int priority, const char *__restrict format,
+ internal::ArgList &args, const tm ×tamp, pid_t pid,
+ const char *default_ident) {
+ if (!state.allows_priority(priority))
+ return false;
+ if ((priority & FACILITY_MASK) == 0)
+ priority |= state.facility;
+
+ const char *ident = state.ident;
+ if (ident == nullptr)
+ ident = default_ident;
+ if (ident == nullptr)
+ ident = "";
+
+ writer.write('<');
+ const IntegerToString<int> priority_str(priority);
+ writer.write(priority_str.view());
+ writer.write('>');
+
+ // RFC 3164 timestamp format: abbreviated month name, space-padded day of
+ // month, and 24-hour HH:MM:SS time
+ // (e.g. "Nov 14 01:02:03 " or "Jan 5 23:59:58 ").
+ if (!strftime_core::strftime_main(&writer, "%b %e %T ", ×tamp)
+ .has_value())
+ return false;
+
+ writer.write(ident);
+ if (state.options & LOG_PID) {
+ writer.write('[');
+ const IntegerToString<pid_t> pid_str(pid);
+ writer.write(pid_str.view());
+ writer.write(']');
+ }
+ writer.write(cpp::string_view(": "));
+
+ if (!printf_core::printf_main(&writer, format, args).has_value())
+ return false;
+
+ const auto &wb = writer.get_write_buffer();
+ if (wb.buff_cur == 0 || wb.buff[wb.buff_cur - 1] != '\n')
+ writer.write('\n');
+
+ return true;
+}
+
+} // namespace detail
+
+void open_log(const char *ident, int options, int facility) {
+ log_mutex.lock();
+ log_state.configure(ident, options, facility);
+ log_mutex.unlock();
+}
+
+void close_log() {
+ log_mutex.lock();
+ log_state.reset();
+ log_mutex.unlock();
+}
+
+int set_log_mask(int mask) {
+ log_mutex.lock();
+ int old_mask = log_state.update_mask(mask);
+ log_mutex.unlock();
+ return old_mask;
+}
+
+void log_message(int priority, const char *__restrict format,
+ internal::ArgList &args) {
+ log_mutex.lock();
+ if (log_state.allows_priority(priority)) {
+ timespec now = {};
+ internal::clock_gettime(CLOCK_REALTIME, &now);
+ tm local_time = {};
+ if (time_utils::localtime_internal(&now.tv_sec, &local_time).has_value()) {
+ pid_t pid = (log_state.options & LOG_PID) ? linux_syscalls::getpid() : 0;
+ char buffer[BUFFER_SIZE];
+ printf_core::Writer writer = printf_core::make_writer(
+ buffer, sizeof(buffer),
+ &printf_core::overflow_write_flush_to_sink<char, stderr_write_hook>);
+ if (detail::write_log_message(writer, log_state, priority, format, args,
+ local_time, pid,
+ program_invocation_short_name))
+ writer.get_write_buffer().flush_to_sink<stderr_write_hook>();
+ }
+ }
+ log_mutex.unlock();
+}
+
+} // namespace syslog_internal
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/syslog/linux/syslog_internal.h b/libc/src/syslog/linux/syslog_internal.h
new file mode 100644
index 0000000000000..e2ecce26f92dc
--- /dev/null
+++ b/libc/src/syslog/linux/syslog_internal.h
@@ -0,0 +1,89 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Internal state and formatting shared by the Linux syslog entrypoints.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SYSLOG_LINUX_SYSLOG_INTERNAL_H
+#define LLVM_LIBC_SRC_SYSLOG_LINUX_SYSLOG_INTERNAL_H
+
+#include "hdr/syslog_macros.h"
+#include "hdr/types/pid_t.h"
+#include "hdr/types/struct_tm.h"
+#include "src/__support/arg_list.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/printf_core/writer.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace syslog_internal {
+
+// Unlocked state and formatting helpers (exposed here for unit testing).
+// Callers operating on shared state must synchronize externally.
+namespace detail {
+
+inline constexpr int LEVEL_MASK = 0x07;
+inline constexpr int FACILITY_MASK = 0x3f8;
+
+struct LogState {
+ const char *ident = nullptr;
+ int options = 0;
+ int facility = LOG_USER;
+ int mask = LOG_UPTO(LOG_DEBUG);
+
+ LIBC_INLINE void configure(const char *new_ident, int new_options,
+ int new_facility) {
+ ident = new_ident;
+ options = new_options;
+ if (new_facility != 0)
+ facility = new_facility & FACILITY_MASK;
+ }
+
+ LIBC_INLINE void reset() {
+ ident = nullptr;
+ options = 0;
+ facility = LOG_USER;
+ }
+
+ LIBC_INLINE int update_mask(int new_mask) {
+ int old_mask = mask;
+ if (new_mask != 0)
+ mask = new_mask;
+ return old_mask;
+ }
+
+ LIBC_INLINE bool allows_priority(int priority) const {
+ return (mask & LOG_MASK(priority & LEVEL_MASK)) != 0;
+ }
+};
+
+// Formats a log message into `writer` if `priority` is permitted by `state`.
+// Uses `default_ident` when `state.ident` is null. Returns true if a message
+// was written, false if filtered or on error.
+bool write_log_message(
+ printf_core::Writer<printf_core::OverflowMode::CALLBACK> &writer,
+ const LogState &state, int priority, const char *__restrict format,
+ internal::ArgList &args, const tm ×tamp, pid_t pid,
+ const char *default_ident);
+
+} // namespace detail
+
+void open_log(const char *ident, int options, int facility);
+
+void close_log();
+
+int set_log_mask(int mask);
+
+void log_message(int priority, const char *__restrict format,
+ internal::ArgList &args);
+
+} // namespace syslog_internal
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SYSLOG_LINUX_SYSLOG_INTERNAL_H
diff --git a/libc/src/syslog/linux/vsyslog.cpp b/libc/src/syslog/linux/vsyslog.cpp
new file mode 100644
index 0000000000000..3c6dcf9686226
--- /dev/null
+++ b/libc/src/syslog/linux/vsyslog.cpp
@@ -0,0 +1,32 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation of vsyslog.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/syslog/vsyslog.h"
+#include "src/__support/arg_list.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/null_check.h"
+#include "src/syslog/linux/syslog_internal.h"
+
+#include <stdarg.h>
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(void, vsyslog,
+ (int priority, const char *format, va_list args)) {
+ LIBC_CRASH_ON_NULLPTR(format);
+ internal::ArgList arg_list(args);
+ syslog_internal::log_message(priority, format, arg_list);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/syslog/openlog.h b/libc/src/syslog/openlog.h
new file mode 100644
index 0000000000000..dc0b4258abecb
--- /dev/null
+++ b/libc/src/syslog/openlog.h
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header for openlog.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SYSLOG_OPENLOG_H
+#define LLVM_LIBC_SRC_SYSLOG_OPENLOG_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+void openlog(const char *ident, int option, int facility);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SYSLOG_OPENLOG_H
diff --git a/libc/src/syslog/setlogmask.h b/libc/src/syslog/setlogmask.h
new file mode 100644
index 0000000000000..c4a92798ee83f
--- /dev/null
+++ b/libc/src/syslog/setlogmask.h
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header for setlogmask.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SYSLOG_SETLOGMASK_H
+#define LLVM_LIBC_SRC_SYSLOG_SETLOGMASK_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int setlogmask(int mask);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SYSLOG_SETLOGMASK_H
diff --git a/libc/src/syslog/syslog.h b/libc/src/syslog/syslog.h
new file mode 100644
index 0000000000000..d29adda6e730e
--- /dev/null
+++ b/libc/src/syslog/syslog.h
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header for syslog.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SYSLOG_SYSLOG_H
+#define LLVM_LIBC_SRC_SYSLOG_SYSLOG_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+void syslog(int priority, const char *format, ...);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SYSLOG_SYSLOG_H
diff --git a/libc/src/syslog/vsyslog.h b/libc/src/syslog/vsyslog.h
new file mode 100644
index 0000000000000..2941087ce0a14
--- /dev/null
+++ b/libc/src/syslog/vsyslog.h
@@ -0,0 +1,27 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header for vsyslog.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SYSLOG_VSYSLOG_H
+#define LLVM_LIBC_SRC_SYSLOG_VSYSLOG_H
+
+#include "src/__support/macros/config.h"
+
+#include <stdarg.h>
+
+namespace LIBC_NAMESPACE_DECL {
+
+void vsyslog(int priority, const char *format, va_list args);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SYSLOG_VSYSLOG_H
diff --git a/libc/test/src/CMakeLists.txt b/libc/test/src/CMakeLists.txt
index d3588e181c8d4..8d31529e68165 100644
--- a/libc/test/src/CMakeLists.txt
+++ b/libc/test/src/CMakeLists.txt
@@ -63,4 +63,5 @@ add_subdirectory(pwd)
if(${LIBC_TARGET_OS} STREQUAL "linux")
add_subdirectory(pthread)
add_subdirectory(semaphore)
+ add_subdirectory(syslog)
endif()
diff --git a/libc/test/src/syslog/CMakeLists.txt b/libc/test/src/syslog/CMakeLists.txt
new file mode 100644
index 0000000000000..235f9b6d96c59
--- /dev/null
+++ b/libc/test/src/syslog/CMakeLists.txt
@@ -0,0 +1,5 @@
+add_libc_testsuite(libc-syslog-tests)
+
+if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+ add_subdirectory(${LIBC_TARGET_OS})
+endif()
diff --git a/libc/test/src/syslog/linux/CMakeLists.txt b/libc/test/src/syslog/linux/CMakeLists.txt
new file mode 100644
index 0000000000000..ea86c2be3670c
--- /dev/null
+++ b/libc/test/src/syslog/linux/CMakeLists.txt
@@ -0,0 +1,72 @@
+if(LIBC_CONF_PRINTF_DISABLE_STRERROR)
+ libc_add_definition(syslog_internal_test_copts
+ "LIBC_COPT_PRINTF_DISABLE_STRERROR")
+endif()
+
+add_libc_test(
+ syslog_internal_test
+ SUITE
+ libc-syslog-tests
+ SRCS
+ syslog_internal_test.cpp
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.hdr.syslog_macros
+ libc.hdr.types.pid_t
+ libc.hdr.types.size_t
+ libc.hdr.types.struct_tm
+ libc.src.__support.CPP.string_view
+ libc.src.__support.arg_list
+ libc.src.__support.libc_errno
+ libc.src.__support.printf_core.core_structs
+ libc.src.__support.printf_core.writer
+ libc.src.string.memory_utils.inline_memcpy
+ libc.src.syslog.linux.syslog_internal
+ COMPILE_OPTIONS
+ ${syslog_internal_test_copts}
+)
+
+add_libc_test(
+ setlogmask_test
+ SUITE
+ libc-syslog-tests
+ SRCS
+ setlogmask_test.cpp
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.hdr.syslog_macros
+ libc.src.__support.libc_errno
+ libc.src.syslog.setlogmask
+)
+
+add_libc_test(
+ openlog_syslog_closelog_test
+ SUITE
+ libc-syslog-tests
+ SRCS
+ openlog_syslog_closelog_test.cpp
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.hdr.syslog_macros
+ libc.src.__support.libc_errno
+ libc.src.syslog.closelog
+ libc.src.syslog.openlog
+ libc.src.syslog.setlogmask
+ libc.src.syslog.syslog
+)
+
+add_libc_test(
+ vsyslog_test
+ SUITE
+ libc-syslog-tests
+ SRCS
+ vsyslog_test.cpp
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.hdr.syslog_macros
+ libc.src.__support.libc_errno
+ libc.src.syslog.closelog
+ libc.src.syslog.openlog
+ libc.src.syslog.setlogmask
+ libc.src.syslog.vsyslog
+)
diff --git a/libc/test/src/syslog/linux/openlog_syslog_closelog_test.cpp b/libc/test/src/syslog/linux/openlog_syslog_closelog_test.cpp
new file mode 100644
index 0000000000000..c55c785504a1b
--- /dev/null
+++ b/libc/test/src/syslog/linux/openlog_syslog_closelog_test.cpp
@@ -0,0 +1,87 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for openlog, syslog and closelog.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "hdr/syslog_macros.h"
+#include "src/__support/libc_errno.h"
+#include "src/syslog/closelog.h"
+#include "src/syslog/openlog.h"
+#include "src/syslog/setlogmask.h"
+#include "src/syslog/syslog.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcOpenlogSyslogCloselogTest =
+ LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+TEST_F(LlvmLibcOpenlogSyslogCloselogTest, CloselogWithoutOpenlog) {
+ LIBC_NAMESPACE::closelog();
+}
+
+TEST_F(LlvmLibcOpenlogSyslogCloselogTest, OpenWithNdelayLogAndClose) {
+ LIBC_NAMESPACE::openlog("llvm-libc-test", LOG_PID | LOG_NDELAY, LOG_USER);
+ LIBC_NAMESPACE::syslog(LOG_DEBUG, "openlog_syslog_closelog_test %d", 1);
+ LIBC_NAMESPACE::closelog();
+}
+
+TEST_F(LlvmLibcOpenlogSyslogCloselogTest, SyslogWithoutOpenlog) {
+ LIBC_NAMESPACE::syslog(LOG_USER | LOG_DEBUG, "openlog_syslog_closelog_test");
+ LIBC_NAMESPACE::closelog();
+}
+
+TEST_F(LlvmLibcOpenlogSyslogCloselogTest, CloselogTwice) {
+ LIBC_NAMESPACE::openlog("llvm-libc-test", LOG_ODELAY, LOG_USER);
+ LIBC_NAMESPACE::closelog();
+ LIBC_NAMESPACE::closelog();
+}
+
+TEST_F(LlvmLibcOpenlogSyslogCloselogTest, OpenlogWithNullIdentAndZeroOptions) {
+ LIBC_NAMESPACE::openlog(nullptr, 0, 0);
+ LIBC_NAMESPACE::syslog(LOG_INFO, "null ident %s", "ok");
+ LIBC_NAMESPACE::closelog();
+}
+
+TEST_F(LlvmLibcOpenlogSyslogCloselogTest, OpenlogReconfigureAndOptions) {
+ LIBC_NAMESPACE::openlog("first-ident", LOG_ODELAY | LOG_NOWAIT, LOG_LOCAL0);
+ LIBC_NAMESPACE::openlog("second-ident", LOG_PID | LOG_CONS | LOG_NDELAY,
+ LOG_LOCAL1);
+ LIBC_NAMESPACE::syslog(LOG_DEBUG, "reconfigured");
+ LIBC_NAMESPACE::syslog(LOG_LOCAL2 | LOG_INFO, "explicit facility");
+ LIBC_NAMESPACE::closelog();
+}
+
+TEST_F(LlvmLibcOpenlogSyslogCloselogTest, MaskedPriorityAndMaskPersistence) {
+ int original = LIBC_NAMESPACE::setlogmask(LOG_MASK(LOG_EMERG));
+ LIBC_NAMESPACE::openlog("llvm-libc-test", LOG_ODELAY, LOG_USER);
+ ASSERT_EQ(LIBC_NAMESPACE::setlogmask(0), LOG_MASK(LOG_EMERG));
+ LIBC_NAMESPACE::syslog(LOG_DEBUG, "masked %m");
+ LIBC_NAMESPACE::syslog(LOG_LOCAL0 | LOG_DEBUG, "masked with facility");
+ LIBC_NAMESPACE::closelog();
+ ASSERT_EQ(LIBC_NAMESPACE::setlogmask(0), LOG_MASK(LOG_EMERG));
+ LIBC_NAMESPACE::setlogmask(original);
+}
+
+TEST_F(LlvmLibcOpenlogSyslogCloselogTest, PreservesErrno) {
+ libc_errno = EINVAL;
+ LIBC_NAMESPACE::openlog("llvm-libc-test", LOG_PID | LOG_ODELAY, LOG_USER);
+ ASSERT_EQ(static_cast<int>(libc_errno), EINVAL);
+ LIBC_NAMESPACE::syslog(LOG_DEBUG, "errno preserved: %m");
+ ASSERT_EQ(static_cast<int>(libc_errno), EINVAL);
+ LIBC_NAMESPACE::closelog();
+ ASSERT_ERRNO_EQ(EINVAL);
+}
+
+TEST_F(LlvmLibcOpenlogSyslogCloselogTest, NullFormatCrashes) {
+ EXPECT_DEATH([] { LIBC_NAMESPACE::syslog(LOG_DEBUG, nullptr); },
+ WITH_SIGNAL(-1));
+}
diff --git a/libc/test/src/syslog/linux/setlogmask_test.cpp b/libc/test/src/syslog/linux/setlogmask_test.cpp
new file mode 100644
index 0000000000000..5c97887373fb9
--- /dev/null
+++ b/libc/test/src/syslog/linux/setlogmask_test.cpp
@@ -0,0 +1,55 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for setlogmask.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "hdr/syslog_macros.h"
+#include "src/__support/libc_errno.h"
+#include "src/syslog/setlogmask.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcSetlogmaskTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+TEST_F(LlvmLibcSetlogmaskTest, DefaultAllowsAllPriorities) {
+ ASSERT_EQ(LIBC_NAMESPACE::setlogmask(0), LOG_UPTO(LOG_DEBUG));
+}
+
+TEST_F(LlvmLibcSetlogmaskTest, RoundTrip) {
+ int original = LIBC_NAMESPACE::setlogmask(LOG_UPTO(LOG_ERR));
+ ASSERT_EQ(LIBC_NAMESPACE::setlogmask(LOG_MASK(LOG_INFO)), LOG_UPTO(LOG_ERR));
+ int combined = LOG_MASK(LOG_EMERG) | LOG_MASK(LOG_INFO);
+ ASSERT_EQ(LIBC_NAMESPACE::setlogmask(combined), LOG_MASK(LOG_INFO));
+ ASSERT_EQ(LIBC_NAMESPACE::setlogmask(original), combined);
+ ASSERT_EQ(LIBC_NAMESPACE::setlogmask(0), original);
+}
+
+TEST_F(LlvmLibcSetlogmaskTest, ZeroLeavesMaskUnchanged) {
+ int original = LIBC_NAMESPACE::setlogmask(0);
+ ASSERT_EQ(LIBC_NAMESPACE::setlogmask(0), original);
+
+ int custom = LOG_MASK(LOG_ERR);
+ ASSERT_EQ(LIBC_NAMESPACE::setlogmask(custom), original);
+ ASSERT_EQ(LIBC_NAMESPACE::setlogmask(0), custom);
+ ASSERT_EQ(LIBC_NAMESPACE::setlogmask(0), custom);
+ ASSERT_EQ(LIBC_NAMESPACE::setlogmask(original), custom);
+}
+
+TEST_F(LlvmLibcSetlogmaskTest, PreservesErrno) {
+ libc_errno = EINVAL;
+ int original = LIBC_NAMESPACE::setlogmask(LOG_UPTO(LOG_ERR));
+ ASSERT_EQ(static_cast<int>(libc_errno), EINVAL);
+ ASSERT_EQ(LIBC_NAMESPACE::setlogmask(0), LOG_UPTO(LOG_ERR));
+ ASSERT_EQ(static_cast<int>(libc_errno), EINVAL);
+ ASSERT_EQ(LIBC_NAMESPACE::setlogmask(original), LOG_UPTO(LOG_ERR));
+ ASSERT_ERRNO_EQ(EINVAL);
+}
diff --git a/libc/test/src/syslog/linux/syslog_internal_test.cpp b/libc/test/src/syslog/linux/syslog_internal_test.cpp
new file mode 100644
index 0000000000000..1beaf67d056a6
--- /dev/null
+++ b/libc/test/src/syslog/linux/syslog_internal_test.cpp
@@ -0,0 +1,308 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for syslog_internal.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "hdr/syslog_macros.h"
+#include "hdr/types/pid_t.h"
+#include "hdr/types/size_t.h"
+#include "hdr/types/struct_tm.h"
+#include "src/__support/CPP/string_view.h"
+#include "src/__support/arg_list.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/printf_core/core_structs.h"
+#include "src/__support/printf_core/writer.h"
+#include "src/string/memory_utils/inline_memcpy.h"
+#include "src/syslog/linux/syslog_internal.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/Test.h"
+
+#include <stdarg.h>
+
+using LIBC_NAMESPACE::cpp::string_view;
+using LIBC_NAMESPACE::printf_core::overflow_write_flush_to_sink;
+using LIBC_NAMESPACE::syslog_internal::detail::LogState;
+
+struct CaptureBuffer {
+ char data[2048] = {};
+ size_t length = 0;
+
+ static int write_hook(string_view str, void *state) {
+ auto *capture = static_cast<CaptureBuffer *>(state);
+ size_t remaining = sizeof(capture->data) - capture->length;
+ size_t to_copy = str.size() < remaining ? str.size() : remaining;
+ LIBC_NAMESPACE::inline_memcpy(capture->data + capture->length, str.data(),
+ to_copy);
+ capture->length += to_copy;
+ return LIBC_NAMESPACE::printf_core::WRITE_OK;
+ }
+
+ void clear() { length = 0; }
+ string_view view() const { return string_view(data, length); }
+};
+
+static bool format_to_capture(const LogState &state, int priority,
+ const tm ×tamp, pid_t pid,
+ const char *default_ident, CaptureBuffer &capture,
+ const char *format, ...) {
+ capture.clear();
+ char scratch[64];
+ LIBC_NAMESPACE::printf_core::Writer writer =
+ LIBC_NAMESPACE::printf_core::make_writer(
+ scratch, sizeof(scratch),
+ &overflow_write_flush_to_sink<char, CaptureBuffer::write_hook>,
+ &capture);
+ va_list vlist;
+ va_start(vlist, format);
+ LIBC_NAMESPACE::internal::ArgList args(vlist);
+ va_end(vlist);
+ bool wrote = LIBC_NAMESPACE::syslog_internal::detail::write_log_message(
+ writer, state, priority, format, args, timestamp, pid, default_ident);
+ if (wrote)
+ writer.get_write_buffer().flush_to_sink<CaptureBuffer::write_hook>(
+ &capture);
+ return wrote;
+}
+
+using LlvmLibcSyslogInternalTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+TEST_F(LlvmLibcSyslogInternalTest, DefaultStateUsesLogUserAndDefaultIdent) {
+ LogState state;
+ CaptureBuffer capture;
+ tm time = {};
+ time.tm_year = 126;
+ time.tm_mon = 10;
+ time.tm_mday = 14;
+ time.tm_hour = 1;
+ time.tm_min = 2;
+ time.tm_sec = 3;
+
+ ASSERT_TRUE(format_to_capture(state, LOG_INFO, time, 1234, "myprog", capture,
+ "hello %d", 42));
+ ASSERT_EQ(capture.view(),
+ string_view("<14>Nov 14 01:02:03 myprog: hello 42\n"));
+}
+
+TEST_F(LlvmLibcSyslogInternalTest, ConfigureWithPidAndIdent) {
+ LogState state;
+ CaptureBuffer capture;
+ tm time = {};
+ time.tm_year = 126;
+ time.tm_mon = 10;
+ time.tm_mday = 14;
+ time.tm_hour = 1;
+ time.tm_min = 2;
+ time.tm_sec = 3;
+
+ state.configure("prog", LOG_PID, LOG_USER);
+ ASSERT_TRUE(format_to_capture(state, LOG_INFO, time, 1234, "ignored", capture,
+ "hello %d", 42));
+ ASSERT_EQ(capture.view(),
+ string_view("<14>Nov 14 01:02:03 prog[1234]: hello 42\n"));
+}
+
+TEST_F(LlvmLibcSyslogInternalTest, WithoutPidPadsSingleDigitDay) {
+ LogState state;
+ CaptureBuffer capture;
+ tm time = {};
+ time.tm_year = 126;
+ time.tm_mon = 0;
+ time.tm_mday = 5;
+ time.tm_hour = 23;
+ time.tm_min = 59;
+ time.tm_sec = 58;
+
+ state.configure("prog", 0, LOG_USER);
+ ASSERT_TRUE(format_to_capture(state, LOG_INFO, time, 1234, nullptr, capture,
+ "%s", "msg"));
+ ASSERT_EQ(capture.view(), string_view("<14>Jan 5 23:59:58 prog: msg\n"));
+}
+
+TEST_F(LlvmLibcSyslogInternalTest, FacilitySelectionAndOverride) {
+ LogState state;
+ CaptureBuffer capture;
+ tm time = {};
+ time.tm_year = 126;
+ time.tm_mon = 10;
+ time.tm_mday = 14;
+ time.tm_hour = 1;
+ time.tm_min = 2;
+ time.tm_sec = 3;
+
+ state.configure("prog", 0, LOG_LOCAL0);
+ ASSERT_TRUE(
+ format_to_capture(state, LOG_ERR, time, 0, nullptr, capture, "default"));
+ ASSERT_EQ(capture.view(),
+ string_view("<131>Nov 14 01:02:03 prog: default\n"));
+
+ // Explicit facility in priority overrides the openlog default.
+ ASSERT_TRUE(format_to_capture(state, LOG_LOCAL7 | LOG_DEBUG, time, 0, nullptr,
+ capture, "explicit"));
+ ASSERT_EQ(capture.view(),
+ string_view("<191>Nov 14 01:02:03 prog: explicit\n"));
+
+ // Passing facility == 0 preserves the current default facility.
+ state.configure("prog2", LOG_NOWAIT, 0);
+ ASSERT_TRUE(
+ format_to_capture(state, LOG_ERR, time, 0, nullptr, capture, "kept"));
+ ASSERT_EQ(capture.view(), string_view("<131>Nov 14 01:02:03 prog2: kept\n"));
+}
+
+TEST_F(LlvmLibcSyslogInternalTest, ResetClearsAttributesAndKeepsMask) {
+ LogState state;
+ CaptureBuffer capture;
+ tm time = {};
+ time.tm_year = 126;
+ time.tm_mon = 10;
+ time.tm_mday = 14;
+ time.tm_hour = 1;
+ time.tm_min = 2;
+ time.tm_sec = 3;
+
+ state.configure("prog", LOG_PID, LOG_LOCAL0);
+ ASSERT_EQ(state.update_mask(LOG_MASK(LOG_ERR)), LOG_UPTO(LOG_DEBUG));
+ state.reset();
+
+ ASSERT_EQ(state.update_mask(0), LOG_MASK(LOG_ERR));
+ ASSERT_TRUE(format_to_capture(state, LOG_ERR, time, 99, nullptr, capture,
+ "after close"));
+ ASSERT_EQ(capture.view(), string_view("<11>Nov 14 01:02:03 : after close\n"));
+}
+
+TEST_F(LlvmLibcSyslogInternalTest, UpdateMaskFiltersMessages) {
+ LogState state;
+ CaptureBuffer capture;
+ tm time = {};
+ time.tm_year = 126;
+ time.tm_mon = 10;
+ time.tm_mday = 14;
+ time.tm_hour = 1;
+ time.tm_min = 2;
+ time.tm_sec = 3;
+
+ state.configure("prog", 0, LOG_USER);
+ ASSERT_EQ(state.update_mask(LOG_UPTO(LOG_ERR)), LOG_UPTO(LOG_DEBUG));
+
+ ASSERT_FALSE(format_to_capture(state, LOG_DEBUG, time, 0, nullptr, capture,
+ "dropped"));
+ ASSERT_EQ(capture.length, size_t(0));
+ ASSERT_FALSE(format_to_capture(state, LOG_LOCAL0 | LOG_INFO, time, 0, nullptr,
+ capture, "also dropped"));
+ ASSERT_EQ(capture.length, size_t(0));
+
+ ASSERT_TRUE(format_to_capture(state, LOG_LOCAL0 | LOG_ERR, time, 0, nullptr,
+ capture, "delivered"));
+ ASSERT_EQ(capture.view(),
+ string_view("<131>Nov 14 01:02:03 prog: delivered\n"));
+}
+
+TEST_F(LlvmLibcSyslogInternalTest, PreservesExistingTrailingNewline) {
+ LogState state;
+ CaptureBuffer capture;
+ tm time = {};
+ time.tm_year = 126;
+ time.tm_mon = 10;
+ time.tm_mday = 14;
+ time.tm_hour = 1;
+ time.tm_min = 2;
+ time.tm_sec = 3;
+
+ state.configure("prog", 0, LOG_USER);
+ ASSERT_TRUE(format_to_capture(state, LOG_INFO, time, 0, nullptr, capture,
+ "already ended\n"));
+ ASSERT_EQ(capture.view(),
+ string_view("<14>Nov 14 01:02:03 prog: already ended\n"));
+}
+
+TEST_F(LlvmLibcSyslogInternalTest, FlushesMessagesLongerThanBuffer) {
+ LogState state;
+ CaptureBuffer capture;
+ tm time = {};
+ time.tm_year = 126;
+ time.tm_mon = 10;
+ time.tm_mday = 14;
+ time.tm_hour = 1;
+ time.tm_min = 2;
+ time.tm_sec = 3;
+
+ state.configure("prog", 0, LOG_USER);
+ ASSERT_TRUE(format_to_capture(state, LOG_INFO, time, 0, nullptr, capture,
+ "%120s", "end"));
+ ASSERT_EQ(capture.length, size_t(147));
+ ASSERT_EQ(capture.view().substr(0, 26),
+ string_view("<14>Nov 14 01:02:03 prog: "));
+ ASSERT_EQ(capture.view().substr(capture.length - 4), string_view("end\n"));
+}
+
+TEST_F(LlvmLibcSyslogInternalTest, EscapedPercentMIsLiteral) {
+ LogState state;
+ CaptureBuffer capture;
+ tm time = {};
+ time.tm_year = 126;
+ time.tm_mon = 10;
+ time.tm_mday = 14;
+ time.tm_hour = 1;
+ time.tm_min = 2;
+ time.tm_sec = 3;
+
+ state.configure("prog", 0, LOG_USER);
+ libc_errno = EINVAL;
+ ASSERT_TRUE(format_to_capture(state, LOG_INFO, time, 0, nullptr, capture,
+ "literal %%m"));
+ ASSERT_ERRNO_EQ(EINVAL);
+ ASSERT_EQ(capture.view(),
+ string_view("<14>Nov 14 01:02:03 prog: literal %m\n"));
+}
+
+#ifndef LIBC_COPT_PRINTF_DISABLE_STRERROR
+TEST_F(LlvmLibcSyslogInternalTest, PercentMFormatsErrnoAndConvertsNoArgs) {
+ LogState state;
+ CaptureBuffer capture;
+ tm time = {};
+ time.tm_year = 126;
+ time.tm_mon = 10;
+ time.tm_mday = 14;
+ time.tm_hour = 1;
+ time.tm_min = 2;
+ time.tm_sec = 3;
+
+ state.configure("prog", 0, LOG_USER);
+ libc_errno = EINVAL;
+ ASSERT_TRUE(format_to_capture(state, LOG_INFO, time, 0, nullptr, capture,
+ "%s: %m (%d) [%m]", "open", 7));
+ ASSERT_ERRNO_EQ(EINVAL);
+ ASSERT_EQ(capture.view(),
+ string_view("<14>Nov 14 01:02:03 prog: open: Invalid argument (7) "
+ "[Invalid argument]\n"));
+}
+
+TEST_F(LlvmLibcSyslogInternalTest, PercentMWithPositionalArguments) {
+ LogState state;
+ CaptureBuffer capture;
+ tm time = {};
+ time.tm_year = 126;
+ time.tm_mon = 10;
+ time.tm_mday = 14;
+ time.tm_hour = 1;
+ time.tm_min = 2;
+ time.tm_sec = 3;
+
+ state.configure("prog", 0, LOG_USER);
+ libc_errno = EINVAL;
+ ASSERT_TRUE(format_to_capture(state, LOG_INFO, time, 0, nullptr, capture,
+ "%2$d: %m (%1$s)", "open", 7));
+ ASSERT_ERRNO_EQ(EINVAL);
+ ASSERT_EQ(capture.view(),
+ string_view("<14>Nov 14 01:02:03 prog: 7: Invalid argument "
+ "(open)\n"));
+}
+#endif // LIBC_COPT_PRINTF_DISABLE_STRERROR
diff --git a/libc/test/src/syslog/linux/vsyslog_test.cpp b/libc/test/src/syslog/linux/vsyslog_test.cpp
new file mode 100644
index 0000000000000..7be96867fbf81
--- /dev/null
+++ b/libc/test/src/syslog/linux/vsyslog_test.cpp
@@ -0,0 +1,63 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for vsyslog.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "hdr/syslog_macros.h"
+#include "src/__support/libc_errno.h"
+#include "src/syslog/closelog.h"
+#include "src/syslog/openlog.h"
+#include "src/syslog/setlogmask.h"
+#include "src/syslog/vsyslog.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/Test.h"
+
+#include <stdarg.h>
+
+static void call_vsyslog(int priority, const char *format, ...) {
+ va_list args;
+ va_start(args, format);
+ LIBC_NAMESPACE::vsyslog(priority, format, args);
+ va_end(args);
+}
+
+using LlvmLibcVsyslogTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+TEST_F(LlvmLibcVsyslogTest, OpenLogAndClose) {
+ LIBC_NAMESPACE::openlog("llvm-libc-test", LOG_PID | LOG_NDELAY, LOG_USER);
+ call_vsyslog(LOG_DEBUG, "vsyslog_test %s %d", "msg", 42);
+ LIBC_NAMESPACE::closelog();
+}
+
+TEST_F(LlvmLibcVsyslogTest, WithoutOpenlog) {
+ call_vsyslog(LOG_USER | LOG_DEBUG, "vsyslog_test without openlog %d", 1);
+ LIBC_NAMESPACE::closelog();
+}
+
+TEST_F(LlvmLibcVsyslogTest, MaskedPriority) {
+ int original = LIBC_NAMESPACE::setlogmask(LOG_MASK(LOG_EMERG));
+ call_vsyslog(LOG_DEBUG, "masked %m");
+ LIBC_NAMESPACE::setlogmask(original);
+ LIBC_NAMESPACE::closelog();
+}
+
+TEST_F(LlvmLibcVsyslogTest, PreservesErrno) {
+ libc_errno = EINVAL;
+ call_vsyslog(LOG_DEBUG, "vsyslog errno: %m");
+ ASSERT_EQ(static_cast<int>(libc_errno), EINVAL);
+ LIBC_NAMESPACE::closelog();
+ ASSERT_ERRNO_EQ(EINVAL);
+}
+
+TEST_F(LlvmLibcVsyslogTest, NullFormatCrashes) {
+ EXPECT_DEATH([] { call_vsyslog(LOG_DEBUG, nullptr); }, WITH_SIGNAL(-1));
+}
More information about the libc-commits
mailing list