File indexing completed on 2024-05-19 05:33:31
0001 /* 0002 SPDX-FileCopyrightText: 2016 Ivan Čukić <ivan.cukic(at)kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0005 */ 0006 0007 #ifndef UTILS_DEBUG_H 0008 #define UTILS_DEBUG_H 0009 0010 #include <iostream> 0011 #include <string> 0012 0013 // Black 0;30 Dark Gray 1;30 0014 // Blue 0;34 Light Blue 1;34 0015 // Green 0;32 Light Green 1;32 0016 // Cyan 0;36 Light Cyan 1;36 0017 // Red 0;31 Light Red 1;31 0018 // Purple 0;35 Light Purple 1;35 0019 // Brown 0;33 Yellow 1;33 0020 // Light Gray 0;37 White 1;37 0021 0022 0023 #define COLOR_RESET "\033[0m" 0024 0025 #define DEBUG_COLOR_GRAY "\033[30;1m" 0026 #define DEBUG_COLOR_RED "\033[31;1m" 0027 #define DEBUG_COLOR_GREEN "\033[32;1m" 0028 #define DEBUG_COLOR_YELLOW "\033[33;1m" 0029 #define DEBUG_COLOR_BLUE "\033[34;1m" 0030 #define DEBUG_COLOR_PURPLE "\033[35;1m" 0031 #define DEBUG_COLOR_CYAN "\033[36;1m" 0032 0033 #define DEBUG_COLOR_SLIM_BLUE "\033[34m" 0034 #define DEBUG_COLOR_SLIM_GREEN "\033[32m" 0035 #define DEBUG_COLOR_SLIM_CYAN "\033[36m" 0036 #define DEBUG_COLOR_SLIM_RED "\033[31m" 0037 #define DEBUG_COLOR_SLIM_PURPLE "\033[35m" 0038 #define DEBUG_COLOR_SLIM_YELLOW "\033[33m" 0039 #define DEBUG_COLOR_SLIM_GRAY "\033[30m" 0040 0041 #define DEBUG_COLOR_VALUE COLOR_RESET DEBUG_COLOR_SLIM_BLUE 0042 0043 namespace debug { 0044 0045 template <typename T> 0046 struct pretty_ptr { 0047 pretty_ptr(T* ptr) 0048 : ptr{ptr} 0049 { 0050 } 0051 0052 T *ptr; 0053 }; 0054 0055 0056 template <typename T> 0057 std::string colorize(const T& value) 0058 { 0059 short hash = ((intptr_t)value) % 8; 0060 std::string basecol(DEBUG_COLOR_GRAY); 0061 basecol[3] = '0' + hash; 0062 0063 return basecol + std::to_string(value) + COLOR_RESET; 0064 } 0065 0066 0067 template <typename T> 0068 std::ostream& operator<< (std::ostream& out, const pretty_ptr<T>& pretty) 0069 { 0070 short ptrhash = ((intptr_t)pretty.ptr / 32) % 8; 0071 // std::string basecol("\033[30m"); 0072 std::string basecol(DEBUG_COLOR_GRAY); 0073 basecol[3] = '0' + ptrhash; 0074 0075 return out << basecol << '[' << (void*)pretty.ptr << ']' << COLOR_RESET; 0076 } 0077 0078 0079 template <typename T> 0080 std::ostream& debug_for(T* ptr) 0081 { 0082 return std::cerr << pretty_ptr{ptr} << ' '; 0083 } 0084 0085 0086 enum class color { 0087 gray, 0088 red, 0089 green, 0090 yellow, 0091 blue, 0092 purple, 0093 cyan 0094 }; 0095 0096 0097 template <typename = void> 0098 class debug_impl { 0099 public: 0100 template <typename T> 0101 debug_impl& operator<< (const T& value) 0102 { 0103 m_out << value; 0104 return *this; 0105 } 0106 0107 debug_impl& operator<< (color c) 0108 { 0109 m_out << ( c == color::gray ? DEBUG_COLOR_GRAY 0110 : c == color::red ? DEBUG_COLOR_RED 0111 : c == color::green ? DEBUG_COLOR_GREEN 0112 : c == color::yellow ? DEBUG_COLOR_YELLOW 0113 : c == color::blue ? DEBUG_COLOR_BLUE 0114 : c == color::purple ? DEBUG_COLOR_PURPLE 0115 : c == color::cyan ? DEBUG_COLOR_CYAN 0116 : COLOR_RESET ); 0117 return *this; 0118 } 0119 0120 debug_impl() 0121 : m_out{std::cerr} 0122 { 0123 } 0124 0125 debug_impl(color c) 0126 : m_out{std::cerr} 0127 { 0128 operator<<(c); 0129 } 0130 0131 ~debug_impl() 0132 { 0133 m_out << COLOR_RESET << std::endl; 0134 } 0135 0136 0137 private: 0138 std::ostream& m_out; 0139 }; 0140 0141 0142 inline debug_impl<> out() 0143 { 0144 return debug_impl<>{}; 0145 } 0146 0147 0148 inline debug_impl<> out(color c) 0149 { 0150 return debug_impl<>{c}; 0151 } 0152 0153 0154 } // namespace debug 0155 0156 #endif // include guard end 0157