Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions loguru.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
#include <atomic>
#include <cctype>
#include <chrono>
#include <cinttypes>
#include <cstdarg>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
Expand Down Expand Up @@ -578,15 +580,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
#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));
return Text(strdup(buff));
#elif defined(_WIN32)
char buff[256];
strerror_s(buff, sizeof(buff), errno);
return Text(STRDUP(buff));
#else
Expand Down Expand Up @@ -1076,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
Expand All @@ -1100,18 +1106,21 @@ 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();
// 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<uint64_t>(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.
const auto thread_id = std::hash<std::thread::id>{}(std::this_thread::get_id());
#endif

if (right_align_hex_id) {
snprintf(buffer, static_cast<size_t>(length), "%*X", static_cast<int>(length - 1), static_cast<unsigned>(thread_id));
snprintf(buffer, static_cast<size_t>(length), "%*" PRIX64, static_cast<int>(length - 1), static_cast<uint64_t>(thread_id));
} else {
snprintf(buffer, static_cast<size_t>(length), "%X", static_cast<unsigned>(thread_id));
snprintf(buffer, static_cast<size_t>(length), "%" PRIX64, static_cast<uint64_t>(thread_id));
}
}
}
Expand Down