From 20da2c7db803c4a23b529d7dbb2992bab42a9477 Mon Sep 17 00:00:00 2001 From: Johan Wikman Date: Mon, 13 Feb 2017 16:13:26 +0200 Subject: [PATCH] Add mxs_strerror Thread-safe version of strerror; thread local buffer used for storing the message. The performance penalty of a thread local buffer is not likely to be significant, since this is only called in an error situation that anyway is likely to interrupt the normal processing. --- include/maxscale/log_manager.h | 17 +++++++++++++++++ server/core/log_manager.cc | 8 ++++++++ 2 files changed, 25 insertions(+) diff --git a/include/maxscale/log_manager.h b/include/maxscale/log_manager.h index 307fc6e89..ecd10a824 100644 --- a/include/maxscale/log_manager.h +++ b/include/maxscale/log_manager.h @@ -200,4 +200,21 @@ enum trailing NULL. If longer, it will be cut. */ }; +/** + * Return a thread specific pointer to a string describing the error + * code passed as argument. The string is obtained using strerror_r. + * + * @param error One of the errno error codes. + * + * @return Thread specific pointer to string describing the error code. + * + * @attention The function is thread safe, but not re-entrant. That is, + * calling it twice with different error codes between two sequence points + * will not work. E.g: + * + * printf("EINVAL = %s, EACCESS = %s", + * mxs_strerror(EINVAL), mxs_strerror(EACCESS)); + */ +const char* mxs_strerror(int error); + MXS_END_DECLS diff --git a/server/core/log_manager.cc b/server/core/log_manager.cc index 1c6903acf..db2d0ca53 100644 --- a/server/core/log_manager.cc +++ b/server/core/log_manager.cc @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -3045,3 +3046,10 @@ int mxs_log_message(int priority, return err; } + +const char* mxs_strerror(int error) +{ + static thread_local char errbuf[MXS_STRERROR_BUFLEN]; + + return strerror_r(error, errbuf, sizeof(errbuf)); +}