Skip to content

Commit 814a19b

Browse files
authored
Merge pull request #1864 from jimklimov/issue-1858-debug
Add debug verbosity setting for upssched, add `upsdebugx` for `parse_at()`, and add help lines for `-h/-V` in many tools
2 parents 17a8ab5 + b73921e commit 814a19b

File tree

9 files changed

+91
-22
lines changed

9 files changed

+91
-22
lines changed

clients/upsc.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ static void usage(const char *prog)
5757
printf(" -c - lists each client connected on <ups>, one per line.\n");
5858
printf(" <ups> - upsd server, <upsname>[@<hostname>[:<port>]] form\n");
5959

60+
printf("\nCommon arguments:\n");
61+
printf(" -V - display the version of this software\n");
62+
printf(" -h - display this help text\n");
63+
6064
nut_report_config_flags();
6165
}
6266

clients/upscmd.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ static void usage(const char *prog)
5454
printf("Administration program to initiate instant commands on UPS hardware.\n");
5555
printf("\n");
5656
printf(" -h display this help text\n");
57+
printf(" -V display the version of this software\n");
5758
printf(" -l <ups> show available commands on UPS <ups>\n");
5859
printf(" -u <username> set username for command authentication\n");
5960
printf(" -p <password> set password for command authentication\n");

clients/upslog.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ static void help(const char *prog)
161161
printf(" -m <tuple> - Monitor UPS <ups,logfile>\n");
162162
printf(" - Example: -m myups@server,/var/log/myups.log\n");
163163
printf(" -u <user> - Switch to <user> if started as root\n");
164+
printf(" -V - Display the version of this software\n");
165+
printf(" -h - Display this help text\n");
164166

165167
printf("\n");
166168
printf("Some valid format string escapes:\n");

clients/upsmon.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2175,6 +2175,7 @@ static void help(const char *arg_progname)
21752175
printf(" -D raise debugging level (and stay foreground by default)\n");
21762176
printf(" -F stay foregrounded even if no debugging is enabled\n");
21772177
printf(" -B stay backgrounded even if debugging is bumped\n");
2178+
printf(" -V display the version of this software\n");
21782179
printf(" -h display this help\n");
21792180
printf(" -K checks POWERDOWNFLAG, sets exit code to 0 if set\n");
21802181
printf(" -p always run privileged (disable privileged parent)\n");

clients/upsrw.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ static void usage(const char *prog)
5353
printf("Demo program to set variables within UPS hardware.\n");
5454
printf("\n");
5555
printf(" -h display this help text\n");
56+
printf(" -V display the version of this software\n");
5657
printf(" -s <variable> specify variable to be changed\n");
5758
printf(" use -s VAR=VALUE to avoid prompting for value\n");
5859
printf(" -l show all possible read/write variables.\n");

clients/upssched.c

Lines changed: 73 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ typedef struct ttype_s {
6868
static ttype_t *thead = NULL;
6969
static conn_t *connhead = NULL;
7070
static char *cmdscript = NULL, *pipefn = NULL, *lockfn = NULL;
71-
static int verbose = 0; /* use for debugging */
7271

7372
/* ups name and notify type (string) as received from upsmon */
7473
static const char *upsname, *notify_type;
@@ -163,7 +162,7 @@ static void checktimers(void)
163162
if (emptyctr < EMPTY_WAIT)
164163
return;
165164

166-
if (verbose)
165+
if (nut_debug_level)
167166
upslogx(LOG_INFO, "Timer queue empty, exiting");
168167

169168
#ifdef UPSSCHED_RACE_TEST
@@ -185,7 +184,7 @@ static void checktimers(void)
185184
tmpnext = tmp->next;
186185

187186
if (now >= tmp->etime) {
188-
if (verbose)
187+
if (nut_debug_level)
189188
upslogx(LOG_INFO, "Event: %s ", tmp->name);
190189

191190
exec_cmd(tmp->name);
@@ -215,7 +214,7 @@ static void start_timer(const char *name, const char *ofsstr)
215214
return;
216215
}
217216

218-
if (verbose)
217+
if (nut_debug_level)
219218
upslogx(LOG_INFO, "New timer: %s (%ld seconds)", name, ofs);
220219

221220
/* now add to the queue */
@@ -243,7 +242,7 @@ static void cancel_timer(const char *name, const char *cname)
243242

244243
for (tmp = thead; tmp != NULL; tmp = tmp->next) {
245244
if (!strcmp(tmp->name, name)) { /* match */
246-
if (verbose)
245+
if (nut_debug_level)
247246
upslogx(LOG_INFO, "Cancelling timer: %s", name);
248247
removetimer(tmp);
249248
return;
@@ -252,7 +251,7 @@ static void cancel_timer(const char *name, const char *cname)
252251

253252
/* this is not necessarily an error */
254253
if (cname && cname[0]) {
255-
if (verbose)
254+
if (nut_debug_level)
256255
upslogx(LOG_INFO, "Cancel %s, event: %s", name, cname);
257256

258257
exec_cmd(cname);
@@ -779,7 +778,7 @@ static void start_daemon(TYPE_FD lockfd)
779778

780779
pipefd = open_sock();
781780

782-
if (verbose)
781+
if (nut_debug_level)
783782
upslogx(LOG_INFO, "Timer daemon started");
784783

785784
/* release the parent */
@@ -851,7 +850,7 @@ static void start_daemon(TYPE_FD lockfd)
851850
}
852851
pipefd = open_sock();
853852

854-
if (verbose)
853+
if (nut_debug_level)
855854
upslogx(LOG_INFO, "Timer daemon started");
856855

857856
/* drop the lock now that the background is running */
@@ -1191,33 +1190,56 @@ static void parse_at(const char *ntype, const char *un, const char *cmd,
11911190
}
11921191

11931192
/* check upsname: does this apply to us? */
1194-
if (strcmp(upsname, un) != 0)
1195-
if (strcmp(un, "*") != 0)
1193+
upsdebugx(2, "%s: is '%s' in AT command the '%s' we were launched to process?",
1194+
__func__, un, upsname);
1195+
if (strcmp(upsname, un) != 0) {
1196+
if (strcmp(un, "*") != 0) {
1197+
upsdebugx(1, "%s: SKIP: '%s' in AT command "
1198+
"did not match the '%s' UPSNAME "
1199+
"we were launched to process",
1200+
__func__, un, upsname);
11961201
return; /* not for us, and not the wildcard */
1202+
} else {
1203+
upsdebugx(1, "%s: this AT command is for a wildcard: matched", __func__);
1204+
}
1205+
} else {
1206+
upsdebugx(1, "%s: '%s' in AT command matched the '%s' "
1207+
"UPSNAME we were launched to process",
1208+
__func__, un, upsname);
1209+
}
11971210

11981211
/* see if the current notify type matches the one from the .conf */
1199-
if (strcasecmp(notify_type, ntype) != 0)
1212+
if (strcasecmp(notify_type, ntype) != 0) {
1213+
upsdebugx(1, "%s: SKIP: '%s' in AT command "
1214+
"did not match the '%s' NOTIFYTYPE "
1215+
"we were launched to process",
1216+
__func__, ntype, notify_type);
12001217
return;
1218+
}
12011219

12021220
/* if command is valid, send it to the daemon (which may start it) */
12031221

12041222
if (!strcmp(cmd, "START-TIMER")) {
1223+
upsdebugx(1, "%s: processing %s", __func__, cmd);
12051224
sendcmd("START", ca1, ca2);
12061225
return;
12071226
}
12081227

12091228
if (!strcmp(cmd, "CANCEL-TIMER")) {
1229+
upsdebugx(1, "%s: processing %s", __func__, cmd);
12101230
sendcmd("CANCEL", ca1, ca2);
12111231
return;
12121232
}
12131233

12141234
if (!strcmp(cmd, "EXECUTE")) {
1235+
upsdebugx(1, "%s: processing %s", __func__, cmd);
1236+
12151237
if (ca1[0] == '\0') {
12161238
upslogx(LOG_ERR, "Empty EXECUTE command argument");
12171239
return;
12181240
}
12191241

1220-
if (verbose)
1242+
if (nut_debug_level)
12211243
upslogx(LOG_INFO, "Executing command: %s", ca1);
12221244

12231245
exec_cmd(ca1);
@@ -1324,17 +1346,48 @@ static void checkconf(void)
13241346
pconf_finish(&ctx);
13251347
}
13261348

1349+
static void help(const char *arg_progname)
1350+
__attribute__((noreturn));
1351+
1352+
static void help(const char *arg_progname)
1353+
{
1354+
printf("upssched: upsmon's scheduling helper for offset timers\n");
1355+
printf("Practical behavior is managed by UPSNAME and NOTIFYTYPE envvars\n");
1356+
1357+
printf("\nUsage: %s [OPTIONS]\n\n", arg_progname);
1358+
printf(" -D raise debugging level (and stay foreground by default)\n");
1359+
printf(" -V display the version of this software\n");
1360+
printf(" -h display this help\n");
1361+
1362+
nut_report_config_flags();
1363+
1364+
exit(EXIT_SUCCESS);
1365+
}
1366+
1367+
13271368
int main(int argc, char **argv)
13281369
{
1329-
const char *prog = NULL;
1330-
/* More a use for argc to avoid warnings than a real need: */
1331-
if (argc > 0) {
1332-
xbasename(argv[0]);
1333-
} else {
1334-
xbasename("upssched");
1335-
}
1370+
const char *prog = xbasename(argv[0]);
1371+
int i;
1372+
1373+
while ((i = getopt(argc, argv, "+DVh")) != -1) {
1374+
switch (i) {
1375+
case 'D':
1376+
nut_debug_level++;
1377+
break;
1378+
1379+
case 'h':
1380+
help(argv[0]);
1381+
#ifndef HAVE___ATTRIBUTE__NORETURN
1382+
break;
1383+
#endif
13361384

1337-
verbose = 1; /* TODO: remove when done testing, or add -D */
1385+
case 'V':
1386+
/* just show the optional CONFIG_FLAGS banner */
1387+
nut_report_config_flags();
1388+
exit(EXIT_SUCCESS);
1389+
}
1390+
}
13381391

13391392
/* normally we don't have stderr, so get this going to syslog early */
13401393
open_syslog(prog);

docs/developers.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,12 @@ if (condition) {
542542
}
543543
-------------------------------------------------------------------------------
544544

545+
NOTE: Earlier revisions of coding style might suggest avoiding braces if just
546+
one line is added as condition/loop/etc. handling code. Current approach is to
547+
welcome them even for single lines: on one hand, this confirms the intention
548+
that only this line is the conditional code; on another, this minimizes the
549+
context differences for later code comparisons, relocation, refactoring, etc.
550+
545551
Un-used variables and function arguments
546552
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
547553

server/upsd.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,11 +1499,11 @@ static void help(const char *arg_progname)
14991499
printf(" -F stay foregrounded even if no debugging is enabled\n");
15001500
printf(" -FF stay foregrounded and still save the PID file\n");
15011501
printf(" -B stay backgrounded even if debugging is bumped\n");
1502-
printf(" -h display this help\n");
1502+
printf(" -h display this help text\n");
1503+
printf(" -V display the version of this software\n");
15031504
printf(" -r <dir> chroots to <dir>\n");
15041505
printf(" -q raise log level threshold\n");
15051506
printf(" -u <user> switch to <user> (if started as root)\n");
1506-
printf(" -V display the version of this software\n");
15071507
printf(" -4 IPv4 only\n");
15081508
printf(" -6 IPv6 only\n");
15091509

tools/nut-scanner/nut-scanner.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ static void show_usage()
337337
printf(" -N, --disp_nut_conf: Display result in the ups.conf format\n");
338338
printf(" -P, --disp_parsable: Display result in a parsable format\n");
339339
printf("\nMiscellaneous options:\n");
340+
printf(" -h, --help: display this help text\n");
340341
printf(" -V, --version: Display NUT version\n");
341342
printf(" -a, --available: Display available bus that can be scanned\n");
342343
printf(" -q, --quiet: Display only scan result. No information on currently scanned bus is displayed.\n");

0 commit comments

Comments
 (0)