nuttx-mirror/drivers/syslog/vsyslog.c

277 lines
7.7 KiB
C
Raw Normal View History

/****************************************************************************
* drivers/syslog/vsyslog.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 <stdio.h>
#include <syslog.h>
#include <errno.h>
#include <nuttx/arch.h>
#include <nuttx/init.h>
#include <nuttx/clock.h>
#include <nuttx/streams.h>
#include <nuttx/syslog/syslog.h>
#include "syslog.h"
/****************************************************************************
* Private Data
****************************************************************************/
#if defined(CONFIG_SYSLOG_COLOR_OUTPUT)
static FAR const char * const g_priority_color[] =
{
"\e[31;1;5m", /* LOG_EMERG, Red, Bold, Blinking */
"\e[31;1m", /* LOG_ALERT, Red, Bold */
"\e[31;1m", /* LOG_CRIT, Red, Bold */
"\e[31m", /* LOG_ERR, Red */
"\e[33m", /* LOG_WARNING, Yellow */
"\e[1m", /* LOG_NOTICE, Bold */
"", /* LOG_INFO, Normal */
"\e[2m", /* LOG_DEBUG, Dim */
};
#endif
#if defined(CONFIG_SYSLOG_PRIORITY)
static FAR const char * const g_priority_str[] =
{
"EMERG", "ALERT", "CRIT", "ERROR",
"WARN", "NOTICE", "INFO", "DEBUG"
};
#endif
/****************************************************************************
2016-06-20 03:59:43 +08:00
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: nx_vsyslog
*
* Description:
* nx_vsyslog() handles the system logging system calls. It is functionally
* equivalent to vsyslog() except that (1) the per-process priority
* filtering has already been performed and the va_list parameter is
* passed by reference. That is because the va_list is a structure in
* some compilers and passing of structures in the NuttX sycalls does
* not work.
2016-06-20 20:10:56 +08:00
*
****************************************************************************/
int nx_vsyslog(int priority, FAR const IPTR char *fmt, FAR va_list *ap)
{
struct lib_syslograwstream_s stream;
int ret = 0;
#ifdef CONFIG_SYSLOG_PROCESS_NAME
FAR struct tcb_s *tcb = nxsched_get_tcb(nxsched_gettid());
#endif
#ifdef CONFIG_SYSLOG_TIMESTAMP
struct timespec ts;
# if defined(CONFIG_SYSLOG_TIMESTAMP_FORMATTED)
struct tm tm;
2020-12-29 22:23:27 +08:00
char date_buf[CONFIG_SYSLOG_TIMESTAMP_BUFFER];
# endif
2020-12-29 22:23:27 +08:00
#endif
/* Wrap the low-level output in a stream object and let lib_vsprintf
* do the work.
*/
lib_syslograwstream_open(&stream);
#ifdef CONFIG_SYSLOG_TIMESTAMP
ts.tv_sec = 0;
ts.tv_nsec = 0;
# if defined(CONFIG_SYSLOG_TIMESTAMP_FORMATTED)
memset(&tm, 0, sizeof(tm));
# endif
/* Get the current time. Since debug output may be generated very early
* in the start-up sequence, hardware timer support may not yet be
* available.
*/
if (OSINIT_HW_READY())
{
# if defined(CONFIG_SYSLOG_TIMESTAMP_REALTIME)
2017-08-04 22:02:52 +08:00
/* Use CLOCK_REALTIME if so configured */
clock_gettime(CLOCK_REALTIME, &ts);
# else
/* Prefer monotonic when enabled, as it can be synchronized to
* RTC with clock_resynchronize.
*/
clock_gettime(CLOCK_MONOTONIC, &ts);
# endif
/* Prepend the message with the current time, if available */
# if defined(CONFIG_SYSLOG_TIMESTAMP_FORMATTED)
# if defined(CONFIG_SYSLOG_TIMESTAMP_LOCALTIME)
localtime_r(&ts.tv_sec, &tm);
# else
gmtime_r(&ts.tv_sec, &tm);
# endif
# endif
}
2020-12-29 22:23:27 +08:00
# if defined(CONFIG_SYSLOG_TIMESTAMP_FORMATTED)
date_buf[0] = '\0';
strftime(date_buf, CONFIG_SYSLOG_TIMESTAMP_BUFFER,
CONFIG_SYSLOG_TIMESTAMP_FORMAT, &tm);
# endif
#endif
#if defined(CONFIG_SYSLOG_COLOR_OUTPUT) || defined(CONFIG_SYSLOG_TIMESTAMP) || \
defined(CONFIG_SMP) || defined(CONFIG_SYSLOG_PROCESSID) || \
defined(CONFIG_SYSLOG_PRIORITY) || defined(CONFIG_SYSLOG_PREFIX) || \
defined(CONFIG_SYSLOG_PROCESS_NAME)
ret = lib_sprintf_internal(&stream.common,
#if defined(CONFIG_SYSLOG_COLOR_OUTPUT)
/* Reset the terminal style. */
"\e[0m"
#endif
#ifdef CONFIG_SYSLOG_TIMESTAMP
# if defined(CONFIG_SYSLOG_TIMESTAMP_FORMATTED)
# if defined(CONFIG_SYSLOG_TIMESTAMP_FORMAT_MICROSECOND)
"[%s.%06ld] "
# else
"[%s] "
# endif
# else
MacOs: fix the sim compile warning in MacOS CC: clk/clk_fixed_rate.c clk/clk_divider.c:177:14: warning: taking the absolute value of unsigned type 'unsigned int' has no effect [-Wabsolute-value] return abs(rate - now) < abs(rate - best); ^ clk/clk_divider.c:177:14: note: remove the call to 'abs' since unsigned values cannot be negative return abs(rate - now) < abs(rate - best); ^~~ clk/clk_divider.c:177:32: warning: taking the absolute value of unsigned type 'unsigned int' has no effect [-Wabsolute-value] return abs(rate - now) < abs(rate - best); ^ clk/clk_divider.c:177:32: note: remove the call to 'abs' since unsigned values cannot be negative return abs(rate - now) < abs(rate - best); ^~~ CC: mm_heap/mm_initialize.c 2 warnings generated. clk/clk.c:1324:11: warning: variable 'irqflags' is used uninitialized whenever 'if' condition is true [-Wsometimes-uninitialized] if (clk->parents == NULL) ^~~~~~~~~~~~~~~~~~~~ clk/clk.c:1341:19: note: uninitialized use occurs here clk_list_unlock(irqflags); ^~~~~~~~ clk/clk.c:1324:7: note: remove the 'if' if its condition is always false if (clk->parents == NULL) ^~~~~~~~~~~~~~~~~~~~~~~~~ clk/clk.c:1255:22: note: initialize the variable 'irqflags' to silence this warning irqstate_t irqflags; ^ = 0 CC: clk/clk_mux.c clk/clk_multiplier.c:110:14: warning: taking the absolute value of unsigned type 'unsigned int' has no effect [-Wabsolute-value] return abs(rate - new) < abs(rate - best); ^ clk/clk_multiplier.c:110:14: note: remove the call to 'abs' since unsigned values cannot be negative return abs(rate - new) < abs(rate - best); ^~~ clk/clk_multiplier.c:110:32: warning: taking the absolute value of unsigned type 'unsigned int' has no effect [-Wabsolute-value] return abs(rate - new) < abs(rate - best); ^ clk/clk_multiplier.c:110:32: note: remove the call to 'abs' since unsigned values cannot be negative return abs(rate - new) < abs(rate - best); ^~~ clk/clk_mux.c:47:14: warning: taking the absolute value of unsigned type 'unsigned int' has no effect [-Wabsolute-value] return abs(now - rate) < abs(best - rate); ^ clk/clk_mux.c:47:14: note: remove the call to 'abs' since unsigned values cannot be negative return abs(now - rate) < abs(best - rate); ^~~ clk/clk_mux.c:47:32: warning: taking the absolute value of unsigned type 'unsigned int' has no effect [-Wabsolute-value] return abs(now - rate) < abs(best - rate); ^ clk/clk_mux.c:47:32: note: remove the call to 'abs' since unsigned values cannot be negative return abs(now - rate) < abs(best - rate); ^~~ AS: sim/sim_fork_x86.S 2 warnings generated. 1 warning2 warnings generated. generated. iperf.c:325:14: warning: format specifies type 'uintmax_t' (aka 'unsigned long long') but the argument has type 'unsigned long' [-Wformat] now_len -last_len, ^~~~~~~~~~~~~~~~~ iperf.c:340:14: warning: format specifies type 'uintmax_t' (aka 'unsigned long long') but the argument has type 'uintmax_t' (aka 'unsigned long') [-Wformat] now_len, ^~~~~~~ CC: misc/rpmsgblk_server.c 2 warnings generated. :28: warning: format specifies type 'uintmax_t' (aka 'unsigned long long') but the argument has type 'uintmax_t' (aka 'unsigned long') [-Wformat] (uintmax_t)skip); ^~~~~~~~~~~~~~~ nsh_dbgcmds.c:473:20: warning: format specifies type 'uintmax_t' (aka 'unsigned long long') but the argument has type 'uintmax_t' (aka 'unsigned long') [-Wformat] (uintmax_t)position); ^~~~~~~~~~~~~~~~~~~ CC: nsh_main.c 2 warnings generated. return INTMAX_MIN; ~~~~~~ ^~~~~~~~~~ CC: nsh_system.c note: expanded from macro 'INTMAX_MIN' ^~~~~~~~~ /Users/vela/work/nuttx/include/stdint.h:65:41: note: expanded from macro 'INT64_MIN' ~~~~~~~~~~~^~~ inttypes/lib_strtoimax.c:103:37: warning: implicit conversion from 'long long' to 'intmax_t' (aka 'long') changes value from -9223372036854775808 to 0 [-Wconstant-conversion] return (accum == limit) ? INTMAX_MIN : -(intmax_t)accum; ~~~~~~ ^~~~~~~~~~ /Users/vela/work/nuttx/include/stdint.h:136:29: note: expanded from macro 'INTMAX_MIN' ^~~~~~~~~ /Users/vela/work/nuttx/include/stdint.h:65:41: note: expanded from macro 'INT64_MIN' ~~~~~~~~~~~^~~ inttypes/lib_strtoimax.c:106:17: warning: result of comparison of constant 9223372036854775807 with expression of type 'uintmax_t' (aka 'unsigned long') is always false [-Wtautological-constant-out-of-range-compare] if (accum > INTMAX_MAX) ~~~~~ ^ ~~~~~~~~~~ inttypes/lib_strtoimax.c:109:18: warning: implicit conversion from 'long long' to 'intmax_t' (aka 'long') changes value from 9223372036854775807 to -1 [-Wconstant-conversion] return INTMAX_MAX; ~~~~~~ ^~~~~~~~~~ /Users/vela/work/nuttx/include/stdint.h:137:29: note: expanded from macro 'INTMAX_MAX' ^~~~~~~~~ /Users/vela/work/nuttx/include/stdint.h:66:29: note: expanded from macro 'INT64_MAX' ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ /Users/vela/work/nuttx/include/arch/inttypes.h:35:23: note: expanded from macro 'INT64_C' ^~~~~~~ <scratch space>:12:1: note: expanded from here 9223372036854775807ll ^~~~~~~~~~~~~~~~~~~~~ syslog/vsyslog.c:212:32: warning: format specifies type 'uintmax_t' (aka 'unsigned long long') but the argument has type 'uintmax_t' (aka 'unsigned long') [-Wformat] , (uintmax_t)ts.tv_sec ^~~~~~~~~~~~~~~~~~~~ CC: iob/iob_free.c 4 warnings generated. 1 warning generated. CC: mqueue/mq_msgqalloc.c inttypes/lib_strtoumax.c:91:23: warning: implicit conversion from 'unsigned long long' to 'uintmax_t' (aka 'unsigned long') changes value from 18446744073709551615 to 4294967295 [-Wconstant-conversion] accum = UINTMAX_MAX; ~ ^~~~~~~~~~~ /Users/vela/work/nuttx/include/stdint.h:140:29: note: expanded from macro 'UINTMAX_MAX' ^~~~~~~~~~ /Users/vela/work/nuttx/include/stdint.h:67:29: note: expanded from macro 'UINT64_MAX' ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /Users/vela/work/nuttx/include/arch/inttypes.h:36:23: note: expanded from macro 'UINT64_C' ^~~~~~~~ <scratch space>:8:1: note: expanded from here 18446744073709551615ull ^~~~~~~~~~~~~~~~~~~~~~~ CC: icmp/icmp_ioctl.c 1 warning generated. time/lib_strftime.c:584:52: warning: format specifies type 'uintmax_t' (aka 'unsigned long long') but the argument has type 'uintmax_t' (aka 'unsigned long') [-Wformat] len = snprintf(dest, chleft, "%ju", (uintmax_t)mktime(&tmp)); ~~~ ^~~~~~~~~~~~~~~~~~~~~~~ %ju Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
2024-10-17 17:06:11 +08:00
"[%5ju.%06ld] "
# endif
#endif
#if defined(CONFIG_SMP)
"[CPU%d] "
#endif
#if defined(CONFIG_SYSLOG_PROCESSID)
/* Prepend the Thread ID */
"[%2d] "
#endif
#if defined(CONFIG_SYSLOG_COLOR_OUTPUT)
/* Set the terminal style according to message priority. */
"%s"
#endif
#if defined(CONFIG_SYSLOG_PRIORITY)
/* Prepend the message priority. */
"[%6s] "
#endif
#if defined(CONFIG_SYSLOG_PREFIX)
/* Prepend the prefix, if available */
"[%s] "
#endif
#ifdef CONFIG_SYSLOG_PROCESS_NAME
/* Prepend the thread name */
"%s: "
#endif
#ifdef CONFIG_SYSLOG_TIMESTAMP
# if defined(CONFIG_SYSLOG_TIMESTAMP_FORMATTED)
# if defined(CONFIG_SYSLOG_TIMESTAMP_FORMAT_MICROSECOND)
, date_buf, ts.tv_nsec / NSEC_PER_USEC
# else
, date_buf
# endif
# else
, (uintmax_t)ts.tv_sec
, ts.tv_nsec / NSEC_PER_USEC
# endif
#endif
#if defined(CONFIG_SMP)
, this_cpu()
#endif
#if defined(CONFIG_SYSLOG_PROCESSID)
/* Prepend the Thread ID */
, nxsched_gettid()
#endif
2021-01-16 22:20:49 +08:00
#if defined(CONFIG_SYSLOG_COLOR_OUTPUT)
/* Set the terminal style according to message priority. */
, g_priority_color[priority]
2021-01-16 22:20:49 +08:00
#endif
2021-01-18 17:38:41 +08:00
#if defined(CONFIG_SYSLOG_PRIORITY)
/* Prepend the message priority. */
2021-01-18 17:38:41 +08:00
, g_priority_str[priority]
2021-01-18 17:38:41 +08:00
#endif
#if defined(CONFIG_SYSLOG_PREFIX)
/* Prepend the prefix, if available */
, CONFIG_SYSLOG_PREFIX_STRING
#endif
#ifdef CONFIG_SYSLOG_PROCESS_NAME
/* Prepend the thread name */
, get_task_name(tcb)
#endif
);
#endif /* CONFIG_SYSLOG_COLOR_OUTPUT || CONFIG_SYSLOG_TIMESTAMP || ... */
/* Generate the output */
ret += lib_vsprintf_internal(&stream.common, fmt, *ap);
if (stream.last_ch != '\n')
{
lib_stream_putc(&stream.common, '\n');
ret++;
}
2021-01-16 22:20:49 +08:00
#if defined(CONFIG_SYSLOG_COLOR_OUTPUT)
/* Reset the terminal style back to normal. */
ret += lib_stream_puts(&stream.common, "\e[0m", sizeof("\e[0m"));
2021-01-16 22:20:49 +08:00
#endif
/* Flush and destroy the syslog stream buffer */
lib_syslograwstream_close(&stream);
return ret;
}