On Sunday, July 6, 2025 10:27:17 AM Pacific Daylight Time Hal Murray wrote:
> Interesting.  Thanks.
> 
> > Currently it adds 16 bytes per line logged.
> 
> I think 16 is a bit too much.
> 
> > The code is pretty simple and I think it should be  portable.
> > +                       fprintf(term_file, "%s[%d.%ld]: ", prog, pid,
> > (long  int)pthread_self());
> 
> The Linux man page says that pthread_t might be a struct.
> 
> What OS are you running on?  I think Linux uses a whole process for each
> thread.  If you are running on Linux, try using gettid() rather than
> getpid().  That shouldn't take any extra space on the printed line.
> (Well, an extra space if the value of the ID grows by a digit when
> printed.)

The 16 could probably be truncated a bit.

On Linux pthread_t is a long int; other systems will vary.

I am running on Linux. I was going for portable.

Log snippet with PID + TID:
2025-07-06T11:14:13 ntpd[536274.536274]: DNS: dns_probe: 
time.cloudflare.com:1234, cast_flags:1, flags:21901
2025-07-06T11:14:13 ntpd[536274.536310]: NTSc: DNS lookup of 
time.cloudflare.com:1234 took 0.068 sec

A differently weighted patch:
```
--- a/libntp/msyslog.c
+++ b/libntp/msyslog.c
@@ -106,6 +106,7 @@ addto_syslog(
        bool            log_to_term;
        bool            log_to_file;
        int             pid;
+       long            tid;
        const char *    nl_or_empty;
        const char *    human_time;
        char            tbuf[TIMESTAMP_LEN];
@@ -145,6 +146,7 @@ addto_syslog(
                pid = getpid();
        else    /* suppress gcc pot. uninit. warning */
                pid = -1;
+       tid = gettid();
 
        /* syslog() adds trailing \n if not present */
        if ('\n' != msg[strlen(msg) - 1]) {
@@ -160,7 +162,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, tid);
                fprintf(term_file, "%s%s", msg, nl_or_empty);
                fflush(term_file);
        }
@@ -181,7 +183,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, tid, msg, 
nl_or_empty);
                IGNORE(write(fileno(syslog_file), buf, strlen(buf)));
        }
 }
```


_______________________________________________
devel mailing list
devel@ntpsec.org
https://lists.ntpsec.org/mailman/listinfo/devel

Reply via email to