walnux/libs/libc/syslog/lib_setlogmask.c
Michal Lenc eeb4a0de83 setlogmask: fix setlogmask behavior according to POSIX standard
POSIX states "If the maskpri argument is 0, the current log mask is
not modified." The current implementation in NuttX doesn't
respect this and thus is in a clear violation with a strict POSIX
compliance rule in The Inviolable Principles of NuttX.

This commit therefore changes the behavior to the expected one. Passing
argument 0 doesn't change the current log mask, but just returns the
old one. Completely disabling logging at runtime is thus not possible,
but you may set the highest priority LOG_EMERG only to disable most of
the messages. Default can still be set to no logging with
CONFIG_SYSLOG_DEFAULT_MASK configuration option.

Signed-off-by: Michal Lenc <michallenc@seznam.cz>
2025-05-02 16:29:08 +02:00

88 lines
3.4 KiB
C

/****************************************************************************
* libs/libc/syslog/lib_setlogmask.c
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdint.h>
#include <syslog.h>
#include "syslog/syslog.h"
/****************************************************************************
* Public Data
****************************************************************************/
/* The currently enabled set of syslog priorities */
uint8_t g_syslog_mask = CONFIG_SYSLOG_DEFAULT_MASK;
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: setlogmask
*
* Description:
* The setlogmask() function sets the logmask and returns the previous
* mask. If the mask argument is 0, the current logmask is not modified.
*
* The SYSLOG priorities are: LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR,
* LOG_WARNING, LOG_NOTICE, LOG_INFO, and LOG_DEBUG. The bit corresponding
* to a priority p is LOG_MASK(p); LOG_UPTO(p) provides the mask of all
* priorities in the above list up to and including p.
*
* NOTE: setlogmask is not a thread-safe, re-entrant function. Concurrent
* use of setlogmask() will have undefined behavior.
*
* REVISIT: Per POSIX the syslog mask should be a per-process value but in
* NuttX, the scope of the mask is dependent on the nature of the build:
*
* Flat Build: There is one, global SYSLOG mask that controls all output.
* Protected Build: There are two SYSLOG masks. One within the kernel
* that controls only kernel output. And one in user-space that controls
* only user SYSLOG output.
* Kernel Build: The kernel build is compliant with the POSIX requirement:
* There will be one mask for each user process, controlling the SYSLOG
* output only form that process. There will be a separate mask
* accessible only in the kernel code to control kernel SYSLOG output.
*
****************************************************************************/
int setlogmask(int mask)
{
uint8_t oldmask;
oldmask = g_syslog_mask;
if (mask != 0)
{
/* If the mask argument is 0, the current logmask is not modified. */
g_syslog_mask = (uint8_t)mask;
}
return oldmask;
}