Blender V4.3
clog.c File Reference
#include <assert.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include "MEM_guardedalloc.h"
#include "CLG_log.h"

Go to the source code of this file.

Classes

struct  CLG_IDFilter
 
struct  CLogContext
 
struct  CLogStringBuf
 

Macros

#define __STDC_FORMAT_MACROS
 
#define STREQ(a, b)   (strcmp(a, b) == 0)
 
#define STREQLEN(a, b, n)   (strncmp(a, b, n) == 0)
 
#define PATHSEP_CHAR   '/'
 

Typedefs

Internal Types
typedef struct CLG_IDFilter CLG_IDFilter
 
typedef struct CLogContext CLogContext
 

Functions

Context Type Access
static bool clg_ctx_filter_check (CLogContext *ctx, const char *identifier)
 
static CLG_LogTypeclg_ctx_type_find_by_name (CLogContext *ctx, const char *identifier)
 
static CLG_LogTypeclg_ctx_type_register (CLogContext *ctx, const char *identifier)
 
static void clg_ctx_error_action (CLogContext *ctx)
 
static void clg_ctx_fatal_action (CLogContext *ctx)
 
static void clg_ctx_backtrace (CLogContext *ctx)
 
static uint64_t clg_timestamp_ticks_get (void)
 
Logging API
static void write_timestamp (CLogStringBuf *cstr, const uint64_t timestamp_tick_start)
 
static void write_severity (CLogStringBuf *cstr, enum CLG_Severity severity, bool use_color)
 
static void write_type (CLogStringBuf *cstr, const CLG_LogType *lg)
 
static void write_file_line_fn (CLogStringBuf *cstr, const char *file_line, const char *fn, const bool use_basename)
 
void CLG_log_str (const CLG_LogType *lg, enum CLG_Severity severity, const char *file_line, const char *fn, const char *message)
 
void CLG_logf (const CLG_LogType *lg, enum CLG_Severity severity, const char *file_line, const char *fn, const char *format,...)
 
Logging Context API
static void CLG_ctx_output_set (CLogContext *ctx, void *file_handle)
 
static void CLG_ctx_output_use_basename_set (CLogContext *ctx, int value)
 
static void CLG_ctx_output_use_timestamp_set (CLogContext *ctx, int value)
 
static void CLT_ctx_error_fn_set (CLogContext *ctx, void(*error_fn)(void *file_handle))
 
static void CLG_ctx_fatal_fn_set (CLogContext *ctx, void(*fatal_fn)(void *file_handle))
 
static void CLG_ctx_backtrace_fn_set (CLogContext *ctx, void(*backtrace_fn)(void *file_handle))
 
static void clg_ctx_type_filter_append (CLG_IDFilter **flt_list, const char *type_match, int type_match_len)
 
static void CLG_ctx_type_filter_exclude (CLogContext *ctx, const char *type_match, int type_match_len)
 
static void CLG_ctx_type_filter_include (CLogContext *ctx, const char *type_match, int type_match_len)
 
static void CLG_ctx_level_set (CLogContext *ctx, int level)
 
static CLogContextCLG_ctx_init (void)
 
static void CLG_ctx_free (CLogContext *ctx)
 
Logging Reference API

Use to avoid look-ups each time.

void CLG_logref_init (CLG_LogRef *clg_ref)
 
int CLG_color_support_get (CLG_LogRef *clg_ref)
 

Mini Buffer Functionality

Use so we can do a single call to write.

#define CLOG_BUF_LEN_INIT   512
 
typedef struct CLogStringBuf CLogStringBuf
 
static void clg_str_init (CLogStringBuf *cstr, char *buf_stack, uint buf_stack_len)
 
static void clg_str_free (CLogStringBuf *cstr)
 
static void clg_str_reserve (CLogStringBuf *cstr, const uint len)
 
static void clg_str_append_with_len (CLogStringBuf *cstr, const char *str, const uint len)
 
static void clg_str_append (CLogStringBuf *cstr, const char *str)
 
static void clg_str_vappendf (CLogStringBuf *cstr, const char *format, va_list args)
 

Internal Utilities

#define COLOR_LEN   (COLOR_RESET + 1)
 
enum  eCLogColor {
  COLOR_DEFAULT , COLOR_RED , COLOR_GREEN , COLOR_YELLOW ,
  COLOR_RESET
}
 
static const char * clg_color_table [COLOR_LEN] = {NULL}
 
static const char * clg_severity_str [CLG_SEVERITY_LEN]
 
static void clg_color_table_init (bool use_color)
 
static const char * clg_severity_as_text (enum CLG_Severity severity)
 
static enum eCLogColor clg_severity_to_color (enum CLG_Severity severity)
 

Public Logging API

Currently uses global context.

static struct CLogContextg_ctx = NULL
 
void CLG_init (void)
 
void CLG_exit (void)
 
void CLG_output_set (void *file_handle)
 
void CLG_output_use_basename_set (int value)
 
void CLG_output_use_timestamp_set (int value)
 
void CLG_error_fn_set (void(*error_fn)(void *file_handle))
 
void CLG_fatal_fn_set (void(*fatal_fn)(void *file_handle))
 
void CLG_backtrace_fn_set (void(*fatal_fn)(void *file_handle))
 
void CLG_type_filter_exclude (const char *type_match, int type_match_len)
 
void CLG_type_filter_include (const char *type_match, int type_match_len)
 
void CLG_level_set (int level)
 

Macro Definition Documentation

◆ __STDC_FORMAT_MACROS

#define __STDC_FORMAT_MACROS

Definition at line 40 of file clog.c.

◆ CLOG_BUF_LEN_INIT

#define CLOG_BUF_LEN_INIT   512

Definition at line 111 of file clog.c.

Referenced by CLG_log_str(), and CLG_logf().

◆ COLOR_LEN

#define COLOR_LEN   (COLOR_RESET + 1)

Definition at line 224 of file clog.c.

Referenced by clg_color_table_init().

◆ PATHSEP_CHAR

#define PATHSEP_CHAR   '/'

Definition at line 56 of file clog.c.

Referenced by write_file_line_fn().

◆ STREQ

#define STREQ ( a,
b )   (strcmp(a, b) == 0)

Definition at line 50 of file clog.c.

Referenced by clg_ctx_filter_check(), and clg_ctx_type_find_by_name().

◆ STREQLEN

#define STREQLEN ( a,
b,
n )   (strncmp(a, b, n) == 0)

Definition at line 51 of file clog.c.

Referenced by clg_ctx_filter_check().

Typedef Documentation

◆ CLG_IDFilter

typedef struct CLG_IDFilter CLG_IDFilter

◆ CLogContext

typedef struct CLogContext CLogContext

◆ CLogStringBuf

typedef struct CLogStringBuf CLogStringBuf

Enumeration Type Documentation

◆ eCLogColor

enum eCLogColor
Enumerator
COLOR_DEFAULT 
COLOR_RED 
COLOR_GREEN 
COLOR_YELLOW 
COLOR_RESET 

Definition at line 216 of file clog.c.

Function Documentation

◆ CLG_backtrace_fn_set()

void CLG_backtrace_fn_set ( void(* fatal_fn )(void *file_handle))

Definition at line 736 of file clog.c.

References CLG_ctx_backtrace_fn_set(), CLogContext::fatal_fn, and g_ctx.

Referenced by arg_handle_log_show_backtrace_set().

◆ CLG_color_support_get()

int CLG_color_support_get ( CLG_LogRef * clg_ref)

◆ clg_color_table_init()

static void clg_color_table_init ( bool use_color)
static

Definition at line 231 of file clog.c.

References clg_color_table, COLOR_DEFAULT, COLOR_GREEN, COLOR_LEN, COLOR_RED, COLOR_RESET, and COLOR_YELLOW.

Referenced by CLG_init().

◆ clg_ctx_backtrace()

static void clg_ctx_backtrace ( CLogContext * ctx)
static

Definition at line 378 of file clog.c.

References CLogContext::backtrace_fn, CLogContext::callbacks, and CLogContext::output_file.

Referenced by CLG_log_str(), and CLG_logf().

◆ CLG_ctx_backtrace_fn_set()

static void CLG_ctx_backtrace_fn_set ( CLogContext * ctx,
void(* backtrace_fn )(void *file_handle) )
static

Definition at line 605 of file clog.c.

References CLogContext::backtrace_fn, and CLogContext::callbacks.

Referenced by CLG_backtrace_fn_set().

◆ clg_ctx_error_action()

static void clg_ctx_error_action ( CLogContext * ctx)
static

Definition at line 362 of file clog.c.

References CLogContext::callbacks, CLogContext::error_fn, NULL, and CLogContext::output_file.

Referenced by CLG_logf().

◆ clg_ctx_fatal_action()

static void clg_ctx_fatal_action ( CLogContext * ctx)
static

Definition at line 369 of file clog.c.

References CLogContext::callbacks, CLogContext::fatal_fn, NULL, and CLogContext::output_file.

Referenced by CLG_log_str(), and CLG_logf().

◆ CLG_ctx_fatal_fn_set()

static void CLG_ctx_fatal_fn_set ( CLogContext * ctx,
void(* fatal_fn )(void *file_handle) )
static

Action on fatal severity.

Definition at line 600 of file clog.c.

References CLogContext::callbacks, and CLogContext::fatal_fn.

Referenced by CLG_fatal_fn_set().

◆ clg_ctx_filter_check()

static bool clg_ctx_filter_check ( CLogContext * ctx,
const char * identifier )
static

Filter the identifier based on very basic globbing.

  • foo exact match of foo.
  • foo.bar exact match for foo.bar
  • foo.* match for foo & foo.bar & foo.bar.baz
  • *bar* match for foo.bar & baz.bar & foo.barbaz
  • * matches everything.

Definition at line 300 of file clog.c.

References CLogContext::filters, len, CLG_IDFilter::match, MEM_callocN, MEM_freeN(), CLG_IDFilter::next, NULL, STREQ, and STREQLEN.

Referenced by clg_ctx_type_register().

◆ CLG_ctx_free()

static void CLG_ctx_free ( CLogContext * ctx)
static

◆ CLG_ctx_init()

static CLogContext * CLG_ctx_init ( void )
static

Definition at line 646 of file clog.c.

References CLG_ctx_output_set(), CLogContext::default_type, CLogContext::level, MEM_callocN, and NULL.

Referenced by CLG_init().

◆ CLG_ctx_level_set()

static void CLG_ctx_level_set ( CLogContext * ctx,
int level )
static

Definition at line 638 of file clog.c.

References CLogContext::default_type, CLogContext::level, and CLogContext::types.

Referenced by CLG_level_set().

◆ CLG_ctx_output_set()

static void CLG_ctx_output_set ( CLogContext * ctx,
void * file_handle )
static

Definition at line 554 of file clog.c.

References CLogContext::output, CLogContext::output_file, and CLogContext::use_color.

Referenced by CLG_ctx_init(), and CLG_output_set().

◆ CLG_ctx_output_use_basename_set()

static void CLG_ctx_output_use_basename_set ( CLogContext * ctx,
int value )
static

Definition at line 580 of file clog.c.

References CLogContext::use_basename.

Referenced by CLG_output_use_basename_set().

◆ CLG_ctx_output_use_timestamp_set()

static void CLG_ctx_output_use_timestamp_set ( CLogContext * ctx,
int value )
static

◆ clg_ctx_type_filter_append()

static void clg_ctx_type_filter_append ( CLG_IDFilter ** flt_list,
const char * type_match,
int type_match_len )
static

◆ CLG_ctx_type_filter_exclude()

static void CLG_ctx_type_filter_exclude ( CLogContext * ctx,
const char * type_match,
int type_match_len )
static

Definition at line 624 of file clog.c.

References clg_ctx_type_filter_append(), and CLogContext::filters.

Referenced by CLG_type_filter_exclude().

◆ CLG_ctx_type_filter_include()

static void CLG_ctx_type_filter_include ( CLogContext * ctx,
const char * type_match,
int type_match_len )
static

Definition at line 631 of file clog.c.

References clg_ctx_type_filter_append(), and CLogContext::filters.

Referenced by CLG_type_filter_include().

◆ clg_ctx_type_find_by_name()

static CLG_LogType * clg_ctx_type_find_by_name ( CLogContext * ctx,
const char * identifier )
static
Note
This should never be called per logging call. Searching is only to get an initial handle.

Definition at line 336 of file clog.c.

References NULL, STREQ, and CLogContext::types.

Referenced by clg_ctx_type_register(), and CLG_logref_init().

◆ clg_ctx_type_register()

◆ CLG_error_fn_set()

void CLG_error_fn_set ( void(* error_fn )(void *file_handle))

Definition at line 726 of file clog.c.

References CLT_ctx_error_fn_set(), CLogContext::error_fn, and g_ctx.

Referenced by arg_handle_debug_exit_on_error().

◆ CLG_exit()

void CLG_exit ( void )

◆ CLG_fatal_fn_set()

void CLG_fatal_fn_set ( void(* fatal_fn )(void *file_handle))

Definition at line 731 of file clog.c.

References CLG_ctx_fatal_fn_set(), CLogContext::fatal_fn, and g_ctx.

Referenced by main().

◆ CLG_init()

◆ CLG_level_set()

void CLG_level_set ( int level)

Definition at line 751 of file clog.c.

References CLG_ctx_level_set(), and g_ctx.

Referenced by arg_handle_log_level_set(), and main().

◆ CLG_log_str()

◆ CLG_logf()

◆ CLG_logref_init()

◆ CLG_output_set()

void CLG_output_set ( void * file_handle)

Definition at line 711 of file clog.c.

References CLG_ctx_output_set(), and g_ctx.

Referenced by arg_handle_log_file_set().

◆ CLG_output_use_basename_set()

void CLG_output_use_basename_set ( int value)

Definition at line 716 of file clog.c.

References CLG_ctx_output_use_basename_set(), and g_ctx.

Referenced by arg_handle_log_show_basename_set(), and main().

◆ CLG_output_use_timestamp_set()

void CLG_output_use_timestamp_set ( int value)

Definition at line 721 of file clog.c.

References CLG_ctx_output_use_timestamp_set(), and g_ctx.

Referenced by arg_handle_log_show_timestamp_set().

◆ clg_severity_as_text()

static const char * clg_severity_as_text ( enum CLG_Severity severity)
static

Definition at line 252 of file clog.c.

References CLG_SEVERITY_LEN, clg_severity_str, and int.

Referenced by write_severity().

◆ clg_severity_to_color()

static enum eCLogColor clg_severity_to_color ( enum CLG_Severity severity)
static

◆ clg_str_append()

static void clg_str_append ( CLogStringBuf * cstr,
const char * str )
static

Definition at line 168 of file clog.c.

References clg_str_append_with_len(), and str.

Referenced by CLG_log_str(), CLG_logf(), write_file_line_fn(), write_severity(), and write_type().

◆ clg_str_append_with_len()

static void clg_str_append_with_len ( CLogStringBuf * cstr,
const char * str,
const uint len )
static

◆ clg_str_free()

static void clg_str_free ( CLogStringBuf * cstr)
static

Definition at line 128 of file clog.c.

References CLogStringBuf::data, CLogStringBuf::is_alloc, and MEM_freeN().

Referenced by CLG_log_str(), and CLG_logf().

◆ clg_str_init()

static void clg_str_init ( CLogStringBuf * cstr,
char * buf_stack,
uint buf_stack_len )
static

◆ clg_str_reserve()

static void clg_str_reserve ( CLogStringBuf * cstr,
const uint len )
static

◆ clg_str_vappendf()

static void clg_str_vappendf ( CLogStringBuf * cstr,
const char * format,
va_list args )
static

Definition at line 174 of file clog.c.

References clg_str_reserve(), and len_alloc.

Referenced by CLG_logf().

◆ clg_timestamp_ticks_get()

static uint64_t clg_timestamp_ticks_get ( void )
static

Definition at line 387 of file clog.c.

References NULL.

Referenced by CLG_ctx_output_use_timestamp_set(), and write_timestamp().

◆ CLG_type_filter_exclude()

void CLG_type_filter_exclude ( const char * type_match,
int type_match_len )

Definition at line 741 of file clog.c.

References CLG_ctx_type_filter_exclude(), and g_ctx.

Referenced by arg_handle_log_set().

◆ CLG_type_filter_include()

void CLG_type_filter_include ( const char * type_match,
int type_match_len )

Definition at line 746 of file clog.c.

References CLG_ctx_type_filter_include(), and g_ctx.

Referenced by arg_handle_debug_gpu_set(), and arg_handle_log_set().

◆ CLT_ctx_error_fn_set()

static void CLT_ctx_error_fn_set ( CLogContext * ctx,
void(* error_fn )(void *file_handle) )
static

Action on error severity.

Definition at line 594 of file clog.c.

References CLogContext::callbacks, and CLogContext::error_fn.

Referenced by CLG_error_fn_set().

◆ write_file_line_fn()

static void write_file_line_fn ( CLogStringBuf * cstr,
const char * file_line,
const char * fn,
const bool use_basename )
static

Definition at line 439 of file clog.c.

References clg_str_append(), clg_str_append_with_len(), and PATHSEP_CHAR.

Referenced by CLG_log_str(), and CLG_logf().

◆ write_severity()

static void write_severity ( CLogStringBuf * cstr,
enum CLG_Severity severity,
bool use_color )
static

◆ write_timestamp()

static void write_timestamp ( CLogStringBuf * cstr,
const uint64_t timestamp_tick_start )
static

Definition at line 406 of file clog.c.

References clg_str_append_with_len(), clg_timestamp_ticks_get(), and PRIu64.

Referenced by CLG_log_str(), and CLG_logf().

◆ write_type()

static void write_type ( CLogStringBuf * cstr,
const CLG_LogType * lg )
static

Definition at line 432 of file clog.c.

References clg_str_append(), and CLG_LogType::identifier.

Referenced by CLG_log_str(), and CLG_logf().

Variable Documentation

◆ clg_color_table

const char* clg_color_table[COLOR_LEN] = {NULL}
static

Definition at line 226 of file clog.c.

Referenced by clg_color_table_init(), and write_severity().

◆ clg_severity_str

const char* clg_severity_str[CLG_SEVERITY_LEN]
static
Initial value:
= {
[CLG_SEVERITY_INFO] = "INFO",
[CLG_SEVERITY_WARN] = "WARN",
[CLG_SEVERITY_ERROR] = "ERROR",
[CLG_SEVERITY_FATAL] = "FATAL",
}
@ CLG_SEVERITY_INFO
Definition CLG_log.h:88
@ CLG_SEVERITY_WARN
Definition CLG_log.h:89
@ CLG_SEVERITY_FATAL
Definition CLG_log.h:91
@ CLG_SEVERITY_ERROR
Definition CLG_log.h:90

Definition at line 245 of file clog.c.

Referenced by clg_severity_as_text().

◆ g_ctx