Blender V5.3
clog.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2018-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cassert>
10#include <cstdarg>
11#include <cstdint>
12#include <cstdio>
13#include <cstdlib>
14#include <cstring>
15
16#include <mutex>
17#include <set>
18
19/* Disable for small single threaded programs
20 * to avoid having to link with pthreads. */
21#ifdef WITH_CLOG_PTHREADS
22# include "atomic_ops.h"
23# include <pthread.h>
24#endif
25
26/* For 'isatty' to check for color. */
27#if defined(__unix__) || defined(__APPLE__) || defined(__HAIKU__)
28# include <sys/time.h>
29# include <unistd.h>
30#endif
31
32#if defined(_MSC_VER)
33# include <Windows.h>
34
35# include <VersionHelpers.h> /* This needs to be included after Windows.h. */
36# include <io.h>
37# if !defined(ENABLE_VIRTUAL_TERMINAL_PROCESSING)
38# define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
39# endif
40#endif
41
42/* For printing timestamp. */
43#define __STDC_FORMAT_MACROS
44#include <cinttypes>
45
46#include <algorithm>
47
48/* Only other dependency (could use regular malloc too). */
49#include "MEM_guardedalloc.h"
50
51/* own include. */
52#include "CLG_log.h"
53
54/* Local utility defines */
55#define STREQ(a, b) (strcmp(a, b) == 0)
56#define STREQLEN(a, b, n) (strncmp(a, b, n) == 0)
57
58#ifdef _WIN32
59# define PATHSEP_CHAR '\\'
60#else
61# define PATHSEP_CHAR '/'
62#endif
63
64/* -------------------------------------------------------------------- */
67
68static std::mutex LOG_MUTEX;
69
73 char match[0];
74};
75
79#ifdef WITH_CLOG_PTHREADS
80 pthread_mutex_t types_lock;
81#endif
82
83 /* exclude, include filters. */
90
92 int output;
94
97
102
103 struct {
104 void (*error_fn)(void *file_handle);
105 void (*fatal_fn)(void *file_handle);
106 void (*backtrace_fn)(void *file_handle);
108};
109
111
112/* -------------------------------------------------------------------- */
115
117{
118 /* Inside a function for correct static initialization order, otherwise
119 * all_refs might get null initialized only after logrefs are registered.*/
120 static CLG_LogRef *all_refs = nullptr;
121 return &all_refs;
122}
123
125
126/* -------------------------------------------------------------------- */
131
132#define CLOG_BUF_LEN_INIT 512
133
135 char *data;
136 uint len;
139};
140
141static void clg_str_init(CLogStringBuf *cstr, char *buf_stack, uint buf_stack_len)
142{
143 cstr->data = buf_stack;
144 cstr->len_alloc = buf_stack_len;
145 cstr->len = 0;
146 cstr->is_alloc = false;
147}
148
149static void clg_str_free(CLogStringBuf *cstr)
150{
151 if (cstr->is_alloc) {
152 MEM_delete(cstr->data);
153 }
154}
155
156static void clg_str_reserve(CLogStringBuf *cstr, const uint len)
157{
158 if (len > cstr->len_alloc) {
159 cstr->len_alloc *= 2;
160 cstr->len_alloc = std::max(len, cstr->len_alloc);
161
162 if (cstr->is_alloc) {
163 cstr->data = static_cast<char *>(MEM_realloc_uninitialized(cstr->data, cstr->len_alloc));
164 }
165 else {
166 /* Copy the static buffer. */
167 char *data = MEM_new_array_uninitialized<char>(cstr->len_alloc, __func__);
168 memcpy(data, cstr->data, cstr->len);
169 cstr->data = data;
170 cstr->is_alloc = true;
171 }
172 }
173}
174
175static void clg_str_append_with_len(CLogStringBuf *cstr, const char *str, const uint len)
176{
177 uint len_next = cstr->len + len;
178 clg_str_reserve(cstr, len_next);
179 char *str_dst = cstr->data + cstr->len;
180 memcpy(str_dst, str, len);
181#if 0 /* no need. */
182 str_dst[len] = '\0';
183#endif
184 cstr->len = len_next;
185}
186
187static void clg_str_append(CLogStringBuf *cstr, const char *str)
188{
189 clg_str_append_with_len(cstr, str, strlen(str));
190}
191
192static void clg_str_append_char(CLogStringBuf *cstr, const char c, const uint len)
193{
194 uint len_next = cstr->len + len;
195 clg_str_reserve(cstr, len_next);
196 char *str_dst = cstr->data + cstr->len;
197 memset(str_dst, c, len);
198#if 0 /* no need. */
199 str_dst[len] = '\0';
200#endif
201 cstr->len = len_next;
202}
203
205static void clg_str_vappendf(CLogStringBuf *cstr, const char *format, va_list args)
206{
207 /* Use limit because windows may use '-1' for a formatting error. */
208 const uint len_max = 65535;
209 uint len_avail = cstr->len_alloc - cstr->len;
210 while (true) {
211 va_list args_cpy;
212 va_copy(args_cpy, args);
213 int retval = vsnprintf(cstr->data + cstr->len, len_avail, format, args_cpy);
214 va_end(args_cpy);
215
216 if (retval < 0) {
217 /* Some encoding error happened, not much we can do here, besides skipping/canceling this
218 * message. */
219 break;
220 }
221
222 if ((uint)retval <= len_avail) {
223 /* Copy was successful. */
224 cstr->len += (uint)retval;
225 break;
226 }
227
228 /* `vsnprintf` was not successful, due to lack of allocated space, `retval` contains expected
229 * length of the formatted string, use it to allocate required amount of memory. */
230 uint len_alloc = cstr->len + (uint)retval;
231 if (len_alloc >= len_max) {
232 /* Safe upper-limit, just in case... */
233 break;
234 }
235
236 clg_str_reserve(cstr, len_alloc);
237 len_avail = cstr->len_alloc - cstr->len;
238 }
239}
240
241static void clg_str_indent_multiline(CLogStringBuf *cstr, const uint indent_len)
242{
243 /* If there are multiple lines, indent them the same as the first line for readability. */
244 if (indent_len < 2) {
245 return;
246 }
247
248 uint num_newlines = 0;
249 for (uint i = 0; i < cstr->len; i++) {
250 if (cstr->data[i] == '\n') {
251 num_newlines++;
252 }
253 }
254 if (num_newlines == 0) {
255 return;
256 }
257
258 const char *old_data = cstr->data;
259 const uint old_len = cstr->len;
260 const bool old_is_alloc = cstr->is_alloc;
261
262 cstr->len_alloc = cstr->len + (num_newlines * indent_len);
263 cstr->len = 0;
264 cstr->data = MEM_new_array_uninitialized<char>(cstr->len_alloc, __func__);
265 cstr->is_alloc = true;
266
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;
273 }
274 }
275
276 if (old_is_alloc) {
277 MEM_delete(old_data);
278 }
279}
280
282
283/* -------------------------------------------------------------------- */
286
296#define COLOR_LEN (COLOR_RESET + 1)
297
298static const char *clg_color_table[COLOR_LEN] = {nullptr};
299#ifdef _WIN32
300static DWORD clg_previous_console_mode = 0;
301#endif
302
303static void clg_color_table_init(bool use_color)
304{
305 for (int i = 0; i < COLOR_LEN; i++) {
306 clg_color_table[i] = "";
307 }
308 if (use_color) {
309 clg_color_table[COLOR_DEFAULT] = "\033[1;37m";
310 clg_color_table[COLOR_RED] = "\033[1;31m";
311 clg_color_table[COLOR_GREEN] = "\033[1;32m";
312 clg_color_table[COLOR_YELLOW] = "\033[1;33m";
313 clg_color_table[COLOR_DIM] = "\033[2;37m";
314 clg_color_table[COLOR_RESET] = "\033[0m";
315 }
316}
317
318static const char *clg_level_as_text(enum CLG_Level level)
319{
320 switch (level) {
321 case CLG_LEVEL_FATAL:
322 return "FATAL";
323 case CLG_LEVEL_ERROR:
324 return "ERROR";
325 case CLG_LEVEL_WARN:
326 return "WARNING";
327 case CLG_LEVEL_INFO:
328 return "INFO";
329 case CLG_LEVEL_DEBUG:
330 return "DEBUG";
331 case CLG_LEVEL_TRACE:
332 return "TRACE";
333 }
334
335 return "INVALID_LEVEL";
336}
337
339{
340 switch (level) {
341 case CLG_LEVEL_FATAL:
342 case CLG_LEVEL_ERROR:
343 return COLOR_RED;
344 case CLG_LEVEL_WARN:
345 return COLOR_YELLOW;
346 case CLG_LEVEL_INFO:
347 case CLG_LEVEL_DEBUG:
348 case CLG_LEVEL_TRACE:
349 return COLOR_DEFAULT;
350 }
351 /* should never get here. */
352 assert(false);
353 return COLOR_DEFAULT;
354}
355
357
358/* -------------------------------------------------------------------- */
361
368static bool clg_ctx_filter_check(CLogContext *ctx, const char *identifier)
369{
370 if (ctx->filters[0] == nullptr && ctx->filters[1] == nullptr &&
371 ctx->default_type.level >= CLG_LEVEL_INFO)
372 {
373 /* No filters but level specified? Match everything. */
374 return true;
375 }
376
377 const size_t identifier_len = strlen(identifier);
378 for (uint i = 0; i < 2; i++) {
379 const CLG_IDFilter *flt = ctx->filters[i];
380 while (flt != nullptr) {
381 const size_t len = strlen(flt->match);
382 if (STREQ(flt->match, "*") ||
383 ((len <= identifier_len) && (STREQLEN(identifier, flt->match, len))))
384 {
385 return (bool)i;
386 }
387 if (flt->match[0] == '*' && flt->match[len - 1] == '*') {
388 char *match = MEM_new_array_zeroed<char>(len - 1, __func__);
389 memcpy(match, flt->match + 1, len - 2);
390 const bool success = (strstr(identifier, match) != nullptr);
391 MEM_delete(match);
392 if (success) {
393 return (bool)i;
394 }
395 }
396 else if ((len >= 2) && (STREQLEN(".*", &flt->match[len - 2], 2))) {
397 /* `foo.*` and `foo.bar.*` support kept for backwards compatibility.
398 * `foo` and `foo.bar` now do the same thing. */
399 if (((identifier_len == len - 2) && STREQLEN(identifier, flt->match, len - 2)) ||
400 ((identifier_len >= len - 1) && STREQLEN(identifier, flt->match, len - 1)))
401 {
402 return (bool)i;
403 }
404 }
405 flt = flt->next;
406 }
407 }
408 return false;
409}
410
415static CLG_LogType *clg_ctx_type_find_by_name(CLogContext *ctx, const char *identifier)
416{
417 for (CLG_LogType *ty = ctx->types; ty; ty = ty->next) {
418 if (STREQ(identifier, ty->identifier)) {
419 return ty;
420 }
421 }
422 return nullptr;
423}
424
425static CLG_LogType *clg_ctx_type_register(CLogContext *ctx, const char *identifier)
426{
427 assert(clg_ctx_type_find_by_name(ctx, identifier) == nullptr);
429 ty->next = ctx->types;
430 ctx->types = ty;
431 strncpy(ty->identifier, identifier, sizeof(ty->identifier) - 1);
432 ty->ctx = ctx;
433
434 if (clg_ctx_filter_check(ctx, ty->identifier)) {
435 ty->level = ctx->default_type.level;
436 }
437 else {
438 ty->level = std::min(ctx->default_type.level, CLG_LEVEL_WARN);
439 }
440
441 return ty;
442}
443
445{
446 if (ctx->callbacks.error_fn != nullptr) {
447 ctx->callbacks.error_fn(ctx->output_file);
448 }
449}
450
452{
453 if (ctx->callbacks.fatal_fn != nullptr) {
454 ctx->callbacks.fatal_fn(ctx->output_file);
455 }
456 fflush(ctx->output_file);
457 abort();
458}
459
461{
462 /* NOTE: we avoid writing to 'FILE', for back-trace we make an exception,
463 * if necessary we could have a version of the callback that writes to file
464 * descriptor all at once. */
465 ctx->callbacks.backtrace_fn(ctx->output_file);
466 fflush(ctx->output_file);
467}
468
470{
471 uint64_t tick;
472#if defined(_MSC_VER)
473 tick = GetTickCount64();
474#else
475 struct timeval tv;
476 gettimeofday(&tv, nullptr);
477 tick = tv.tv_sec * 1000 + tv.tv_usec / 1000;
478#endif
479 return tick;
480}
481
483
484/* -------------------------------------------------------------------- */
487
488static void write_timestamp(CLogStringBuf *cstr, const uint64_t timestamp_tick_start)
489{
490 char timestamp_str[128];
491 const uint64_t timestamp = clg_timestamp_ticks_get() - timestamp_tick_start;
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);
496
497 const uint timestamp_len =
498 (h > 0) ?
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);
501
502 clg_str_append_with_len(cstr, timestamp_str, timestamp_len);
503}
504
505static void write_memory(CLogStringBuf *cstr)
506{
507 const uint64_t mem_in_use = MEM_get_memory_in_use() / (1024 * 1024);
508 char memory_str[128];
509 const uint len = snprintf(memory_str, sizeof(memory_str), "%dM", (int)mem_in_use);
510
511 clg_str_append_with_len(cstr, memory_str, len);
512
513 const uint memory_align_width = 5;
514 const uint num_spaces = (len < memory_align_width) ? memory_align_width - len : 0;
515 clg_str_append_char(cstr, ' ', num_spaces + 2);
516}
517
518static void write_level(CLogStringBuf *cstr, enum CLG_Level level, bool use_color)
519{
520 if (level >= CLG_LEVEL_INFO) {
521 return;
522 }
523
524 if (use_color) {
525 enum eCLogColor color = clg_level_to_color(level);
526 clg_str_append(cstr, clg_color_table[color]);
527 clg_str_append(cstr, clg_level_as_text(level));
529 }
530 else {
531 clg_str_append(cstr, clg_level_as_text(level));
532 }
533
534 clg_str_append(cstr, " ");
535}
536
537static void write_type(CLogStringBuf *cstr, const CLG_LogType *lg)
538{
539 const uint len = strlen(lg->identifier);
541
542 const uint type_align_width = 16;
543 const uint num_spaces = (len < type_align_width) ? type_align_width - len : 0;
544 clg_str_append_char(cstr, ' ', num_spaces + 1);
545}
546
548 const char *file_line,
549 const char *fn,
550 const bool use_basename,
551 const bool use_color)
552{
553 if (use_color) {
555 }
556
557 uint file_line_len = strlen(file_line);
558 if (use_basename) {
559 uint file_line_offset = file_line_len;
560 while (file_line_offset-- > 0) {
561 if (file_line[file_line_offset] == PATHSEP_CHAR) {
562 file_line_offset++;
563 break;
564 }
565 }
566 file_line += file_line_offset;
567 file_line_len -= file_line_offset;
568 }
569 clg_str_append_with_len(cstr, file_line, file_line_len);
570
571 clg_str_append(cstr, " ");
572 clg_str_append(cstr, fn);
573
574 if (use_color) {
576 }
577}
578
580 enum CLG_Level level,
581 const char *file_line,
582 const char *fn,
583 const char *message)
584{
585 CLogStringBuf cstr;
586 char cstr_stack_buf[CLOG_BUF_LEN_INIT];
587 clg_str_init(&cstr, cstr_stack_buf, sizeof(cstr_stack_buf));
588
589 if (lg->ctx->use_timestamp) {
591 }
592 if (lg->ctx->use_memory) {
593 write_memory(&cstr);
594 }
595 write_type(&cstr, lg);
596
597 clg_str_append(&cstr, "| ");
598
599 const uint64_t multiline_indent_len = cstr.len;
600
601 write_level(&cstr, level, lg->ctx->use_color);
602
603 clg_str_append(&cstr, message);
604
605 if (lg->ctx->use_source) {
606 clg_str_append(&cstr, "\n");
607 write_file_line_fn(&cstr, file_line, fn, lg->ctx->use_basename, lg->ctx->use_color);
608 }
609
610 clg_str_indent_multiline(&cstr, multiline_indent_len);
611
612 clg_str_append(&cstr, "\n");
613
614 /* Output could be optional. */
615 {
616 /* Mutex to avoid garbled output with threads and multi line output. */
617 std::scoped_lock lock(LOG_MUTEX);
618 int bytes_written = write(lg->ctx->output, cstr.data, cstr.len);
619 (void)bytes_written;
620 }
621
622 clg_str_free(&cstr);
623
624 if (lg->ctx->callbacks.backtrace_fn) {
626 }
627
628 if (level == CLG_LEVEL_FATAL) {
630 }
631}
632
633void CLG_logf(const CLG_LogType *lg,
634 enum CLG_Level level,
635 const char *file_line,
636 const char *fn,
637 const char *format,
638 ...)
639{
640 CLogStringBuf cstr;
641 char cstr_stack_buf[CLOG_BUF_LEN_INIT];
642 clg_str_init(&cstr, cstr_stack_buf, sizeof(cstr_stack_buf));
643
644 if (lg->ctx->use_timestamp) {
646 }
647 if (lg->ctx->use_memory) {
648 write_memory(&cstr);
649 }
650 write_type(&cstr, lg);
651
652 clg_str_append(&cstr, "| ");
653
654 const uint64_t multiline_indent_len = cstr.len;
655
656 write_level(&cstr, level, lg->ctx->use_color);
657
658 {
659 va_list ap;
660 va_start(ap, format);
661 clg_str_vappendf(&cstr, format, ap);
662 va_end(ap);
663 }
664
665 if (lg->ctx->use_source) {
666 clg_str_append(&cstr, "\n");
667 write_file_line_fn(&cstr, file_line, fn, lg->ctx->use_basename, lg->ctx->use_color);
668 }
669
670 clg_str_indent_multiline(&cstr, multiline_indent_len);
671
672 clg_str_append(&cstr, "\n");
673
674 /* Output could be optional. */
675 {
676 /* Mutex to avoid garbled output with threads and multi line output. */
677 std::scoped_lock lock(LOG_MUTEX);
678 int bytes_written = write(lg->ctx->output, cstr.data, cstr.len);
679 (void)bytes_written;
680 }
681
682 clg_str_free(&cstr);
683
684 if (lg->ctx->callbacks.backtrace_fn) {
686 }
687
688 if (level == CLG_LEVEL_ERROR) {
690 }
691
692 if (level == CLG_LEVEL_FATAL) {
694 }
695}
696
697void CLG_log_raw(const CLG_LogType *lg, const char *message)
698{
699 /* Write raw text without any formatting. */
700 int bytes_written = write(lg->ctx->output, message, strlen(message));
701 (void)bytes_written;
702}
703
705
706/* -------------------------------------------------------------------- */
709
710static void CLG_ctx_output_set(CLogContext *ctx, void *file_handle)
711{
712 ctx->output_file = static_cast<FILE *>(file_handle);
713 ctx->output = fileno(ctx->output_file);
714#if defined(__unix__) || defined(__APPLE__)
715 ctx->use_color = isatty(ctx->output);
716#elif defined(WIN32)
717 /* As of Windows 10 build 18298 all the standard consoles supports color
718 * like the Linux Terminal do, but it needs to be turned on.
719 * To turn on colors we need to enable virtual terminal processing by passing the flag
720 * ENABLE_VIRTUAL_TERMINAL_PROCESSING into SetConsoleMode.
721 * If the system doesn't support virtual terminal processing it will fail silently and the flag
722 * will not be set. */
723
724 GetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), &clg_previous_console_mode);
725
726 ctx->use_color = 0;
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)) {
730 ctx->use_color = 1;
731 }
732 }
733#endif
734}
735
736static void CLG_ctx_output_use_source_set(CLogContext *ctx, int value)
737{
738 ctx->use_source = (bool)value;
739}
740
742{
743 ctx->use_basename = (bool)value;
744}
745
747{
748 ctx->use_timestamp = (bool)value;
749 if (ctx->use_timestamp) {
751 }
752}
753
754static void CLG_ctx_output_use_memory_set(CLogContext *ctx, int value)
755{
756 ctx->use_memory = (bool)value;
757}
758
760static void CLT_ctx_error_fn_set(CLogContext *ctx, void (*error_fn)(void *file_handle))
761{
762 ctx->callbacks.error_fn = error_fn;
763}
764
766static void CLG_ctx_fatal_fn_set(CLogContext *ctx, void (*fatal_fn)(void *file_handle))
767{
768 ctx->callbacks.fatal_fn = fatal_fn;
769}
770
771static void CLG_ctx_backtrace_fn_set(CLogContext *ctx, void (*backtrace_fn)(void *file_handle))
772{
773 ctx->callbacks.backtrace_fn = backtrace_fn;
774}
775
777 const char *type_match,
778 int type_match_len)
779{
780 if (type_match_len == 0) {
781 return;
782 }
783 CLG_IDFilter *flt = static_cast<CLG_IDFilter *>(
784 MEM_new_zeroed(sizeof(*flt) + type_match_len + 1, __func__));
785 flt->next = *flt_list;
786 *flt_list = flt;
787 memcpy(flt->match, type_match, type_match_len);
788 /* no need to null terminate since we calloc'd */
789}
790
792 const char *type_match,
793 int type_match_len)
794{
795 clg_ctx_type_filter_append(&ctx->filters[0], type_match, type_match_len);
796}
797
799 const char *type_match,
800 int type_match_len)
801{
802 clg_ctx_type_filter_append(&ctx->filters[1], type_match, type_match_len);
803 if (ctx->default_type.level <= CLG_LEVEL_WARN) {
804 ctx->default_type.level = CLG_LEVEL_INFO;
805 }
806}
807
809{
810 ctx->default_type.level = level;
811 for (CLG_LogType *ty = ctx->types; ty; ty = ty->next) {
812 ty->level = level;
813 }
814}
815
817{
819#ifdef WITH_CLOG_PTHREADS
820 pthread_mutex_init(&ctx->types_lock, nullptr);
821#endif
822 ctx->default_type.level = CLG_LEVEL_WARN;
823 ctx->use_source = true;
824 CLG_ctx_output_set(ctx, stdout);
825
826 return ctx;
827}
828
829static void CLG_ctx_free(CLogContext *ctx)
830{
831#if defined(WIN32)
832 SetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), clg_previous_console_mode);
833#endif
834 while (ctx->types != nullptr) {
835 CLG_LogType *item = ctx->types;
836 ctx->types = item->next;
837 MEM_delete(item);
838 }
839
840 for (CLG_LogRef *ref = *clg_all_refs_p(); ref; ref = ref->next) {
841 ref->type = nullptr;
842 }
843
844 for (uint i = 0; i < 2; i++) {
845 while (ctx->filters[i] != nullptr) {
846 CLG_IDFilter *item = ctx->filters[i];
847 ctx->filters[i] = item->next;
848 MEM_delete(item);
849 }
850 }
851#ifdef WITH_CLOG_PTHREADS
852 pthread_mutex_destroy(&ctx->types_lock);
853#endif
854 MEM_delete(ctx);
855}
856
858
859/* -------------------------------------------------------------------- */
864
865/* We could support multiple at once, for now this seems not needed. */
866static struct CLogContext *g_ctx = nullptr;
867/* Separate to preserve this after freeing context. */
868static bool g_quiet = false;
869
871{
873
875}
876
878{
880}
881
882void CLG_output_set(void *file_handle)
883{
884 CLG_ctx_output_set(g_ctx, file_handle);
885}
886
888{
890}
891
896
901
903{
905}
906
907void CLG_error_fn_set(void (*error_fn)(void *file_handle))
908{
909 CLT_ctx_error_fn_set(g_ctx, error_fn);
910}
911
912void CLG_fatal_fn_set(void (*fatal_fn)(void *file_handle))
913{
914 CLG_ctx_fatal_fn_set(g_ctx, fatal_fn);
915}
916
917void CLG_backtrace_fn_set(void (*fatal_fn)(void *file_handle))
918{
920}
921
922void CLG_type_filter_exclude(const char *type_match, int type_match_len)
923{
924 CLG_ctx_type_filter_exclude(g_ctx, type_match, type_match_len);
925}
926
927void CLG_type_filter_include(const char *type_match, int type_match_len)
928{
929 CLG_ctx_type_filter_include(g_ctx, type_match, type_match_len);
930}
931
933{
934 CLG_ctx_level_set(g_ctx, level);
935}
936
937void CLG_quiet_set(bool quiet)
938{
939 g_quiet = quiet;
940}
941
943{
944 return g_quiet;
945}
946
948
949/* -------------------------------------------------------------------- */
954
956{
957 /* Add to global list of refs, both for setting the type to null on CLG_exit()
958 * and so CLG_logref_list_all can be used to print all categories. */
959 static std::mutex mutex;
960 std::scoped_lock lock(mutex);
961 CLG_LogRef **all_refs = clg_all_refs_p();
962 clg_ref->next = *all_refs;
963 *all_refs = clg_ref;
964}
965
966void CLG_logref_list_all(void (*callback)(const char *identifier, void *user_data),
967 void *user_data)
968{
969 /* Generate sorted list of unique identifiers. */
970 auto cmp = [](const char *a, const char *b) { return std::strcmp(a, b) < 0; };
971 std::set<const char *, decltype(cmp)> identifiers(cmp);
972 for (CLG_LogRef *ref = *clg_all_refs_p(); ref; ref = ref->next) {
973 identifiers.insert(ref->identifier);
974 }
975
976 for (const char *identifier : identifiers) {
977 callback(identifier, user_data);
978 }
979}
980
982{
983 if (g_ctx == nullptr) {
984 fprintf(stderr, "CLG logging used without initialization, aborting.\n");
985 abort();
986 }
987
988#ifdef WITH_CLOG_PTHREADS
989 /* Only runs once when initializing a static type in most cases. */
990 pthread_mutex_lock(&g_ctx->types_lock);
991#endif
992 if (clg_ref->type == nullptr) {
994 if (clg_ty == nullptr) {
995 clg_ty = clg_ctx_type_register(g_ctx, clg_ref->identifier);
996 }
997#ifdef WITH_CLOG_PTHREADS
998 atomic_cas_ptr((void **)&clg_ref->type, clg_ref->type, clg_ty);
999#else
1000 clg_ref->type = clg_ty;
1001#endif
1002 }
1003#ifdef WITH_CLOG_PTHREADS
1004 pthread_mutex_unlock(&g_ctx->types_lock);
1005#endif
1006}
1007
1009{
1010 if (clg_ref->type == nullptr) {
1011 CLG_logref_init(clg_ref);
1012 }
1013 return clg_ref->type->ctx->use_color;
1014}
1015
#define ATTR_PRINTF_FORMAT(format_param, dots_param)
void CLG_type_filter_exclude(const char *type_match, int type_match_len)
Definition clog.cc:922
bool CLG_quiet_get()
Definition clog.cc:942
void CLG_output_set(void *file_handle)
Definition clog.cc:882
void CLG_output_use_basename_set(int value)
Definition clog.cc:892
void CLG_exit()
Definition clog.cc:877
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)
Definition clog.cc:902
void CLG_log_raw(const CLG_LogType *lg, const char *message)
Definition clog.cc:697
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))
Definition clog.cc:907
void CLG_level_set(CLG_Level level)
Definition clog.cc:932
void CLG_output_use_source_set(int value)
Definition clog.cc:887
void CLG_backtrace_fn_set(void(*fatal_fn)(void *file_handle))
Definition clog.cc:917
void CLG_fatal_fn_set(void(*fatal_fn)(void *file_handle))
Definition clog.cc:912
void CLG_logref_list_all(void(*callback)(const char *identifier, void *user_data), void *user_data)
Definition clog.cc:966
void CLG_logref_register(CLG_LogRef *clg_ref)
Definition clog.cc:955
void CLG_type_filter_include(const char *type_match, int type_match_len)
Definition clog.cc:927
void CLG_logref_init(CLG_LogRef *clg_ref)
Definition clog.cc:981
CLG_Level
Definition CLG_log.h:52
@ CLG_LEVEL_ERROR
Definition CLG_log.h:56
@ CLG_LEVEL_DEBUG
Definition CLG_log.h:62
@ CLG_LEVEL_INFO
Definition CLG_log.h:60
@ CLG_LEVEL_FATAL
Definition CLG_log.h:54
@ CLG_LEVEL_WARN
Definition CLG_log.h:58
@ CLG_LEVEL_TRACE
Definition CLG_log.h:64
void CLG_output_use_timestamp_set(int value)
Definition clog.cc:897
void CLG_quiet_set(bool quiet)
Definition clog.cc:937
int CLG_color_support_get(CLG_LogRef *clg_ref)
Definition clog.cc:1008
void CLG_init()
Definition clog.cc:870
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)
volatile int lock
#define STREQLEN(a, b, n)
#define STREQ(a, b)
#define MEM_realloc_uninitialized(vmemh, len)
unsigned long long int uint64_t
static void CLG_ctx_output_set(CLogContext *ctx, void *file_handle)
Definition clog.cc:710
static void write_type(CLogStringBuf *cstr, const CLG_LogType *lg)
Definition clog.cc:537
static uint64_t clg_timestamp_ticks_get()
Definition clog.cc:469
static void clg_str_append_char(CLogStringBuf *cstr, const char c, const uint len)
Definition clog.cc:192
static void clg_str_vappendf(CLogStringBuf *cstr, const char *format, va_list args)
Definition clog.cc:205
static void clg_ctx_fatal_action(CLogContext *ctx)
Definition clog.cc:451
static void CLG_ctx_free(CLogContext *ctx)
Definition clog.cc:829
static void clg_ctx_error_action(CLogContext *ctx)
Definition clog.cc:444
static CLG_LogType * clg_ctx_type_find_by_name(CLogContext *ctx, const char *identifier)
Definition clog.cc:415
static void CLG_ctx_output_use_timestamp_set(CLogContext *ctx, int value)
Definition clog.cc:746
static std::mutex LOG_MUTEX
Definition clog.cc:68
static void clg_str_append_with_len(CLogStringBuf *cstr, const char *str, const uint len)
Definition clog.cc:175
static void write_memory(CLogStringBuf *cstr)
Definition clog.cc:505
static CLogContext * CLG_ctx_init()
Definition clog.cc:816
static void write_timestamp(CLogStringBuf *cstr, const uint64_t timestamp_tick_start)
Definition clog.cc:488
static void CLG_ctx_output_use_source_set(CLogContext *ctx, int value)
Definition clog.cc:736
#define CLOG_BUF_LEN_INIT
Definition clog.cc:132
static void clg_str_append(CLogStringBuf *cstr, const char *str)
Definition clog.cc:187
static void CLG_ctx_output_use_memory_set(CLogContext *ctx, int value)
Definition clog.cc:754
static const char * clg_color_table[COLOR_LEN]
Definition clog.cc:298
static void CLG_ctx_fatal_fn_set(CLogContext *ctx, void(*fatal_fn)(void *file_handle))
Definition clog.cc:766
static bool g_quiet
Definition clog.cc:868
static void clg_ctx_type_filter_append(CLG_IDFilter **flt_list, const char *type_match, int type_match_len)
Definition clog.cc:776
static struct CLogContext * g_ctx
Definition clog.cc:866
static void clg_str_free(CLogStringBuf *cstr)
Definition clog.cc:149
static void write_level(CLogStringBuf *cstr, enum CLG_Level level, bool use_color)
Definition clog.cc:518
static void CLG_ctx_output_use_basename_set(CLogContext *ctx, int value)
Definition clog.cc:741
static CLG_LogType * clg_ctx_type_register(CLogContext *ctx, const char *identifier)
Definition clog.cc:425
static void clg_str_init(CLogStringBuf *cstr, char *buf_stack, uint buf_stack_len)
Definition clog.cc:141
static void clg_ctx_backtrace(CLogContext *ctx)
Definition clog.cc:460
static void CLG_ctx_backtrace_fn_set(CLogContext *ctx, void(*backtrace_fn)(void *file_handle))
Definition clog.cc:771
static CLG_LogRef ** clg_all_refs_p()
Definition clog.cc:116
#define COLOR_LEN
Definition clog.cc:296
static void clg_str_reserve(CLogStringBuf *cstr, const uint len)
Definition clog.cc:156
static void CLG_ctx_level_set(CLogContext *ctx, CLG_Level level)
Definition clog.cc:808
static void CLG_ctx_type_filter_exclude(CLogContext *ctx, const char *type_match, int type_match_len)
Definition clog.cc:791
static void clg_str_indent_multiline(CLogStringBuf *cstr, const uint indent_len)
Definition clog.cc:241
eCLogColor
Definition clog.cc:287
@ COLOR_DEFAULT
Definition clog.cc:288
@ COLOR_DIM
Definition clog.cc:292
@ COLOR_RED
Definition clog.cc:289
@ COLOR_YELLOW
Definition clog.cc:291
@ COLOR_GREEN
Definition clog.cc:290
@ COLOR_RESET
Definition clog.cc:294
static void clg_color_table_init(bool use_color)
Definition clog.cc:303
static const char * clg_level_as_text(enum CLG_Level level)
Definition clog.cc:318
static void write_file_line_fn(CLogStringBuf *cstr, const char *file_line, const char *fn, const bool use_basename, const bool use_color)
Definition clog.cc:547
static enum eCLogColor clg_level_to_color(enum CLG_Level level)
Definition clog.cc:338
static bool clg_ctx_filter_check(CLogContext *ctx, const char *identifier)
Definition clog.cc:368
static void CLG_ctx_type_filter_include(CLogContext *ctx, const char *type_match, int type_match_len)
Definition clog.cc:798
static void CLT_ctx_error_fn_set(CLogContext *ctx, void(*error_fn)(void *file_handle))
Definition clog.cc:760
#define str(s)
#define assert(assertion)
#define PATHSEP_CHAR
size_t(*) MEM_get_memory_in_use(void)
Definition mallocn.cc:65
void * MEM_new_zeroed(size_t len, const char *str)
Definition mallocn.cc:114
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str)
Definition mallocn.cc:119
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str)
Definition mallocn.cc:129
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
char match[0]
Definition clog.cc:73
struct CLG_IDFilter * next
Definition clog.cc:71
const char * identifier
Definition CLG_log.h:81
struct CLG_LogRef * next
Definition CLG_log.h:83
CLG_LogType * type
Definition CLG_log.h:82
char identifier[64]
Definition CLG_log.h:71
CLG_Level level
Definition CLG_log.h:75
struct CLG_LogType * next
Definition CLG_log.h:70
struct CLogContext * ctx
Definition CLG_log.h:73
void(*) backtrace_fn(void *file_handle)
void(*) fatal_fn(void *file_handle)
void(*) error_fn(void *file_handle)
bool use_basename
Definition clog.cc:87
bool use_source
Definition clog.cc:86
bool use_timestamp
Definition clog.cc:88
uint64_t timestamp_tick_start
Definition clog.cc:96
FILE * output_file
Definition clog.cc:93
int output
Definition clog.cc:92
struct CLogContext::@314332104112243113050307245356273277225137335317 callbacks
bool use_memory
Definition clog.cc:89
bool use_color
Definition clog.cc:85
CLG_IDFilter * filters[2]
Definition clog.cc:84
struct CLogContext::@104337011070222130164322347125303021100366321217 default_type
CLG_LogType * types
Definition clog.cc:78
bool is_alloc
Definition clog.cc:138
char * data
Definition clog.cc:135
uint len_alloc
Definition clog.cc:137