clogger · Section 3
ccol_set_thread_name
NAME
ccol_set_thread_name - rename the calling thread and make its log records carry the new name
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/clogger.h> ccol_retval ccol_set_thread_name(const char *thread_name);
DESCRIPTION
Renames the calling thread, with prctl(PR_SET_NAME) on Linux, pthread_setname_np(3) on FreeBSD and macOS, and pthread_set_name_np(3) on OpenBSD. The next record that the thread writes, through any logger, carries the new name in the thread part of its proc field. Every record names the process and the thread that wrote it. A thread reads its process ID, thread ID and thread name once, the first time it logs, and keeps them for the rest of its life, so a record costs no system call for them. This function updates the name that the thread keeps. A child that fork(2) creates reads all three again. The Linux kernel stores at most 15 bytes of a thread name, so the function keeps the first 15 bytes of thread_name on every platform, and the records and tools such as ps(1) and top(1) show the same name. No logger is needed, so a thread can name itself before any logger exists. Only the calling thread is affected.
PARAMETERS
thread_name The new name. It must not be NULL or empty. Only its
first 15 bytes are used.
RETURN VALUE
ccol_success The thread has the new name, and its records
carry it.
ccol_invalid_args thread_name is NULL or empty. Nothing changes.
ccol_not_permitted The system refused the rename, or the platform is
not Linux, FreeBSD, OpenBSD or macOS. Nothing
changes.
NOTES
If a thread is renamed by any other means after its first record, for example with pthread_setname_np(3) or prctl(PR_SET_NAME), its records keep the old name, so rename a thread that logs with this function. Thread-safe. Each thread renames only itself.
EXAMPLES
static void *worker(void *arg) { clog lg = *(clog *)arg; if (ccol_set_thread_name("ingest-worker") != ccol_success) return NULL; clog_info(lg, "started"); /* proc=myapp(1234):ingest-worker(1240) */ return NULL; }
SEE ALSO
clog_create_from_fd_mp(3), clog_create_from_file_path_mp(3), prctl(2)