Logging: Add DEBUG, TRACE severity, replace numeric levels

The numeric levels have no obvious meaning. This removes the distinction
between severity and levels, instead there is a single list of named levels
with defined meaning.

Debug means information that's mainly useful for developers, and trace is for
very verbose code execution tracing.

Pull Request: https://projects.blender.org/blender/blender/pulls/140244
This commit is contained in:
Brecht Van Lommel
2025-06-12 02:20:03 +02:00
parent 8d8e61fefd
commit 4653b65f7c
104 changed files with 1112 additions and 1234 deletions

View File

@@ -74,18 +74,15 @@
struct CLogContext;
/* Don't typedef enums. */
enum CLG_LogFlag {
CLG_FLAG_USE = (1 << 0),
enum CLG_Level {
CLG_LEVEL_FATAL = 0, /* Fatal errors */
CLG_LEVEL_ERROR = 1, /* Errors */
CLG_LEVEL_WARN = 2, /* Warnings */
CLG_LEVEL_INFO = 3, /* Information about devices, files, configuration, user operations */
CLG_LEVEL_DEBUG = 4, /* Debugging information for developers */
CLG_LEVEL_TRACE = 5, /* Very verbose code execution tracing */
};
enum CLG_Severity {
CLG_SEVERITY_INFO = 0,
CLG_SEVERITY_WARN,
CLG_SEVERITY_ERROR,
CLG_SEVERITY_FATAL,
};
#define CLG_SEVERITY_LEN (CLG_SEVERITY_FATAL + 1)
#define CLG_LEVEL_LEN (CLG_LEVEL_TRACE + 1)
/* Each logger ID has one of these. */
struct CLG_LogType {
@@ -94,8 +91,7 @@ struct CLG_LogType {
/** FILE output. */
struct CLogContext *ctx;
/** Control behavior. */
int level;
int flag;
CLG_Level level;
};
struct CLG_LogRef {
@@ -105,12 +101,12 @@ struct CLG_LogRef {
};
void CLG_log_str(const CLG_LogType *lg,
enum CLG_Severity severity,
enum CLG_Level level,
const char *file_line,
const char *fn,
const char *message) _CLOG_ATTR_NONNULL(1, 3, 4, 5);
void CLG_logf(const CLG_LogType *lg,
enum CLG_Severity severity,
enum CLG_Level level,
const char *file_line,
const char *fn,
const char *format,
@@ -132,7 +128,7 @@ void CLG_backtrace_fn_set(void (*fatal_fn)(void *file_handle));
void CLG_type_filter_include(const char *type_match, int type_match_len);
void CLG_type_filter_exclude(const char *type_match, int type_match_len);
void CLG_level_set(int level);
void CLG_level_set(CLG_Level level);
void CLG_logref_init(CLG_LogRef *clg_ref);
@@ -148,49 +144,54 @@ int CLG_color_support_get(CLG_LogRef *clg_ref);
((clg_ref)->type ? (clg_ref)->type : (CLG_logref_init(clg_ref), (clg_ref)->type))
#define CLOG_CHECK(clg_ref, verbose_level, ...) \
((void)CLOG_ENSURE(clg_ref), \
((clg_ref)->type->flag & CLG_FLAG_USE) && ((clg_ref)->type->level >= verbose_level))
((void)CLOG_ENSURE(clg_ref), ((clg_ref)->type->level >= verbose_level))
#define CLOG_AT_SEVERITY(clg_ref, severity, verbose_level, ...) \
#define CLOG_AT_LEVEL(clg_ref, verbose_level, ...) \
{ \
const 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__); \
if (_lg_ty->level >= verbose_level) { \
CLG_logf(_lg_ty, verbose_level, __FILE__ ":" STRINGIFY(__LINE__), __func__, __VA_ARGS__); \
} \
} \
((void)0)
#define CLOG_AT_SEVERITY_NOCHECK(clg_ref, severity, ...) \
#define CLOG_AT_LEVEL_NOCHECK(clg_ref, verbose_level, ...) \
{ \
const CLG_LogType *_lg_ty = CLOG_ENSURE(clg_ref); \
CLG_logf(_lg_ty, severity, __FILE__ ":" STRINGIFY(__LINE__), __func__, __VA_ARGS__); \
CLG_logf(_lg_ty, verbose_level, __FILE__ ":" STRINGIFY(__LINE__), __func__, __VA_ARGS__); \
} \
((void)0)
#define CLOG_STR_AT_SEVERITY(clg_ref, severity, verbose_level, str) \
#define CLOG_STR_AT_LEVEL(clg_ref, verbose_level, str) \
{ \
const 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_ty, severity, __FILE__ ":" STRINGIFY(__LINE__), __func__, str); \
if (_lg_ty->level >= verbose_level) { \
CLG_log_str(_lg_ty, verbose_level, __FILE__ ":" STRINGIFY(__LINE__), __func__, 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_AT_LEVEL_NOCHECK(clg_ref, verbose_level, str) \
{ \
const CLG_LogType *_lg_ty = CLOG_ENSURE(clg_ref); \
CLG_log_str(_lg_ty, verbose_level, __FILE__ ":" STRINGIFY(__LINE__), __func__, str); \
} \
((void)0)
#define CLOG_STR_INFO(clg_ref, level, str) \
CLOG_STR_AT_SEVERITY(clg_ref, CLG_SEVERITY_INFO, level, str)
#define CLOG_STR_WARN(clg_ref, str) CLOG_STR_AT_SEVERITY(clg_ref, CLG_SEVERITY_WARN, 0, str)
#define CLOG_STR_ERROR(clg_ref, str) CLOG_STR_AT_SEVERITY(clg_ref, CLG_SEVERITY_ERROR, 0, str)
#define CLOG_STR_FATAL(clg_ref, str) CLOG_STR_AT_SEVERITY(clg_ref, CLG_SEVERITY_FATAL, 0, str)
#define CLOG_FATAL(clg_ref, ...) CLOG_AT_LEVEL(clg_ref, CLG_LEVEL_FATAL, __VA_ARGS__)
#define CLOG_ERROR(clg_ref, ...) CLOG_AT_LEVEL(clg_ref, CLG_LEVEL_ERROR, __VA_ARGS__)
#define CLOG_WARN(clg_ref, ...) CLOG_AT_LEVEL(clg_ref, CLG_LEVEL_WARN, __VA_ARGS__)
#define CLOG_INFO(clg_ref, ...) CLOG_AT_LEVEL(clg_ref, CLG_LEVEL_INFO, __VA_ARGS__)
#define CLOG_DEBUG(clg_ref, ...) CLOG_AT_LEVEL(clg_ref, CLG_LEVEL_DEBUG, __VA_ARGS__)
#define CLOG_TRACE(clg_ref, ...) CLOG_AT_LEVEL(clg_ref, CLG_LEVEL_TRACE, __VA_ARGS__)
#define CLOG_STR_FATAL(clg_ref, str) CLOG_STR_AT_LEVEL(clg_ref, CLG_LEVEL_FATAL, str)
#define CLOG_STR_ERROR(clg_ref, str) CLOG_STR_AT_LEVEL(clg_ref, CLG_LEVEL_ERROR, str)
#define CLOG_STR_WARN(clg_ref, str) CLOG_STR_AT_LEVEL(clg_ref, CLG_LEVEL_WARN, str)
#define CLOG_STR_INFO(clg_ref, str) CLOG_STR_AT_LEVEL(clg_ref, CLG_LEVEL_INFO, str)
#define CLOG_STR_DEBUG(clg_ref, str) CLOG_STR_AT_LEVEL(clg_ref, CLG_LEVEL_DEBUG, str)
#define CLOG_STR_TRACE(clg_ref, str) CLOG_STR_AT_LEVEL(clg_ref, CLG_LEVEL_TRACE, str)
#define CLOG_INFO_NOCHECK(clg_ref, format, ...) \
CLOG_AT_SEVERITY_NOCHECK(clg_ref, CLG_SEVERITY_INFO, format, __VA_ARGS__)
CLOG_AT_LEVEL_NOCHECK(clg_ref, CLG_LEVEL_INFO, format, __VA_ARGS__)
#define CLOG_STR_INFO_NOCHECK(clg_ref, str) CLOG_STR_AT_LEVEL_NOCHECK(clg_ref, CLG_LEVEL_INFO, str)

View File

@@ -98,7 +98,7 @@ struct CLogContext {
/** For new types. */
struct {
int level;
CLG_Level level;
} default_type;
struct {
@@ -302,42 +302,42 @@ static void clg_color_table_init(bool use_color)
}
}
static const char *clg_severity_as_text(enum CLG_Severity severity)
static const char *clg_level_as_text(enum CLG_Level level)
{
switch (severity) {
case CLG_SEVERITY_INFO:
return "INFO";
case CLG_SEVERITY_WARN:
return "WARNING";
case CLG_SEVERITY_ERROR:
return "ERROR";
case CLG_SEVERITY_FATAL:
switch (level) {
case CLG_LEVEL_FATAL:
return "FATAL";
case CLG_LEVEL_ERROR:
return "ERROR";
case CLG_LEVEL_WARN:
return "WARNING";
case CLG_LEVEL_INFO:
return "INFO";
case CLG_LEVEL_DEBUG:
return "DEBUG";
case CLG_LEVEL_TRACE:
return "TRACE";
}
return "INVALID_SEVERITY";
return "INVALID_LEVEL";
}
static enum eCLogColor clg_severity_to_color(enum CLG_Severity severity)
static enum eCLogColor clg_level_to_color(enum CLG_Level level)
{
assert((unsigned int)severity < CLG_SEVERITY_LEN);
enum eCLogColor color = COLOR_DEFAULT;
switch (severity) {
case CLG_SEVERITY_INFO:
color = COLOR_DEFAULT;
break;
case CLG_SEVERITY_WARN:
color = COLOR_YELLOW;
break;
case CLG_SEVERITY_ERROR:
case CLG_SEVERITY_FATAL:
color = COLOR_RED;
break;
default:
/* should never get here. */
assert(false);
switch (level) {
case CLG_LEVEL_FATAL:
case CLG_LEVEL_ERROR:
return COLOR_RED;
case CLG_LEVEL_WARN:
return COLOR_YELLOW;
case CLG_LEVEL_INFO:
case CLG_LEVEL_DEBUG:
case CLG_LEVEL_TRACE:
return COLOR_DEFAULT;
}
return color;
/* should never get here. */
assert(false);
return COLOR_DEFAULT;
}
/** \} */
@@ -354,7 +354,9 @@ static enum eCLogColor clg_severity_to_color(enum CLG_Severity severity)
*/
static bool clg_ctx_filter_check(CLogContext *ctx, const char *identifier)
{
if (ctx->filters[0] == nullptr && ctx->filters[1] == nullptr && ctx->default_type.level >= 0) {
if (ctx->filters[0] == nullptr && ctx->filters[1] == nullptr &&
ctx->default_type.level >= CLG_LEVEL_INFO)
{
/* No filters but level specified? Match everything. */
return true;
}
@@ -415,11 +417,14 @@ static CLG_LogType *clg_ctx_type_register(CLogContext *ctx, const char *identifi
ctx->types = ty;
strncpy(ty->identifier, identifier, sizeof(ty->identifier) - 1);
ty->ctx = ctx;
ty->level = ctx->default_type.level;
if (clg_ctx_filter_check(ctx, ty->identifier)) {
ty->flag |= CLG_FLAG_USE;
ty->level = ctx->default_type.level;
}
else {
ty->level = std::min(ctx->default_type.level, CLG_LEVEL_WARN);
}
return ty;
}
@@ -497,22 +502,20 @@ static void write_memory(CLogStringBuf *cstr)
clg_str_append_char(cstr, ' ', num_spaces + 2);
}
static void write_severity(CLogStringBuf *cstr, enum CLG_Severity severity, bool use_color)
static void write_level(CLogStringBuf *cstr, enum CLG_Level level, bool use_color)
{
assert((unsigned int)severity < CLG_SEVERITY_LEN);
if (severity == CLG_SEVERITY_INFO) {
if (level >= CLG_LEVEL_INFO) {
return;
}
if (use_color) {
enum eCLogColor color = clg_severity_to_color(severity);
enum eCLogColor color = clg_level_to_color(level);
clg_str_append(cstr, clg_color_table[color]);
clg_str_append(cstr, clg_severity_as_text(severity));
clg_str_append(cstr, clg_level_as_text(level));
clg_str_append(cstr, clg_color_table[COLOR_RESET]);
}
else {
clg_str_append(cstr, clg_severity_as_text(severity));
clg_str_append(cstr, clg_level_as_text(level));
}
clg_str_append(cstr, " ");
@@ -561,7 +564,7 @@ static void write_file_line_fn(CLogStringBuf *cstr,
}
void CLG_log_str(const CLG_LogType *lg,
enum CLG_Severity severity,
enum CLG_Level level,
const char *file_line,
const char *fn,
const char *message)
@@ -582,7 +585,7 @@ void CLG_log_str(const CLG_LogType *lg,
const uint64_t multiline_indent_len = cstr.len;
write_severity(&cstr, severity, lg->ctx->use_color);
write_level(&cstr, level, lg->ctx->use_color);
clg_str_append(&cstr, message);
@@ -609,13 +612,13 @@ void CLG_log_str(const CLG_LogType *lg,
clg_ctx_backtrace(lg->ctx);
}
if (severity == CLG_SEVERITY_FATAL) {
if (level == CLG_LEVEL_FATAL) {
clg_ctx_fatal_action(lg->ctx);
}
}
void CLG_logf(const CLG_LogType *lg,
enum CLG_Severity severity,
enum CLG_Level level,
const char *file_line,
const char *fn,
const char *format,
@@ -637,7 +640,7 @@ void CLG_logf(const CLG_LogType *lg,
const uint64_t multiline_indent_len = cstr.len;
write_severity(&cstr, severity, lg->ctx->use_color);
write_level(&cstr, level, lg->ctx->use_color);
{
va_list ap;
@@ -669,11 +672,11 @@ void CLG_logf(const CLG_LogType *lg,
clg_ctx_backtrace(lg->ctx);
}
if (severity == CLG_SEVERITY_ERROR) {
if (level == CLG_LEVEL_ERROR) {
clg_ctx_error_action(lg->ctx);
}
if (severity == CLG_SEVERITY_FATAL) {
if (level == CLG_LEVEL_FATAL) {
clg_ctx_fatal_action(lg->ctx);
}
}
@@ -733,13 +736,13 @@ static void CLG_ctx_output_use_memory_set(CLogContext *ctx, int value)
ctx->use_memory = (bool)value;
}
/** Action on error severity. */
/** Action on error level. */
static void CLT_ctx_error_fn_set(CLogContext *ctx, void (*error_fn)(void *file_handle))
{
ctx->callbacks.error_fn = error_fn;
}
/** Action on fatal severity. */
/** Action on fatal level. */
static void CLG_ctx_fatal_fn_set(CLogContext *ctx, void (*fatal_fn)(void *file_handle))
{
ctx->callbacks.fatal_fn = fatal_fn;
@@ -777,12 +780,12 @@ static void CLG_ctx_type_filter_include(CLogContext *ctx,
int type_match_len)
{
clg_ctx_type_filter_append(&ctx->filters[1], type_match, type_match_len);
if (ctx->default_type.level == -1) {
ctx->default_type.level = 1;
if (ctx->default_type.level <= CLG_LEVEL_WARN) {
ctx->default_type.level = CLG_LEVEL_INFO;
}
}
static void CLG_ctx_level_set(CLogContext *ctx, int level)
static void CLG_ctx_level_set(CLogContext *ctx, CLG_Level level)
{
ctx->default_type.level = level;
for (CLG_LogType *ty = ctx->types; ty; ty = ty->next) {
@@ -796,7 +799,7 @@ static CLogContext *CLG_ctx_init()
#ifdef WITH_CLOG_PTHREADS
pthread_mutex_init(&ctx->types_lock, nullptr);
#endif
ctx->default_type.level = -1;
ctx->default_type.level = CLG_LEVEL_WARN;
ctx->use_source = true;
CLG_ctx_output_set(ctx, stdout);
@@ -906,7 +909,7 @@ void CLG_type_filter_include(const char *type_match, int type_match_len)
CLG_ctx_type_filter_include(g_ctx, type_match, type_match_len);
}
void CLG_level_set(int level)
void CLG_level_set(CLG_Level level)
{
CLG_ctx_level_set(g_ctx, level);
}