Skip to content

Commit 16bb10a

Browse files
committed
lirc: Don't log responses to STDOUT.
In wait_for_response, we were simply logging anything received to STDOUT. This was done for the benefit of the associated IRC client, where we would want to see these things. However, when used as a library in other applications, we don't necessarily want these going to STDOUT! Instead, use a log callback (specifically, a new verbose log level), as most of the expected responses will be NOTICE messages. That way, they can still get printed out where desired, but won't get logged to STDOUT if not desired. The included client has been updated so that received data should still be logged to its STDOUT mostly as before.
1 parent 22ee2f8 commit 16bb10a

3 files changed

Lines changed: 11 additions & 2 deletions

File tree

client.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ static void __client_log(enum irc_log_level level, int sublevel, const char *fil
123123
print_time(fd);
124124
dprintf(fd, "[%sINFO %s] %s:%d %s() %s", COLOR_CYAN, COLOR_RESET, file, line, func, msg);
125125
break;
126+
case IRC_LOG_VERBOSE:
127+
/* irc.c guarantees this is newline terminated, so we can safely prefix with CR
128+
* to ensure we overwrite the prompt, without worrying about overwriting something we just wrote. */
129+
dprintf(fd, "\r%s", msg);
130+
break;
126131
case IRC_LOG_DEBUG:
127132
if (debug_level >= sublevel) {
128133
print_time(fd);

irc.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ void irc_log_callback(void (*callback)(enum irc_log_level level, int sublevel, c
8888
#define irc_err(fmt, ...) __irc_log(IRC_LOG_ERR, 0, __FILE__, __LINE__, __FUNCTION__, fmt, ## __VA_ARGS__)
8989
#define irc_warn(fmt, ...) __irc_log(IRC_LOG_WARN, 0, __FILE__, __LINE__, __FUNCTION__, fmt, ## __VA_ARGS__)
9090
#define irc_info(fmt, ...) __irc_log(IRC_LOG_INFO, 0, __FILE__, __LINE__, __FUNCTION__, fmt, ## __VA_ARGS__)
91+
#define irc_verbose(fmt, ...) __irc_log(IRC_LOG_VERBOSE, 0, __FILE__, __LINE__, __FUNCTION__, fmt, ## __VA_ARGS__)
9192
#define irc_debug(level, fmt, ...) __irc_log(IRC_LOG_DEBUG, level, __FILE__, __LINE__, __FUNCTION__, fmt, ## __VA_ARGS__)
9293

9394
static void __attribute__ ((format (gnu_printf, 6, 7))) __irc_log(enum irc_log_level level, int sublevel, const char *file, int line, const char *func, const char *fmt, ...)
@@ -709,7 +710,9 @@ static int wait_for_response(struct irc_client *client, char *buf, size_t len, i
709710
}
710711
/* NUL terminate so we can use strstr */
711712
buf[bytes] = '\0'; /* Safe */
712-
printf("%s", buf); /* Print out whatever we received */
713+
/* Print out whatever we receive, typically these are NOTICE messages.
714+
* If it already has a newline, don't add one, but if it doesn't, do. */
715+
irc_verbose("%s%s", buf, strchr(buf, '\n') ? "" : "\n");
713716
if (strstr(buf, s)) {
714717
return 0;
715718
}

irc.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#define LIRC_VERSION_MAJOR 1
1414
#define LIRC_VERSION_MINOR 1
15-
#define LIRC_VERSION_PATCH 0
15+
#define LIRC_VERSION_PATCH 1
1616

1717
/*! \brief Maximum length of an IRC message, including trailing CR LF */
1818
#define IRC_MAX_MSG_LEN 512
@@ -81,6 +81,7 @@ enum irc_log_level {
8181
IRC_LOG_ERR,
8282
IRC_LOG_WARN,
8383
IRC_LOG_INFO,
84+
IRC_LOG_VERBOSE,
8485
IRC_LOG_DEBUG,
8586
};
8687

0 commit comments

Comments
 (0)