Search code examples
c++qt-creatorspdlog

QtCreator "application output" not colored with logs from spdlog (c++)


The title pretty much says it all.

I am running a C++ application that uses SpdLog to format all of my logs. It adds colors according to the level type of the log.

It uses a ansicolor_stdout_sink_mt that works well in the macOS terminal and also if I specify whithin QtCreator to run the app in the terminal.

If I let QtCreator run the app (no terminal) then the output in the Application Output tab is not colored.

I need to have the logs in the Application Output as I want to use the custom output parser since it does not work with the terminal output.

Any idea on how I can manage to have the colors in Application Output ?

Thanks :)


Solution

  • The constructor of that class calls set_color_mode:

    SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::set_color_mode(color_mode mode) {
        switch (mode) {
            case color_mode::always:
                should_do_colors_ = true;
                return;
            case color_mode::automatic:
                should_do_colors_ =
                    details::os::in_terminal(target_file_) && details::os::is_color_terminal();
                return;
            case color_mode::never:
                should_do_colors_ = false;
                return;
            default:
                should_do_colors_ = false;
        }
    }
    

    As you can see, it explicitly asks if the destination file handle is a terminal. Passing color_mode::always to the constructor should do the trick.