21#ifdef WITH_CLOG_PTHREADS
27#if defined(__unix__) || defined(__APPLE__) || defined(__HAIKU__)
35# include <VersionHelpers.h>
37# if !defined(ENABLE_VIRTUAL_TERMINAL_PROCESSING)
38# define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
43#define __STDC_FORMAT_MACROS
55#define STREQ(a, b) (strcmp(a, b) == 0)
56#define STREQLEN(a, b, n) (strncmp(a, b, n) == 0)
59# define PATHSEP_CHAR '\\'
61# define PATHSEP_CHAR '/'
79#ifdef WITH_CLOG_PTHREADS
80 pthread_mutex_t types_lock;
104 void (*
error_fn)(
void *file_handle);
105 void (*
fatal_fn)(
void *file_handle);
132#define CLOG_BUF_LEN_INIT 512
143 cstr->
data = buf_stack;
152 MEM_delete(cstr->
data);
168 memcpy(data, cstr->
data, cstr->
len);
177 uint len_next = cstr->
len +
len;
179 char *str_dst = cstr->
data + cstr->
len;
180 memcpy(str_dst,
str,
len);
184 cstr->
len = len_next;
194 uint len_next = cstr->
len +
len;
196 char *str_dst = cstr->
data + cstr->
len;
197 memset(str_dst, c,
len);
201 cstr->
len = len_next;
208 const uint len_max = 65535;
209 uint len_avail = cstr->len_alloc - cstr->len;
212 va_copy(args_cpy, args);
213 int retval = vsnprintf(cstr->data + cstr->len, len_avail, format, args_cpy);
222 if ((uint)retval <= len_avail) {
224 cstr->len += (uint)retval;
230 uint len_alloc = cstr->len + (uint)retval;
231 if (len_alloc >= len_max) {
237 len_avail = cstr->len_alloc - cstr->len;
244 if (indent_len < 2) {
248 uint num_newlines = 0;
249 for (uint i = 0; i < cstr->
len; i++) {
250 if (cstr->
data[i] ==
'\n') {
254 if (num_newlines == 0) {
258 const char *old_data = cstr->
data;
259 const uint old_len = cstr->
len;
260 const bool old_is_alloc = cstr->
is_alloc;
262 cstr->
len_alloc = cstr->
len + (num_newlines * indent_len);
267 for (uint i = 0; i < old_len; i++) {
268 cstr->
data[cstr->
len++] = old_data[i];
269 if (old_data[i] ==
'\n') {
270 memset(cstr->
data + cstr->
len,
' ', indent_len);
271 cstr->
data[cstr->
len + indent_len - 2] =
'|';
272 cstr->
len += indent_len;
277 MEM_delete(old_data);
296#define COLOR_LEN (COLOR_RESET + 1)
300static DWORD clg_previous_console_mode = 0;
335 return "INVALID_LEVEL";
377 const size_t identifier_len = strlen(identifier);
378 for (uint i = 0; i < 2; i++) {
380 while (flt !=
nullptr) {
381 const size_t len = strlen(flt->
match);
389 memcpy(match, flt->
match + 1,
len - 2);
390 const bool success = (strstr(identifier, match) !=
nullptr);
418 if (
STREQ(identifier, ty->identifier)) {
446 if (ctx->
callbacks.error_fn !=
nullptr) {
453 if (ctx->
callbacks.fatal_fn !=
nullptr) {
473 tick = GetTickCount64();
476 gettimeofday(&tv,
nullptr);
477 tick = tv.tv_sec * 1000 + tv.tv_usec / 1000;
490 char timestamp_str[128];
492 const int h = int(timestamp / (1000 * 60 * 60));
493 const int m = int((timestamp / (1000 * 60)) % 60);
494 const int s = int((timestamp / 1000) % 60);
495 const int r = int(timestamp % 1000);
497 const uint timestamp_len =
499 snprintf(timestamp_str,
sizeof(timestamp_str),
"%.2d:%.2d:%.2d.%.3d ", h, m, s, r) :
500 snprintf(timestamp_str,
sizeof(timestamp_str),
"%.2d:%.2d.%.3d ", m, s, r);
508 char memory_str[128];
509 const uint
len = snprintf(memory_str,
sizeof(memory_str),
"%dM", (
int)mem_in_use);
513 const uint memory_align_width = 5;
514 const uint num_spaces = (
len < memory_align_width) ? memory_align_width -
len : 0;
542 const uint type_align_width = 16;
543 const uint num_spaces = (
len < type_align_width) ? type_align_width -
len : 0;
548 const char *file_line,
550 const bool use_basename,
551 const bool use_color)
557 uint file_line_len = strlen(file_line);
559 uint file_line_offset = file_line_len;
560 while (file_line_offset-- > 0) {
566 file_line += file_line_offset;
567 file_line_len -= file_line_offset;
581 const char *file_line,
587 clg_str_init(&cstr, cstr_stack_buf,
sizeof(cstr_stack_buf));
635 const char *file_line,
642 clg_str_init(&cstr, cstr_stack_buf,
sizeof(cstr_stack_buf));
660 va_start(ap, format);
700 int bytes_written = write(lg->
ctx->
output, message, strlen(message));
712 ctx->
output_file =
static_cast<FILE *
>(file_handle);
714#if defined(__unix__) || defined(__APPLE__)
724 GetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), &clg_previous_console_mode);
727 if (IsWindows10OrGreater() && isatty(ctx->
output)) {
728 DWORD mode = clg_previous_console_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING;
729 if (SetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), mode)) {
773 ctx->
callbacks.backtrace_fn = backtrace_fn;
777 const char *type_match,
780 if (type_match_len == 0) {
785 flt->
next = *flt_list;
787 memcpy(flt->
match, type_match, type_match_len);
792 const char *type_match,
799 const char *type_match,
819#ifdef WITH_CLOG_PTHREADS
820 pthread_mutex_init(&ctx->types_lock,
nullptr);
832 SetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), clg_previous_console_mode);
834 while (ctx->
types !=
nullptr) {
844 for (uint i = 0; i < 2; i++) {
845 while (ctx->
filters[i] !=
nullptr) {
851#ifdef WITH_CLOG_PTHREADS
852 pthread_mutex_destroy(&ctx->types_lock);
959 static std::mutex mutex;
960 std::scoped_lock
lock(mutex);
962 clg_ref->
next = *all_refs;
970 auto cmp = [](
const char *a,
const char *
b) {
return std::strcmp(a,
b) < 0; };
971 std::set<
const char *,
decltype(cmp)> identifiers(cmp);
973 identifiers.insert(ref->identifier);
976 for (
const char *identifier : identifiers) {
977 callback(identifier, user_data);
983 if (
g_ctx ==
nullptr) {
984 fprintf(stderr,
"CLG logging used without initialization, aborting.\n");
988#ifdef WITH_CLOG_PTHREADS
990 pthread_mutex_lock(&
g_ctx->types_lock);
992 if (clg_ref->
type ==
nullptr) {
994 if (clg_ty ==
nullptr) {
997#ifdef WITH_CLOG_PTHREADS
1000 clg_ref->
type = clg_ty;
1003#ifdef WITH_CLOG_PTHREADS
1004 pthread_mutex_unlock(&
g_ctx->types_lock);
1010 if (clg_ref->
type ==
nullptr) {
#define ATTR_PRINTF_FORMAT(format_param, dots_param)
void CLG_type_filter_exclude(const char *type_match, int type_match_len)
void CLG_output_set(void *file_handle)
void CLG_output_use_basename_set(int value)
void void CLG_logf(const CLG_LogType *lg, enum CLG_Level level, const char *file_line, const char *fn, const char *format,...) _CLOG_ATTR_NONNULL(1
void CLG_output_use_memory_set(int value)
void CLG_log_raw(const CLG_LogType *lg, const char *message)
void CLG_log_str(const CLG_LogType *lg, enum CLG_Level level, const char *file_line, const char *fn, const char *message) _CLOG_ATTR_NONNULL(1
void CLG_error_fn_set(void(*error_fn)(void *file_handle))
void CLG_level_set(CLG_Level level)
void CLG_output_use_source_set(int value)
void CLG_backtrace_fn_set(void(*fatal_fn)(void *file_handle))
void CLG_fatal_fn_set(void(*fatal_fn)(void *file_handle))
void CLG_logref_list_all(void(*callback)(const char *identifier, void *user_data), void *user_data)
void CLG_logref_register(CLG_LogRef *clg_ref)
void CLG_type_filter_include(const char *type_match, int type_match_len)
void CLG_logref_init(CLG_LogRef *clg_ref)
void CLG_output_use_timestamp_set(int value)
void CLG_quiet_set(bool quiet)
int CLG_color_support_get(CLG_LogRef *clg_ref)
Read Blender memory allocation and freeing.
Provides wrapper around system-specific atomic primitives, and some extensions (faked-atomic operatio...
ATOMIC_INLINE void * atomic_cas_ptr(void **v, void *old, void *_new)
#define STREQLEN(a, b, n)
#define MEM_realloc_uninitialized(vmemh, len)
unsigned long long int uint64_t
static void CLG_ctx_output_set(CLogContext *ctx, void *file_handle)
static void write_type(CLogStringBuf *cstr, const CLG_LogType *lg)
static uint64_t clg_timestamp_ticks_get()
static void clg_str_append_char(CLogStringBuf *cstr, const char c, const uint len)
static void clg_str_vappendf(CLogStringBuf *cstr, const char *format, va_list args)
static void clg_ctx_fatal_action(CLogContext *ctx)
static void CLG_ctx_free(CLogContext *ctx)
static void clg_ctx_error_action(CLogContext *ctx)
static CLG_LogType * clg_ctx_type_find_by_name(CLogContext *ctx, const char *identifier)
static void CLG_ctx_output_use_timestamp_set(CLogContext *ctx, int value)
static std::mutex LOG_MUTEX
static void clg_str_append_with_len(CLogStringBuf *cstr, const char *str, const uint len)
static void write_memory(CLogStringBuf *cstr)
static CLogContext * CLG_ctx_init()
static void write_timestamp(CLogStringBuf *cstr, const uint64_t timestamp_tick_start)
static void CLG_ctx_output_use_source_set(CLogContext *ctx, int value)
#define CLOG_BUF_LEN_INIT
static void clg_str_append(CLogStringBuf *cstr, const char *str)
static void CLG_ctx_output_use_memory_set(CLogContext *ctx, int value)
static const char * clg_color_table[COLOR_LEN]
static void CLG_ctx_fatal_fn_set(CLogContext *ctx, void(*fatal_fn)(void *file_handle))
static void clg_ctx_type_filter_append(CLG_IDFilter **flt_list, const char *type_match, int type_match_len)
static struct CLogContext * g_ctx
static void clg_str_free(CLogStringBuf *cstr)
static void write_level(CLogStringBuf *cstr, enum CLG_Level level, bool use_color)
static void CLG_ctx_output_use_basename_set(CLogContext *ctx, int value)
static CLG_LogType * clg_ctx_type_register(CLogContext *ctx, const char *identifier)
static void clg_str_init(CLogStringBuf *cstr, char *buf_stack, uint buf_stack_len)
static void clg_ctx_backtrace(CLogContext *ctx)
static void CLG_ctx_backtrace_fn_set(CLogContext *ctx, void(*backtrace_fn)(void *file_handle))
static CLG_LogRef ** clg_all_refs_p()
static void clg_str_reserve(CLogStringBuf *cstr, const uint len)
static void CLG_ctx_level_set(CLogContext *ctx, CLG_Level level)
static void CLG_ctx_type_filter_exclude(CLogContext *ctx, const char *type_match, int type_match_len)
static void clg_str_indent_multiline(CLogStringBuf *cstr, const uint indent_len)
static void clg_color_table_init(bool use_color)
static const char * clg_level_as_text(enum CLG_Level level)
static void write_file_line_fn(CLogStringBuf *cstr, const char *file_line, const char *fn, const bool use_basename, const bool use_color)
static enum eCLogColor clg_level_to_color(enum CLG_Level level)
static bool clg_ctx_filter_check(CLogContext *ctx, const char *identifier)
static void CLG_ctx_type_filter_include(CLogContext *ctx, const char *type_match, int type_match_len)
static void CLT_ctx_error_fn_set(CLogContext *ctx, void(*error_fn)(void *file_handle))
#define assert(assertion)
size_t(*) MEM_get_memory_in_use(void)
void * MEM_new_zeroed(size_t len, const char *str)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str)
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str)
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
struct CLG_IDFilter * next
struct CLG_LogType * next
void(*) backtrace_fn(void *file_handle)
void(*) fatal_fn(void *file_handle)
void(*) error_fn(void *file_handle)
uint64_t timestamp_tick_start
struct CLogContext::@314332104112243113050307245356273277225137335317 callbacks
CLG_IDFilter * filters[2]
struct CLogContext::@104337011070222130164322347125303021100366321217 default_type