Cycles: svn merge -r39870:r40266 https://svn.blender.org/svnroot/bf-blender/trunk...
[blender-staging.git] / source / blender / blenkernel / intern / report.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributor(s): Blender Foundation (2008).
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenkernel/intern/report.c
29  *  \ingroup bke
30  */
31
32
33 #include "MEM_guardedalloc.h"
34
35 #include "BLI_blenlib.h"
36 #include "BLI_dynstr.h"
37 #include "BLI_utildefines.h"
38
39 #include "BKE_report.h"
40 #include "BKE_global.h" /* G.background only */
41
42
43 #include <stdarg.h>
44 #include <stdio.h>
45 #include <string.h>
46
47 static const char *report_type_str(int type)
48 {
49         switch(type) {
50                 case RPT_DEBUG: return "Debug";
51                 case RPT_INFO: return "Info";
52                 case RPT_OPERATOR: return "Operator";
53                 case RPT_WARNING: return "Warning";
54                 case RPT_ERROR: return "Error";
55                 case RPT_ERROR_INVALID_INPUT: return "Invalid Input Error";
56                 case RPT_ERROR_INVALID_CONTEXT: return "Invalid Context Error";
57                 case RPT_ERROR_OUT_OF_MEMORY: return "Out Of Memory Error";
58                 default: return "Undefined Type";
59         }
60 }
61
62 void BKE_reports_init(ReportList *reports, int flag)
63 {
64         if(!reports)
65                 return;
66
67         memset(reports, 0, sizeof(ReportList));
68
69         reports->storelevel= RPT_INFO;
70         reports->printlevel= RPT_ERROR;
71         reports->flag= flag;
72 }
73
74 void BKE_reports_clear(ReportList *reports)
75 {
76         Report *report, *report_next;
77
78         if(!reports)
79                 return;
80
81         report= reports->list.first;
82
83         while (report) {
84                 report_next= report->next;
85                 MEM_freeN((void *)report->message);
86                 MEM_freeN(report);
87                 report= report_next;
88         }
89
90         reports->list.first= reports->list.last= NULL;
91 }
92
93 void BKE_report(ReportList *reports, ReportType type, const char *message)
94 {
95         Report *report;
96         int len;
97
98         /* in background mode always print otherwise there are cases the errors wont be displayed,
99          * but still add to the report list since this is used for python exception handling */
100         if(G.background || !reports || ((reports->flag & RPT_PRINT) && (type >= reports->printlevel))) {
101                 printf("%s: %s\n", report_type_str(type), message);
102                 fflush(stdout); /* this ensures the message is printed before a crash */
103         }
104
105         if(reports && (reports->flag & RPT_STORE) && (type >= reports->storelevel)) {
106                 char *message_alloc;
107                 report= MEM_callocN(sizeof(Report), "Report");
108                 report->type= type;
109                 report->typestr= report_type_str(type);
110
111                 len= strlen(message);
112                 message_alloc= MEM_callocN(sizeof(char)*(len+1), "ReportMessage");
113                 memcpy(message_alloc, message, sizeof(char)*(len+1));
114                 report->message= message_alloc;
115                 report->len= len;
116                 BLI_addtail(&reports->list, report);
117         }
118 }
119
120 void BKE_reportf(ReportList *reports, ReportType type, const char *format, ...)
121 {
122         DynStr *ds;
123         Report *report;
124         va_list args;
125
126         if(G.background || !reports || ((reports->flag & RPT_PRINT) && (type >= reports->printlevel))) {
127                 va_start(args, format);
128                 vprintf(format, args);
129                 va_end(args);
130                 fprintf(stdout, "\n"); /* otherise each report needs to include a \n */
131                 fflush(stdout); /* this ensures the message is printed before a crash */
132         }
133
134         if(reports && (reports->flag & RPT_STORE) && (type >= reports->storelevel)) {
135                 report= MEM_callocN(sizeof(Report), "Report");
136
137                 ds= BLI_dynstr_new();
138                 va_start(args, format);
139                 BLI_dynstr_vappendf(ds, format, args);
140                 va_end(args);
141
142                 report->message= BLI_dynstr_get_cstring(ds);
143                 report->len= BLI_dynstr_get_len(ds);
144                 BLI_dynstr_free(ds);
145
146                 report->type= type;
147                 report->typestr= report_type_str(type);
148
149                 BLI_addtail(&reports->list, report);
150         }
151 }
152
153 void BKE_reports_prepend(ReportList *reports, const char *prepend)
154 {
155         Report *report;
156         DynStr *ds;
157
158         if(!reports)
159                 return;
160
161         for(report=reports->list.first; report; report=report->next) {
162                 ds= BLI_dynstr_new();
163
164                 BLI_dynstr_append(ds, prepend);
165                 BLI_dynstr_append(ds, report->message);
166                 MEM_freeN((void *)report->message);
167
168                 report->message= BLI_dynstr_get_cstring(ds);
169                 report->len= BLI_dynstr_get_len(ds);
170
171                 BLI_dynstr_free(ds);
172         }
173 }
174
175 void BKE_reports_prependf(ReportList *reports, const char *prepend, ...)
176 {
177         Report *report;
178         DynStr *ds;
179         va_list args;
180
181         if(!reports)
182                 return;
183
184         for(report=reports->list.first; report; report=report->next) {
185                 ds= BLI_dynstr_new();
186                 va_start(args, prepend);
187                 BLI_dynstr_vappendf(ds, prepend, args);
188                 va_end(args);
189
190                 BLI_dynstr_append(ds, report->message);
191                 MEM_freeN((void *)report->message);
192
193                 report->message= BLI_dynstr_get_cstring(ds);
194                 report->len= BLI_dynstr_get_len(ds);
195
196                 BLI_dynstr_free(ds);
197         }
198 }
199
200 ReportType BKE_report_print_level(ReportList *reports)
201 {
202         if(!reports)
203                 return RPT_ERROR;
204
205         return reports->printlevel;
206 }
207
208 void BKE_report_print_level_set(ReportList *reports, ReportType level)
209 {
210         if(!reports)
211                 return;
212
213         reports->printlevel= level;
214 }
215
216 ReportType BKE_report_store_level(ReportList *reports)
217 {
218         if(!reports)
219                 return RPT_ERROR;
220
221         return reports->storelevel;
222 }
223
224 void BKE_report_store_level_set(ReportList *reports, ReportType level)
225 {
226         if(!reports)
227                 return;
228
229         reports->storelevel= level;
230 }
231
232 char *BKE_reports_string(ReportList *reports, ReportType level)
233 {
234         Report *report;
235         DynStr *ds;
236         char *cstring;
237
238         if(!reports || !reports->list.first)
239                 return NULL;
240
241         ds= BLI_dynstr_new();
242         for(report=reports->list.first; report; report=report->next)
243                 if(report->type >= level)
244                         BLI_dynstr_appendf(ds, "%s: %s\n", report->typestr, report->message);
245
246         if (BLI_dynstr_get_len(ds))
247                 cstring= BLI_dynstr_get_cstring(ds);
248         else
249                 cstring= NULL;
250
251         BLI_dynstr_free(ds);
252         return cstring;
253 }
254
255 void BKE_reports_print(ReportList *reports, ReportType level)
256 {
257         char *cstring = BKE_reports_string(reports, level);
258         
259         if (cstring == NULL)
260                 return;
261         
262         printf("%s", cstring);
263         fflush(stdout);
264         MEM_freeN(cstring);
265 }
266
267 Report *BKE_reports_last_displayable(ReportList *reports)
268 {
269         Report *report=NULL;
270         
271         for (report= (Report *)reports->list.last; report; report=report->prev) {
272                 if (ELEM3(report->type, RPT_ERROR, RPT_WARNING, RPT_INFO))
273                         return report;
274         }
275         
276         return NULL;
277 }