From 66d48b272e6379acb5ee5f1df532e472e1b142fd Mon Sep 17 00:00:00 2001 From: Jeroen Bakker Date: Tue, 29 Jun 2021 09:52:31 +0200 Subject: Cleanup: GPU Shader Log Parsing. - Added functions to check if the cursor is at a number. - Added function to parse a number. - Joined skip_separator functions. - Added function to check if cursor is at any given set of characters. --- source/blender/gpu/intern/gpu_shader_log.cc | 31 +++++++++++++++++++++++-- source/blender/gpu/intern/gpu_shader_private.hh | 7 +++++- source/blender/gpu/opengl/gl_shader_log.cc | 18 +++++++------- 3 files changed, 44 insertions(+), 12 deletions(-) diff --git a/source/blender/gpu/intern/gpu_shader_log.cc b/source/blender/gpu/intern/gpu_shader_log.cc index 5859afb7fbf..12459b4b721 100644 --- a/source/blender/gpu/intern/gpu_shader_log.cc +++ b/source/blender/gpu/intern/gpu_shader_log.cc @@ -177,14 +177,41 @@ char *GPULogParser::skip_severity(char *log_line, return log_line; } -char *GPULogParser::skip_separators(char *log_line, char sep1, char sep2, char sep3) const +char *GPULogParser::skip_separators(char *log_line, const StringRef separators) const { - while (ELEM(log_line[0], sep1, sep2, sep3)) { + while (at_any(log_line, separators)) { log_line++; } return log_line; } +char *GPULogParser::skip_until(char *log_line, char stop_char) const +{ + char *cursor = log_line; + while (!ELEM(cursor[0], '\n', '\0')) { + if (cursor[0] == stop_char) { + return cursor; + } + cursor++; + } + return log_line; +} + +bool GPULogParser::at_number(const char *log_line) const +{ + return log_line[0] >= '0' && log_line[0] <= '9'; +} + +bool GPULogParser::at_any(const char *log_line, const StringRef chars) const +{ + return chars.find(log_line[0]) != StringRef::not_found; +} + +int GPULogParser::parse_number(const char *log_line, char **r_new_position) const +{ + return (int)strtol(log_line, r_new_position, 10); +} + /** \} */ } // namespace blender::gpu diff --git a/source/blender/gpu/intern/gpu_shader_private.hh b/source/blender/gpu/intern/gpu_shader_private.hh index ebdfc3478f8..65720e457d8 100644 --- a/source/blender/gpu/intern/gpu_shader_private.hh +++ b/source/blender/gpu/intern/gpu_shader_private.hh @@ -21,6 +21,7 @@ #pragma once #include "BLI_span.hh" +#include "BLI_string_ref.hh" #include "GPU_shader.h" #include "gpu_shader_interface.hh" @@ -123,7 +124,11 @@ class GPULogParser { GPULogItem &log_item, const char *error_msg, const char *warning_msg) const; - char *skip_separators(char *log_line, char sep1, char sep2, char sep3) const; + char *skip_separators(char *log_line, const StringRef separators) const; + char *skip_until(char *log_line, char stop_char) const; + bool at_number(const char *log_line) const; + bool at_any(const char *log_line, const StringRef chars) const; + int parse_number(const char *log_line, char **r_new_position) const; MEM_CXX_CLASS_ALLOC_FUNCS("GPULogParser"); }; diff --git a/source/blender/gpu/opengl/gl_shader_log.cc b/source/blender/gpu/opengl/gl_shader_log.cc index 393f852b463..174cc63ad81 100644 --- a/source/blender/gpu/opengl/gl_shader_log.cc +++ b/source/blender/gpu/opengl/gl_shader_log.cc @@ -31,24 +31,24 @@ char *GLLogParser::parse_line(char *log_line, GPULogItem &log_item) { /* Skip ERROR: or WARNING:. */ log_line = skip_severity_prefix(log_line, log_item); - log_line = skip_separators(log_line, ':', '(', ' '); + log_line = skip_separators(log_line, "(: "); /* Parse error line & char numbers. */ - if (log_line[0] >= '0' && log_line[0] <= '9') { + if (at_number(log_line)) { char *error_line_number_end; - log_item.cursor.row = (int)strtol(log_line, &error_line_number_end, 10); + log_item.cursor.row = parse_number(log_line, &error_line_number_end); /* Try to fetch the error character (not always available). */ - if (ELEM(error_line_number_end[0], '(', ':') && error_line_number_end[1] != ' ') { - log_item.cursor.column = (int)strtol(error_line_number_end + 1, &log_line, 10); + if (at_any(error_line_number_end, "(:") && at_number(&error_line_number_end[1])) { + log_item.cursor.column = parse_number(error_line_number_end + 1, &log_line); } else { log_line = error_line_number_end; } /* There can be a 3rd number (case of mesa driver). */ - if (ELEM(log_line[0], '(', ':') && log_line[1] >= '0' && log_line[1] <= '9') { + if (at_any(log_line, "(:") && at_number(&log_line[1])) { log_item.cursor.source = log_item.cursor.row; log_item.cursor.row = log_item.cursor.column; - log_item.cursor.column = (int)strtol(log_line + 1, &error_line_number_end, 10); + log_item.cursor.column = parse_number(log_line + 1, &error_line_number_end); log_line = error_line_number_end; } } @@ -65,11 +65,11 @@ char *GLLogParser::parse_line(char *log_line, GPULogItem &log_item) } } - log_line = skip_separators(log_line, ':', ')', ' '); + log_line = skip_separators(log_line, ":) "); /* Skip to message. Avoid redundant info. */ log_line = skip_severity_keyword(log_line, log_item); - log_line = skip_separators(log_line, ':', ')', ' '); + log_line = skip_separators(log_line, ":) "); return log_line; } -- cgit v1.2.3