simplified us_errno_to_string()

This commit is contained in:
Maxim Devaev
2022-07-30 13:05:00 +03:00
parent 3d7fb8c8dd
commit faa1776407
4 changed files with 17 additions and 18 deletions

View File

@@ -32,7 +32,7 @@
#define US_JLOG_ERROR(x_prefix, x_msg, ...) JANUS_LOG(LOG_ERR, "== %s/%-9s -- " x_msg "\n", US_PLUGIN_NAME, x_prefix, ##__VA_ARGS__) #define US_JLOG_ERROR(x_prefix, x_msg, ...) JANUS_LOG(LOG_ERR, "== %s/%-9s -- " x_msg "\n", US_PLUGIN_NAME, x_prefix, ##__VA_ARGS__)
#define US_JLOG_PERROR(x_prefix, x_msg, ...) { \ #define US_JLOG_PERROR(x_prefix, x_msg, ...) { \
char m_perror_buf[1024] = {0}; \ char *const m_perror_str = us_errno_to_string(errno); \
char *m_perror_ptr = us_errno_to_string(errno, m_perror_buf, 1023); \ JANUS_LOG(LOG_ERR, "[%s/%-9s] " x_msg ": %s\n", US_PLUGIN_NAME, x_prefix, ##__VA_ARGS__, m_perror_str); \
JANUS_LOG(LOG_ERR, "[%s/%-9s] " x_msg ": %s\n", US_PLUGIN_NAME, x_prefix, ##__VA_ARGS__, m_perror_ptr); \ free(m_perror_str); \
} }

View File

@@ -116,9 +116,9 @@ extern pthread_mutex_t us_g_log_mutex;
} }
#define US_LOG_PERROR(x_msg, ...) { \ #define US_LOG_PERROR(x_msg, ...) { \
char m_perror_buf[1024] = {0}; \ char *const m_perror_str = us_errno_to_string(errno); \
char *m_perror_ptr = us_errno_to_string(errno, m_perror_buf, 1024); \ US_LOG_ERROR(x_msg ": %s", ##__VA_ARGS__, m_perror_str); \
US_LOG_ERROR(x_msg ": %s", ##__VA_ARGS__, m_perror_ptr); \ free(m_perror_str); \
} }
#define US_LOG_INFO(x_msg, ...) { \ #define US_LOG_INFO(x_msg, ...) { \
@@ -149,9 +149,9 @@ extern pthread_mutex_t us_g_log_mutex;
#define US_LOG_VERBOSE_PERROR(x_msg, ...) { \ #define US_LOG_VERBOSE_PERROR(x_msg, ...) { \
if (us_g_log_level >= US_LOG_LEVEL_VERBOSE) { \ if (us_g_log_level >= US_LOG_LEVEL_VERBOSE) { \
char m_perror_buf[1024] = {0}; \ char *m_perror_str = us_errno_to_string(errno); \
char *m_perror_ptr = us_errno_to_string(errno, m_perror_buf, 1023); \ US_LOG_PRINTF(US_COLOR_BLUE, "VERB ", US_COLOR_BLUE, x_msg ": %s", ##__VA_ARGS__, m_perror_str); \
US_LOG_PRINTF(US_COLOR_BLUE, "VERB ", US_COLOR_BLUE, x_msg ": %s", ##__VA_ARGS__, m_perror_ptr); \ free(m_perror_str); \
} \ } \
} }

View File

@@ -187,15 +187,14 @@ INLINE int us_flock_timedwait_monotonic(int fd, long double timeout) {
return retval; return retval;
} }
INLINE char *us_errno_to_string(int error, char *buf, size_t size) { INLINE char *us_errno_to_string(int error) {
assert(buf != NULL);
assert(size > 0);
locale_t locale = newlocale(LC_MESSAGES_MASK, "C", NULL); locale_t locale = newlocale(LC_MESSAGES_MASK, "C", NULL);
const char *str = "!!! newlocale() error !!!"; char *buf;
strncpy(buf, (locale ? strerror_l(error, locale) : str), size - 1);
buf[size - 1] = '\0';
if (locale) { if (locale) {
buf = us_strdup(strerror_l(error, locale));
freelocale(locale); freelocale(locale);
} else {
buf = us_strdup("!!! newlocale() error !!!");
} }
return buf; return buf;
} }

View File

@@ -28,11 +28,11 @@ char *us_bufferevent_format_reason(short what) {
US_CALLOC(reason, 2048); US_CALLOC(reason, 2048);
// evutil_socket_error_to_string() is not thread-safe // evutil_socket_error_to_string() is not thread-safe
char perror_buf[1024] = {0}; char *const perror_str = us_errno_to_string(EVUTIL_SOCKET_ERROR());
const char *perror_ptr = us_errno_to_string(EVUTIL_SOCKET_ERROR(), perror_buf, 1024);
bool first = true; bool first = true;
strcat(reason, perror_ptr); strcat(reason, perror_str);
free(perror_str);
strcat(reason, " ("); strcat(reason, " (");
# define FILL_REASON(x_bev, x_name) { \ # define FILL_REASON(x_bev, x_name) { \