A tiling window manager

communications: add WARNX_DEBUG macro for cli client stderr prints

The client cannot use PRINT_DEBUG because it sends to stdout, which we
cannot contaminate. We can use warn(), but don't want that to pollute
the non-DEBUG experience, and we don't want to pollute -DDEBUG, so we
add it under a new -DSENDCMD_DEBUG following the example of INPUT_DEBUG.

However, to avoid ifdefs everywhere, we'll prefer to do that in a macro.
So we just add another macro that uses stderr. It's a bit more compact
than the one that uses stdout, this one uses __VA_ARGS__ as a straight
text macro, this should compile on BSD and Linux (only latter tested).

We could not use a modification of PRINT_DEBUG because it would have
needed fprintf(file, ...) and the way the macro was written it would
require passing ((args like this)) because it had a naked "printf args"
and relied on getting first level "()" stripped in the macro expansion.
This was a bit strange. It does mean there's now inconsistency between
PRINT_DEBUG and WARNX_DEBUG usage (former needs '(())', latter '()') but
the latter macro is only used by one function anyways and only
appropriate for the client so usage will probably never expand.

authored by

Scott Mcdermott and committed by jcs.org 0c1e20a7 a29862f0

+22 -23
+2 -1
Makefile
··· 15 15 16 16 # uncomment to enable debugging 17 17 #CFLAGS+= -g -DDEBUG=1 18 - # and this for input-specific debugging 18 + # and this for subsystem-specific debugging 19 19 #CFLAGS+= -DINPUT_DEBUG=1 20 + #CFLAGS+= -DSENDCMD_DEBUG=1 20 21 21 22 BINDIR= ${DESTDIR}$(PREFIX)/bin 22 23 MANDIR= ${DESTDIR}$(PREFIX)/man/man1
+10 -22
communications.c
··· 94 94 int flags = 0x0; 95 95 FILE *outf = NULL; 96 96 97 - #ifdef DEBUG 97 + #ifdef SENDCMD_DEBUG 98 98 pid_t pid = getpid(); 99 - warnx("send_command_%d: enter", pid); 99 + char *dpfx = xsprintf("send_command_%d", pid); 100 100 #endif 101 + WARNX_DEBUG("%s: enter\n", dpfx); 101 102 102 103 len = 1 + strlen((char *)cmd) + 2; 103 104 wcmd = malloc(len); ··· 128 129 while ((count = recv(fd, &ret, BUFSZ, flags))) { 129 130 bufstart = ret; 130 131 if (firstloop) { 131 - #ifdef DEBUG 132 - warnx("send_command_%d: first receive, count %zu", 133 - pid, count); 134 - #endif 132 + WARNX_DEBUG("%s: first recv: %zu\n", dpfx, count); 135 133 /* first byte is exit status */ 136 134 success = *ret; 137 135 outf = success ? stdout : stderr; ··· 149 147 flags += MSG_DONTWAIT; 150 148 } 151 149 if (count == -1) { 152 - #ifdef DEBUG 153 - char *e = strerror(errno); 154 - #endif 155 - if (errno == EAGAIN || errno == ECONNRESET) { 156 - #ifdef DEBUG 157 - warnx("send_command_%d: finish: %s", pid, e); 158 - #endif 150 + WARNX_DEBUG("%s: finish errno: %d\n", dpfx, errno); 151 + if (errno == EAGAIN || errno == ECONNRESET) 159 152 return success; 160 - } 161 - #ifdef DEBUG 162 - warnx("send_command_%d: recvfail, err: %s", pid, e); 163 - #endif 164 153 } 165 154 ret[count] = '\0'; 166 155 fprintf(outf, "%s", bufstart); 167 156 fflush(outf); 168 157 firstloop = 0; 169 - #ifdef DEBUG 170 - warnx("send_command_%d: looping", pid); 171 - #endif 158 + WARNX_DEBUG("%s: looping\n", dpfx); 172 159 } 173 - #ifdef DEBUG 174 - warnx("send_command_%d: no more bytes", pid); 160 + WARNX_DEBUG("%s: no more bytes\n", dpfx); 161 + #ifdef SENDCMD_DEBUG 162 + free(dpfx); 175 163 #endif 176 164 177 165 return success;
+10
sdorfehs.h
··· 52 52 #define PRINT_DEBUG(fmt) do {} while (0) 53 53 #endif /* DEBUG */ 54 54 55 + #ifdef SENDCMD_DEBUG 56 + #define WARNX_DEBUG(fmt, ...) \ 57 + do { \ 58 + fprintf (stderr, fmt, __VA_ARGS__); \ 59 + fflush (stderr); \ 60 + } while (0) 61 + #else 62 + #define WARNX_DEBUG(fmt, ...) do {} while (0) 63 + #endif /* SENDCMD_DEBUG */ 64 + 55 65 #ifdef INPUT_DEBUG 56 66 #define PRINT_INPUT_DEBUG(fmt) \ 57 67 do { \