Skip to content

Commit

Permalink
testsuite: fix test server TAP logging
Browse files Browse the repository at this point in the history
Problem: some TAP unit test emit debug output prefixed with "#: ",
which is legal TAP but looks funny.

Change diod_log_init() to not append ": " to the log prefix if it already
ends in a space, and change the test server to do that.

Also skip the log prefix if diod_log_init() is called with a NULL argument,
anticipating the next commit.
  • Loading branch information
garlick committed Jan 19, 2025
1 parent dca6145 commit 61c2cbe
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
24 changes: 18 additions & 6 deletions src/libdiod/diod_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,27 @@

#include "diod_log.h"

static char *prog = NULL;

static char log_prefix[32] = { 0 };
static char *filename = NULL;
static FILE *logf = NULL;

/* If 'path' is a program path, prefix is "name: ", where name is basename (path).
* If 'path' ends in a space, omit the ": ", e.g. path of "# " is used verbatim.
* If 'path' is NULL, there is no log prefix.
*/
void
diod_log_init (char *p)
diod_log_init (char *path)
{
prog = basename (p);
if (path) {
char *cp = strrchr (path, '/');
snprintf (log_prefix,
sizeof (log_prefix),
"%s%s",
cp ? cp + 1 : path,
isspace (path[strlen (path) - 1]) ? "" : ": ");
}
else
log_prefix[0] = '\0';
logf = stderr;
}

Expand Down Expand Up @@ -93,7 +105,7 @@ _verr (int errnum, const char *fmt, va_list ap)
char *s = strerror_r (errnum, errbuf, sizeof (errbuf)); /* GNU version */
#endif
vsnprintf (buf, sizeof (buf), fmt, ap); /* ignore overflow */
fprintf (logf, "%s: %s: %s\n", prog, buf, s);
fprintf (logf, "%s%s: %s\n", log_prefix, buf, s);
fflush (logf);
}
}
Expand All @@ -105,7 +117,7 @@ diod_log_msg (const char *fmt, va_list ap)

if (logf) {
vsnprintf (buf, sizeof (buf), fmt, ap); /* ignore overflow */
fprintf (logf, "%s: %s\n", prog, buf);
fprintf (logf, "%s%s\n", log_prefix, buf);
fflush (logf);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/libtest/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Npsrv *test_server_create (const char *testdir, int flags, int *client_fd)
int s[2];
Npsrv *srv;

diod_log_init ("#"); // add TAP compatible prefix on stderr logs
diod_log_init ("# "); // add TAP compatible prefix on stderr logs
diod_conf_init ();
diod_conf_set_auth_required (0);

Expand Down

0 comments on commit 61c2cbe

Please sign in to comment.