Skip to content

Commit 4761ab4

Browse files
committed
clients/upslog.c, docs/man/upslog.txt, NEWS.adoc: Added -N to prefix %UPSHOST%%t before the format string
Signed-off-by: Jim Klimov <jimklimov+nut@gmail.com>
1 parent 38e6d38 commit 4761ab4

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

NEWS.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,8 @@ https://github.com/networkupstools/nut/milestone/11
333333
then recommended to use `%UPSHOST%` in a custom formatting string).
334334
* Fixed printing of `%UPSHOST%` when multiple systems are being logged.
335335
* A `%t` for a TAB character can now be used in the formatting string.
336+
* Added `-N` to prefix `%UPSHOST%%t` before the format string (whether
337+
default or custom). Useful when logging many systems into same target.
336338
* Added `-D` for debugging (and foregrounding by default), like with
337339
other NUT daemons.
338340
* Added systemd and SMF service integration. [#1803]

clients/upslog.c

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ static void help(const char *prog)
194194

195195
printf(" -f <format> - Log format. See below for details.\n");
196196
printf(" - Use -f \"<format>\" so your shell doesn't break it up.\n");
197+
printf(" -N - Prefix \"%%UPSHOST%%%%t\" before the format (default/custom)");
198+
printf(" - Useful when logging many systems into same target.\n");
197199
printf(" -i <interval> - Time between updates, in seconds\n");
198200
printf(" -d <count> - Exit after specified amount of updates\n");
199201
printf(" -l <logfile> - Log file name, or - for stdout (foreground by default)\n");
@@ -493,7 +495,7 @@ static void run_flist(const struct monhost_ups_t *monhost_ups_print)
493495

494496
int main(int argc, char **argv)
495497
{
496-
int interval = 30, i, foreground = -1;
498+
int interval = 30, i, foreground = -1, prefix_UPSHOST = 0, logformat_allocated = 0;
497499
size_t monhost_len = 0, loop_count = 0;
498500
const char *prog = xbasename(argv[0]);
499501
time_t now, nextpoll = 0;
@@ -508,7 +510,7 @@ int main(int argc, char **argv)
508510

509511
print_banner_once(prog, 0);
510512

511-
while ((i = getopt(argc, argv, "+hDs:l:i:d:f:u:Vp:FBm:")) != -1) {
513+
while ((i = getopt(argc, argv, "+hDs:l:i:d:Nf:u:Vp:FBm:")) != -1) {
512514
switch(i) {
513515
case 'h':
514516
help(prog);
@@ -598,6 +600,10 @@ int main(int argc, char **argv)
598600
logformat = optarg;
599601
break;
600602

603+
case 'N':
604+
prefix_UPSHOST = 1;
605+
break;
606+
601607
case 'u':
602608
user = optarg;
603609
break;
@@ -655,6 +661,7 @@ int main(int argc, char **argv)
655661

656662
logformat = xmalloc(LARGEBUF);
657663
memset(logformat, '\0', LARGEBUF);
664+
logformat_allocated = 1;
658665

659666
for (i = 3; i < argc; i++)
660667
snprintfcat(logformat, LARGEBUF, "%s ", argv[i]);
@@ -694,6 +701,23 @@ int main(int argc, char **argv)
694701
if (!logformat)
695702
fatalx(EXIT_FAILURE, "No format defined - but this should be impossible");
696703

704+
if (prefix_UPSHOST) {
705+
char *s = xstrdup(logformat);
706+
if (s) {
707+
if (!logformat_allocated) {
708+
logformat = xmalloc(LARGEBUF);
709+
if (!logformat)
710+
fatalx(EXIT_FAILURE, "Failed re-allocation to prepend UPSHOST to formatting string");
711+
memset(logformat, '\0', LARGEBUF);
712+
}
713+
snprintf(logformat, LARGEBUF, "%%UPSHOST%%%%t%s", s);
714+
free(s);
715+
} else {
716+
upslogx(LOG_WARNING, "Failed to prepend UPSHOST to formatting string");
717+
}
718+
}
719+
upsdebugx(1, "logformat: %s", logformat);
720+
697721
/* shouldn't happen */
698722
if (!monhost_len)
699723
fatalx(EXIT_FAILURE, "No UPS defined for monitoring - use -s <system> -l <logfile>, or use -m <ups,logfile>");
@@ -855,7 +879,7 @@ int main(int argc, char **argv)
855879
if (monhost_ups_current->logtarget->logfile != stdout)
856880
upslogx(LOG_INFO, "NOTE: File %s is already receiving other logs",
857881
monhost_ups_current->logtarget->logfn);
858-
upslogx(LOG_INFO, "NOTE: Consider adding %%UPSHOST%% to the log formatting string");
882+
upslogx(LOG_INFO, "NOTE: Consider adding %%UPSHOST%% to the log formatting string, e.g. pass -N on CLI");
859883
}
860884
} else {
861885
if (strcmp(monhost_ups_current->logtarget->logfn, "-") == 0)
@@ -967,6 +991,11 @@ int main(int argc, char **argv)
967991
upscli_disconnect(monhost_ups_current->ups);
968992
}
969993

994+
if (logformat_allocated) {
995+
free(logformat);
996+
logformat = NULL;
997+
}
998+
970999
exit(EXIT_SUCCESS);
9711000
}
9721001

docs/man/upslog.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ The default format string is:
6060
%VAR ups.load% [%VAR ups.status%] %VAR ups.temperature%
6161
%VAR input.frequency%
6262

63+
*-N*::
64+
Prefix `%UPSHOST%%t` before the format string (whether default or custom).
65+
Useful when logging many systems into same target.
66+
6367
*-i* 'interval'::
6468

6569
Wait this many seconds between polls. This defaults to 30 seconds.

0 commit comments

Comments
 (0)