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