[Bf-blender-cvs] [0d1f65e5166] master: GPU: Use CLOG to for debug output

Clément Foucault noreply at git.blender.org
Sat Oct 24 20:07:18 CEST 2020


Commit: 0d1f65e5166c4822d9e01e7d3ce20f7df972550b
Author: Clément Foucault
Date:   Fri Oct 23 19:29:27 2020 +0200
Branches: master
https://developer.blender.org/rB0d1f65e5166c4822d9e01e7d3ce20f7df972550b

GPU: Use CLOG to for debug output

This removes the escape color control caracters when the output
does not supports it (i.e: file output, windows cmd).

===================================================================

M	source/blender/gpu/CMakeLists.txt
M	source/blender/gpu/intern/gpu_debug.cc
M	source/blender/gpu/intern/gpu_shader.cc
M	source/blender/gpu/intern/gpu_shader_interface.cc
M	source/blender/gpu/intern/gpu_shader_private.hh
M	source/blender/gpu/opengl/gl_debug.cc
M	source/blender/gpu/opengl/gl_shader.cc

===================================================================

diff --git a/source/blender/gpu/CMakeLists.txt b/source/blender/gpu/CMakeLists.txt
index 482e4086452..92f540f859f 100644
--- a/source/blender/gpu/CMakeLists.txt
+++ b/source/blender/gpu/CMakeLists.txt
@@ -44,6 +44,7 @@ set(INC
   ../nodes
   ../nodes/intern
 
+  ../../../intern/clog
   ../../../intern/ghost
   ../../../intern/glew-mx
   ../../../intern/guardedalloc
diff --git a/source/blender/gpu/intern/gpu_debug.cc b/source/blender/gpu/intern/gpu_debug.cc
index d7944f0de50..63e7024b74b 100644
--- a/source/blender/gpu/intern/gpu_debug.cc
+++ b/source/blender/gpu/intern/gpu_debug.cc
@@ -74,7 +74,7 @@ void GPU_debug_get_groups_names(int name_buf_len, char *r_name_buf)
   for (StringRef &name : stack) {
     sz += BLI_snprintf_rlen(r_name_buf + sz, name_buf_len - sz, "%s > ", name.data());
   }
-  r_name_buf[sz - 2] = ':';
+  r_name_buf[sz - 3] = '\0';
 }
 
 /* Return true if inside a debug group with the same name. */
diff --git a/source/blender/gpu/intern/gpu_shader.cc b/source/blender/gpu/intern/gpu_shader.cc
index 05c81c2efeb..5f46f57c09a 100644
--- a/source/blender/gpu/intern/gpu_shader.cc
+++ b/source/blender/gpu/intern/gpu_shader.cc
@@ -23,6 +23,7 @@
 
 #include "MEM_guardedalloc.h"
 
+#include "BLI_dynstr.h"
 #include "BLI_math_base.h"
 #include "BLI_math_vector.h"
 #include "BLI_path_util.h"
@@ -47,8 +48,12 @@
 #include "gpu_context_private.hh"
 #include "gpu_shader_private.hh"
 
+#include "CLG_log.h"
+
 extern "C" char datatoc_gpu_shader_colorspace_lib_glsl[];
 
+static CLG_LogRef LOG = {"gpu.shader"};
+
 using namespace blender;
 using namespace blender::gpu;
 
@@ -56,12 +61,21 @@ using namespace blender::gpu;
 /** \name Debug functions
  * \{ */
 
-void Shader::print_errors(Span<const char *> sources, char *log, const char *stage)
+void Shader::print_log(Span<const char *> sources, char *log, const char *stage, const bool error)
 {
   const char line_prefix[] = "      | ";
+  char err_col[] = "\033[31;1m";
+  char warn_col[] = "\033[33;1m";
+  char info_col[] = "\033[0;2m";
+  char reset_col[] = "\033[0;0m";
   char *sources_combined = BLI_string_join_arrayN((const char **)sources.data(), sources.size());
+  DynStr *dynstr = BLI_dynstr_new();
+
+  if (!CLG_color_support_get(&LOG)) {
+    err_col[0] = warn_col[0] = info_col[0] = reset_col[0] = '\0';
+  }
 
-  fprintf(stderr, "GPUShader: Compilation Log : %s : %s\n", this->name, stage);
+  BLI_dynstr_appendf(dynstr, "\n");
 
   char *log_line = log, *line_end;
   char *error_line_number_end;
@@ -136,10 +150,10 @@ void Shader::print_errors(Span<const char *> sources, char *log, const char *sta
     }
     /* Separate from previous block. */
     if (last_error_line != error_line) {
-      fprintf(stderr, "\033[90m%s\033[39m\n", line_prefix);
+      BLI_dynstr_appendf(dynstr, "%s%s%s\n", info_col, line_prefix, reset_col);
     }
     else if (error_char != last_error_char) {
-      fprintf(stderr, "%s\n", line_prefix);
+      BLI_dynstr_appendf(dynstr, "%s\n", line_prefix);
     }
     /* Print line from the source file that is producing the error. */
     if ((error_line != -1) && (error_line != last_error_line || error_char != last_error_char)) {
@@ -159,24 +173,24 @@ void Shader::print_errors(Span<const char *> sources, char *log, const char *sta
       /* Print error source. */
       if (found_line_id) {
         if (error_line != last_error_line) {
-          fprintf(stderr, "%5d | ", src_line_index);
+          BLI_dynstr_appendf(dynstr, "%5d | ", src_line_index);
         }
         else {
-          fprintf(stderr, line_prefix);
+          BLI_dynstr_appendf(dynstr, line_prefix);
         }
-        fwrite(src_line, (src_line_end + 1) - src_line, 1, stderr);
+        BLI_dynstr_nappend(dynstr, src_line, (src_line_end + 1) - src_line);
         /* Print char offset. */
-        fprintf(stderr, line_prefix);
+        BLI_dynstr_appendf(dynstr, line_prefix);
         if (error_char != -1) {
           for (int i = 0; i < error_char; i++) {
-            fprintf(stderr, " ");
+            BLI_dynstr_appendf(dynstr, " ");
           }
-          fprintf(stderr, "^");
+          BLI_dynstr_appendf(dynstr, "^");
         }
-        fprintf(stderr, "\n");
+        BLI_dynstr_appendf(dynstr, "\n");
       }
     }
-    fprintf(stderr, line_prefix);
+    BLI_dynstr_appendf(dynstr, line_prefix);
     /* Skip to message. Avoid redundant info. */
     const char *keywords[] = {"error", "warning"};
     for (int i = 0; i < ARRAY_SIZE(prefix); i++) {
@@ -191,22 +205,32 @@ void Shader::print_errors(Span<const char *> sources, char *log, const char *sta
       log_line++;
     }
     if (type == 0) {
-      fprintf(stderr, "\033[31;1mError\033[0;2m: ");
+      BLI_dynstr_appendf(dynstr, "%s%s%s: ", err_col, "Error", info_col);
     }
     else if (type == 1) {
-      fprintf(stderr, "\033[33;1mWarning\033[0;2m: ");
+      BLI_dynstr_appendf(dynstr, "%s%s%s: ", warn_col, "Warning", info_col);
     }
     /* Print the error itself. */
-    fprintf(stderr, "\033[2m");
-    fwrite(log_line, (line_end + 1) - log_line, 1, stderr);
-    fprintf(stderr, "\033[0m");
+    BLI_dynstr_appendf(dynstr, info_col);
+    BLI_dynstr_nappend(dynstr, log_line, (line_end + 1) - log_line);
+    BLI_dynstr_appendf(dynstr, reset_col);
     /* Continue to next line. */
     log_line = line_end + 1;
     last_error_line = error_line;
     last_error_char = error_char;
   }
-  fprintf(stderr, "\n");
   MEM_freeN(sources_combined);
+
+  CLG_Severity severity = error ? CLG_SEVERITY_ERROR : CLG_SEVERITY_WARN;
+
+  if (((LOG.type->flag & CLG_FLAG_USE) && (LOG.type->level >= 0)) ||
+      (severity >= CLG_SEVERITY_WARN)) {
+    const char *_str = BLI_dynstr_get_cstring(dynstr);
+    CLG_log_str(LOG.type, severity, this->name, stage, _str);
+    MEM_freeN((void *)_str);
+  }
+
+  BLI_dynstr_free(dynstr);
 }
 
 /** \} */
diff --git a/source/blender/gpu/intern/gpu_shader_interface.cc b/source/blender/gpu/intern/gpu_shader_interface.cc
index 4a7c7bc15a3..e5fb8025e7f 100644
--- a/source/blender/gpu/intern/gpu_shader_interface.cc
+++ b/source/blender/gpu/intern/gpu_shader_interface.cc
@@ -85,7 +85,6 @@ void ShaderInterface::debug_print(void)
   char *name_buf = name_buffer_;
   const char format[] = "      | %.8x : %4d : %s\n";
 
-  printf("      \033[1mGPUShaderInterface : \033[0m\n");
   if (attrs.size() > 0) {
     printf("\n    Attributes :\n");
   }
diff --git a/source/blender/gpu/intern/gpu_shader_private.hh b/source/blender/gpu/intern/gpu_shader_private.hh
index b7acc0f9353..eac39dccd81 100644
--- a/source/blender/gpu/intern/gpu_shader_private.hh
+++ b/source/blender/gpu/intern/gpu_shader_private.hh
@@ -70,7 +70,7 @@ class Shader {
   };
 
  protected:
-  void print_errors(Span<const char *> sources, char *log, const char *stage);
+  void print_log(Span<const char *> sources, char *log, const char *stage, const bool error);
 };
 
 /* Syntacting suggar. */
diff --git a/source/blender/gpu/opengl/gl_debug.cc b/source/blender/gpu/opengl/gl_debug.cc
index b2b05124463..848c0c462fe 100644
--- a/source/blender/gpu/opengl/gl_debug.cc
+++ b/source/blender/gpu/opengl/gl_debug.cc
@@ -33,6 +33,8 @@
 #include "GPU_debug.h"
 #include "GPU_platform.h"
 
+#include "CLG_log.h"
+
 #include "glew-mx.h"
 
 #include "gl_context.hh"
@@ -42,6 +44,8 @@
 
 #include <stdio.h>
 
+static CLG_LogRef LOG = {"gpu.debug"};
+
 /* Avoid too much NVidia buffer info in the output log. */
 #define TRIM_NVIDIA_BUFFER_INFO 1
 
@@ -61,8 +65,6 @@ namespace blender::gpu::debug {
 #  define APIENTRY
 #endif
 
-#define VERBOSE 1
-
 static void APIENTRY debug_callback(GLenum UNUSED(source),
                                     GLenum type,
                                     GLuint UNUSED(id),
@@ -86,36 +88,47 @@ static void APIENTRY debug_callback(GLenum UNUSED(source),
 
   const char format[] = "GPUDebug: %s%s%s\033[0m\n";
 
+  const bool use_color = CLG_color_support_get(&LOG);
+
   if (ELEM(severity, GL_DEBUG_SEVERITY_LOW, GL_DEBUG_SEVERITY_NOTIFICATION)) {
-    if (VERBOSE) {
-      fprintf(stderr, format, "\033[2m", "", message);
+    if (((LOG.type->flag & CLG_FLAG_USE) && (LOG.type->level >= CLG_SEVERITY_INFO))) {
+      const char *format = use_color ? "\033[2m%s\033[0m" : "%s";
+      CLG_logf(LOG.type, CLG_SEVERITY_INFO, "Notification", "", format, message);
     }
   }
   else {
     char debug_groups[512] = "";
     GPU_debug_get_groups_names(sizeof(debug_groups), debug_groups);
+    CLG_Severity clog_severity;
 
     switch (type) {
       case GL_DEBUG_TYPE_ERROR:
       case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR:
       case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR:
-        fprintf(stderr, format, "\033[31;1mError\033[39m: ", debug_groups, message);
+        clog_severity = CLG_SEVERITY_ERROR;
         break;
       case GL_DEBUG_TYPE_PORTABILITY:
       case GL_DEBUG_TYPE_PERFORMANCE:
       case GL_DEBUG_TYPE_OTHER:
       case GL_DEBUG_TYPE_MARKER: /* KHR has this, ARB does not */
       default:
-        fprintf(stderr, format, "\033[33;1mWarning\033[39m: ", debug_groups, message);
+        clog_severity = CLG_SEVERITY_WARN;
         break;
     }
 
-    if (VERBOSE && severity == GL_DEBUG_SEVERITY_HIGH) {
-      /* Focus on error message. */
-      fprintf(stderr, "\033[2m");
-      BLI_system_backtrace(stderr);
-      fprintf(stderr, "\033[0m\n");
-      fflush(stderr);
+    if (((LOG.type->flag & CLG_FLAG_USE) && (LOG.type->level >= clog_severity))) {
+      CLG_logf(LOG.type, clog_severity, debug_groups, "", message);
+      if (severity == GL_DEBUG_SEVERITY_HIGH) {
+        /* Focus on error message. */
+        if (use_color) {
+          fprintf(stderr, "\033[2m");
+        }
+        BLI_system_backtrace(stderr);
+        if (use_color) {
+          fprintf(stderr, "\033[0m\n");
+        }
+        fflush(stderr);
+      }
     }
   }
 }
@@ -125,6 +138,8 @@ static void APIENTRY debug_callback(GLenum UNUSED(source),
 /* This function needs to be called once per context. */
 void init_gl_callbacks(void)
 {
+  CLOG_ENSURE(&LOG);
+
   char msg[256] = "";
   const char format[] = "Successfully hooked OpenGL debug callback using %s";
 
@@ -154,7 +169,7 @@ void init_gl_callbacks(void)
                             msg);
   }
   else {
-    fprintf(stderr, "GPUDeb

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list