[Bf-blender-cvs] [891c1cfc9a3] master: C Logging: use instead of printf for messages

Campbell Barton noreply at git.blender.org
Fri Mar 30 18:58:07 CEST 2018


Commit: 891c1cfc9a355171215821fc91b694273503f139
Author: Campbell Barton
Date:   Thu Mar 29 20:38:32 2018 +0200
Branches: master
https://developer.blender.org/rB891c1cfc9a355171215821fc91b694273503f139

C Logging: use instead of printf for messages

- See `--log` help message for usage.
- Supports enabling categories.
- Color severity.
- Optionally logs to a file.
- Currently use to replace printf calls in wm module.

See D3120 for details.

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

M	build_files/cmake/macros.cmake
M	intern/CMakeLists.txt
A	intern/clog/CLG_log.h
A	intern/clog/CMakeLists.txt
A	intern/clog/clog.c
M	release/scripts/startup/bl_ui/space_view3d.py
M	source/blender/blenkernel/BKE_global.h
M	source/blender/blenkernel/CMakeLists.txt
M	source/blender/blenkernel/intern/blender.c
M	source/blender/depsgraph/intern/eval/deg_eval.cc
M	source/blender/editors/util/CMakeLists.txt
M	source/blender/python/mathutils/mathutils_Euler.c
M	source/blender/windowmanager/CMakeLists.txt
M	source/blender/windowmanager/WM_types.h
M	source/blender/windowmanager/intern/wm_event_system.c
M	source/blender/windowmanager/intern/wm_init_exit.c
M	source/blender/windowmanager/intern/wm_keymap.c
M	source/blender/windowmanager/intern/wm_operators.c
M	source/creator/CMakeLists.txt
M	source/creator/creator.c
M	source/creator/creator_args.c

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

diff --git a/build_files/cmake/macros.cmake b/build_files/cmake/macros.cmake
index f4d37c192ba..f0cff75c417 100644
--- a/build_files/cmake/macros.cmake
+++ b/build_files/cmake/macros.cmake
@@ -674,6 +674,7 @@ function(SETUP_BLENDER_SORTED_LIBS)
 		extern_sdlew
 
 		bf_intern_glew_mx
+		bf_intern_clog
 	)
 
 	if(NOT WITH_SYSTEM_GLOG)
diff --git a/intern/CMakeLists.txt b/intern/CMakeLists.txt
index bfe230250ae..499b1048655 100644
--- a/intern/CMakeLists.txt
+++ b/intern/CMakeLists.txt
@@ -24,6 +24,7 @@
 # ***** END GPL LICENSE BLOCK *****
 
 # add_subdirectory(atomic)  # header only
+add_subdirectory(clog)
 add_subdirectory(string)
 add_subdirectory(ghost)
 add_subdirectory(guardedalloc)
diff --git a/intern/clog/CLG_log.h b/intern/clog/CLG_log.h
new file mode 100644
index 00000000000..2da5a7d367c
--- /dev/null
+++ b/intern/clog/CLG_log.h
@@ -0,0 +1,147 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+#ifndef __CLOG_H__
+#define __CLOG_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+#ifdef __GNUC__
+#  define _CLOG_ATTR_NONNULL(args ...) __attribute__((nonnull(args)))
+#else
+#  define _CLOG_ATTR_NONNULL(...)
+#endif
+
+#ifdef __GNUC__
+#  define _CLOG_ATTR_PRINTF_FORMAT(format_param, dots_param) __attribute__((format(printf, format_param, dots_param)))
+#else
+#  define _CLOG_ATTR_PRINTF_FORMAT(format_param, dots_param)
+#endif
+
+#define STRINGIFY_ARG(x) "" #x
+#define STRINGIFY_APPEND(a, b) "" a #b
+#define STRINGIFY(x) STRINGIFY_APPEND("", x)
+
+struct CLogContext;
+
+/* Don't typedef enums. */
+enum CLG_LogFlag {
+	CLG_FLAG_USE = (1 << 0),
+};
+
+enum CLG_Severity {
+	CLG_SEVERITY_INFO = 0,
+	CLG_SEVERITY_WARN,
+	CLG_SEVERITY_ERROR,
+	CLG_SEVERITY_FATAL,
+};
+#define CLG_SEVERITY_LEN (CLG_SEVERITY_FATAL + 1)
+
+/* Each logger ID has one of these. */
+typedef struct CLG_LogType {
+	struct CLG_LogType *next;
+	char identifier[64];
+	/** FILE output. */
+	struct CLogContext *ctx;
+	/** Control behavior. */
+	int level;
+	enum CLG_LogFlag flag;
+} CLG_LogType;
+
+typedef struct CLG_LogRef {
+	const char *identifier;
+	CLG_LogType *type;
+} CLG_LogRef;
+
+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, 3, 4, 5);
+void CLG_logf(
+        CLG_LogType *lg, enum CLG_Severity severity, const char *file_line, const char *fn,
+        const char *format, ...)
+	_CLOG_ATTR_NONNULL(1, 3, 4, 5) _CLOG_ATTR_PRINTF_FORMAT(5, 6);
+
+/* Main initializer and distructor (per session, not logger). */
+void CLG_init(void);
+void CLG_exit(void);
+
+void CLG_output_set(void *file_handle);
+void CLG_fatal_fn_set(void (*fatal_fn)(void *file_handle));
+
+void CLG_type_filter(const char *type_filter, int type_filter_len);
+
+void CLG_logref_init(CLG_LogRef *clg_ref);
+
+/** Declare outside function, declare as extern in header. */
+#define CLG_LOGREF_DECLARE_GLOBAL(var, id) \
+	static CLG_LogRef _static_ ## var = {id}; \
+	CLG_LogRef *var = &_static_ ## var
+
+/** Initialize struct once. */
+#define CLOG_ENSURE(clg_ref) \
+	((clg_ref)->type ? (clg_ref)->type : (CLG_logref_init(clg_ref), (clg_ref)->type))
+
+#define CLOG_AT_SEVERITY(clg_ref, severity, verbose_level, ...) { \
+	CLG_LogType *_lg_ty = CLOG_ENSURE(clg_ref); \
+	if (((_lg_ty->flag & CLG_FLAG_USE) && (_lg_ty->level >= verbose_level)) || (severity >= CLG_SEVERITY_WARN)) { \
+		CLG_logf(_lg_ty, severity, __FILE__ ":" STRINGIFY(__LINE__), __func__, __VA_ARGS__); \
+	} \
+} ((void)0)
+
+#define CLOG_STR_AT_SEVERITY(clg_ref, severity, verbose_level, str) { \
+	CLG_LogType *_lg_ty = CLOG_ENSURE(clg_ref); \
+	if (((_lg_ty->flag & CLG_FLAG_USE) && (_lg_ty->level >= verbose_level)) || (severity >= CLG_SEVERITY_WARN)) { \
+		CLG_log_str(lg, severity, __FILE__ ":" STRINGIFY(__LINE__), __func__, str); \
+	} \
+} ((void)0)
+
+#define CLOG_STR_AT_SEVERITY_N(clg_ref, severity, verbose_level, str) { \
+	CLG_LogType *_lg_ty = CLOG_ENSURE(clg_ref); \
+	if (((_lg_ty->flag & CLG_FLAG_USE) && (_lg_ty->level >= verbose_level)) || (severity >= CLG_SEVERITY_WARN)) { \
+		const char *_str = str; \
+		CLG_log_str(_lg_ty, severity, __FILE__ ":" STRINGIFY(__LINE__), __func__, _str); \
+		MEM_freeN((void *)_str); \
+	} \
+} ((void)0)
+
+#define CLOG_INFO(clg_ref, level, ...) CLOG_AT_SEVERITY(clg_ref, CLG_SEVERITY_INFO, level, __VA_ARGS__)
+#define CLOG_WARN(clg_ref, ...)        CLOG_AT_SEVERITY(clg_ref, CLG_SEVERITY_WARN, 0, __VA_ARGS__)
+#define CLOG_ERROR(clg_ref, ...)       CLOG_AT_SEVERITY(clg_ref, CLG_SEVERITY_ERROR, 0, __VA_ARGS__)
+#define CLOG_FATAL(clg_ref, ...)       CLOG_AT_SEVERITY(clg_ref, CLG_SEVERITY_FATAL, 0, __VA_ARGS__)
+
+#define CLOG_STR_INFO(clg_ref, level, ...) CLOG_STR_AT_SEVERITY(clg_ref, CLG_SEVERITY_INFO, level, __VA_ARGS__)
+#define CLOG_STR_WARN(clg_ref, ...)        CLOG_STR_AT_SEVERITY(clg_ref, CLG_SEVERITY_WARN, 0, __VA_ARGS__)
+#define CLOG_STR_ERROR(clg_ref, ...)       CLOG_STR_AT_SEVERITY(clg_ref, CLG_SEVERITY_ERROR, 0, __VA_ARGS__)
+#define CLOG_STR_FATAL(clg_ref, ...)       CLOG_STR_AT_SEVERITY(clg_ref, CLG_SEVERITY_FATAL, 0, __VA_ARGS__)
+
+/* Allocated string which is immediately freed. */
+#define CLOG_STR_INFO_N(clg_ref, level, ...) CLOG_STR_AT_SEVERITY_N(clg_ref, CLG_SEVERITY_INFO, level, __VA_ARGS__)
+#define CLOG_STR_WARN_N(clg_ref, ...)        CLOG_STR_AT_SEVERITY_N(clg_ref, CLG_SEVERITY_WARN, 0, __VA_ARGS__)
+#define CLOG_STR_ERROR_N(clg_ref, ...)       CLOG_STR_AT_SEVERITY_N(clg_ref, CLG_SEVERITY_ERROR, 0, __VA_ARGS__)
+#define CLOG_STR_FATAL_N(clg_ref, ...)       CLOG_STR_AT_SEVERITY_N(clg_ref, CLG_SEVERITY_FATAL, 0, __VA_ARGS__)
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __CLOG_H__ */
diff --git a/intern/clog/CMakeLists.txt b/intern/clog/CMakeLists.txt
new file mode 100644
index 00000000000..8bf7b66f7ec
--- /dev/null
+++ b/intern/clog/CMakeLists.txt
@@ -0,0 +1,34 @@
+# ***** BEGIN GPL LICENSE BLOCK *****
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software Foundation,
+# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# ***** END GPL LICENSE BLOCK *****
+
+set(INC
+	.
+	../guardedalloc
+)
+
+set(INC_SYS
+
+)
+
+set(SRC
+	clog.c
+
+	CLG_log.h
+)
+
+blender_add_lib(bf_intern_clog "${SRC}" "${INC}" "${INC_SYS}")
diff --git a/intern/clog/clog.c b/intern/clog/clog.c
new file mode 100644
index 00000000000..66267dd6df2
--- /dev/null
+++ b/intern/clog/clog.c
@@ -0,0 +1,514 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+#include <stdarg.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+#include <assert.h>
+
+/* For 'isatty' to check for color. */
+#if defined(__unix__)
+#  include <unistd.h>
+#endif
+
+#include "MEM_guardedalloc.h"
+
+/* own include. */
+#include "CLG_log.h"
+
+/* Local utility defines */
+#define STREQ(a, b) (strcmp(a, b) == 0)
+#define STREQLEN(a, b, n) (strncmp(a, b, n) == 0)
+
+/* -------------------------------------------------------------------- */
+/** \name Internal Types
+ * \{ */
+
+typedef struct CLG_IDFilter {
+	struct CLG_IDFilter *next;
+	/** Over alloc. */
+	char match[0];
+} CLG_IDFilter;
+
+typedef struct CLogContext {
+	/** Single linked list of types.  */
+	CLG_LogType *types;
+	CLG_IDFilter *filters;
+	bool use_color;
+
+	/** Borrowed, not owned. */
+	FILE *output;
+
+	/** For new types. */
+	struct {
+		int level;
+	} default_type;
+
+	struct {
+		void (*fatal_fn)(void *file_handle);
+	} callbacks;
+} CLogContext;
+
+/** \} */
+
+/* -------------------------------------------------------------------- */
+/** \name Mini Buffer Functionality
+ *
+ * Use so we can do a single call to write.
+ * \{ */
+
+#define CLOG_BUF_LEN_INIT 512
+
+typedef struct CLogStringBuf {
+	char *data;
+	uint  len;
+	uint  len_alloc;
+	bool is_alloc;
+} CLogStringBuf;
+
+static void clg_str_init(CLogStringBuf *cstr, char *buf_stack, uint buf_stack_len)
+{
+	cstr->data = buf_stack;
+	cstr->len_alloc = buf_stack_len;
+	cstr->len = 0;
+	cstr->is_alloc = false;
+}
+
+static void clg_str_free(CLogStringBuf *cstr)
+{
+	if (cstr->is_alloc) {
+		MEM_freeN(cstr->data);
+	}
+}
+
+static void clg_str_reserve(CLo

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list