Repository: gabime/spdlog
File: include/spdlog/sinks/ansicolor_sink.h (C/C++)

Code before:
    void flush() override
    {
        std::lock_guard<mutex_t> lock(mutex_);
        fflush(target_file_);
    }

    void set_pattern(const std::string &pattern) final
    {
        std::lock_guard<mutex_t> lock(mutex_);
        formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern));
    }

    void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) override
    {
        std::lock_guard<mutex_t> lock(mutex_);
        formatter_ = std::move(sink_formatter);
    }

    bool should_color()
    {
        return should_do_colors_;
    }

    void force_color_output(bool value)
    {
        should_do_colors_ = value || can_do_colors_;
    }

private:
    void print_ccode_(const std::string &color_code)
    {
        fwrite(color_code.data(), sizeof(char), color_code.size(), target_file_);
    }
    void print_range_(const fmt::memory_buffer &formatted, size_t start, size_t end)
    {
        fwrite(formatted.data() + start, sizeof(char), end - start, target_file_);
    }

    FILE *target_file_;
    mutex_t &mutex_;

    bool can_do_colors_;
    bool should_do_colors_;
    std::unordered_map<level::level_enum, std::string, level::level_hasher> colors_;
};

using ansicolor_stdout_sink_mt = ansicolor_sink<details::console_stdout, details::console_mutex>;
using ansicolor_stdout_sink_st = ansicolor_sink<details::console_stdout, details::console_nullmutex>;

using ansicolor_stderr_sink_mt = ansicolor_sink<details::console_stderr, details::console_mutex>;
using ansicolor_stderr_sink_st = ansicolor_sink<details::console_stderr, details::console_nullmutex>;

Review comment (suggestion):
A bit confusing. It is called force so should be forced.  I think can_do_colors_ is not needed. just set here should_do_colors_ = value

Code after:
    {
        std::lock_guard<mutex_t> lock(mutex_);
        fflush(target_file_);
    }

    void set_pattern(const std::string &pattern) final
    {
        std::lock_guard<mutex_t> lock(mutex_);
        formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern));
    }

    void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) override
    {
        std::lock_guard<mutex_t> lock(mutex_);
        formatter_ = std::move(sink_formatter);
    }

    bool should_color()
    {
        return should_do_colors_;
    }

    void 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;
        }
    }

private:
    void print_ccode_(const std::string &color_code)
    {
        fwrite(color_code.data(), sizeof(char), color_code.size(), target_file_);
    }
    void print_range_(const fmt::memory_buffer &formatted, size_t start, size_t end)
    {
        fwrite(formatted.data() + start, sizeof(char), end - start, target_file_);
    }

    FILE *target_file_;
    mutex_t &mutex_;