Added output capture callback.
This commit is contained in:
parent
87f89adc44
commit
c29b82dfe9
16
output.c
16
output.c
|
@ -32,6 +32,9 @@ struct outbuf {
|
||||||
static struct outbuf stdout_buf;
|
static struct outbuf stdout_buf;
|
||||||
static struct outbuf stderr_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)
|
static int write_text(struct outbuf *out, const char *buf, FILE *fout)
|
||||||
{
|
{
|
||||||
int want_color = opdb_get_boolean("color");
|
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') {
|
if (*buf == '\n') {
|
||||||
out->buf[out->len] = 0;
|
out->buf[out->len] = 0;
|
||||||
fprintf(fout, "%s\n", out->buf);
|
fprintf(fout, "%s\n", out->buf);
|
||||||
|
if (capture_func)
|
||||||
|
capture_func(capture_data, out->buf);
|
||||||
out->len = 0;
|
out->len = 0;
|
||||||
} else if ((want_color || !out->in_code) &&
|
} else if ((want_color || !out->in_code) &&
|
||||||
out->len + 1 < sizeof(out->buf)) {
|
out->len + 1 < sizeof(out->buf)) {
|
||||||
|
@ -90,3 +95,14 @@ void pr_error(const char *prefix)
|
||||||
{
|
{
|
||||||
printc_err("%s: %s\n", prefix, strerror(errno));
|
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;
|
||||||
|
}
|
||||||
|
|
12
output.h
12
output.h
|
@ -30,4 +30,16 @@ int printc_err(const char *fmt, ...);
|
||||||
|
|
||||||
void pr_error(const char *prefix);
|
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
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue