bugfix [#21036] crash with glsl shading
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 "DNA_listBase.h"
31
32 #include "BLI_blenlib.h"
33 #include "BLI_dynstr.h"
34
35 #include "BKE_report.h"
36
37 #include <stdarg.h>
38 #include <stdio.h>
39 #include <string.h>
40
41 #ifdef _WIN32
42 #ifndef vsnprintf
43 #define vsnprintf _vsnprintf
44 #endif
45 #endif
46
47 static 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_INFO;
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(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         if(!reports || ((reports->flag & RPT_PRINT) && (type >= reports->printlevel))) {
99                 printf("%s: %s\n", report_type_str(type), message);
100                 fflush(stdout); /* this ensures the message is printed before a crash */
101         }
102
103         if(reports && (reports->flag & RPT_STORE) && (type >= reports->storelevel)) {
104                 report= MEM_callocN(sizeof(Report), "Report");
105                 report->type= type;
106                 report->typestr= report_type_str(type);
107
108                 len= strlen(message);
109                 report->message= MEM_callocN(sizeof(char)*(len+1), "ReportMessage");
110                 memcpy(report->message, message, sizeof(char)*(len+1));
111                 report->len= len;
112                 BLI_addtail(&reports->list, report);
113         }
114 }
115
116 void BKE_reportf(ReportList *reports, ReportType type, const char *format, ...)
117 {
118         DynStr *ds;
119         Report *report;
120         va_list args;
121
122         if(!reports || ((reports->flag & RPT_PRINT) && (type >= reports->printlevel))) {
123                 va_start(args, format);
124                 vprintf(format, args);
125                 va_end(args);
126                 fflush(stdout); /* this ensures the message is printed before a crash */
127         }
128
129         if(reports && (reports->flag & RPT_STORE) && (type >= reports->storelevel)) {
130                 report= MEM_callocN(sizeof(Report), "Report");
131
132                 ds= BLI_dynstr_new();
133                 va_start(args, format);
134                 BLI_dynstr_vappendf(ds, format, args);
135                 va_end(args);
136
137                 report->message= BLI_dynstr_get_cstring(ds);
138                 report->len= BLI_dynstr_get_len(ds);
139                 BLI_dynstr_free(ds);
140
141                 report->type= type;
142                 report->typestr= report_type_str(type);
143
144                 BLI_addtail(&reports->list, report);
145         }
146 }
147
148 void BKE_reports_prepend(ReportList *reports, const char *prepend)
149 {
150         Report *report;
151         DynStr *ds;
152
153         if(!reports)
154                 return;
155
156         for(report=reports->list.first; report; report=report->next) {
157                 ds= BLI_dynstr_new();
158
159                 BLI_dynstr_append(ds, prepend);
160                 BLI_dynstr_append(ds, report->message);
161                 MEM_freeN(report->message);
162
163                 report->message= BLI_dynstr_get_cstring(ds);
164                 report->len= BLI_dynstr_get_len(ds);
165
166                 BLI_dynstr_free(ds);
167         }
168 }
169
170 void BKE_reports_prependf(ReportList *reports, const char *prepend, ...)
171 {
172         Report *report;
173         DynStr *ds;
174         va_list args;
175
176         if(!reports)
177                 return;
178
179         for(report=reports->list.first; report; report=report->next) {
180                 ds= BLI_dynstr_new();
181                 va_start(args, prepend);
182                 BLI_dynstr_vappendf(ds, prepend, args);
183                 va_end(args);
184
185                 BLI_dynstr_append(ds, report->message);
186                 MEM_freeN(report->message);
187
188                 report->message= BLI_dynstr_get_cstring(ds);
189                 report->len= BLI_dynstr_get_len(ds);
190
191                 BLI_dynstr_free(ds);
192         }
193 }
194
195 ReportType BKE_report_print_level(ReportList *reports)
196 {
197         if(!reports)
198                 return RPT_ERROR;
199
200         return reports->printlevel;
201 }
202
203 void BKE_report_print_level_set(ReportList *reports, ReportType level)
204 {
205         if(!reports)
206                 return;
207
208         reports->printlevel= level;
209 }
210
211 ReportType BKE_report_store_level(ReportList *reports)
212 {
213         if(!reports)
214                 return RPT_ERROR;
215
216         return reports->storelevel;
217 }
218
219 void BKE_report_store_level_set(ReportList *reports, ReportType level)
220 {
221         if(!reports)
222                 return;
223
224         reports->storelevel= level;
225 }
226
227 char *BKE_reports_string(ReportList *reports, ReportType level)
228 {
229         Report *report;
230         DynStr *ds;
231         char *cstring;
232
233         if(!reports || !reports->list.first)
234                 return NULL;
235
236         ds= BLI_dynstr_new();
237         for(report=reports->list.first; report; report=report->next)
238                 if(report->type >= level)
239                         BLI_dynstr_appendf(ds, "%s: %s\n", report->typestr, report->message);
240
241         if (BLI_dynstr_get_len(ds))
242                 cstring= BLI_dynstr_get_cstring(ds);
243         else
244                 cstring= NULL;
245
246         BLI_dynstr_free(ds);
247         return cstring;
248 }
249
250 void BKE_reports_print(ReportList *reports, ReportType level)
251 {
252         char *cstring = BKE_reports_string(reports, level);
253         
254         if (cstring == NULL)
255                 return;
256         
257         printf("%s", cstring);
258         fflush(stdout);
259         MEM_freeN(cstring);
260 }
261