Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-11-15 09:58:15

0001 // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
0002 // Distributed under the MIT License (http://opensource.org/licenses/MIT)
0003 
0004 #pragma once
0005 
0006 #ifndef SPDLOG_HEADER_ONLY
0007 #    include <spdlog/sinks/ansicolor_sink.h>
0008 #endif
0009 
0010 #include <spdlog/pattern_formatter.h>
0011 #include <spdlog/details/os.h>
0012 
0013 namespace spdlog {
0014 namespace sinks {
0015 
0016 template<typename ConsoleMutex>
0017 SPDLOG_INLINE ansicolor_sink<ConsoleMutex>::ansicolor_sink(FILE *target_file, color_mode mode)
0018     : target_file_(target_file)
0019     , mutex_(ConsoleMutex::mutex())
0020     , formatter_(details::make_unique<spdlog::pattern_formatter>())
0021 
0022 {
0023     set_color_mode(mode);
0024     colors_[level::trace] = to_string_(white);
0025     colors_[level::debug] = to_string_(cyan);
0026     colors_[level::info] = to_string_(green);
0027     colors_[level::warn] = to_string_(yellow_bold);
0028     colors_[level::err] = to_string_(red_bold);
0029     colors_[level::critical] = to_string_(bold_on_red);
0030     colors_[level::off] = to_string_(reset);
0031 }
0032 
0033 template<typename ConsoleMutex>
0034 SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::set_color(level::level_enum color_level, string_view_t color)
0035 {
0036     std::lock_guard<mutex_t> lock(mutex_);
0037     colors_[static_cast<size_t>(color_level)] = to_string_(color);
0038 }
0039 
0040 template<typename ConsoleMutex>
0041 SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::log(const details::log_msg &msg)
0042 {
0043     // Wrap the originally formatted message in color codes.
0044     // If color is not supported in the terminal, log as is instead.
0045     std::lock_guard<mutex_t> lock(mutex_);
0046     msg.color_range_start = 0;
0047     msg.color_range_end = 0;
0048     memory_buf_t formatted;
0049     formatter_->format(msg, formatted);
0050     if (should_do_colors_ && msg.color_range_end > msg.color_range_start)
0051     {
0052         // before color range
0053         print_range_(formatted, 0, msg.color_range_start);
0054         // in color range
0055         print_ccode_(colors_[static_cast<size_t>(msg.level)]);
0056         print_range_(formatted, msg.color_range_start, msg.color_range_end);
0057         print_ccode_(reset);
0058         // after color range
0059         print_range_(formatted, msg.color_range_end, formatted.size());
0060     }
0061     else // no color
0062     {
0063         print_range_(formatted, 0, formatted.size());
0064     }
0065     fflush(target_file_);
0066 }
0067 
0068 template<typename ConsoleMutex>
0069 SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::flush()
0070 {
0071     std::lock_guard<mutex_t> lock(mutex_);
0072     fflush(target_file_);
0073 }
0074 
0075 template<typename ConsoleMutex>
0076 SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::set_pattern(const std::string &pattern)
0077 {
0078     std::lock_guard<mutex_t> lock(mutex_);
0079     formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern));
0080 }
0081 
0082 template<typename ConsoleMutex>
0083 SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter)
0084 {
0085     std::lock_guard<mutex_t> lock(mutex_);
0086     formatter_ = std::move(sink_formatter);
0087 }
0088 
0089 template<typename ConsoleMutex>
0090 SPDLOG_INLINE bool ansicolor_sink<ConsoleMutex>::should_color()
0091 {
0092     return should_do_colors_;
0093 }
0094 
0095 template<typename ConsoleMutex>
0096 SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::set_color_mode(color_mode mode)
0097 {
0098     switch (mode)
0099     {
0100     case color_mode::always:
0101         should_do_colors_ = true;
0102         return;
0103     case color_mode::automatic:
0104         should_do_colors_ = details::os::in_terminal(target_file_) && details::os::is_color_terminal();
0105         return;
0106     case color_mode::never:
0107         should_do_colors_ = false;
0108         return;
0109     default:
0110         should_do_colors_ = false;
0111     }
0112 }
0113 
0114 template<typename ConsoleMutex>
0115 SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::print_ccode_(const string_view_t &color_code)
0116 {
0117     fwrite(color_code.data(), sizeof(char), color_code.size(), target_file_);
0118 }
0119 
0120 template<typename ConsoleMutex>
0121 SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::print_range_(const memory_buf_t &formatted, size_t start, size_t end)
0122 {
0123     fwrite(formatted.data() + start, sizeof(char), end - start, target_file_);
0124 }
0125 
0126 template<typename ConsoleMutex>
0127 SPDLOG_INLINE std::string ansicolor_sink<ConsoleMutex>::to_string_(const string_view_t &sv)
0128 {
0129     return std::string(sv.data(), sv.size());
0130 }
0131 
0132 // ansicolor_stdout_sink
0133 template<typename ConsoleMutex>
0134 SPDLOG_INLINE ansicolor_stdout_sink<ConsoleMutex>::ansicolor_stdout_sink(color_mode mode)
0135     : ansicolor_sink<ConsoleMutex>(stdout, mode)
0136 {}
0137 
0138 // ansicolor_stderr_sink
0139 template<typename ConsoleMutex>
0140 SPDLOG_INLINE ansicolor_stderr_sink<ConsoleMutex>::ansicolor_stderr_sink(color_mode mode)
0141     : ansicolor_sink<ConsoleMutex>(stderr, mode)
0142 {}
0143 
0144 } // namespace sinks
0145 } // namespace spdlog