Skip to content

Commit

Permalink
Consolidate fprintf() calls in ldmsd_log() and ovis_log()
Browse files Browse the repository at this point in the history
The majority of the code uses ldmsd_log(), but a few ldmsd plugins and
the authentication plugins use ovis_log(). This mixed use of logging
functions creates a possibility of message interleaving. ovis_log()
prints to stdout, and both functions write to the same FILE* stream as
ldmsd sets stdout to the log file.

Modified both functions to use a single fprintf() call per log message
instead of multiple calls. While this doesn't eliminate the possibility
of message interleaving between threads, consolidating to a single
fprintf() per message reduces the opportunities for interruption
compared to multiple fprintf() calls.
  • Loading branch information
nichamon authored and tom95858 committed Oct 28, 2024
1 parent 6318c9e commit 4f4e595
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 24 deletions.
17 changes: 8 additions & 9 deletions ldms/src/ldmsd/ldmsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,19 +297,17 @@ int __log(enum ldmsd_loglevel level, char *msg, struct timeval *tv, struct tm *t
return 0;
}

char log_time[200];
if (log_time_sec) {
fprintf(log_fp, "%lu.%06lu: ", tv->tv_sec, tv->tv_usec);
snprintf(log_time, 199, "%lu.%06lu: ", tv->tv_sec, tv->tv_usec);
} else {
char dtsz[200];
if (strftime(dtsz, sizeof(dtsz), "%a %b %d %H:%M:%S %Y", tm))
fprintf(log_fp, "%s: ", dtsz);
strftime(log_time, 199, "%a %b %d %H:%M:%S %Y", tm);
}

if (level < LDMSD_LALL) {
fprintf(log_fp, "%-10s: ", ldmsd_loglevel_names[level]);
}

fprintf(log_fp, "%s", msg);
fprintf(log_fp, "%s: %-10s: %s",
log_time,
((level < LDMSD_LALL)?ldmsd_loglevel_names[level]:""),
msg);

return 0;
}
Expand Down Expand Up @@ -2300,6 +2298,7 @@ int main(int argc, char *argv[])

/* Initialize LDMS */
umask(0);
ovis_log_init(NULL, ovis_log_str_to_level("ERROR"), 0);
if (!auth_name)
auth_name = DEFAULT_AUTH_NAME;
if (-1 == banner)
Expand Down
27 changes: 12 additions & 15 deletions lib/src/ovis_log/ovis_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -651,30 +651,25 @@ static int __log(ovis_log_t log, int level, char *msg,

int rc;
FILE *f;
char log_time[200] = "";

if (!log_fp)
f = stdout;
else
f = log_fp;

if (default_modes & OVIS_LOG_M_TS) {
rc = fprintf(f, "%lu.%06lu:", tv->tv_sec, tv->tv_usec);
snprintf(log_time, 199, "%lu.%06lu:", tv->tv_sec, tv->tv_usec);
} else if (default_modes & OVIS_LOG_M_DT) {
char dtsz[200];
if (strftime(dtsz, sizeof(dtsz), "%a %b %d %H:%M:%S %Y", tm))
rc = fprintf(f, "%s:", dtsz);
else
rc = -EINVAL; /* not expected with gnu libc */
} else {
rc = 0;
strftime(log_time, 199, "%a %b %d %H:%M:%S %Y", tm);
}
if (rc < 0)
return rc;

/* Print the level name */
rc = fprintf(f, "%9s:", ((level == OVIS_LALWAYS)?"":ovis_loglevel_names[level]));
if (rc < 0)
return rc;

rc = fprintf(f, " %s: %s", log->name, msg);
rc = fprintf(f, "%s: %-10s: %s: %s",
log_time,
((level == OVIS_LALWAYS)?"":ovis_loglevel_names[level]),
log->name,
msg);
if (rc < 0)
return rc;
return 0;
Expand Down Expand Up @@ -945,6 +940,8 @@ int ovis_vlog(ovis_log_t log, int level, const char *fmt, va_list ap)
/* No workers, so directly log to the file. */
rc = __log(log, level, msg, &tv, &tm);
free(msg);
if (log_fp != OVIS_LOG_SYSLOG)
fflush(log_fp);
goto out;
}

Expand Down

0 comments on commit 4f4e595

Please sign in to comment.