Is there a way to redirect stdout/stderr to a string?

Yes, you can redirect it to an std::stringstream: std::stringstream buffer; std::streambuf * old = std::cout.rdbuf(buffer.rdbuf()); std::cout << “Bla” << std::endl; std::string text = buffer.str(); // text will now contain “Bla\n” You can use a simple guard class to make sure the buffer is always reset: struct cout_redirect { cout_redirect( std::streambuf * new_buffer ) : old( … Read more

c popen won’t catch stderr

popen gives you a file handle on a process’ stdout, not its stderr. Its first argument is interpreted as a shell command, so you can do redirections in it: FILE *p = popen(“prog 2>&1”, “r”); or, if you don’t want the stdout at all, FILE *p = popen(“prog 2>&1 >/dev/null”, “r”); (Any other file besides … Read more

Standard input and output units in Fortran 90?

If you have a Fortran 2003 compiler, the intrinsic module iso_fortran_env defines the variables input_unit, output_unit and error_unit which point to standard in, standard out and standard error respectively. I tend to use something like #ifdef f2003 use, intrinsic :: iso_fortran_env, only : stdin=>input_unit, & stdout=>output_unit, & stderr=>error_unit #else #define stdin 5 #define stdout 6 … Read more

How to redirect stderr in Python?

I have a piece of software I wrote for work that captures stderr to a file like so: import sys sys.stderr = open(‘C:\\err.txt’, ‘w’) so it’s definitely possible. I believe your problem is that you are creating two instances of writer. Maybe something more like: import sys class writer(object): log = [] def write(self, data): … Read more

Python logging split between stdout and stderr [duplicate]

This seems to do what I want: #!/usr/bin/python import sys import logging class InfoFilter(logging.Filter): def filter(self, rec): return rec.levelno in (logging.DEBUG, logging.INFO) logger = logging.getLogger(“__name__”) logger.setLevel(logging.DEBUG) h1 = logging.StreamHandler(sys.stdout) h1.setLevel(logging.DEBUG) h1.addFilter(InfoFilter()) h2 = logging.StreamHandler() h2.setLevel(logging.WARNING) logger.addHandler(h1) logger.addHandler(h2)