Logging with threads
James Browning
jamesb192 at jamesb192.com
Sun Jul 6 10:43:19 UTC 2025
On Saturday, July 5, 2025 10:40:16 PM Pacific Daylight Time Hal Murray via
devel wrote:
> I'd like msyslog to indicate which thread is doing the logging.
>
> The main thread in ntpd currently creates up to 3 threads.
> One for DNS, and a NTS thread for each of IPv4 and IPv6.
> The DNS thread gets created and joined, but there is only one at any time.
> We may need more NTS threads in the future.
>
> Linux has gettid() that returns a pid_t for the current thread.
> So a line in wscript to see if gettid exissts and an ifdef
> in msyslog.c to use it would solve my problem on Linux.
>
> FreeBSD uses a pointer for pthread_t
> We could teach msyslog to keep a small table of pthread_t
> and then print [pid.idx] rather than [pid]
>
> Anybody thought about this area? Any good ideas?
I built a toy version and I am running it on a machine. Currently it adds 16
bytes per line logged. The code is pretty simple and I think it should be
portable. I feel this is unworthy of consideration though. Better yet would be
a translation table or macro.
2025-07-06T03:06:33 ntpd[532173.123383191787520]: DNS: dns_probe:
nts.ntp.se:3443, cast_flags:1, flags:21901
2025-07-06T03:06:33 ntpd[532173.123383162848960]: NTSc: DNS lookup of
nts.ntp.se:3443 took 0.053 sec
```
--- a/libntp/msyslog.c
+++ b/libntp/msyslog.c
@@ -8,6 +8,7 @@
#include "config.h"
#include <limits.h>
+#include <pthread.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
@@ -160,7 +161,7 @@ addto_syslog(
if (msyslog_include_timestamp)
fprintf(term_file, "%s ", human_time);
if (termlogit_pid)
- fprintf(term_file, "%s[%d]: ", prog, pid);
+ fprintf(term_file, "%s[%d.%ld]: ", prog, pid, (long
int)pthread_self());
fprintf(term_file, "%s%s", msg, nl_or_empty);
fflush(term_file);
}
@@ -181,7 +182,7 @@ addto_syslog(
if (msyslog_include_timestamp)
snprintf(buf, sizeof(buf), "%s ", human_time);
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf) - 1,
- "%s[%d]: %s%s", prog, pid, msg, nl_or_empty);
+ "%s[%d.%ld]: %s%s", prog, pid, (long
int)pthread_self(), msg, nl_or_empty);
IGNORE(write(fileno(syslog_file), buf, strlen(buf)));
}
}
```
wrong...
https://pubs.opengroup.org/onlinepubs/9799919799/functions/pthread_self.html
https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/sys_types.h.html
More information about the devel
mailing list