1 /* -*- mesa-c++ -*- 2 * Copyright 2018-2019 Collabora LTD 3 * Author: Gert Wollny <[email protected]> 4 * SPDX-License-Identifier: MIT 5 */ 6 7 #ifndef SFN_STDERR_STREAMLOG_H 8 #define SFN_STDERR_STREAMLOG_H 9 10 #include "compiler/nir/nir.h" 11 12 #include <fstream> 13 #include <ostream> 14 #include <streambuf> 15 16 namespace r600 { 17 /* Implement some logging for shader-from-nir 18 19 */ 20 21 class stderr_streambuf : public std::streambuf { 22 public: 23 stderr_streambuf(); 24 25 protected: 26 int sync(); 27 int overflow(int c); 28 std::streamsize xsputn(const char *s, std::streamsize n); 29 }; 30 31 class SfnLog { 32 public: 33 enum LogFlag { 34 instr = 1 << 0, 35 r600ir = 1 << 1, 36 cc = 1 << 2, 37 err = 1 << 3, 38 shader_info = 1 << 4, 39 test_shader = 1 << 5, 40 reg = 1 << 6, 41 io = 1 << 7, 42 assembly = 1 << 8, 43 flow = 1 << 9, 44 merge = 1 << 10, 45 tex = 1 << 11, 46 trans = 1 << 12, 47 schedule = 1 << 13, 48 opt = 1 << 14, 49 all = (1 << 15) - 1, 50 nomerge = 1 << 16, 51 steps = 1 << 17, 52 noopt = 1 << 18, 53 warn = 1 << 20, 54 }; 55 56 SfnLog(); 57 58 /** a special handling to set the output level "inline" 59 \param l the level of the following messages 60 */ 61 SfnLog& operator<<(LogFlag const l); 62 63 /* general output routine; output is only given, if the log flags and the 64 * currently active log mask overlap 65 \returns a reference to this object 66 */ 67 template <class T> SfnLog& operator<<(const T& text) 68 { 69 if (m_active_log_flags & m_log_mask) 70 m_output << text; 71 72 return *this; 73 } 74 75 /* A funny construct to enable std::endl to work on this stream 76 idea of Dave Brondsema: 77 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=8567 78 */ 79 SfnLog& operator<<(std::ostream& (*f)(std::ostream&)); 80 81 SfnLog& operator<<(nir_shader& sh); 82 83 SfnLog& operator<<(nir_instr& instr); 84 has_debug_flag(uint64_t flag)85 int has_debug_flag(uint64_t flag) { return (m_log_mask & flag) == flag; } 86 87 private: 88 uint64_t m_active_log_flags; 89 uint64_t m_log_mask; 90 stderr_streambuf m_buf; 91 std::ostream m_output; 92 }; 93 94 class SfnTrace { 95 public: 96 SfnTrace(SfnLog::LogFlag flag, const char *msg); 97 ~SfnTrace(); 98 99 private: 100 SfnLog::LogFlag m_flag; 101 const char *m_msg; 102 static int m_indention; 103 }; 104 105 #ifndef NDEBUG 106 #define SFN_TRACE_FUNC(LEVEL, MSG) SfnTrace __trace(LEVEL, MSG) 107 #else 108 #define SFN_TRACE_FUNC(LEVEL, MSG) 109 #endif 110 111 extern SfnLog sfn_log; 112 113 } // namespace r600 114 #endif // SFN_STDERR_STREAMBUF_H 115