From fb6ed0e4dc40eb7c10b737e44d74637f811795ec Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Mon, 9 Oct 2023 01:38:41 -0400 Subject: [PATCH 1/6] Fixed minor warning about unused variable In the final else, buff is not used, so duplicated into the branches where it is actually used. --- loguru.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/loguru.cpp b/loguru.cpp index 7eaadb7..e08cc66 100644 --- a/loguru.cpp +++ b/loguru.cpp @@ -578,15 +578,17 @@ namespace loguru Text errno_as_text() { - char buff[256]; #if defined(__GLIBC__) && defined(_GNU_SOURCE) // GNU Version + char buff[256]; return Text(STRDUP(strerror_r(errno, buff, sizeof(buff)))); #elif defined(__APPLE__) || _POSIX_C_SOURCE >= 200112L // XSI Version + char buff[256]; strerror_r(errno, buff, sizeof(buff)); return Text(strdup(buff)); #elif defined(_WIN32) + char buff[256]; strerror_s(buff, sizeof(buff), errno); return Text(STRDUP(buff)); #else From 05637ceaceba25af08024a31d2819453d7c88741 Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Wed, 11 Oct 2023 16:04:45 -0400 Subject: [PATCH 2/6] Use getthrid() on OpenBSD for fallback thread global thread id This fixes a compiler error on that platform, because of the nearby assumption that pthread_t is an integer type, when it is in fact a pointer on OpenBSD (and macOS). --- loguru.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/loguru.cpp b/loguru.cpp index e08cc66..dd3f7de 100644 --- a/loguru.cpp +++ b/loguru.cpp @@ -1102,6 +1102,8 @@ namespace loguru #elif defined(__FreeBSD__) long thread_id; (void)thr_self(&thread_id); + #elif defined(__OpenBSD__) + pid_t thread_id = getthrid(); #elif LOGURU_PTHREADS uint64_t thread_id = pthread_self(); #else From 6572aa15f4ebdd56b22ee28405ce4508a9a57820 Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Mon, 9 Oct 2023 01:41:35 -0400 Subject: [PATCH 3/6] Fixed possibly lossy truncation from uint64_t/long to unsigned Also added explict cast for dubious non-portable conversion of pthread_t to integer. --- loguru.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/loguru.cpp b/loguru.cpp index dd3f7de..2b98808 100644 --- a/loguru.cpp +++ b/loguru.cpp @@ -34,7 +34,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -1105,7 +1107,8 @@ namespace loguru #elif defined(__OpenBSD__) pid_t thread_id = getthrid(); #elif LOGURU_PTHREADS - uint64_t thread_id = pthread_self(); + // Here we rely on the opaque pthread_t being of integer type, which is the case on linux, but not other platforms. + uint64_t thread_id = static_cast(pthread_self()); #else // This ID does not correllate to anything we can get from the OS, // so this is the worst way to get the ID. @@ -1113,9 +1116,9 @@ namespace loguru #endif if (right_align_hex_id) { - snprintf(buffer, static_cast(length), "%*X", static_cast(length - 1), static_cast(thread_id)); + snprintf(buffer, static_cast(length), "%*" PRIX64, static_cast(length - 1), static_cast(thread_id)); } else { - snprintf(buffer, static_cast(length), "%X", static_cast(thread_id)); + snprintf(buffer, static_cast(length), "%" PRIX64, static_cast(thread_id)); } } } From eb8141c6a001d8d7f56b0d00fa9e214744eeb6c5 Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Mon, 9 Oct 2023 01:44:44 -0400 Subject: [PATCH 4/6] Fixed -Wundef warning: check that _POSIX_C_SOURCE is defined before checking it --- loguru.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/loguru.cpp b/loguru.cpp index 2b98808..08183bf 100644 --- a/loguru.cpp +++ b/loguru.cpp @@ -584,7 +584,7 @@ namespace loguru // GNU Version char buff[256]; return Text(STRDUP(strerror_r(errno, buff, sizeof(buff)))); - #elif defined(__APPLE__) || _POSIX_C_SOURCE >= 200112L + #elif defined(__APPLE__) || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) // XSI Version char buff[256]; strerror_r(errno, buff, sizeof(buff)); From 20b0e839dc05000167a319e1c133d649e1f5fa7a Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Wed, 11 Oct 2023 15:26:49 -0400 Subject: [PATCH 5/6] Use strerror_r() on FreeBSD and OpenBSD strerror_r has been available since FreeBSD 4.4 and OpenBSD 3.3 (both many years ago). --- loguru.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/loguru.cpp b/loguru.cpp index 08183bf..6ff9f32 100644 --- a/loguru.cpp +++ b/loguru.cpp @@ -584,7 +584,7 @@ namespace loguru // GNU Version char buff[256]; return Text(STRDUP(strerror_r(errno, buff, sizeof(buff)))); - #elif defined(__APPLE__) || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) + #elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) // XSI Version char buff[256]; strerror_r(errno, buff, sizeof(buff)); From fa9bb57772e4f771590cc7c755943ef9d3377adf Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Wed, 11 Oct 2023 15:28:19 -0400 Subject: [PATCH 6/6] On FreeBSD and OpenBSD use `pthread_get_name_np` spelling instead of `pthread_getname_np` Annoyingly, these functions are spelt differently on different OSes. --- loguru.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/loguru.cpp b/loguru.cpp index 6ff9f32..64ef8e5 100644 --- a/loguru.cpp +++ b/loguru.cpp @@ -1080,6 +1080,8 @@ namespace loguru } else { buffer[0] = 0; } + #elif defined(__FreeBSD__) || defined(__OpenBSD__) + pthread_get_name_np(pthread_self(), buffer, length); #elif LOGURU_PTHREADS // Ask the OS about the thread name. // This is what we *want* to do on all platforms, but