Blender V5.3
report.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <atomic>
10#include <cerrno>
11#include <cstdarg>
12#include <cstdio>
13#include <cstring>
14#include <mutex>
15
16#include "MEM_guardedalloc.h"
17
18#include "BLI_dynstr.hh"
19#include "BLI_fileops.hh"
20#include "BLI_listbase.hh"
21#include "BLI_string.hh"
22#include "BLI_string_utils.hh"
23#include "BLI_utildefines.hh"
24
25#include "BLT_translation.hh"
26
27#include "BKE_global.hh" /* G.background only */
28#include "BKE_report.hh"
29
30#include "CLG_log.h"
31
32namespace blender {
33
34static CLG_LogRef LOG = {"reports"};
35
37{
38 static std::atomic<int> report_session_uid_counter{0};
39 return report_session_uid_counter.fetch_add(1, std::memory_order_relaxed) + 1;
40}
41
42void BKE_report_log(eReportType type, const char *message, CLG_LogRef *log)
43{
44 switch (type) {
45 case RPT_DEBUG:
46 CLOG_STR_DEBUG(log, message);
47 break;
48 case RPT_INFO:
49 case RPT_OPERATOR:
50 case RPT_PROPERTY:
51 CLOG_INFO_NOCHECK(log, "%s", message);
52 break;
53 case RPT_WARNING:
54 CLOG_STR_WARN(log, message);
55 break;
56 case RPT_ERROR:
60 CLOG_STR_ERROR(log, message);
61 break;
62 }
63}
64
66{
67 switch (type) {
68 case RPT_DEBUG:
69 return RPT_("Debug");
70 case RPT_INFO:
71 return RPT_("Info");
72 case RPT_OPERATOR:
73 return RPT_("Operator");
74 case RPT_PROPERTY:
75 return RPT_("Property");
76 case RPT_WARNING:
77 return RPT_("Warning");
78 case RPT_ERROR:
79 return RPT_("Error");
81 return RPT_("Invalid Input Error");
83 return RPT_("Invalid Context Error");
85 return RPT_("Out Of Memory Error");
86 default:
87 return RPT_("Undefined Type");
88 }
89}
90
91void BKE_reports_init(ReportList *reports, int flag)
92{
93 if (!reports) {
94 return;
95 }
96
97 *reports = ReportList{};
98
99 reports->storelevel = RPT_INFO;
100 reports->printlevel = RPT_ERROR;
101 reports->flag = flag;
102
103 reports->lock = MEM_new<std::mutex>(__func__);
104}
105
107{
108 if (!reports) {
109 return;
110 }
111
112 BKE_reports_clear(reports);
113
114 MEM_delete(reports->lock);
115 reports->lock = nullptr;
116}
117
119{
120 Report *report, *report_next;
121
122 if (!reports) {
123 return;
124 }
125
126 std::scoped_lock lock(*reports->lock);
127
128 report = reports->list.first();
129
130 while (report) {
131 report_next = report->next;
132 MEM_delete(report->message);
133 MEM_delete(report);
134 report = report_next;
135 }
136
137 reports->list.clear_no_delete();
138}
139
141{
142 reports->lock->lock();
143}
144
146{
147 reports->lock->unlock();
148}
149
150void BKE_reports_move_to_reports(ReportList *reports_dst, ReportList *reports_src)
151{
152 BLI_assert(reports_dst);
153 if (!reports_src) {
154 return;
155 }
156
157 std::scoped_lock lock(*reports_src->lock, *reports_dst->lock);
158
159 BLI_movelisttolist(&reports_dst->list, &reports_src->list);
160}
161
162void BKE_report(ReportList *reports, eReportType type, const char *_message)
163{
164 Report *report;
165 int len;
166 const char *message = RPT_(_message);
167
168 if (BKE_reports_print_test(reports, type)) {
169 BKE_report_log(type, message, &LOG);
170 fflush(stdout); /* this ensures the message is printed before a crash */
171 }
172
173 if (reports && (reports->flag & RPT_STORE) && (type >= reports->storelevel)) {
174 std::scoped_lock lock(*reports->lock);
175
176 char *message_alloc;
177 report = MEM_new<Report>("Report");
178 report->type = type;
179 report->typestr = BKE_report_type_str(type);
180
181 len = strlen(message);
182 message_alloc = MEM_new_array_uninitialized<char>(size_t(len) + 1, "ReportMessage");
183 memcpy(message_alloc, message, sizeof(char) * (len + 1));
184 report->message = message_alloc;
185 report->len = len;
187 BLI_addtail(&reports->list, report);
188 }
189}
190
191void BKE_reportf(ReportList *reports, eReportType type, const char *_format, ...)
192{
193 Report *report;
194 va_list args;
195 const char *format = RPT_(_format);
196
197 if (BKE_reports_print_test(reports, type)) {
198 va_start(args, _format);
199 const char *message = BLI_vsprintfN(format, args);
200 va_end(args);
201 BKE_report_log(type, message, &LOG);
202 fflush(stdout); /* this ensures the message is printed before a crash */
203 MEM_delete(message);
204 }
205
206 if (reports && (reports->flag & RPT_STORE) && (type >= reports->storelevel)) {
207 std::scoped_lock lock(*reports->lock);
208
209 report = MEM_new<Report>("Report");
210
211 va_start(args, _format);
212 report->message = BLI_vsprintfN(format, args);
213 va_end(args);
214
215 report->len = strlen(report->message);
216 report->type = type;
217 report->typestr = BKE_report_type_str(type);
219
220 BLI_addtail(&reports->list, report);
221 }
222}
223
227static void reports_prepend_impl(ReportList *reports, const char *prepend)
228{
229 /* Caller must ensure. */
230 BLI_assert(reports && reports->list.first());
231
232 std::scoped_lock lock(*reports->lock);
233
234 const size_t prefix_len = strlen(prepend);
235 for (Report &report : reports->list) {
236 char *message = BLI_string_joinN(prepend, report.message);
237 MEM_delete(report.message);
238 report.message = message;
239 report.len += prefix_len;
240 BLI_assert(report.len == strlen(message));
241 }
242}
243
244void BKE_reports_prepend(ReportList *reports, const char *prepend)
245{
246 if (!reports || !reports->list.first()) {
247 return;
248 }
249 reports_prepend_impl(reports, RPT_(prepend));
250}
251
252void BKE_reports_prependf(ReportList *reports, const char *prepend_format, ...)
253{
254 if (!reports || !reports->list.first()) {
255 return;
256 }
257 va_list args;
258 va_start(args, prepend_format);
259 char *prepend = BLI_vsprintfN(RPT_(prepend_format), args);
260 va_end(args);
261
262 reports_prepend_impl(reports, prepend);
263
264 MEM_delete(prepend);
265}
266
268{
269 if (!reports) {
270 return RPT_ERROR;
271 }
272
273 return eReportType(reports->printlevel);
274}
275
277{
278 if (!reports) {
279 return;
280 }
281
282 std::scoped_lock lock(*reports->lock);
283
284 reports->printlevel = level;
285}
286
288{
289 if (!reports) {
290 return RPT_ERROR;
291 }
292
293 return eReportType(reports->storelevel);
294}
295
297{
298 if (!reports) {
299 return;
300 }
301
302 std::scoped_lock lock(*reports->lock);
303
304 reports->storelevel = level;
305}
306
308{
309 DynStr *ds;
310 char *cstring;
311
312 if (!reports || !reports->list.first()) {
313 return nullptr;
314 }
315
316 std::scoped_lock lock(*reports->lock);
317
318 ds = BLI_dynstr_new();
319 for (Report &report : reports->list) {
320 if (report.type >= level) {
321 BLI_dynstr_appendf(ds, "%s: %s\n", report.typestr, report.message);
322 }
323 }
324
325 if (BLI_dynstr_get_len(ds)) {
326 cstring = BLI_dynstr_get_cstring(ds);
327 }
328 else {
329 cstring = nullptr;
330 }
331
332 BLI_dynstr_free(ds);
333 return cstring;
334}
335
337{
338 if (reports == nullptr) {
339 return true;
340 }
341 if (reports->flag & RPT_PRINT_HANDLED_BY_OWNER) {
342 return false;
343 }
344 /* In background mode always print otherwise there are cases the errors won't be displayed,
345 * but still add to the report list since this is used for Python exception handling. */
346 if (G.background) {
347 return true;
348 }
349
350 /* Common case. */
351 return (reports->flag & RPT_PRINT) && (type >= reports->printlevel);
352}
353
355{
356 if (reports == nullptr) {
357 return;
358 }
359
360 for (Report &report : reports->list) {
361 if (report.type >= level) {
362 BKE_report_log(eReportType(report.type), report.message, log);
363 }
364 }
365}
366
368{
369 char *cstring = BKE_reports_string(reports, level);
370
371 if (cstring == nullptr) {
372 return;
373 }
374
375 /* A trailing newline is already part of `cstring`. */
376 fputs(cstring, stdout);
377 fflush(stdout);
378 MEM_delete(cstring);
379}
380
382{
383 std::scoped_lock lock(*reports->lock);
384
385 for (Report &report : reports->list.items_reversed()) {
386 if (ELEM(report.type, RPT_ERROR, RPT_WARNING, RPT_INFO)) {
387 return &report;
388 }
389 }
390
391 return nullptr;
392}
393
395{
396 if (reports != nullptr) {
397 std::scoped_lock lock(*reports->lock);
398
399 for (Report &report : reports->list) {
400 if (report.type >= level) {
401 return true;
402 }
403 }
404 }
405 return false;
406}
407
408bool BKE_report_write_file_fp(FILE *fp, ReportList *reports, const char *header)
409{
410 if (header) {
411 fputs(header, fp);
412 }
413
414 std::scoped_lock lock(*reports->lock);
415
416 for (Report &report : reports->list) {
417 fprintf(fp, "%s # %s\n", report.message, report.typestr);
418 }
419
420 return true;
421}
422
423bool BKE_report_write_file(const char *filepath, ReportList *reports, const char *header)
424{
425 FILE *fp;
426
427 errno = 0;
428 fp = BLI_fopen(filepath, "wb");
429 if (fp == nullptr) {
430 fprintf(stderr,
431 "Unable to save '%s': %s\n",
432 filepath,
433 errno ? strerror(errno) : "Unknown error opening file");
434 return false;
435 }
436
437 BKE_report_write_file_fp(fp, reports, header);
438
439 fclose(fp);
440
441 return true;
442}
443
444} // namespace blender
A dynamically sized string ADT.
File and directory operations.
#define BLI_string_joinN(...)
#define RPT_(msgid)
#define CLOG_STR_DEBUG(clg_ref, str)
Definition CLG_log.h:199
#define CLOG_STR_ERROR(clg_ref, str)
Definition CLG_log.h:196
#define CLOG_INFO_NOCHECK(clg_ref, format,...)
Definition CLG_log.h:204
#define CLOG_STR_WARN(clg_ref, str)
Definition CLG_log.h:197
Read Blender memory allocation and freeing.
volatile int lock
#define ELEM(...)
#define log
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
Report * BKE_reports_last_displayable(ReportList *reports)
Definition report.cc:381
void BLI_dynstr_appendf(DynStr *__restrict ds, const char *__restrict format,...) ATTR_PRINTF_FORMAT(2
@ RPT_ERROR_INVALID_INPUT
Definition BKE_report.hh:42
@ RPT_ERROR_INVALID_CONTEXT
Definition BKE_report.hh:43
@ RPT_OPERATOR
Definition BKE_report.hh:38
@ RPT_ERROR_OUT_OF_MEMORY
Definition BKE_report.hh:44
@ RPT_PROPERTY
Definition BKE_report.hh:39
@ RPT_WARNING
Definition BKE_report.hh:40
static int report_session_uid_counter_get_next()
Definition report.cc:36
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
bool BKE_report_write_file(const char *filepath, ReportList *reports, const char *header)
Definition report.cc:423
void BKE_reports_free(ReportList *reports)
Definition report.cc:106
void BKE_report_print_level_set(ReportList *reports, eReportType level)
Definition report.cc:276
void void BKE_reports_prepend(ReportList *reports, const char *prepend)
Definition report.cc:244
void BKE_reports_init(ReportList *reports, int flag)
Definition report.cc:91
FILE * BLI_fopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
static void reports_prepend_impl(ReportList *reports, const char *prepend)
Definition report.cc:227
void void void BLI_movelisttolist(ListBase *dst, ListBase *src) ATTR_NONNULL(1
Definition listbase.cc:32
void BKE_reports_move_to_reports(ReportList *reports_dst, ReportList *reports_src)
Definition report.cc:150
void BKE_reports_clear(ReportList *reports)
Definition report.cc:118
void BKE_report_store_level_set(ReportList *reports, eReportType level)
Definition report.cc:296
const char * BKE_report_type_str(eReportType type)
Definition report.cc:65
void BLI_dynstr_free(DynStr *ds) ATTR_NONNULL()
Global G
Definition blender.cc:57
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
bool BKE_reports_contain(ReportList *reports, eReportType level)
Definition report.cc:394
eReportType BKE_report_store_level(ReportList *reports)
Definition report.cc:287
void BKE_reports_unlock(ReportList *reports)
Definition report.cc:145
char * BLI_dynstr_get_cstring(const DynStr *ds) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void BKE_reports_print(ReportList *reports, eReportType level)
Definition report.cc:367
void BKE_report_log(eReportType type, const char *message, CLG_LogRef *log)
Definition report.cc:42
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
void BKE_reports_lock(ReportList *reports)
Definition report.cc:140
static CLG_LogRef LOG
Definition logging.cpp:12
void BKE_reports_prependf(ReportList *reports, const char *prepend_format,...) ATTR_PRINTF_FORMAT(2
Definition report.cc:252
char char * BLI_vsprintfN(const char *__restrict format, va_list args) ATTR_NONNULL(1) ATTR_MALLOC ATTR_PRINTF_FORMAT(1
@ RPT_PRINT_HANDLED_BY_OWNER
Definition BKE_report.hh:62
char * BKE_reports_string(ReportList *reports, eReportType level)
Definition report.cc:307
int BLI_dynstr_get_len(const DynStr *ds) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
bool BKE_reports_print_test(const ReportList *reports, eReportType type)
Definition report.cc:336
DynStr * BLI_dynstr_new() ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
Definition BLI_dynstr.cc:39
void BKE_reports_log(ReportList *reports, eReportType level, CLG_LogRef *log)
Definition report.cc:354
void eReportType BKE_report_print_level(ReportList *reports)
Definition report.cc:267
bool BKE_report_write_file_fp(FILE *fp, ReportList *reports, const char *header)
Definition report.cc:408
ListBaseT< Report > list
Definition BKE_report.hh:79
std::mutex * lock
Definition BKE_report.hh:89
const char * typestr
Definition BKE_report.hh:72
const char * message
Definition BKE_report.hh:73