Skip to content

Commit

Permalink
common: errno explicitly provided to ERR() macro.
Browse files Browse the repository at this point in the history
Remove "!..." logic from error message format string.
Use separate argument to either use or not use errno in error message.
Remove non-posix version of util_strerror.

Signed-off-by: Tomasz Gromadzki <tomasz.gromadzki@intel.com>
  • Loading branch information
grom72 committed Jan 29, 2024
1 parent e3debc6 commit 3ae2638
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 53 deletions.
38 changes: 11 additions & 27 deletions src/core/out.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -449,7 +432,8 @@ out_error(const char *file, int line, const char *func,
#endif

end:
errno = oerrno;
if (use_errno)
errno = oerrno;
}

/*
Expand Down Expand Up @@ -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);
}
Expand Down
12 changes: 6 additions & 6 deletions src/core/out.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions src/core/util.h
Original file line number Diff line number Diff line change
@@ -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.
*
Expand Down Expand Up @@ -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),
Expand Down
11 changes: 0 additions & 11 deletions src/core/util_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
4 changes: 2 additions & 2 deletions src/test/out_err/out_err.c
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
4 changes: 2 additions & 2 deletions src/tools/pmempool/output.c
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);

Check warning on line 777 in src/tools/pmempool/output.c

View check run for this annotation

Codecov / codecov/patch

src/tools/pmempool/output.c#L777

Added line #L777 was not covered by tests
return "<error>";
}

Expand Down
6 changes: 3 additions & 3 deletions src/tools/pmempool/output.h
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
Expand Down

0 comments on commit 3ae2638

Please sign in to comment.