commit 61e99a2c37ab2a5ab5b5595c690650aceabc73b5
parent a29621793317d679d6e43271c5e7390db4f038aa
Author: Louis Burda <quent.burda@gmail.com>
Date: Tue, 22 Sep 2020 11:45:30 +0200
Simplify debug print to a single function
Diffstat:
M | wmsl.c | | | 49 | ++++++++++++++++++++++++++----------------------- |
1 file changed, 26 insertions(+), 23 deletions(-)
diff --git a/wmsl.c b/wmsl.c
@@ -34,7 +34,7 @@ static void sleep_till_next();
static float get_elapsed();
static void set_status_var(const char *str);
static void clean_status();
-static char* time_str();
+static void debug(const char *fmtstr, ...);
static const char* pid_file = "/tmp/.wmsl-pid";
static char status_text[STATUSMAX];
@@ -66,9 +66,7 @@ signal_handler(int sig, siginfo_t *info, void *context)
block_id = info->si_value.sival_int;
if (!block_id || info->si_pid == pid) return;
- if (verbose)
- printf("[%s] received signal for block with id %i\n",
- time_str(), block_id);
+ debug("received signal for block with id %i\n", block_id);
for (i = 0; i < ARRSIZE(blocks); i++) {
if (blocks[i].id == block_id) {
@@ -96,8 +94,7 @@ update_blocks()
int i, output_len, update;
char tmp_output[OUTPUTMAX];
- if (verbose)
- printf("[%s] checking block output..\n", time_str());
+ debug("checking block output..\n");
memcpy(status_text, prefix, ARRSIZE(prefix) - 1);
@@ -128,9 +125,7 @@ update_blocks()
|| strncmp(tmp_output, blocks[i].output, output_len)) {
memcpy(blocks[i].output, tmp_output, output_len);
blocks[i].output[output_len] = '\0';
- if (verbose)
- printf("[%s] new output from cmd: %s\n",
- time_str(), blocks[i].command);
+ debug("new output from cmd: %s\n", blocks[i].command);
update = 1;
}
} else {
@@ -167,15 +162,13 @@ sleep_till_next()
if (minsleep < 1)
minsleep = 1;
- if (verbose)
- printf("[%s] sleeping for %i seconds\n", time_str(), minsleep);
+ debug("sleeping for %i seconds\n", minsleep);
alarm(minsleep);
pause();
last_sleep = get_elapsed();
- if (verbose)
- printf("[%s] slept for %f seconds\n", time_str(), last_sleep);
+ debug("slept for %f seconds\n", last_sleep);
}
float
@@ -212,17 +205,27 @@ clean_status()
exit(EXIT_FAILURE);
}
-char*
-time_str()
+void
+debug(const char *fmtstr, ...)
{
- static char buf[9];
- time_t epoch;
- struct tm *tm;
-
- time(&epoch);
- tm = localtime(&epoch);
- strftime(buf, ARRSIZE(buf), "%T", tm);
- return buf;
+ char buf[9], *fmtalloc;
+ time_t epoch;
+ struct tm *tm;
+ va_list ap;
+ int len;
+
+ if (!verbose)
+ return;
+
+ time(&epoch);
+ tm = localtime(&epoch);
+
+ va_start(ap, fmtstr);
+ fmtalloc = malloc(strlen(fmtstr) + 12);
+ strftime(fmtalloc, 12, "[%T] ", tm);
+ strcpy(fmtalloc + 11, fmtstr);
+ vprintf(fmtalloc, ap);
+ va_end(ap);
}
int