#include "esp_rom_sys.h"
ssize_t write_fn(void* cookie, const char* buf, ssize_t size)
{
/* redirect the bytes somewhere; writing to Serial just for an example */
Serial.print("got ");
Serial.print(size);
Serial.println(" bytes:");
Serial.print(" > ");
Serial.write((uint8_t*) buf, size);
Serial.println();
return size;
}
void ets_putc_handler(char c)
{
/* this gets called from various ets_printf / esp_rom_printf calls */
static char buf[256];
static size_t buf_pos = 0;
buf[buf_pos] = c;
buf_pos++;
if (c == '\n' || buf_pos == sizeof(buf)) {
/* flush */
write_fn(NULL, buf, buf_pos);
buf_pos = 0;
}
}
void setup() {
/* redirect stdout */
stdout = funopen(NULL, NULL, &write_fn, NULL, NULL);
static char linebuf[256];
setvbuf(stdout, linebuf, _IOLBF, sizeof(linebuf));
/* redirect ets_printf / esp_rom_printf output */
ets_install_putc1(&ets_putc_handler);
Serial.begin(115200);
printf("this should be intercepted\n");
ets_printf("and this\r\n");
log_e("and this as well");
Serial.println("but not this");
}
void loop() {
delay(10);
}