refactor error message-related functions

split message print fuctions (mpr_*), strerrno, and mscp_get/set_error
into print.c/h and strerrno.c/h.

ToDo: revise usages of priv_set_errv and pr_* functions.
This commit is contained in:
Ryo Nakamura
2024-02-06 21:54:04 +09:00
parent 76892a69f9
commit 4f0669f8f8
18 changed files with 268 additions and 292 deletions

41
src/strerrno.c Normal file
View File

@@ -0,0 +1,41 @@
/* SPDX-License-Identifier: GPL-3.0-only */
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <strerrno.h>
#define STRERRNO_TLS_BUFSIZ 128
__thread char tls_strerrno_buf[STRERRNO_TLS_BUFSIZ];
const char *strerrno(void)
{
snprintf(tls_strerrno_buf, sizeof(tls_strerrno_buf), "%s", "strerror_r error");
strerror_r(errno, tls_strerrno_buf, sizeof(tls_strerrno_buf));
return tls_strerrno_buf;
}
#define PRIV_ERR_BUFSIZ (1 << 12)
static char priv_err_buf[PRIV_ERR_BUFSIZ], internal[PRIV_ERR_BUFSIZ];
void priv_set_err(const char *fmt, ...)
{
va_list va;
/* arguments may contains priv_err_buf. Thus, we build the
* string in a internal buffer, and then copy it to
* priv_err_buf. */
memset(internal, 0, sizeof(internal));
va_start(va, fmt);
vsnprintf(internal, sizeof(internal), fmt, va);
va_end(va);
snprintf(priv_err_buf, sizeof(priv_err_buf), "%s", internal);
}
const char *priv_get_err()
{
return priv_err_buf;
}