api: Refactor write_msg to use separate out/err buffers

This commit is contained in:
Thiago de Arruda
2014-06-26 15:29:38 -03:00
parent 0e20afe37e
commit f17668234a

View File

@@ -512,23 +512,24 @@ void vim_unsubscribe(uint64_t channel_id, String event)
/// `emsg` instead of `msg` to print each line) /// `emsg` instead of `msg` to print each line)
static void write_msg(String message, bool to_err) static void write_msg(String message, bool to_err)
{ {
static int pos = 0; static int out_pos = 0, err_pos = 0;
static char line_buf[LINE_BUFFER_SIZE]; static char out_line_buf[LINE_BUFFER_SIZE], err_line_buf[LINE_BUFFER_SIZE];
#define PUSH_CHAR(i, pos, line_buf, msg) \
if (message.data[i] == NL || pos == LINE_BUFFER_SIZE - 1) { \
line_buf[pos] = NUL; \
msg((uint8_t *)line_buf); \
pos = 0; \
continue; \
} \
\
line_buf[pos++] = message.data[i];
for (uint32_t i = 0; i < message.size; i++) { for (uint32_t i = 0; i < message.size; i++) {
if (message.data[i] == NL || pos == LINE_BUFFER_SIZE - 1) { if (to_err) {
// Flush line PUSH_CHAR(i, err_pos, err_line_buf, emsg);
line_buf[pos] = NUL; } else {
if (to_err) { PUSH_CHAR(i, out_pos, out_line_buf, msg);
emsg((uint8_t *)line_buf);
} else {
msg((uint8_t *)line_buf);
}
pos = 0;
continue;
} }
line_buf[pos++] = message.data[i];
} }
} }