doxygen: blender/blenkernel tagged.
[blender.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 #ifdef _WIN32
48 #ifndef vsnprintf
49 #define vsnprintf _vsnprintf
50 #endif
51 #endif
52
53 static const char *report_type_str(int type)
54 {
55         switch(type) {
56                 case RPT_DEBUG: return "Debug";
57                 case RPT_INFO: return "Info";
58                 case RPT_OPERATOR: return "Operator";
59                 case RPT_WARNING: return "Warning";
60                 case RPT_ERROR: return "Error";
61                 case RPT_ERROR_INVALID_INPUT: return "Invalid Input Error";
62                 case RPT_ERROR_INVALID_CONTEXT: return "Invalid Context Error";
63                 case RPT_ERROR_OUT_OF_MEMORY: return "Out Of Memory Error";
64                 default: return "Undefined Type";
65         }
66 }
67
68 void BKE_reports_init(ReportList *reports, int flag)
69 {
70         if(!reports)
71                 return;
72
73         memset(reports, 0, sizeof(ReportList));
74
75         reports->storelevel= RPT_INFO;
76         reports->printlevel= RPT_ERROR;
77         reports->flag= flag;
78 }
79
80 void BKE_reports_clear(ReportList *reports)
81 {
82         Report *report, *report_next;
83
84         if(!reports)
85                 return;
86
87         report= reports->list.first;
88
89         while (report) {
90                 report_next= report->next;
91                 MEM_freeN((void *)report->message);
92                 MEM_freeN(report);
93                 report= report_next;
94         }
95
96         reports->list.first= reports->list.last= NULL;
97 }
98
99 void BKE_report(ReportList *reports, ReportType type, const char *message)
100 {
101         Report *report;
102         int len;
103
104     /* in background mode always print otherwise there are cases the errors wont be displayed,
105          * but still add to the report list since this is used for python exception handling */
106         if(G.background || !reports || ((reports->flag & RPT_PRINT) && (type >= reports->printlevel))) {
107                 printf("%s: %s\n", report_type_str(type), message);
108                 fflush(stdout); /* this ensures the message is printed before a crash */
109         }
110
111         if(reports && (reports->flag & RPT_STORE) && (type >= reports->storelevel)) {
112                 char *message_alloc;
113                 report= MEM_callocN(sizeof(Report), "Report");
114                 report->type= type;
115                 report->typestr= report_type_str(type);
116
117                 len= strlen(message);
118                 message_alloc= MEM_callocN(sizeof(char)*(len+1), "ReportMessage");
119                 memcpy(message_alloc, message, sizeof(char)*(len+1));
120                 report->message= message_alloc;
121                 report->len= len;
122                 BLI_addtail(&reports->list, report);
123         }
124 }
125
126 void BKE_reportf(ReportList *reports, ReportType type, const char *format, ...)
127 {
128         DynStr *ds;
129         Report *report;
130         va_list args;
131
132         if(G.background || !reports || ((reports->flag & RPT_PRINT) && (type >= reports->printlevel))) {
133                 va_start(args, format);
134                 vprintf(format, args);
135                 va_end(args);
136                 fprintf(stdout, "\n"); /* otherise each report needs to include a \n */
137                 fflush(stdout); /* this ensures the message is printed before a crash */
138         }
139
140         if(reports && (reports->flag & RPT_STORE) && (type >= reports->storelevel)) {
141                 report= MEM_callocN(sizeof(Report), "Report");
142
143                 ds= BLI_dynstr_new();
144                 va_start(args, format);
145                 BLI_dynstr_vappendf(ds, format, args);
146                 va_end(args);
147
148                 report->message= BLI_dynstr_get_cstring(ds);
149                 report->len= BLI_dynstr_get_len(ds);
150                 BLI_dynstr_free(ds);
151
152                 report->type= type;
153                 report->typestr= report_type_str(type);
154
155                 BLI_addtail(&reports->list, report);
156         }
157 }
158
159 void BKE_reports_prepend(ReportList *reports, const char *prepend)
160 {
161         Report *report;
162         DynStr *ds;
163
164         if(!reports)
165                 return;
166
167         for(report=reports->list.first; report; report=report->next) {
168                 ds= BLI_dynstr_new();
169
170                 BLI_dynstr_append(ds, prepend);
171                 BLI_dynstr_append(ds, report->message);
172                 MEM_freeN((void *)report->message);
173
174                 report->message= BLI_dynstr_get_cstring(ds);
175                 report->len= BLI_dynstr_get_len(ds);
176
177                 BLI_dynstr_free(ds);
178         }
179 }
180
181 void BKE_reports_prependf(ReportList *reports, const char *prepend, ...)
182 {
183         Report *report;
184         DynStr *ds;
185         va_list args;
186
187         if(!reports)
188                 return;
189
190         for(report=reports->list.first; report; report=report->next) {
191                 ds= BLI_dynstr_new();
192                 va_start(args, prepend);
193                 BLI_dynstr_vappendf(ds, prepend, args);
194                 va_end(args);
195
196                 BLI_dynstr_append(ds, report->message);
197                 MEM_freeN((void *)report->message);
198
199                 report->message= BLI_dynstr_get_cstring(ds);
200                 report->len= BLI_dynstr_get_len(ds);
201
202                 BLI_dynstr_free(ds);
203         }
204 }
205
206 ReportType BKE_report_print_level(ReportList *reports)
207 {
208         if(!reports)
209                 return RPT_ERROR;
210
211         return reports->printlevel;
212 }
213
214 void BKE_report_print_level_set(ReportList *reports, ReportType level)
215 {
216         if(!reports)
217                 return;
218
219         reports->printlevel= level;
220 }
221
222 ReportType BKE_report_store_level(ReportList *reports)
223 {
224         if(!reports)
225                 return RPT_ERROR;
226
227         return reports->storelevel;
228 }
229
230 void BKE_report_store_level_set(ReportList *reports, ReportType level)
231 {
232         if(!reports)
233                 return;
234
235         reports->storelevel= level;
236 }
237
238 char *BKE_reports_string(ReportList *reports, ReportType level)
239 {
240         Report *report;
241         DynStr *ds;
242         char *cstring;
243
244         if(!reports || !reports->list.first)
245                 return NULL;
246
247         ds= BLI_dynstr_new();
248         for(report=reports->list.first; report; report=report->next)
249                 if(report->type >= level)
250                         BLI_dynstr_appendf(ds, "%s: %s\n", report->typestr, report->message);
251
252         if (BLI_dynstr_get_len(ds))
253                 cstring= BLI_dynstr_get_cstring(ds);
254         else
255                 cstring= NULL;
256
257         BLI_dynstr_free(ds);
258         return cstring;
259 }
260
261 void BKE_reports_print(ReportList *reports, ReportType level)
262 {
263         char *cstring = BKE_reports_string(reports, level);
264         
265         if (cstring == NULL)
266                 return;
267         
268         printf("%s", cstring);
269         fflush(stdout);
270         MEM_freeN(cstring);
271 }
272
273 Report *BKE_reports_last_displayable(ReportList *reports)
274 {
275         Report *report=NULL;
276         
277         for (report= (Report *)reports->list.last; report; report=report->prev) {
278                 if (ELEM3(report->type, RPT_ERROR, RPT_WARNING, RPT_INFO))
279                         return report;
280         }
281         
282         return NULL;
283 }