Blender  V3.3
gpu_shader_log.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2021 Blender Foundation. All rights reserved. */
3 
8 #include "MEM_guardedalloc.h"
9 
10 #include "BLI_dynstr.h"
11 #include "BLI_string.h"
12 #include "BLI_string_utils.h"
13 #include "BLI_vector.hh"
14 
16 #include "gpu_shader_private.hh"
17 
18 #include "CLG_log.h"
19 
20 static CLG_LogRef LOG = {"gpu.shader"};
21 
22 namespace blender::gpu {
23 
24 /* -------------------------------------------------------------------- */
28 /* Number of lines before and after the error line to print for compilation errors. */
29 #define DEBUG_CONTEXT_LINES 0
34 #define DEBUG_DEPENDENCIES 0
35 
37  char *log,
38  const char *stage,
39  const bool error,
40  GPULogParser *parser)
41 {
42  const char line_prefix[] = " | ";
43  char err_col[] = "\033[31;1m";
44  char warn_col[] = "\033[33;1m";
45  char info_col[] = "\033[0;2m";
46  char reset_col[] = "\033[0;0m";
47  char *sources_combined = BLI_string_join_arrayN((const char **)sources.data(), sources.size());
48  DynStr *dynstr = BLI_dynstr_new();
49 
50  if (!CLG_color_support_get(&LOG)) {
51  err_col[0] = warn_col[0] = info_col[0] = reset_col[0] = '\0';
52  }
53 
54  BLI_dynstr_appendf(dynstr, "\n");
55 
56 #if DEBUG_DEPENDENCIES
58  dynstr, "%s%sIncluded files (in order):%s\n", info_col, line_prefix, reset_col);
59 #endif
60 
61  Vector<int64_t> sources_end_line;
62  for (StringRefNull src : sources) {
63  int64_t cursor = 0, line_count = 0;
64  while ((cursor = src.find('\n', cursor) + 1)) {
65  line_count++;
66  }
67  if (sources_end_line.is_empty() == false) {
68  line_count += sources_end_line.last();
69  }
70  sources_end_line.append(line_count);
71 #if DEBUG_DEPENDENCIES
73  if (!filename.is_empty()) {
75  dynstr, "%s%s %s%s\n", info_col, line_prefix, filename.c_str(), reset_col);
76  }
77 #endif
78  }
79  if (sources_end_line.size() == 0) {
80  sources_end_line.append(0);
81  }
82 
83  char *log_line = log, *line_end;
84 
85  LogCursor previous_location;
86 
87  bool found_line_id = false;
88  while ((line_end = strchr(log_line, '\n'))) {
89  /* Skip empty lines. */
90  if (line_end == log_line) {
91  log_line++;
92  continue;
93  }
94 
95  /* Silence not useful lines. */
96  StringRef logref = StringRefNull(log_line).substr(0, (size_t)line_end - (size_t)log_line);
97  if (logref.endswith(" shader failed to compile with the following errors:") ||
98  logref.endswith(" No code generated")) {
99  log_line += (size_t)line_end - (size_t)log_line;
100  continue;
101  }
102 
103  GPULogItem log_item;
104  log_line = parser->parse_line(log_line, log_item);
105 
106  /* Sanitize output. Really bad values can happen when the error line is buggy. */
107  if (log_item.cursor.source >= sources.size()) {
108  log_item.cursor.source = -1;
109  }
110  if (log_item.cursor.row >= sources_end_line.last()) {
111  log_item.cursor.source = -1;
112  log_item.cursor.row = -1;
113  }
114 
115  if (log_item.cursor.row == -1) {
116  found_line_id = false;
117  }
118  else if (log_item.source_base_row && log_item.cursor.source > 0) {
119  log_item.cursor.row += sources_end_line[log_item.cursor.source - 1];
120  }
121 
122  const char *src_line = sources_combined;
123 
124  /* Separate from previous block. */
125  if (previous_location.source != log_item.cursor.source ||
126  previous_location.row != log_item.cursor.row) {
127  BLI_dynstr_appendf(dynstr, "%s%s%s\n", info_col, line_prefix, reset_col);
128  }
129  else if (log_item.cursor.column != previous_location.column) {
130  BLI_dynstr_appendf(dynstr, "%s\n", line_prefix);
131  }
132  /* Print line from the source file that is producing the error. */
133  if ((log_item.cursor.row != -1) && (log_item.cursor.row != previous_location.row ||
134  log_item.cursor.column != previous_location.column)) {
135  const char *src_line_end;
136  found_line_id = false;
137  /* error_line is 1 based in this case. */
138  int src_line_index = 1;
139  while ((src_line_end = strchr(src_line, '\n'))) {
140  if (src_line_index >= log_item.cursor.row) {
141  found_line_id = true;
142  break;
143  }
144  if (src_line_index >= log_item.cursor.row - DEBUG_CONTEXT_LINES) {
145  BLI_dynstr_appendf(dynstr, "%5d | ", src_line_index);
146  BLI_dynstr_nappend(dynstr, src_line, (src_line_end + 1) - src_line);
147  }
148  /* Continue to next line. */
149  src_line = src_line_end + 1;
150  src_line_index++;
151  }
152  /* Print error source. */
153  if (found_line_id) {
154  if (log_item.cursor.row != previous_location.row) {
155  BLI_dynstr_appendf(dynstr, "%5d | ", src_line_index);
156  }
157  else {
158  BLI_dynstr_appendf(dynstr, line_prefix);
159  }
160  BLI_dynstr_nappend(dynstr, src_line, (src_line_end + 1) - src_line);
161  /* Print char offset. */
162  BLI_dynstr_appendf(dynstr, line_prefix);
163  if (log_item.cursor.column != -1) {
164  for (int i = 0; i < log_item.cursor.column; i++) {
165  BLI_dynstr_appendf(dynstr, " ");
166  }
167  BLI_dynstr_appendf(dynstr, "^");
168  }
169  BLI_dynstr_appendf(dynstr, "\n");
170 
171  /* Skip the error line. */
172  src_line = src_line_end + 1;
173  src_line_index++;
174  while ((src_line_end = strchr(src_line, '\n'))) {
175  if (src_line_index > log_item.cursor.row + DEBUG_CONTEXT_LINES) {
176  break;
177  }
178  BLI_dynstr_appendf(dynstr, "%5d | ", src_line_index);
179  BLI_dynstr_nappend(dynstr, src_line, (src_line_end + 1) - src_line);
180  /* Continue to next line. */
181  src_line = src_line_end + 1;
182  src_line_index++;
183  }
184  }
185  }
186  BLI_dynstr_appendf(dynstr, line_prefix);
187 
188  /* Search the correct source index. */
189  int row_in_file = log_item.cursor.row;
190  int source_index = log_item.cursor.source;
191  if (source_index <= 0) {
192  for (auto i : sources_end_line.index_range()) {
193  if (log_item.cursor.row <= sources_end_line[i]) {
194  source_index = i;
195  break;
196  }
197  }
198  }
199  if (source_index > 0) {
200  row_in_file -= sources_end_line[source_index - 1];
201  }
202  /* Print the filename the error line is coming from. */
203  if (source_index > 0) {
205  sources[source_index]);
206  if (!filename.is_empty()) {
207  BLI_dynstr_appendf(dynstr,
208  "%s%s:%d:%d: %s",
209  info_col,
210  filename.c_str(),
211  row_in_file,
212  log_item.cursor.column + 1,
213  reset_col);
214  }
215  }
216 
217  if (log_item.severity == Severity::Error) {
218  BLI_dynstr_appendf(dynstr, "%s%s%s: ", err_col, "Error", info_col);
219  }
220  else if (log_item.severity == Severity::Error) {
221  BLI_dynstr_appendf(dynstr, "%s%s%s: ", warn_col, "Warning", info_col);
222  }
223  /* Print the error itself. */
224  BLI_dynstr_append(dynstr, info_col);
225  BLI_dynstr_nappend(dynstr, log_line, (line_end + 1) - log_line);
226  BLI_dynstr_append(dynstr, reset_col);
227  /* Continue to next line. */
228  log_line = line_end + 1;
229  previous_location = log_item.cursor;
230  }
231  // printf("%s", sources_combined);
232  MEM_freeN(sources_combined);
233 
235 
236  if (((LOG.type->flag & CLG_FLAG_USE) && (LOG.type->level >= 0)) ||
237  (severity >= CLG_SEVERITY_WARN)) {
238  const char *_str = BLI_dynstr_get_cstring(dynstr);
239  CLG_log_str(LOG.type, severity, this->name, stage, _str);
240  MEM_freeN((void *)_str);
241  }
242 
243  BLI_dynstr_free(dynstr);
244 }
245 
246 char *GPULogParser::skip_severity(char *log_line,
247  GPULogItem &log_item,
248  const char *error_msg,
249  const char *warning_msg) const
250 {
251  if (STREQLEN(log_line, error_msg, strlen(error_msg))) {
252  log_line += strlen(error_msg);
253  log_item.severity = Severity::Error;
254  }
255  else if (STREQLEN(log_line, warning_msg, strlen(warning_msg))) {
256  log_line += strlen(warning_msg);
257  log_item.severity = Severity::Warning;
258  }
259  return log_line;
260 }
261 
262 char *GPULogParser::skip_separators(char *log_line, const StringRef separators) const
263 {
264  while (at_any(log_line, separators)) {
265  log_line++;
266  }
267  return log_line;
268 }
269 
270 char *GPULogParser::skip_until(char *log_line, char stop_char) const
271 {
272  char *cursor = log_line;
273  while (!ELEM(cursor[0], '\n', '\0')) {
274  if (cursor[0] == stop_char) {
275  return cursor;
276  }
277  cursor++;
278  }
279  return log_line;
280 }
281 
282 bool GPULogParser::at_number(const char *log_line) const
283 {
284  return log_line[0] >= '0' && log_line[0] <= '9';
285 }
286 
287 bool GPULogParser::at_any(const char *log_line, const StringRef chars) const
288 {
289  return chars.find(log_line[0]) != StringRef::not_found;
290 }
291 
292 int GPULogParser::parse_number(const char *log_line, char **r_new_position) const
293 {
294  return (int)strtol(log_line, r_new_position, 10);
295 }
296 
299 } // namespace blender::gpu
A dynamically sized string ADT.
DynStr * BLI_dynstr_new(void) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
Definition: BLI_dynstr.c:50
void BLI_dynstr_nappend(DynStr *__restrict ds, const char *cstr, int len) ATTR_NONNULL()
Definition: BLI_dynstr.c:94
char * BLI_dynstr_get_cstring(const DynStr *ds) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: BLI_dynstr.c:256
void BLI_dynstr_free(DynStr *ds) ATTR_NONNULL()
Definition: BLI_dynstr.c:281
void BLI_dynstr_appendf(DynStr *__restrict ds, const char *__restrict format,...) ATTR_PRINTF_FORMAT(2
void BLI_dynstr_append(DynStr *__restrict ds, const char *cstr) ATTR_NONNULL()
Definition: BLI_dynstr.c:75
char * BLI_string_join_arrayN(const char *strings[], uint strings_len) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: string_utils.c:384
#define STREQLEN(a, b, n)
#define ELEM(...)
CLG_Severity
Definition: CLG_log.h:85
@ CLG_SEVERITY_WARN
Definition: CLG_log.h:87
@ CLG_SEVERITY_ERROR
Definition: CLG_log.h:88
@ CLG_FLAG_USE
Definition: CLG_log.h:82
void CLG_log_str(CLG_LogType *lg, enum CLG_Severity severity, const char *file_line, const char *fn, const char *message) _CLOG_ATTR_NONNULL(1
int CLG_color_support_get(CLG_LogRef *clg_ref)
Definition: clog.c:787
Read Guarded memory(de)allocation.
constexpr const T * data() const
Definition: BLI_span.hh:203
constexpr int64_t size() const
Definition: BLI_span.hh:240
static constexpr int64_t not_found
constexpr int64_t find(char c, int64_t pos=0) const
constexpr bool is_empty() const
constexpr StringRef substr(int64_t start, int64_t size) const
constexpr bool endswith(StringRef suffix) const
constexpr const char * c_str() const
int64_t size() const
Definition: BLI_vector.hh:694
void append(const T &value)
Definition: BLI_vector.hh:433
const T & last(const int64_t n=0) const
Definition: BLI_vector.hh:663
bool is_empty() const
Definition: BLI_vector.hh:706
IndexRange index_range() const
Definition: BLI_vector.hh:920
char * skip_separators(char *log_line, const StringRef separators) const
char * skip_severity(char *log_line, GPULogItem &log_item, const char *error_msg, const char *warning_msg) const
virtual char * parse_line(char *log_line, GPULogItem &log_item)=0
bool at_number(const char *log_line) const
int parse_number(const char *log_line, char **r_new_position) const
bool at_any(const char *log_line, const StringRef chars) const
char * skip_until(char *log_line, char stop_char) const
void print_log(Span< const char * > sources, char *log, const char *stage, bool error, GPULogParser *parser)
EvaluationStage stage
Definition: deg_eval.cc:89
SyclQueue void void * src
#define DEBUG_CONTEXT_LINES
static CLG_LogRef LOG
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
ccl_device_inline float3 log(float3 v)
Definition: math_float3.h:397
static void error(const char *str)
Definition: meshlaplacian.c:51
StringRefNull gpu_shader_dependency_get_filename_from_source_string(const StringRefNull source_string)
__int64 int64_t
Definition: stdint.h:89
CLG_LogType * type
Definition: CLG_log.h:106
enum CLG_LogFlag flag
Definition: CLG_log.h:101
int level
Definition: CLG_log.h:100