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