Added output capture callback.

This commit is contained in:
Daniel Beer 2010-08-17 11:11:48 +12:00
parent 87f89adc44
commit c29b82dfe9
2 changed files with 28 additions and 0 deletions

View File

@ -32,6 +32,9 @@ struct outbuf {
static struct outbuf stdout_buf;
static struct outbuf stderr_buf;
static capture_func_t capture_func;
static void *capture_data;
static int write_text(struct outbuf *out, const char *buf, FILE *fout)
{
int want_color = opdb_get_boolean("color");
@ -47,6 +50,8 @@ static int write_text(struct outbuf *out, const char *buf, FILE *fout)
if (*buf == '\n') {
out->buf[out->len] = 0;
fprintf(fout, "%s\n", out->buf);
if (capture_func)
capture_func(capture_data, out->buf);
out->len = 0;
} else if ((want_color || !out->in_code) &&
out->len + 1 < sizeof(out->buf)) {
@ -90,3 +95,14 @@ void pr_error(const char *prefix)
{
printc_err("%s: %s\n", prefix, strerror(errno));
}
void capture_start(capture_func_t func, void *data)
{
capture_func = func;
capture_data = data;
}
void capture_end(void)
{
capture_func = NULL;
}

View File

@ -30,4 +30,16 @@ int printc_err(const char *fmt, ...);
void pr_error(const char *prefix);
/* Capture output. Capturing is started by calling capture_begin() with
* a callback function. The callback is invoked for each line of output
* printed to either stdout or stderr (output still goes to
* stdout/stderr as well).
*
* Capture is ended by calling capture_end().
*/
typedef void (*capture_func_t)(void *user_data, const char *text);
void capture_start(capture_func_t, void *user_data);
void capture_end(void);
#endif