svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r20855:20928
[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_WARNING: return "Warning";
53                 case RPT_ERROR: return "Error";
54                 case RPT_ERROR_INVALID_INPUT: return "Invalid Input Error";
55                 case RPT_ERROR_INVALID_CONTEXT: return "Invalid Context Error";
56                 case RPT_ERROR_OUT_OF_MEMORY: return "Out Of Memory Error";
57                 default: return "Undefined Type";
58         }
59 }
60
61 void BKE_reports_init(ReportList *reports, int flag)
62 {
63         if(!reports)
64                 return;
65
66         memset(reports, 0, sizeof(ReportList));
67
68         reports->storelevel= RPT_WARNING;
69         reports->printlevel= RPT_WARNING;
70         reports->flag= flag;
71 }
72
73 void BKE_reports_clear(ReportList *reports)
74 {
75         Report *report;
76
77         if(!reports)
78                 return;
79
80         for(report=reports->list.first; report; report=report->next)
81                 MEM_freeN(report->message);
82
83         BLI_freelistN(&reports->list);
84 }
85
86 void BKE_report(ReportList *reports, ReportType type, const char *message)
87 {
88         Report *report;
89         int len;
90
91         if(!reports || ((reports->flag & RPT_PRINT) && (type >= reports->printlevel))) {
92                 printf("%s: %s\n", report_type_str(type), message);
93                 fflush(stdout); /* this ensures the message is printed before a crash */
94         }
95
96         if(reports && (reports->flag & RPT_STORE) && (type >= reports->storelevel)) {
97                 report= MEM_callocN(sizeof(Report), "Report");
98                 report->type= type;
99                 report->typestr= report_type_str(type);
100
101                 len= strlen(message);
102                 report->message= MEM_callocN(sizeof(char)*(len+1), "ReportMessage");
103                 memcpy(report->message, message, sizeof(char)*(len+1));
104                 
105                 BLI_addtail(&reports->list, report);
106         }
107 }
108
109 void BKE_reportf(ReportList *reports, ReportType type, const char *format, ...)
110 {
111         DynStr *ds;
112         Report *report;
113         va_list args;
114
115         if(!reports || ((reports->flag & RPT_PRINT) && (type >= reports->printlevel))) {
116                 va_start(args, format);
117                 vprintf(format, args);
118                 va_end(args);
119                 fflush(stdout); /* this ensures the message is printed before a crash */
120         }
121
122         if(reports && (reports->flag & RPT_STORE) && (type >= reports->storelevel)) {
123                 report= MEM_callocN(sizeof(Report), "Report");
124
125                 ds= BLI_dynstr_new();
126                 va_start(args, format);
127                 BLI_dynstr_vappendf(ds, format, args);
128                 va_end(args);
129
130                 report->message= BLI_dynstr_get_cstring(ds);
131
132                 BLI_dynstr_free(ds);
133
134                 report->type= type;
135                 report->typestr= report_type_str(type);
136
137                 BLI_addtail(&reports->list, report);
138         }
139 }
140
141 void BKE_reports_prepend(ReportList *reports, const char *prepend)
142 {
143         Report *report;
144         DynStr *ds;
145
146         if(!reports)
147                 return;
148
149         for(report=reports->list.first; report; report=report->next) {
150                 ds= BLI_dynstr_new();
151
152                 BLI_dynstr_append(ds, prepend);
153                 BLI_dynstr_append(ds, report->message);
154                 MEM_freeN(report->message);
155
156                 report->message= BLI_dynstr_get_cstring(ds);
157
158                 BLI_dynstr_free(ds);
159         }
160 }
161
162 void BKE_reports_prependf(ReportList *reports, const char *prepend, ...)
163 {
164         Report *report;
165         DynStr *ds;
166         va_list args;
167
168         if(!reports)
169                 return;
170
171         for(report=reports->list.first; report; report=report->next) {
172                 ds= BLI_dynstr_new();
173                 va_start(args, prepend);
174                 BLI_dynstr_vappendf(ds, prepend, args);
175                 va_end(args);
176
177                 BLI_dynstr_append(ds, report->message);
178                 MEM_freeN(report->message);
179
180                 report->message= BLI_dynstr_get_cstring(ds);
181
182                 BLI_dynstr_free(ds);
183         }
184 }
185
186 ReportType BKE_report_print_level(ReportList *reports)
187 {
188         if(!reports)
189                 return RPT_ERROR;
190
191         return reports->printlevel;
192 }
193
194 void BKE_report_print_level_set(ReportList *reports, ReportType level)
195 {
196         if(!reports)
197                 return;
198
199         reports->printlevel= level;
200 }
201
202 ReportType BKE_report_store_level(ReportList *reports)
203 {
204         if(!reports)
205                 return RPT_ERROR;
206
207         return reports->storelevel;
208 }
209
210 void BKE_report_store_level_set(ReportList *reports, ReportType level)
211 {
212         if(!reports)
213                 return;
214
215         reports->storelevel= level;
216 }
217
218 char *BKE_reports_string(ReportList *reports, ReportType level)
219 {
220         Report *report;
221         DynStr *ds;
222         char *cstring;
223
224         if(!reports)
225                 return NULL;
226
227         ds= BLI_dynstr_new();
228         for(report=reports->list.first; report; report=report->next)
229                 if(report->type >= level)
230                         BLI_dynstr_appendf(ds, "%s: %s\n", report->typestr, report->message);
231
232         if (BLI_dynstr_get_len(ds))
233                 cstring= BLI_dynstr_get_cstring(ds);
234         else
235                 cstring= NULL;
236
237         BLI_dynstr_free(ds);
238         return cstring;
239 }
240
241 void BKE_reports_print(ReportList *reports, ReportType level)
242 {
243         char *cstring = BKE_reports_string(reports, level);
244         
245         if (cstring == NULL)
246                 return;
247         
248         printf("%s", cstring);
249         fflush(stdout);
250         MEM_freeN(cstring);
251 }
252