[libc-commits] [libc] [libc] Implement syslog functions (PR #244029)
Michael Jones via libc-commits
libc-commits at lists.llvm.org
Fri Nov 20 10:04:40 PST 2026
https://github.com/michaelrj-google updated https://github.com/llvm/llvm-project/pull/244029
>From 319280e9f3fcef6bd0ab2ef6fc56b9ada7529edf Mon Sep 17 00:00:00 2001
From: Michael Jones <michaelrj at google.com>
Date: Wed, 18 Nov 2026 22:31:15 +0000
Subject: [PATCH 1/5] [libc] Implement syslog functions
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.
---
libc/config/linux/aarch64/entrypoints.txt | 7 +
libc/config/linux/riscv/entrypoints.txt | 7 +
libc/config/linux/x86_64/entrypoints.txt | 7 +
libc/hdr/CMakeLists.txt | 8 +
libc/hdr/syslog_macros.h | 27 ++
libc/include/llvm-libc-macros/syslog-macros.h | 4 +
libc/include/syslog.yaml | 45 ++-
libc/src/CMakeLists.txt | 4 +
libc/src/syslog/CMakeLists.txt | 38 +++
libc/src/syslog/closelog.h | 25 ++
libc/src/syslog/linux/CMakeLists.txt | 93 ++++++
libc/src/syslog/linux/closelog.cpp | 23 ++
libc/src/syslog/linux/openlog.cpp | 26 ++
libc/src/syslog/linux/setlogmask.cpp | 25 ++
libc/src/syslog/linux/syslog.cpp | 34 ++
libc/src/syslog/linux/syslog_internal.cpp | 140 ++++++++
libc/src/syslog/linux/syslog_internal.h | 83 +++++
libc/src/syslog/linux/vsyslog.cpp | 32 ++
libc/src/syslog/openlog.h | 25 ++
libc/src/syslog/setlogmask.h | 25 ++
libc/src/syslog/syslog.h | 25 ++
libc/src/syslog/vsyslog.h | 27 ++
libc/test/src/CMakeLists.txt | 1 +
libc/test/src/syslog/CMakeLists.txt | 5 +
libc/test/src/syslog/linux/CMakeLists.txt | 72 ++++
.../linux/openlog_syslog_closelog_test.cpp | 87 +++++
.../test/src/syslog/linux/setlogmask_test.cpp | 55 ++++
.../src/syslog/linux/syslog_internal_test.cpp | 308 ++++++++++++++++++
libc/test/src/syslog/linux/vsyslog_test.cpp | 63 ++++
29 files changed, 1319 insertions(+), 2 deletions(-)
create mode 100644 libc/hdr/syslog_macros.h
create mode 100644 libc/src/syslog/CMakeLists.txt
create mode 100644 libc/src/syslog/closelog.h
create mode 100644 libc/src/syslog/linux/CMakeLists.txt
create mode 100644 libc/src/syslog/linux/closelog.cpp
create mode 100644 libc/src/syslog/linux/openlog.cpp
create mode 100644 libc/src/syslog/linux/setlogmask.cpp
create mode 100644 libc/src/syslog/linux/syslog.cpp
create mode 100644 libc/src/syslog/linux/syslog_internal.cpp
create mode 100644 libc/src/syslog/linux/syslog_internal.h
create mode 100644 libc/src/syslog/linux/vsyslog.cpp
create mode 100644 libc/src/syslog/openlog.h
create mode 100644 libc/src/syslog/setlogmask.h
create mode 100644 libc/src/syslog/syslog.h
create mode 100644 libc/src/syslog/vsyslog.h
create mode 100644 libc/test/src/syslog/CMakeLists.txt
create mode 100644 libc/test/src/syslog/linux/CMakeLists.txt
create mode 100644 libc/test/src/syslog/linux/openlog_syslog_closelog_test.cpp
create mode 100644 libc/test/src/syslog/linux/setlogmask_test.cpp
create mode 100644 libc/test/src/syslog/linux/syslog_internal_test.cpp
create mode 100644 libc/test/src/syslog/linux/vsyslog_test.cpp
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 4dbf3fab75ff43..99678f528cda26 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 be15b84a24d7b3..abe25295b90e6f 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 4b7891ccb16911..2ab819aaa860f5 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 bc873756ec836d..ec2a47ad9694f6 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 00000000000000..7fbb3851463bae
--- /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 688127a67eae1e..b917c6c35bc0ee 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 2452b22ad506bb..aca6f7d9c440ec 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 8805fc196af4a1..08a23e715ca479 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 00000000000000..fc96908fd45898
--- /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 00000000000000..457550531e7dcb
--- /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 00000000000000..0651a4a161433e
--- /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 00000000000000..6b584a16f4c903
--- /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 00000000000000..ee823344dc7b76
--- /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 00000000000000..8e5cac6dfe9563
--- /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 00000000000000..3948a537660573
--- /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 00000000000000..7f397245ba41e3
--- /dev/null
+++ b/libc/src/syslog/linux/syslog_internal.cpp
@@ -0,0 +1,140 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 {
+
+static constexpr size_t BUFFER_SIZE = 1024;
+
+static Mutex log_mutex(/*is_priority_inherit=*/false, /*is_recursive=*/false,
+ /*is_robust=*/false, /*is_pshared=*/false);
+LIBC_CONSTINIT static LogState log_state;
+
+LIBC_INLINE int stderr_write_hook(cpp::string_view str, void *) {
+ write_to_stderr(str);
+ return printf_core::WRITE_OK;
+}
+
+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 *timestamp, 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 ", timestamp)
+ .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;
+}
+
+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 (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 00000000000000..d690470f640836
--- /dev/null
+++ b/libc/src/syslog/linux/syslog_internal.h
@@ -0,0 +1,83 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 {
+
+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 *timestamp, pid_t pid,
+ const char *default_ident);
+
+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 00000000000000..3c6dcf9686226e
--- /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 00000000000000..dc0b4258abecbb
--- /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 00000000000000..c4a92798ee83ff
--- /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 00000000000000..d29adda6e730e5
--- /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 00000000000000..2941087ce0a145
--- /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 d3588e181c8d40..8d31529e681658 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 00000000000000..235f9b6d96c59b
--- /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 00000000000000..c8a708259a494f
--- /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 00000000000000..c55c785504a1ba
--- /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 00000000000000..5c97887373fb98
--- /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 00000000000000..94639fdba9c4e7
--- /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::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 *timestamp, 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::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 00000000000000..7be96867fbf81b
--- /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));
+}
>From 9b1f0eef49d8e4f8e34f72bddcd66d6190d93900 Mon Sep 17 00:00:00 2001
From: Michael Jones <michaelrj at google.com>
Date: Wed, 18 Nov 2026 22:42:05 +0000
Subject: [PATCH 2/5] format cmake
---
libc/test/src/syslog/linux/CMakeLists.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/test/src/syslog/linux/CMakeLists.txt b/libc/test/src/syslog/linux/CMakeLists.txt
index c8a708259a494f..ea86c2be3670c8 100644
--- a/libc/test/src/syslog/linux/CMakeLists.txt
+++ b/libc/test/src/syslog/linux/CMakeLists.txt
@@ -1,6 +1,6 @@
if(LIBC_CONF_PRINTF_DISABLE_STRERROR)
libc_add_definition(syslog_internal_test_copts
- "LIBC_COPT_PRINTF_DISABLE_STRERROR")
+ "LIBC_COPT_PRINTF_DISABLE_STRERROR")
endif()
add_libc_test(
>From 14a75d86f89b863177939c094a920ed647a3a14c Mon Sep 17 00:00:00 2001
From: Michael Jones <michaelrj at google.com>
Date: Fri, 20 Nov 2026 03:27:45 +0000
Subject: [PATCH 3/5] address comments
---
libc/src/syslog/linux/syslog_internal.cpp | 25 +++++++----
libc/src/syslog/linux/syslog_internal.h | 8 +++-
.../src/syslog/linux/syslog_internal_test.cpp | 42 +++++++++----------
3 files changed, 45 insertions(+), 30 deletions(-)
diff --git a/libc/src/syslog/linux/syslog_internal.cpp b/libc/src/syslog/linux/syslog_internal.cpp
index 7f397245ba41e3..670833693142d5 100644
--- a/libc/src/syslog/linux/syslog_internal.cpp
+++ b/libc/src/syslog/linux/syslog_internal.cpp
@@ -38,21 +38,27 @@
namespace LIBC_NAMESPACE_DECL {
namespace syslog_internal {
-static constexpr size_t BUFFER_SIZE = 1024;
+namespace {
-static Mutex log_mutex(/*is_priority_inherit=*/false, /*is_recursive=*/false,
- /*is_robust=*/false, /*is_pshared=*/false);
-LIBC_CONSTINIT static LogState log_state;
+constexpr size_t BUFFER_SIZE = 1024;
+
+Mutex log_mutex(/*is_priority_inherit=*/false, /*is_recursive=*/false,
+ /*is_robust=*/false, /*is_pshared=*/false);
+LIBC_CONSTINIT internal::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 internal {
+
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 *timestamp, pid_t pid,
+ internal::ArgList &args, const tm ×tamp, pid_t pid,
const char *default_ident) {
if (!state.allows_priority(priority))
return false;
@@ -73,7 +79,7 @@ bool write_log_message(
// 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 ", timestamp)
+ if (!strftime_core::strftime_main(&writer, "%b %e %T ", ×tamp)
.has_value())
return false;
@@ -96,6 +102,8 @@ bool write_log_message(
return true;
}
+} // namespace internal
+
void open_log(const char *ident, int options, int facility) {
log_mutex.lock();
log_state.configure(ident, options, facility);
@@ -128,8 +136,9 @@ void log_message(int priority, const char *__restrict format,
printf_core::Writer writer = printf_core::make_writer(
buffer, sizeof(buffer),
&printf_core::overflow_write_flush_to_sink<char, stderr_write_hook>);
- if (write_log_message(writer, log_state, priority, format, args,
- &local_time, pid, program_invocation_short_name))
+ if (internal::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>();
}
}
diff --git a/libc/src/syslog/linux/syslog_internal.h b/libc/src/syslog/linux/syslog_internal.h
index d690470f640836..afdb1d0ba76443 100644
--- a/libc/src/syslog/linux/syslog_internal.h
+++ b/libc/src/syslog/linux/syslog_internal.h
@@ -24,6 +24,10 @@
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 internal {
+
inline constexpr int LEVEL_MASK = 0x07;
inline constexpr int FACILITY_MASK = 0x3f8;
@@ -65,9 +69,11 @@ struct LogState {
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 *timestamp, pid_t pid,
+ internal::ArgList &args, const tm ×tamp, pid_t pid,
const char *default_ident);
+} // namespace internal
+
void open_log(const char *ident, int options, int facility);
void close_log();
diff --git a/libc/test/src/syslog/linux/syslog_internal_test.cpp b/libc/test/src/syslog/linux/syslog_internal_test.cpp
index 94639fdba9c4e7..6c196359f6a2ae 100644
--- a/libc/test/src/syslog/linux/syslog_internal_test.cpp
+++ b/libc/test/src/syslog/linux/syslog_internal_test.cpp
@@ -30,7 +30,7 @@
using LIBC_NAMESPACE::cpp::string_view;
using LIBC_NAMESPACE::printf_core::overflow_write_flush_to_sink;
-using LIBC_NAMESPACE::syslog_internal::LogState;
+using LIBC_NAMESPACE::syslog_internal::internal::LogState;
struct CaptureBuffer {
char data[2048] = {};
@@ -51,7 +51,7 @@ struct CaptureBuffer {
};
static bool format_to_capture(const LogState &state, int priority,
- const tm *timestamp, pid_t pid,
+ const tm ×tamp, pid_t pid,
const char *default_ident, CaptureBuffer &capture,
const char *format, ...) {
capture.clear();
@@ -65,7 +65,7 @@ static bool format_to_capture(const LogState &state, int priority,
va_start(vlist, format);
LIBC_NAMESPACE::internal::ArgList args(vlist);
va_end(vlist);
- bool wrote = LIBC_NAMESPACE::syslog_internal::write_log_message(
+ bool wrote = LIBC_NAMESPACE::syslog_internal::internal::write_log_message(
writer, state, priority, format, args, timestamp, pid, default_ident);
if (wrote)
writer.get_write_buffer().flush_to_sink<CaptureBuffer::write_hook>(
@@ -86,7 +86,7 @@ TEST_F(LlvmLibcSyslogInternalTest, DefaultStateUsesLogUserAndDefaultIdent) {
time.tm_min = 2;
time.tm_sec = 3;
- ASSERT_TRUE(format_to_capture(state, LOG_INFO, &time, 1234, "myprog", capture,
+ 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"));
@@ -104,8 +104,8 @@ TEST_F(LlvmLibcSyslogInternalTest, ConfigureWithPidAndIdent) {
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_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"));
}
@@ -122,7 +122,7 @@ TEST_F(LlvmLibcSyslogInternalTest, WithoutPidPadsSingleDigitDay) {
time.tm_sec = 58;
state.configure("prog", 0, LOG_USER);
- ASSERT_TRUE(format_to_capture(state, LOG_INFO, &time, 1234, nullptr, capture,
+ 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"));
}
@@ -140,20 +140,20 @@ TEST_F(LlvmLibcSyslogInternalTest, FacilitySelectionAndOverride) {
state.configure("prog", 0, LOG_LOCAL0);
ASSERT_TRUE(
- format_to_capture(state, LOG_ERR, &time, 0, nullptr, capture, "default"));
+ 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_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"));
+ 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"));
}
@@ -173,7 +173,7 @@ TEST_F(LlvmLibcSyslogInternalTest, ResetClearsAttributesAndKeepsMask) {
state.reset();
ASSERT_EQ(state.update_mask(0), LOG_MASK(LOG_ERR));
- ASSERT_TRUE(format_to_capture(state, LOG_ERR, &time, 99, nullptr, capture,
+ 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"));
}
@@ -192,14 +192,14 @@ TEST_F(LlvmLibcSyslogInternalTest, UpdateMaskFiltersMessages) {
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,
+ 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_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,
+ 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"));
@@ -217,7 +217,7 @@ TEST_F(LlvmLibcSyslogInternalTest, PreservesExistingTrailingNewline) {
time.tm_sec = 3;
state.configure("prog", 0, LOG_USER);
- ASSERT_TRUE(format_to_capture(state, LOG_INFO, &time, 0, nullptr, capture,
+ 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"));
@@ -235,7 +235,7 @@ TEST_F(LlvmLibcSyslogInternalTest, FlushesMessagesLongerThanBuffer) {
time.tm_sec = 3;
state.configure("prog", 0, LOG_USER);
- ASSERT_TRUE(format_to_capture(state, LOG_INFO, &time, 0, nullptr, capture,
+ 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),
@@ -256,7 +256,7 @@ TEST_F(LlvmLibcSyslogInternalTest, EscapedPercentMIsLiteral) {
state.configure("prog", 0, LOG_USER);
libc_errno = EINVAL;
- ASSERT_TRUE(format_to_capture(state, LOG_INFO, &time, 0, nullptr, capture,
+ ASSERT_TRUE(format_to_capture(state, LOG_INFO, time, 0, nullptr, capture,
"literal %%m"));
ASSERT_ERRNO_EQ(EINVAL);
ASSERT_EQ(capture.view(),
@@ -277,7 +277,7 @@ TEST_F(LlvmLibcSyslogInternalTest, PercentMFormatsErrnoAndConvertsNoArgs) {
state.configure("prog", 0, LOG_USER);
libc_errno = EINVAL;
- ASSERT_TRUE(format_to_capture(state, LOG_INFO, &time, 0, nullptr, capture,
+ 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(),
@@ -298,7 +298,7 @@ TEST_F(LlvmLibcSyslogInternalTest, PercentMWithPositionalArguments) {
state.configure("prog", 0, LOG_USER);
libc_errno = EINVAL;
- ASSERT_TRUE(format_to_capture(state, LOG_INFO, &time, 0, nullptr, capture,
+ 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(),
>From 9840916722613cb770481a357c25c1615c741884 Mon Sep 17 00:00:00 2001
From: Michael Jones <michaelrj at google.com>
Date: Fri, 20 Nov 2026 03:32:36 +0000
Subject: [PATCH 4/5] format
---
libc/src/syslog/linux/syslog_internal.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/libc/src/syslog/linux/syslog_internal.cpp b/libc/src/syslog/linux/syslog_internal.cpp
index 670833693142d5..4b61d20a5826e6 100644
--- a/libc/src/syslog/linux/syslog_internal.cpp
+++ b/libc/src/syslog/linux/syslog_internal.cpp
@@ -137,8 +137,8 @@ void log_message(int priority, const char *__restrict format,
buffer, sizeof(buffer),
&printf_core::overflow_write_flush_to_sink<char, stderr_write_hook>);
if (internal::write_log_message(writer, log_state, priority, format, args,
- local_time, pid,
- program_invocation_short_name))
+ local_time, pid,
+ program_invocation_short_name))
writer.get_write_buffer().flush_to_sink<stderr_write_hook>();
}
}
>From 4a1f3a5a53221bfb3f9be4a14252375eb17e3d40 Mon Sep 17 00:00:00 2001
From: Michael Jones <michaelrj at google.com>
Date: Fri, 20 Nov 2026 18:03:44 +0000
Subject: [PATCH 5/5] rename from internal to detail
It caused an issue by overlapping with the internal namespace ArgList is
in.
---
libc/src/syslog/linux/syslog_internal.cpp | 12 ++++++------
libc/src/syslog/linux/syslog_internal.h | 4 ++--
libc/test/src/syslog/linux/syslog_internal_test.cpp | 4 ++--
3 files changed, 10 insertions(+), 10 deletions(-)
diff --git a/libc/src/syslog/linux/syslog_internal.cpp b/libc/src/syslog/linux/syslog_internal.cpp
index 4b61d20a5826e6..83382e3356f889 100644
--- a/libc/src/syslog/linux/syslog_internal.cpp
+++ b/libc/src/syslog/linux/syslog_internal.cpp
@@ -44,7 +44,7 @@ constexpr size_t BUFFER_SIZE = 1024;
Mutex log_mutex(/*is_priority_inherit=*/false, /*is_recursive=*/false,
/*is_robust=*/false, /*is_pshared=*/false);
-LIBC_CONSTINIT internal::LogState log_state;
+LIBC_CONSTINIT detail::LogState log_state;
LIBC_INLINE int stderr_write_hook(cpp::string_view str, void *) {
write_to_stderr(str);
@@ -53,7 +53,7 @@ LIBC_INLINE int stderr_write_hook(cpp::string_view str, void *) {
} // namespace
-namespace internal {
+namespace detail {
bool write_log_message(
printf_core::Writer<printf_core::OverflowMode::CALLBACK> &writer,
@@ -102,7 +102,7 @@ bool write_log_message(
return true;
}
-} // namespace internal
+} // namespace detail
void open_log(const char *ident, int options, int facility) {
log_mutex.lock();
@@ -136,9 +136,9 @@ void log_message(int priority, const char *__restrict format,
printf_core::Writer writer = printf_core::make_writer(
buffer, sizeof(buffer),
&printf_core::overflow_write_flush_to_sink<char, stderr_write_hook>);
- if (internal::write_log_message(writer, log_state, priority, format, args,
- local_time, pid,
- program_invocation_short_name))
+ 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>();
}
}
diff --git a/libc/src/syslog/linux/syslog_internal.h b/libc/src/syslog/linux/syslog_internal.h
index afdb1d0ba76443..e2ecce26f92dc0 100644
--- a/libc/src/syslog/linux/syslog_internal.h
+++ b/libc/src/syslog/linux/syslog_internal.h
@@ -26,7 +26,7 @@ namespace syslog_internal {
// Unlocked state and formatting helpers (exposed here for unit testing).
// Callers operating on shared state must synchronize externally.
-namespace internal {
+namespace detail {
inline constexpr int LEVEL_MASK = 0x07;
inline constexpr int FACILITY_MASK = 0x3f8;
@@ -72,7 +72,7 @@ bool write_log_message(
internal::ArgList &args, const tm ×tamp, pid_t pid,
const char *default_ident);
-} // namespace internal
+} // namespace detail
void open_log(const char *ident, int options, int facility);
diff --git a/libc/test/src/syslog/linux/syslog_internal_test.cpp b/libc/test/src/syslog/linux/syslog_internal_test.cpp
index 6c196359f6a2ae..1beaf67d056a62 100644
--- a/libc/test/src/syslog/linux/syslog_internal_test.cpp
+++ b/libc/test/src/syslog/linux/syslog_internal_test.cpp
@@ -30,7 +30,7 @@
using LIBC_NAMESPACE::cpp::string_view;
using LIBC_NAMESPACE::printf_core::overflow_write_flush_to_sink;
-using LIBC_NAMESPACE::syslog_internal::internal::LogState;
+using LIBC_NAMESPACE::syslog_internal::detail::LogState;
struct CaptureBuffer {
char data[2048] = {};
@@ -65,7 +65,7 @@ static bool format_to_capture(const LogState &state, int priority,
va_start(vlist, format);
LIBC_NAMESPACE::internal::ArgList args(vlist);
va_end(vlist);
- bool wrote = LIBC_NAMESPACE::syslog_internal::internal::write_log_message(
+ 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>(
More information about the libc-commits
mailing list