From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 62E1A43D49; Mon, 25 Mar 2024 21:55:31 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 29D7D40EDB; Mon, 25 Mar 2024 21:54:34 +0100 (CET) Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) by mails.dpdk.org (Postfix) with ESMTP id 9D41D40E36 for ; Mon, 25 Mar 2024 21:54:28 +0100 (CET) Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-1e0d82c529fso5676955ad.2 for ; Mon, 25 Mar 2024 13:54:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20230601.gappssmtp.com; s=20230601; t=1711400068; x=1712004868; darn=dpdk.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=BzCso9sKu6dCrjxH8A6EQo0nwJbP6WzCWG5/oETewgQ=; b=ZnfbW7hNVNsxdhVztvhGeX95AmCAu4RGNr/PbVX4a5YX1Od6vBznqshdclOnPoPcfh xBMzICjQ5lnYa0bpfCLTnM1GV9n40NAYLW7BGsNOl+45nYDCV6FVwEqy8auE3K9qhxrs QTUBHWh6ToZI/+WQIneegw4l1CTODJrKcrRcwUM0lZRANyTaKPPk3aE575oBjJN1gSs8 s79LyHGicbZnqzChyAaRonQsNKVYMAdHAfcuyFzieTllts2xkMVQo70W/i3pRGnsAUvg NV68HThmW5A3nnGltEtKtnI9UlR1tFx9xpmxe3fKD4Vh0jMmbi72ljq5YkXGQrx2f6lc CLlw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711400068; x=1712004868; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=BzCso9sKu6dCrjxH8A6EQo0nwJbP6WzCWG5/oETewgQ=; b=RA7nLY/z9+hQ+LChDP1W+2sS8gIy4HQfxpl3u4NjWtHVLKO53WbGTQR0qCNgLkP9rQ cs0aTLzVHubk+2JPFTVTL9q/P8A8FbRRF3YQujyEZfQQ0d2w0qIWNp6sCuygAX1qerLs /74S1zmSM4dsp1NHVqPzJp+LNdwLn9hb0GMh8wqZRGaUM92wb/uyVDG7iUD77sAtFevY kPDcY2ltfRwN1tpVyfNmO44N8dxk5U5O/g8I8Mh8EvNyAuhK+tLfy8wLPIU+qIywbEYn MDQznuzuV7Xfq+aAtUb+QEBYIKCzoa5LhMKjj5gVGnWQ4o1KlTG3X8OxSvMVlsc7tKnV Kb4g== X-Gm-Message-State: AOJu0YxFyrv9uIWcbw198MmzzWyO2AmNRyAIRWpaqORhQB7GXjU30Mi6 BpWO4DCekhd4wsLuv9quAZo3dtc+g7oB3L5zYPnkNiVgRYEc+BVU3SyeJ1zms51Wle3RBFgQ3QZ D X-Google-Smtp-Source: AGHT+IGU1NoqUXTfuqcSERumdWI47EVx+YEnr9woZqwfXZHt5gb5dGKdHn+tnvMM7FP8ImXVSsf/+w== X-Received: by 2002:a17:902:ea05:b0:1e0:a7c2:af8f with SMTP id s5-20020a170902ea0500b001e0a7c2af8fmr7633604plg.5.1711400067717; Mon, 25 Mar 2024 13:54:27 -0700 (PDT) Received: from hermes.local (204-195-123-203.wavecable.com. [204.195.123.203]) by smtp.gmail.com with ESMTPSA id b18-20020a170903229200b001e0b5eee802sm3164487plh.123.2024.03.25.13.54.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Mar 2024 13:54:27 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger Subject: [PATCH v12 11/14] log: add optional support of syslog Date: Mon, 25 Mar 2024 13:47:08 -0700 Message-ID: <20240325205405.669897-12-stephen@networkplumber.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240325205405.669897-1-stephen@networkplumber.org> References: <20200814173441.23086-1-stephen@networkplumber.org> <20240325205405.669897-1-stephen@networkplumber.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Log to syslog only if option is specified. And if syslog is used then normally only log to syslog, don't duplicate output. Also enables syslog support on FreeBSD. Signed-off-by: Stephen Hemminger --- doc/guides/linux_gsg/linux_eal_parameters.rst | 27 ----- doc/guides/prog_guide/log_lib.rst | 17 +++ lib/eal/common/eal_common_options.c | 2 +- lib/log/log.c | 101 ++++++++++++++++-- 4 files changed, 111 insertions(+), 36 deletions(-) diff --git a/doc/guides/linux_gsg/linux_eal_parameters.rst b/doc/guides/linux_gsg/linux_eal_parameters.rst index ea8f381391..d86f94d8a8 100644 --- a/doc/guides/linux_gsg/linux_eal_parameters.rst +++ b/doc/guides/linux_gsg/linux_eal_parameters.rst @@ -108,30 +108,3 @@ Memory-related options * ``--match-allocations`` Free hugepages back to system exactly as they were originally allocated. - -Other options -~~~~~~~~~~~~~ - -* ``--syslog `` - - Set syslog facility. Valid syslog facilities are:: - - auth - cron - daemon - ftp - kern - lpr - mail - news - syslog - user - uucp - local0 - local1 - local2 - local3 - local4 - local5 - local6 - local7 diff --git a/doc/guides/prog_guide/log_lib.rst b/doc/guides/prog_guide/log_lib.rst index 504eefe1d2..abaedc7212 100644 --- a/doc/guides/prog_guide/log_lib.rst +++ b/doc/guides/prog_guide/log_lib.rst @@ -83,6 +83,23 @@ To prefix all console messages with ISO format time the syntax is:: /path/to/app --log-timestamp=iso +Log output +~~~~~~~~~~ + +If desired, messages can be redirected to syslog (on Linux and FreeBSD) with the ``--syslog`` +option. There are three possible settings for this option: + +*always* + Redirect all log output to syslog. + +*auto* + Use console if it is a terminal, and use syslog if is not. + +*both* + Print to both console and syslog. + +If ``--syslog`` option is not specified, then only console (stderr) will be used. + Using Logging APIs to Generate Log Messages diff --git a/lib/eal/common/eal_common_options.c b/lib/eal/common/eal_common_options.c index aa1faad45c..6f0fd151c1 100644 --- a/lib/eal/common/eal_common_options.c +++ b/lib/eal/common/eal_common_options.c @@ -91,7 +91,7 @@ eal_long_options[] = { {OPT_PROC_TYPE, 1, NULL, OPT_PROC_TYPE_NUM }, {OPT_SOCKET_MEM, 1, NULL, OPT_SOCKET_MEM_NUM }, {OPT_SOCKET_LIMIT, 1, NULL, OPT_SOCKET_LIMIT_NUM }, - {OPT_SYSLOG, 1, NULL, OPT_SYSLOG_NUM }, + {OPT_SYSLOG, 2, NULL, OPT_SYSLOG_NUM }, {OPT_VDEV, 1, NULL, OPT_VDEV_NUM }, {OPT_VFIO_INTR, 1, NULL, OPT_VFIO_INTR_NUM }, {OPT_VFIO_VF_TOKEN, 1, NULL, OPT_VFIO_VF_TOKEN_NUM }, diff --git a/lib/log/log.c b/lib/log/log.c index 2dca91306e..d8974c66db 100644 --- a/lib/log/log.c +++ b/lib/log/log.c @@ -13,15 +13,17 @@ #include #include +#ifdef RTE_EXEC_ENV_WINDOWS +#include +#else +#include +#endif + #include #include #include "log_internal.h" -#ifdef RTE_EXEC_ENV_WINDOWS -#include -#endif - struct rte_log_dynamic_type { const char *name; uint32_t loglevel; @@ -36,14 +38,25 @@ enum eal_log_time_format { EAL_LOG_TIMESTAMP_ISO, }; +enum eal_log_syslog { + EAL_LOG_SYSLOG_NONE = 0, /* do not use syslog */ + EAL_LOG_SYSLOG_AUTO, /* use syslog only if not a terminal */ + EAL_LOG_SYSLOG_ALWAYS, /* always use syslog */ + EAL_LOG_SYSLOG_BOTH, /* log to both syslog and stderr */ +}; + typedef int (*log_print_t)(FILE *f, uint32_t level, const char *fmt, va_list ap); static int log_print(FILE *f, uint32_t level, const char *format, va_list ap); + /** The rte_log structure. */ static struct rte_logs { uint32_t type; /**< Bitfield with enabled logs. */ uint32_t level; /**< Log level. */ FILE *file; /**< Output file set by rte_openlog_stream, or NULL. */ +#ifndef RTE_EXEC_ENV_WINDOWS + enum eal_log_syslog syslog_opt; +#endif log_print_t print_func; enum eal_log_time_format time_format; @@ -532,9 +545,23 @@ rte_log(uint32_t level, uint32_t logtype, const char *format, ...) /* Placeholder */ int -eal_log_syslog(const char *mode __rte_unused) +eal_log_syslog(const char *str) { +#ifdef RTE_EXEC_ENV_WINDOWS + RTE_SET_USED(str); return -1; +#else + if (str == NULL || strcmp(str, "auto") == 0) + /* log to syslog only if stderr is not a terminal */ + rte_logs.syslog_opt = EAL_LOG_SYSLOG_AUTO; + else if (strcmp(str, "both") == 0) + rte_logs.syslog_opt = EAL_LOG_SYSLOG_BOTH; + else if (strcmp(str, "always") == 0) + rte_logs.syslog_opt = EAL_LOG_SYSLOG_ALWAYS; + else + return -1; + return 0; +#endif } /* Set the log timestamp format */ @@ -706,17 +733,75 @@ log_print_with_timestamp(FILE *f, uint32_t level, return log_print(f, level, format, ap); } +#ifndef RTE_EXEC_ENV_WINDOWS /* - * Called by rte_eal_init + * wrapper for log stream to put messages into syslog + * useful for cases like: + * rte_hex_dump(rte_get_log_stream(), ...) */ -void -eal_log_init(const char *id __rte_unused) +static ssize_t +syslog_log_write(__rte_unused void *c, const char *buf, size_t size) +{ + /* Syslog error levels are from 0 to 7, so subtract 1 to convert */ + syslog(rte_log_cur_msg_loglevel() - 1, "%.*s", (int)size, buf); + return size; +} + +static int +syslog_log_close(__rte_unused void *c) { + closelog(); + return 0; +} + +static cookie_io_functions_t syslog_log_func = { + .write = syslog_log_write, + .close = syslog_log_close, +}; +#endif + + +/* Choose how log output is directed */ +static void +log_output_selection(const char *id) +{ + RTE_SET_USED(id); + +#ifndef RTE_EXEC_ENV_WINDOWS + bool is_terminal = isatty(STDERR_FILENO); + + if (!(rte_logs.syslog_opt == EAL_LOG_SYSLOG_NONE || + (rte_logs.syslog_opt == EAL_LOG_SYSLOG_AUTO && is_terminal))) { + int flags = LOG_NDELAY | LOG_PID; + +#ifdef LOG_PERROR + if (rte_logs.syslog_opt == EAL_LOG_SYSLOG_BOTH) + flags |= LOG_PERROR; +#endif + openlog(id, flags, is_terminal ? LOG_USER : LOG_DAEMON); + + /* redirect other log messages to syslog as well */ + FILE *log_stream = fopencookie(NULL, "w", syslog_log_func); + if (log_stream != NULL) + default_log_stream = log_stream; + return; + } +#endif if (rte_logs.time_format != EAL_LOG_TIMESTAMP_NONE) rte_logs.print_func = log_print_with_timestamp; +} +/* + * Called by rte_eal_init + */ +void +eal_log_init(const char *id) +{ + rte_logs.print_func = log_print; default_log_stream = stderr; + log_output_selection(id); + #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG RTE_LOG(NOTICE, EAL, "Debug dataplane logs available - lower performance\n"); -- 2.43.0