Is it possible to intercept cout and cerr?

I have a library that sends messages to the console using std :: cout and std :: cerr.

I know, I can redirect this in my graphics application. But, what interests me is if there is a way to determine if cout or cerr was used before the message really turned red?

Background: I would like to be able to change the color of the text and put before the message a line like "Error:" if the text is received from cerr.

eg. if there was a line

cerr << "File not found" << endl;

there is some kind of callback or event in the library that I can use to make sure that I can execute

cerr << "Error: ";

for my part, before the original error message without touching the source library?

Note that redirecting to a string, as in the redirect stdout / stderr question to a string , will require that I know when the library creates the output and does something with that string. Instead, I am looking for a kind of automatic mechanism.

+4
source share

Source: https://habr.com/ru/post/1666621/


All Articles