Skip to content

Commit

Permalink
common/common.c, include/common.h: introduce a common method for NUT …
Browse files Browse the repository at this point in the history
…programs to print_banner_once() [networkupstools#2573]

Signed-off-by: Jim Klimov <[email protected]>
  • Loading branch information
jimklimov committed Aug 5, 2024
1 parent 27eecfd commit 4c510e9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
20 changes: 20 additions & 0 deletions common/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,26 @@ int banner_is_disabled(void)
return value;
}

int print_banner_once(const char *prog, int even_if_disabled)
{
static int printed = 0;
static int ret = -1;

if (printed)
return ret;

if (!banner_is_disabled() || even_if_disabled) {
ret = printf("Network UPS Tools %s %s%s\n",
prog, UPS_VERSION,
even_if_disabled == 2 ? "\n" : "");
fflush(stdout);
if (ret > 0)
printed = 1;
}

return ret;
}

/* enable writing upslog_with_errno() and upslogx() type messages to
the syslog */
void syslogbit_set(void)
Expand Down
12 changes: 12 additions & 0 deletions include/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,18 @@ extern const char *UPS_VERSION;
* hide the NUT tool name+version banners; show them by default */
int banner_is_disabled(void);

/* Some NUT programs have historically printed their banner at start-up
* always, and so did not print one in help()/usage() or handling `-V`
* like others did. Now that we have NUT_QUIET_INIT_BANNER, we need a
* way to print that banner (regardless of the flag in some cases).
* The "even_if_disabled" should be 0 for initial banner of those
* programs (so the envvar would hide it), 1 -V case and 2 in -h case
* (for a blank line after). As before, the banner is printed to stdout.
* Returns the result of printf() involved. Remembers to not print again
* if the earlier printf() was successful.
*/
int print_banner_once(const char *prog, int even_if_disabled);

/* Normally we can (attempt to) use the syslog or Event Log (WIN32),
* but environment variable NUT_DEBUG_SYSLOG allows to bypass it, and
* perhaps keep daemons logging to stderr (e.g. in NUT Integration Test
Expand Down

0 comments on commit 4c510e9

Please sign in to comment.