Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using C/C++, how do you print to stdout in color, but only if the terminal supports it?

Tags:

People also ask

Does printf write to stdout?

printf by default writes on stdout , if you want to write to a specific stream you should use fprintf which accepts a FILE* as the destination stream. Also it's "std" out because it's called "standard" output.

How do you change the color of a terminal in C++?

The simplest way you can do is: #include <stdlib. h> system("Color F3");


I have a program that log things to STDOUT.

Those log entries have an associated "log level" (debug, warning, information, and so on) and I'd like to give the user the ability to color this output, depending on the log level.

What is the way of determining if STDOUT is attached to a terminal that is-color capable ? And how can I output my text with colors then ?

I'm looking for a solution that involves only C or C++ calls. I guess curses (or ncurses) has something to ease this, but I'd like to avoid its use to keep my dependencies to a minimum.