From ed349cb1d3481182214dd43572745cc2a89a5dec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Janouch?= Date: Sun, 28 Jun 2015 16:16:19 +0200 Subject: [PATCH] degesch: logging cleanup --- degesch.c | 105 +++++++++++++++++++++++++----------------------------- 1 file changed, 48 insertions(+), 57 deletions(-) diff --git a/degesch.c b/degesch.c index d951e60..22a9a4c 100644 --- a/degesch.c +++ b/degesch.c @@ -2471,9 +2471,9 @@ log_formatter (struct app_context *ctx, struct buffer_line *line = buffer_line_new (); line->flags = flags; line->when = time (NULL); - line->formatter = xmalloc (sizeof *line->formatter); // Move the formater inside + line->formatter = xmalloc (sizeof *line->formatter); *line->formatter = *f; LIST_APPEND_WITH_TAIL (buffer->lines, buffer->lines_tail, line); @@ -2540,6 +2540,22 @@ log_full (struct app_context *ctx, struct server *s, struct buffer *buffer, // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +// Lines that are used in more than one place + +#define log_nick_self(s, buffer, new_) \ + log_server_status ((s), (buffer), "You are now known as #n", (new_)) +#define log_nick(s, buffer, old, new_) \ + log_server_status ((s), (buffer), "#n is now known as #n", (old), (new_)) + +#define log_outcoming_notice(s, buffer, who, text) \ + log_server_status ((s), (buffer), "#s(#n): #m", "Notice", (who), (text)) +#define log_outcoming_privmsg(s, buffer, prefixes, who, text) \ + log_server ((s), (buffer), 0, "<#s#n> #m", (prefixes), (who), (text)) +#define log_outcoming_action(s, buffer, who, text) \ + log_server ((s), (buffer), 0, " #a*#r #n #m", ATTR_ACTION, (who), (text)) + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + static struct buffer * buffer_by_name (struct app_context *ctx, const char *name) { @@ -4472,13 +4488,10 @@ irc_handle_nick (struct server *s, const struct irc_message *msg) str_map_find (&s->irc_buffer_map, user->nickname); if (pm_buffer) { - // TODO: make macros of the two cases? (They are repeated below.) if (irc_is_this_us (s, msg->prefix)) - log_server (s, pm_buffer, BUFFER_LINE_STATUS, "#s #n", - "You are now known as", new_nickname); + log_nick_self (s, pm_buffer, new_nickname); else - log_server (s, pm_buffer, BUFFER_LINE_STATUS, "#n #s #n", - msg->prefix, "is now known as", new_nickname); + log_nick (s, pm_buffer, msg->prefix, new_nickname); } // The new nickname may collide with a user referenced by a PM buffer, @@ -4533,10 +4546,8 @@ irc_handle_nick (struct server *s, const struct irc_message *msg) while ((buffer = str_map_iter_next (&iter))) { // We've already done that - if (buffer == pm_buffer) - continue; - log_server (s, buffer, BUFFER_LINE_STATUS, "#s #n", - "You are now known as", new_nickname); + if (buffer != pm_buffer) + log_nick_self (s, buffer, new_nickname); } } else @@ -4547,9 +4558,7 @@ irc_handle_nick (struct server *s, const struct irc_message *msg) struct buffer *buffer = str_map_find (&s->irc_buffer_map, iter->channel->name); hard_assert (buffer != NULL); - - log_server (s, pm_buffer, BUFFER_LINE_STATUS, "#n #s #n", - msg->prefix, "is now known as", new_nickname); + log_nick (s, buffer, msg->prefix, new_nickname); } } @@ -4581,15 +4590,13 @@ irc_handle_notice_text (struct server *s, if (buffer) { - // TODO: factor out? char *nick = irc_cut_nickname (msg->prefix); // IRCv3.2 echo-message could otherwise cause us to highlight ourselves if (!irc_is_this_us (s, msg->prefix) && irc_is_highlight (s, text->str)) log_server (s, buffer, BUFFER_LINE_STATUS | BUFFER_LINE_HIGHLIGHT, "#a#s(#S)#r: #m", ATTR_HIGHLIGHT, "Notice", nick, text->str); else - log_server (s, buffer, BUFFER_LINE_STATUS, - "#s(#n): #m", "Notice", msg->prefix, text->str); + log_outcoming_notice (s, buffer, msg->prefix, text->str); free (nick); } } @@ -4745,44 +4752,39 @@ irc_handle_privmsg_text (struct server *s, { const char *target = msg->params.vector[0]; struct buffer *buffer = irc_get_buffer_for_message (s, msg, target); + char *nick = irc_cut_nickname (msg->prefix); const char *prefixes = ""; if (irc_is_channel (s, target)) { - char *nickname = irc_cut_nickname (msg->prefix); - struct user *user; struct channel *channel; struct channel_user *channel_user; - if ((user = str_map_find (&s->irc_users, nickname)) + if ((user = str_map_find (&s->irc_users, nick)) && (channel = str_map_find (&s->irc_channels, target)) && (channel_user = irc_channel_get_user (channel, user))) prefixes = channel_user->prefixes.str; - - free (nickname); } if (buffer) { - // TODO: some more obvious indication of highlights // IRCv3.2 echo-message could otherwise cause us to highlight ourselves - bool is_highlight = !irc_is_this_us (s, msg->prefix) - && irc_is_highlight (s, text->str); - - // TODO: factor out? - char *nick = irc_cut_nickname (msg->prefix); - if (is_action) - log_server (s, buffer, is_highlight ? BUFFER_LINE_HIGHLIGHT : 0, - " #a*#r #n #m", - is_highlight ? ATTR_HIGHLIGHT : ATTR_ACTION, - msg->prefix, text->str); - else if (is_highlight) + if (irc_is_this_us (s, msg->prefix) || !irc_is_highlight (s, text->str)) + { + if (is_action) + log_outcoming_action (s, buffer, nick, text->str); + else + log_outcoming_privmsg (s, buffer, prefixes, nick, text->str); + } + else if (is_action) + log_server (s, buffer, BUFFER_LINE_HIGHLIGHT, + " #a*#r #n #m", ATTR_HIGHLIGHT, msg->prefix, text->str); + else log_server (s, buffer, BUFFER_LINE_HIGHLIGHT, "#a<#s#s>#r #m", ATTR_HIGHLIGHT, prefixes, nick, text->str); - else - log_server (s, buffer, 0, "<#s#n> #m", prefixes, nick, text->str); - free (nick); } + + free (nick); } static void @@ -5700,28 +5702,24 @@ end: } static void -log_outcoming_action (struct server *s, +log_autosplit_action (struct server *s, struct send_autosplit_args *a, struct buffer *buffer, const char *line) { (void) a; if (buffer && soft_assert (s->irc_user)) - { - // TODO: factor out, copied from irc_handle_privmsg_text() - log_server (s, buffer, 0, " #a*#r #n #m", - ATTR_ACTION, s->irc_user->nickname, line); - } + log_outcoming_action (s, buffer, s->irc_user->nickname, line); // This can only be sent from a user or channel buffer } #define SEND_AUTOSPLIT_ACTION(s, target, message) \ send_autosplit_message ((s), (struct send_autosplit_args) \ - { "PRIVMSG", (target), (message), log_outcoming_action, \ + { "PRIVMSG", (target), (message), log_autosplit_action, \ "\x01" "ACTION ", "\x01" }) static void -log_outcoming_privmsg (struct server *s, +log_autosplit_privmsg (struct server *s, struct send_autosplit_args *a, struct buffer *buffer, const char *line) { const char *prefixes = ""; @@ -5737,36 +5735,29 @@ log_outcoming_privmsg (struct server *s, } if (buffer && soft_assert (s->irc_user)) - { - // TODO: factor out, copied from irc_handle_privmsg_text() - log_server (s, buffer, 0, - "<#s#n> #m", prefixes, s->irc_user->nickname, line); - } + log_outcoming_privmsg (s, buffer, + prefixes, s->irc_user->nickname, line); else log_server_status (s, s->buffer, "MSG(#n): #m", a->target, line); } #define SEND_AUTOSPLIT_PRIVMSG(s, target, message) \ send_autosplit_message ((s), (struct send_autosplit_args) \ - { "PRIVMSG", (target), (message), log_outcoming_privmsg, "", "" }) + { "PRIVMSG", (target), (message), log_autosplit_privmsg, "", "" }) static void -log_outcoming_notice (struct server *s, +log_autosplit_notice (struct server *s, struct send_autosplit_args *a, struct buffer *buffer, const char *line) { if (buffer && soft_assert (s->irc_user)) - { - // TODO: factor out, copied from irc_handle_notice_text() - log_server (s, buffer, BUFFER_LINE_STATUS, - "#s(#n): #m", "Notice", s->irc_user->nickname, line); - } + log_outcoming_notice (s, buffer, s->irc_user->nickname, line); else log_server_status (s, s->buffer, "Notice -> #n: #m", a->target, line); } #define SEND_AUTOSPLIT_NOTICE(s, target, message) \ send_autosplit_message ((s), (struct send_autosplit_args) \ - { "NOTICE", (target), (message), log_outcoming_notice, "", "" }) + { "NOTICE", (target), (message), log_autosplit_notice, "", "" }) // --- Configuration dumper ----------------------------------------------------