From: Andre Noll Date: Tue, 7 Jan 2014 16:55:32 +0000 (+0000) Subject: gui: Make curses_log() work also when curses is not active. X-Git-Tag: v0.5.3~12^2~25 X-Git-Url: http://git.tue.mpg.de/?a=commitdiff_plain;h=fc002ac17bca672e912dfc0a390b20eb79ed87a0;p=paraslash.git gui: Make curses_log() work also when curses is not active. This allows to get rid of all fprintf(stderr, ...) constructs and simplifies the code a bit. --- diff --git a/gui.c b/gui.c index 57e922b0..6e3c3e1e 100644 --- a/gui.c +++ b/gui.c @@ -383,7 +383,6 @@ static int first_visible_rbe(unsigned *lines) int rbe_lines; if (!rbe) return i - 1; -// fprintf(stderr, "found: %s\n", rbe->msg); rbe_lines = NUM_LINES(rbe->len); if (rbe_lines > bot.lines) return -1; @@ -467,7 +466,6 @@ static void rb_add_entry(int color, char *msg) new->len = len; new->msg = msg; old = ringbuffer_add(bot_win_rb, new); -// fprintf(stderr, "added: %s\n", new->msg); if (old) { free(old->msg); free(old); @@ -516,29 +514,22 @@ static int loglevel; static __printf_2_3 void curses_log(int ll, const char *fmt,...) { - int color; - char *msg; va_list ap; - unsigned bytes; - if (ll < loglevel || !curses_active()) + if (ll < loglevel) return; - switch (ll) { - case LL_DEBUG: - case LL_INFO: - case LL_NOTICE: - color = COLOR_MSG; - break; - default: - color = COLOR_ERRMSG; - } va_start(ap, fmt); - bytes = xvasprintf(&msg, fmt, ap); + if (curses_active()) { + int color = ll <= LL_NOTICE? COLOR_MSG : COLOR_ERRMSG; + char *msg; + unsigned bytes = xvasprintf(&msg, fmt, ap); + if (bytes > 0 && msg[bytes - 1] == '\n') + msg[bytes - 1] = '\0'; /* cut trailing newline */ + rb_add_entry(color, msg); + wrefresh(bot.win); + } else if (cmd_pid <= 0) /* no external command running */ + vfprintf(stderr, fmt, ap); va_end(ap); - if (bytes > 0 && msg[bytes - 1] == '\n') - msg[bytes - 1] = '\0'; /* cut trailing newline */ - rb_add_entry(color, msg); - wrefresh(bot.win); } __printf_2_3 void (*para_log)(int, const char*, ...) = curses_log; @@ -1247,7 +1238,6 @@ static void com_scroll_up(void) break; rbe_lines = NUM_LINES(rbe->len); lines += rbe_lines; -// fprintf(stderr, "msg: %s\n", rbe->msg); wattron(bot.win, COLOR_PAIR(rbe->color)); waddstr(bot.win, "\n"); waddstr(bot.win, rbe->msg); @@ -1464,11 +1454,9 @@ int main(int argc, char *argv[]) if (conf.help_given || conf.detailed_help_given) print_help_and_die(); cf = configfile_exists(); - if (!cf && conf.config_file_given) { - fprintf(stderr, "can not read config file %s\n", + if (!cf && conf.config_file_given) + msg_n_exit(EXIT_FAILURE, "can not read config file %s\n", conf.config_file_arg); - exit(EXIT_FAILURE); - } if (cf) { struct gui_cmdline_parser_params params = { .override = 0, @@ -1480,10 +1468,8 @@ int main(int argc, char *argv[]) gui_cmdline_parser_config_file(cf, &conf, ¶ms); loglevel = get_loglevel_by_name(conf.loglevel_arg); } - if (check_key_map_args() < 0) { - fprintf(stderr, "invalid key map\n"); - exit(EXIT_FAILURE); - } + if (check_key_map_args() < 0) + msg_n_exit(EXIT_FAILURE, "invalid key map\n"); theme_init(conf.theme_arg, &theme); setup_signal_handling(); bot_win_rb = ringbuffer_new(RINGBUFFER_SIZE);