1bf215546Sopenharmony_ci/* -*- mesa-c++  -*-
2bf215546Sopenharmony_ci *
3bf215546Sopenharmony_ci * Copyright (c) 2018-2019 Collabora LTD
4bf215546Sopenharmony_ci *
5bf215546Sopenharmony_ci * Author: Gert Wollny <gert.wollny@collabora.com>
6bf215546Sopenharmony_ci *
7bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
8bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
9bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
10bf215546Sopenharmony_ci * on the rights to use, copy, modify, merge, publish, distribute, sub
11bf215546Sopenharmony_ci * license, and/or sell copies of the Software, and to permit persons to whom
12bf215546Sopenharmony_ci * the Software is furnished to do so, subject to the following conditions:
13bf215546Sopenharmony_ci *
14bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
15bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
16bf215546Sopenharmony_ci * Software.
17bf215546Sopenharmony_ci *
18bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21bf215546Sopenharmony_ci * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
22bf215546Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23bf215546Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24bf215546Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE.
25bf215546Sopenharmony_ci */
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#ifndef SFN_STDERR_STREAMLOG_H
28bf215546Sopenharmony_ci#define SFN_STDERR_STREAMLOG_H
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ci#include <streambuf>
32bf215546Sopenharmony_ci#include <ostream>
33bf215546Sopenharmony_ci#include <fstream>
34bf215546Sopenharmony_ci#include "compiler/nir/nir.h"
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_cinamespace r600 {
37bf215546Sopenharmony_ci/* Implement some logging for shader-from-nir
38bf215546Sopenharmony_ci
39bf215546Sopenharmony_ci*/
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_ciclass stderr_streambuf : public std::streambuf
42bf215546Sopenharmony_ci{
43bf215546Sopenharmony_cipublic:
44bf215546Sopenharmony_ci   stderr_streambuf();
45bf215546Sopenharmony_ciprotected:
46bf215546Sopenharmony_ci   int sync();
47bf215546Sopenharmony_ci   int overflow(int c);
48bf215546Sopenharmony_ci   std::streamsize xsputn ( const char *s, std::streamsize n );
49bf215546Sopenharmony_ci};
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ciclass SfnLog {
52bf215546Sopenharmony_cipublic:
53bf215546Sopenharmony_ci   enum LogFlag {
54bf215546Sopenharmony_ci      instr = 1 << 0,
55bf215546Sopenharmony_ci      r600ir = 1 << 1,
56bf215546Sopenharmony_ci      cc     = 1 << 2,
57bf215546Sopenharmony_ci      err    = 1 << 3,
58bf215546Sopenharmony_ci      shader_info = 1 << 4,
59bf215546Sopenharmony_ci      test_shader = 1 << 5,
60bf215546Sopenharmony_ci      reg = 1 << 6,
61bf215546Sopenharmony_ci      io = 1 << 7,
62bf215546Sopenharmony_ci      assembly = 1 << 8,
63bf215546Sopenharmony_ci      flow = 1 << 9,
64bf215546Sopenharmony_ci      merge = 1 << 10,
65bf215546Sopenharmony_ci      tex = 1 << 11,
66bf215546Sopenharmony_ci      trans = 1 << 12,
67bf215546Sopenharmony_ci      schedule = 1 << 13,
68bf215546Sopenharmony_ci      opt = 1 << 14,
69bf215546Sopenharmony_ci      all = (1 << 15) - 1,
70bf215546Sopenharmony_ci      nomerge = 1 << 16,
71bf215546Sopenharmony_ci      steps = 1 << 17,
72bf215546Sopenharmony_ci      noopt = 1 << 18
73bf215546Sopenharmony_ci   };
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_ci   SfnLog();
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_ci   /** a special handling to set the output level "inline"
78bf215546Sopenharmony_ci       \param l the level of the following messages
79bf215546Sopenharmony_ci     */
80bf215546Sopenharmony_ci   SfnLog& operator << (LogFlag const l);
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_ci   /* general output routine; output is only given, if the log flags and the
83bf215546Sopenharmony_ci    * currently active log mask overlap
84bf215546Sopenharmony_ci      \returns a reference to this object
85bf215546Sopenharmony_ci   */
86bf215546Sopenharmony_ci   template <class T>
87bf215546Sopenharmony_ci   SfnLog& operator << (const T&  text)
88bf215546Sopenharmony_ci   {
89bf215546Sopenharmony_ci      if (m_active_log_flags & m_log_mask)
90bf215546Sopenharmony_ci         m_output << text;
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_ci      return *this;
93bf215546Sopenharmony_ci   }
94bf215546Sopenharmony_ci
95bf215546Sopenharmony_ci   /* A funny construct to enable std::endl to work on this stream
96bf215546Sopenharmony_ci      idea of Dave Brondsema:
97bf215546Sopenharmony_ci      http://gcc.gnu.org/bugzilla/show_bug.cgi?id=8567
98bf215546Sopenharmony_ci    */
99bf215546Sopenharmony_ci   SfnLog& operator << (std::ostream & (*f)(std::ostream&));
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_ci   SfnLog& operator << (nir_shader &sh);
102bf215546Sopenharmony_ci
103bf215546Sopenharmony_ci   SfnLog& operator << (nir_instr& instr);
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_ci   int has_debug_flag(uint64_t flag) {
106bf215546Sopenharmony_ci      return (m_log_mask & flag) == flag;
107bf215546Sopenharmony_ci   }
108bf215546Sopenharmony_ci
109bf215546Sopenharmony_ciprivate:
110bf215546Sopenharmony_ci   uint64_t m_active_log_flags;
111bf215546Sopenharmony_ci   uint64_t m_log_mask;
112bf215546Sopenharmony_ci   stderr_streambuf m_buf;
113bf215546Sopenharmony_ci   std::ostream m_output;
114bf215546Sopenharmony_ci};
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_ciclass SfnTrace {
117bf215546Sopenharmony_cipublic:
118bf215546Sopenharmony_ci   SfnTrace(SfnLog::LogFlag flag, const char *msg);
119bf215546Sopenharmony_ci   ~SfnTrace();
120bf215546Sopenharmony_ciprivate:
121bf215546Sopenharmony_ci   SfnLog::LogFlag m_flag;
122bf215546Sopenharmony_ci   const char *m_msg;
123bf215546Sopenharmony_ci   static int m_indention;
124bf215546Sopenharmony_ci};
125bf215546Sopenharmony_ci
126bf215546Sopenharmony_ci
127bf215546Sopenharmony_ci#ifndef NDEBUG
128bf215546Sopenharmony_ci#define SFN_TRACE_FUNC(LEVEL, MSG) SfnTrace __trace(LEVEL, MSG)
129bf215546Sopenharmony_ci#else
130bf215546Sopenharmony_ci#define SFN_TRACE_FUNC(LEVEL, MSG)
131bf215546Sopenharmony_ci#endif
132bf215546Sopenharmony_ci
133bf215546Sopenharmony_ciextern SfnLog sfn_log;
134bf215546Sopenharmony_ci
135bf215546Sopenharmony_ci}
136bf215546Sopenharmony_ci#endif // SFN_STDERR_STREAMBUF_H
137