diff --git a/src/core/out.c b/src/core/out.c index 4d220a64358..4b5ea1ccc94 100644 --- a/src/core/out.c +++ b/src/core/out.c @@ -311,8 +311,6 @@ out_common(const char *file, int line, const char *func, int level, const char *sep = ""; char errstr[UTIL_MAX_ERR_MSG] = ""; - unsigned long olast_error = 0; - if (file) { char *f = strrchr(file, OS_DIR_SEPARATOR); if (f) @@ -335,15 +333,7 @@ out_common(const char *file, int line, const char *func, int level, if (*fmt == '!') { sep = ": "; fmt++; - if (*fmt == '!') { - fmt++; - /* it will abort on non Windows OS */ - util_strwinerror(olast_error, errstr, - UTIL_MAX_ERR_MSG); - } else { - util_strerror(oerrno, errstr, UTIL_MAX_ERR_MSG); - } - + util_strerror(oerrno, errstr, UTIL_MAX_ERR_MSG); } ret = vsnprintf(&buf[cc], MAXPRINT - cc, fmt, ap); if (ret < 0) { @@ -365,11 +355,12 @@ out_common(const char *file, int line, const char *func, int level, * out_error -- common error output code, all error messages go through here */ static void -out_error(const char *file, int line, const char *func, +out_error(int use_errno, const char *file, int line, const char *func, const char *suffix, const char *fmt, va_list ap) { - int oerrno = errno; - unsigned long olast_error = 0; + int oerrno = 0; + if (use_errno) + oerrno = errno; unsigned cc = 0; unsigned print_msg = 1; int ret; @@ -392,17 +383,9 @@ out_error(const char *file, int line, const char *func, fmt++; } - if (*fmt == '!') { + if (use_errno) { sep = ": "; - fmt++; - if (*fmt == '!') { - fmt++; - /* it will abort on non Windows OS */ - util_strwinerror(olast_error, errstr, - UTIL_MAX_ERR_MSG); - } else { - util_strerror(oerrno, errstr, UTIL_MAX_ERR_MSG); - } + util_strerror(oerrno, errstr, UTIL_MAX_ERR_MSG); } ret = vsnprintf(&errormsg[cc], MAXPRINT, fmt, ap); @@ -449,7 +432,8 @@ out_error(const char *file, int line, const char *func, #endif end: - errno = oerrno; + if (use_errno) + errno = oerrno; } /* @@ -528,13 +512,13 @@ out_fatal(const char *file, int line, const char *func, * out_err -- output an error message */ void -out_err(const char *file, int line, const char *func, +out_err(int use_errno, const char *file, int line, const char *func, const char *fmt, ...) { va_list ap; va_start(ap, fmt); - out_error(file, line, func, "\n", fmt, ap); + out_error(use_errno, file, line, func, "\n", fmt, ap); va_end(ap); } diff --git a/src/core/out.h b/src/core/out.h index 22d9b520a2f..c0914ac2c1c 100644 --- a/src/core/out.h +++ b/src/core/out.h @@ -184,18 +184,18 @@ out_fatal_abort(const char *file, int line, const char *func, ASSERTne_rt(lhs, rhs);\ } while (0) -#define ERR(...)\ - out_err(__FILE__, __LINE__, __func__, __VA_ARGS__) +#define ERR(use_errno, ...)\ + out_err(use_errno, __FILE__, __LINE__, __func__, __VA_ARGS__) #define ERR_W_ERRNO(f, ...)\ do {\ - ERR("*!" f, ##__VA_ARGS__);\ + ERR(1, "*" f, ##__VA_ARGS__);\ CORE_LOG_ERROR_WITH_ERRNO(f, ##__VA_ARGS__);\ } while (0) #define ERR_WO_ERRNO(f, ...)\ do {\ - ERR("*" f, ##__VA_ARGS__);\ + ERR(0, "*" f, ##__VA_ARGS__);\ CORE_LOG_ERROR(f, ##__VA_ARGS__);\ } while (0) @@ -209,8 +209,8 @@ void out_log_va(const char *file, int line, const char *func, int level, const char *fmt, va_list ap); void out_log(const char *file, int line, const char *func, int level, const char *fmt, ...) FORMAT_PRINTF(5, 6); -void out_err(const char *file, int line, const char *func, - const char *fmt, ...) FORMAT_PRINTF(4, 5); +void out_err(int use_errno, const char *file, int line, const char *func, + const char *fmt, ...) FORMAT_PRINTF(5, 6); void NORETURN out_fatal(const char *file, int line, const char *func, const char *fmt, ...) FORMAT_PRINTF(4, 5); const char *out_get_errormsg(void); diff --git a/src/core/util.h b/src/core/util.h index 0224aaa774d..003a48da1b9 100644 --- a/src/core/util.h +++ b/src/core/util.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: BSD-3-Clause */ -/* Copyright 2014-2023, Intel Corporation */ +/* Copyright 2014-2024, Intel Corporation */ /* * Copyright (c) 2016-2020, Microsoft Corporation. All rights reserved. * @@ -101,7 +101,6 @@ int util_snprintf(char *str, size_t size, #define UTIL_MAX_ERR_MSG 128 void util_strerror(int errnum, char *buff, size_t bufflen); -void util_strwinerror(unsigned long err, char *buff, size_t bufflen); void util_set_alloc_funcs( void *(*malloc_func)(size_t size), diff --git a/src/core/util_posix.c b/src/core/util_posix.c index b3bd924f34a..2d7558e48c8 100644 --- a/src/core/util_posix.c +++ b/src/core/util_posix.c @@ -23,17 +23,6 @@ util_strerror(int errnum, char *buff, size_t bufflen) strerror_r(errnum, buff, bufflen); } -/* - * util_strwinerror -- should never be called on posix OS - abort() - */ -void -util_strwinerror(unsigned long err, char *buff, size_t bufflen) -{ - /* suppress unused-parameter errors */ - SUPPRESS_UNUSED(err, buff, bufflen); - abort(); -} - /* * util_part_realpath -- get canonicalized absolute pathname * diff --git a/src/test/out_err/out_err.c b/src/test/out_err/out_err.c index a3b6552487c..fca1809a9b9 100644 --- a/src/test/out_err/out_err.c +++ b/src/test/out_err/out_err.c @@ -41,13 +41,13 @@ main(int argc, char *argv[]) errno = EBADF; strerror_r(errno, buff, UT_MAX_ERR_MSG); - out_err(__FILE__, 100, __func__, + out_err(0, __FILE__, 100, __func__, "ERR1: %s:%d", buff, 1234); UT_OUT("%s", out_get_errormsg()); errno = EBADF; strerror_r(errno, buff, UT_MAX_ERR_MSG); - out_err(NULL, 0, NULL, + out_err(0, NULL, 0, NULL, "ERR2: %s:%d", buff, 1234); UT_OUT("%s", out_get_errormsg()); diff --git a/src/tools/pmempool/output.c b/src/tools/pmempool/output.c index 63df4f5244b..19b2527464a 100644 --- a/src/tools/pmempool/output.c +++ b/src/tools/pmempool/output.c @@ -1,5 +1,5 @@ // SPDX-License-Identifier: BSD-3-Clause -/* Copyright 2014-2023, Intel Corporation */ +/* Copyright 2014-2024, Intel Corporation */ /* * output.c -- definitions of output printing related functions @@ -774,7 +774,7 @@ out_get_incompat_features_str(uint32_t incompat) /* print the value and the left square bracket */ ret = util_snprintf(str_buff, STR_MAX, "0x%x [", incompat); if (ret < 0) { - ERR("snprintf for incompat features: %d", ret); + ERR(0, "snprintf for incompat features: %d", ret); return ""; } diff --git a/src/tools/pmempool/output.h b/src/tools/pmempool/output.h index 80922f2ec0a..0b86ac1f759 100644 --- a/src/tools/pmempool/output.h +++ b/src/tools/pmempool/output.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: BSD-3-Clause */ -/* Copyright 2014-2023, Intel Corporation */ +/* Copyright 2014-2024, Intel Corporation */ /* * output.h -- declarations of output printing related functions @@ -14,8 +14,8 @@ void out_set_stream(FILE *stream); void out_set_prefix(const char *prefix); void out_set_col_width(unsigned col_width); void outv_err(const char *fmt, ...) FORMAT_PRINTF(1, 2); -void out_err(const char *file, int line, const char *func, - const char *fmt, ...) FORMAT_PRINTF(4, 5); +void out_err(int use_errno, const char *file, int line, const char *func, + const char *fmt, ...) FORMAT_PRINTF(5, 6); void outv_err_vargs(const char *fmt, va_list ap); void outv_indent(int vlevel, int i); void outv(int vlevel, const char *fmt, ...) FORMAT_PRINTF(2, 3);